mirror of
https://github.com/home-assistant/core.git
synced 2026-04-17 23:53:49 +01:00
69 lines
2.2 KiB
Python
69 lines
2.2 KiB
Python
"""Config flow for UniFi Access integration."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import logging
|
|
from typing import Any
|
|
|
|
from unifi_access_api import ApiAuthError, ApiConnectionError, UnifiAccessApiClient
|
|
import voluptuous as vol
|
|
|
|
from homeassistant.config_entries import ConfigFlow, ConfigFlowResult
|
|
from homeassistant.const import CONF_API_TOKEN, CONF_HOST, CONF_VERIFY_SSL
|
|
from homeassistant.helpers.aiohttp_client import async_get_clientsession
|
|
|
|
from .const import DOMAIN
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
|
class UnifiAccessConfigFlow(ConfigFlow, domain=DOMAIN):
|
|
"""Handle a config flow for UniFi Access."""
|
|
|
|
VERSION = 1
|
|
MINOR_VERSION = 1
|
|
|
|
async def async_step_user(
|
|
self, user_input: dict[str, Any] | None = None
|
|
) -> ConfigFlowResult:
|
|
"""Handle the initial step."""
|
|
errors: dict[str, str] = {}
|
|
|
|
if user_input is not None:
|
|
session = async_get_clientsession(
|
|
self.hass, verify_ssl=user_input[CONF_VERIFY_SSL]
|
|
)
|
|
client = UnifiAccessApiClient(
|
|
host=user_input[CONF_HOST],
|
|
api_token=user_input[CONF_API_TOKEN],
|
|
session=session,
|
|
verify_ssl=user_input[CONF_VERIFY_SSL],
|
|
)
|
|
try:
|
|
await client.authenticate()
|
|
except ApiAuthError:
|
|
errors["base"] = "invalid_auth"
|
|
except ApiConnectionError:
|
|
errors["base"] = "cannot_connect"
|
|
except Exception:
|
|
_LOGGER.exception("Unexpected exception")
|
|
errors["base"] = "unknown"
|
|
else:
|
|
self._async_abort_entries_match({CONF_HOST: user_input[CONF_HOST]})
|
|
return self.async_create_entry(
|
|
title="UniFi Access",
|
|
data=user_input,
|
|
)
|
|
|
|
return self.async_show_form(
|
|
step_id="user",
|
|
data_schema=vol.Schema(
|
|
{
|
|
vol.Required(CONF_HOST): str,
|
|
vol.Required(CONF_API_TOKEN): str,
|
|
vol.Required(CONF_VERIFY_SSL, default=False): bool,
|
|
}
|
|
),
|
|
errors=errors,
|
|
)
|