Home Assistant Unofficial Reference 2024.12.1
config_flow.py
Go to the documentation of this file.
1 """Config flow for Switcher integration."""
2 
3 from __future__ import annotations
4 
5 from collections.abc import Mapping
6 import logging
7 from typing import Any, Final
8 
9 from aioswitcher.bridge import SwitcherBase
10 from aioswitcher.device.tools import validate_token
11 import voluptuous as vol
12 
13 from homeassistant.config_entries import ConfigFlow, ConfigFlowResult
14 from homeassistant.const import CONF_TOKEN, CONF_USERNAME
15 
16 from .const import DOMAIN
17 from .utils import async_discover_devices
18 
19 _LOGGER = logging.getLogger(__name__)
20 
21 
22 CONFIG_SCHEMA: Final = vol.Schema(
23  {
24  vol.Required(CONF_USERNAME, default=""): str,
25  vol.Required(CONF_TOKEN, default=""): str,
26  }
27 )
28 
29 
30 class SwitcherFlowHandler(ConfigFlow, domain=DOMAIN):
31  """Handle Switcher config flow."""
32 
33  VERSION = 1
34 
35  username: str | None = None
36  token: str | None = None
37  discovered_devices: dict[str, SwitcherBase] = {}
38 
39  async def async_step_user(
40  self, user_input: dict[str, Any] | None = None
41  ) -> ConfigFlowResult:
42  """Handle the start of the config flow."""
43  self.discovered_devicesdiscovered_devices = await async_discover_devices()
44 
45  return self.async_show_formasync_show_formasync_show_form(step_id="confirm")
46 
47  async def async_step_confirm(
48  self, user_input: dict[str, Any] | None = None
49  ) -> ConfigFlowResult:
50  """Handle user-confirmation of the config flow."""
51  if len(self.discovered_devicesdiscovered_devices) == 0:
52  return self.async_abortasync_abortasync_abort(reason="no_devices_found")
53 
54  for device_id, device in self.discovered_devicesdiscovered_devices.items():
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()
59 
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)
68 
69  token_is_valid = await validate_token(
70  user_input[CONF_USERNAME], user_input[CONF_TOKEN]
71  )
72  if token_is_valid:
73  return await self._create_entry()
74  errors["base"] = "invalid_auth"
75 
76  return self.async_show_formasync_show_formasync_show_form(
77  step_id="credentials", data_schema=CONFIG_SCHEMA, errors=errors
78  )
79 
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()
85 
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] = {}
91 
92  if user_input is not None:
93  token_is_valid = await validate_token(
94  user_input[CONF_USERNAME], user_input[CONF_TOKEN]
95  )
96  if token_is_valid:
97  return self.async_update_reload_and_abortasync_update_reload_and_abort(
98  self._get_reauth_entry_get_reauth_entry(), data_updates=user_input
99  )
100  errors["base"] = "invalid_auth"
101 
102  return self.async_show_formasync_show_formasync_show_form(
103  step_id="reauth_confirm",
104  data_schema=CONFIG_SCHEMA,
105  errors=errors,
106  )
107 
108  async def _create_entry(self) -> ConfigFlowResult:
109  return self.async_create_entryasync_create_entryasync_create_entry(
110  title="Switcher",
111  data={
112  CONF_USERNAME: self.usernameusername,
113  CONF_TOKEN: self.tokentoken,
114  },
115  )
ConfigFlowResult async_step_user(self, dict[str, Any]|None user_input=None)
Definition: config_flow.py:41
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)
Definition: discovery.py:43
er.RegistryEntry _create_entry(er.EntityRegistry entity_registry, str tag_id, str|None name)
Definition: __init__.py:78
config_entries.ConfigFlowResult async_step_confirm(self, dict[str, Any]|None user_input=None)