mirror of
https://github.com/home-assistant/core.git
synced 2026-07-06 06:15:33 +01:00
7da5b10b51
Co-authored-by: bryan <185078974@qq.com> Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com>
67 lines
2.2 KiB
Python
67 lines
2.2 KiB
Python
"""Config flow for Aidot integration."""
|
|
|
|
from typing import Any
|
|
|
|
from aidot.client import AidotClient
|
|
from aidot.const import CONF_ID, DEFAULT_COUNTRY_CODE, SUPPORTED_COUNTRY_CODES
|
|
from aidot.exceptions import AidotUserOrPassIncorrect
|
|
from aiohttp import ClientError
|
|
import voluptuous as vol
|
|
|
|
from homeassistant.config_entries import ConfigFlow, ConfigFlowResult
|
|
from homeassistant.const import CONF_COUNTRY_CODE, CONF_PASSWORD, CONF_USERNAME
|
|
from homeassistant.helpers import selector
|
|
from homeassistant.helpers.aiohttp_client import async_get_clientsession
|
|
|
|
from .const import DOMAIN
|
|
|
|
DATA_SCHEMA = vol.Schema(
|
|
{
|
|
vol.Required(
|
|
CONF_COUNTRY_CODE,
|
|
default=DEFAULT_COUNTRY_CODE,
|
|
): selector.CountrySelector(
|
|
selector.CountrySelectorConfig(
|
|
countries=SUPPORTED_COUNTRY_CODES,
|
|
)
|
|
),
|
|
vol.Required(CONF_USERNAME): str,
|
|
vol.Required(CONF_PASSWORD): str,
|
|
}
|
|
)
|
|
|
|
|
|
class AidotConfigFlow(ConfigFlow, domain=DOMAIN):
|
|
"""Handle aidot config flow."""
|
|
|
|
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:
|
|
client = AidotClient(
|
|
session=async_get_clientsession(self.hass),
|
|
country_code=user_input[CONF_COUNTRY_CODE],
|
|
username=user_input[CONF_USERNAME],
|
|
password=user_input[CONF_PASSWORD],
|
|
)
|
|
try:
|
|
login_info = await client.async_post_login()
|
|
except AidotUserOrPassIncorrect:
|
|
errors["base"] = "invalid_auth"
|
|
except TimeoutError, ClientError:
|
|
errors["base"] = "cannot_connect"
|
|
|
|
if not errors:
|
|
await self.async_set_unique_id(login_info[CONF_ID])
|
|
self._abort_if_unique_id_configured()
|
|
return self.async_create_entry(
|
|
title=f"{user_input[CONF_USERNAME]} {user_input[CONF_COUNTRY_CODE]}",
|
|
data=login_info,
|
|
)
|
|
|
|
return self.async_show_form(
|
|
step_id="user", data_schema=DATA_SCHEMA, errors=errors
|
|
)
|