1 """Config flow for Switcher integration."""
3 from __future__
import annotations
5 from collections.abc
import Mapping
7 from typing
import Any, Final
9 from aioswitcher.bridge
import SwitcherBase
10 from aioswitcher.device.tools
import validate_token
11 import voluptuous
as vol
16 from .const
import DOMAIN
17 from .utils
import async_discover_devices
19 _LOGGER = logging.getLogger(__name__)
22 CONFIG_SCHEMA: Final = vol.Schema(
24 vol.Required(CONF_USERNAME, default=
""): str,
25 vol.Required(CONF_TOKEN, default=
""): str,
31 """Handle Switcher config flow."""
35 username: str |
None =
None
36 token: str |
None =
None
37 discovered_devices: dict[str, SwitcherBase] = {}
40 self, user_input: dict[str, Any] |
None =
None
41 ) -> ConfigFlowResult:
42 """Handle the start of the config flow."""
48 self, user_input: dict[str, Any] |
None =
None
49 ) -> ConfigFlowResult:
50 """Handle user-confirmation of the config flow."""
55 if device.token_needed:
56 _LOGGER.debug(
"Device with ID %s requires a token", device_id)
57 return await self.async_step_credentials()
58 return await self._create_entry()
60 async
def async_step_credentials(
61 self, user_input: dict[str, Any] |
None =
None
62 ) -> ConfigFlowResult:
63 """Handle the credentials step."""
64 errors: dict[str, str] = {}
65 if user_input
is not None:
66 self.
usernameusername = user_input.get(CONF_USERNAME)
67 self.
tokentoken = user_input.get(CONF_TOKEN)
69 token_is_valid = await validate_token(
70 user_input[CONF_USERNAME], user_input[CONF_TOKEN]
73 return await self._create_entry()
74 errors[
"base"] =
"invalid_auth"
77 step_id=
"credentials", data_schema=CONFIG_SCHEMA, errors=errors
80 async
def async_step_reauth(
81 self, user_input: Mapping[str, Any]
82 ) -> ConfigFlowResult:
83 """Handle configuration by re-auth."""
84 return await self.async_step_reauth_confirm()
86 async
def async_step_reauth_confirm(
87 self, user_input: dict[str, Any] |
None =
None
88 ) -> ConfigFlowResult:
89 """Dialog that informs the user that reauth is required."""
90 errors: dict[str, str] = {}
92 if user_input
is not None:
93 token_is_valid = await validate_token(
94 user_input[CONF_USERNAME], user_input[CONF_TOKEN]
100 errors[
"base"] =
"invalid_auth"
103 step_id=
"reauth_confirm",
104 data_schema=CONFIG_SCHEMA,
112 CONF_USERNAME: self.
usernameusername,
113 CONF_TOKEN: self.
tokentoken,
ConfigFlowResult async_step_user(self, dict[str, Any]|None user_input=None)
ConfigEntry _get_reauth_entry(self)
ConfigFlowResult async_create_entry(self, *str title, Mapping[str, Any] data, str|None description=None, Mapping[str, str]|None description_placeholders=None, Mapping[str, Any]|None options=None)
ConfigFlowResult async_update_reload_and_abort(self, ConfigEntry entry, *str|None|UndefinedType unique_id=UNDEFINED, str|UndefinedType title=UNDEFINED, Mapping[str, Any]|UndefinedType data=UNDEFINED, Mapping[str, Any]|UndefinedType data_updates=UNDEFINED, Mapping[str, Any]|UndefinedType options=UNDEFINED, str|UndefinedType reason=UNDEFINED, bool reload_even_if_entry_is_unchanged=True)
ConfigFlowResult async_abort(self, *str reason, Mapping[str, str]|None description_placeholders=None)
ConfigFlowResult async_show_form(self, *str|None step_id=None, vol.Schema|None data_schema=None, dict[str, str]|None errors=None, Mapping[str, str]|None description_placeholders=None, bool|None last_step=None, str|None preview=None)
_FlowResultT async_show_form(self, *str|None step_id=None, vol.Schema|None data_schema=None, dict[str, str]|None errors=None, Mapping[str, str]|None description_placeholders=None, bool|None last_step=None, str|None preview=None)
_FlowResultT async_create_entry(self, *str|None title=None, Mapping[str, Any] data, str|None description=None, Mapping[str, str]|None description_placeholders=None)
_FlowResultT async_abort(self, *str reason, Mapping[str, str]|None description_placeholders=None)
list[ElkSystem] async_discover_devices(HomeAssistant hass, int timeout, str|None address=None)
er.RegistryEntry _create_entry(er.EntityRegistry entity_registry, str tag_id, str|None name)
config_entries.ConfigFlowResult async_step_confirm(self, dict[str, Any]|None user_input=None)