Home Assistant Unofficial Reference 2024.12.1
config_flow.py
Go to the documentation of this file.
1 """Config flow for Tedee integration."""
2 
3 from collections.abc import Mapping
4 import logging
5 from typing import Any
6 
7 from aiotedee import (
8  TedeeAuthException,
9  TedeeClient,
10  TedeeClientException,
11  TedeeDataUpdateException,
12  TedeeLocalAuthException,
13 )
14 import voluptuous as vol
15 
16 from homeassistant.components.webhook import async_generate_id as webhook_generate_id
17 from homeassistant.config_entries import (
18  SOURCE_REAUTH,
19  SOURCE_RECONFIGURE,
20  ConfigFlow,
21  ConfigFlowResult,
22 )
23 from homeassistant.const import CONF_HOST, CONF_WEBHOOK_ID
24 from homeassistant.helpers.aiohttp_client import async_get_clientsession
25 
26 from .const import CONF_LOCAL_ACCESS_TOKEN, DOMAIN, NAME
27 
28 _LOGGER = logging.getLogger(__name__)
29 
30 
31 class TedeeConfigFlow(ConfigFlow, domain=DOMAIN):
32  """Handle a config flow for Tedee."""
33 
34  VERSION = 1
35  MINOR_VERSION = 2
36 
37  async def async_step_user(
38  self, user_input: dict[str, Any] | None = None
39  ) -> ConfigFlowResult:
40  """Handle the initial step."""
41  errors: dict[str, str] = {}
42 
43  if user_input is not None:
44  if self.sourcesourcesourcesource == SOURCE_REAUTH:
45  host = self._get_reauth_entry_get_reauth_entry().data[CONF_HOST]
46  else:
47  host = user_input[CONF_HOST]
48  local_access_token = user_input[CONF_LOCAL_ACCESS_TOKEN]
49  tedee_client = TedeeClient(
50  local_token=local_access_token,
51  local_ip=host,
52  session=async_get_clientsession(self.hass),
53  )
54  try:
55  local_bridge = await tedee_client.get_local_bridge()
56  except (TedeeAuthException, TedeeLocalAuthException):
57  errors[CONF_LOCAL_ACCESS_TOKEN] = "invalid_api_key"
58  except TedeeClientException:
59  errors[CONF_HOST] = "invalid_host"
60  except TedeeDataUpdateException as exc:
61  _LOGGER.error("Error during local bridge discovery: %s", exc)
62  errors["base"] = "cannot_connect"
63  else:
64  await self.async_set_unique_idasync_set_unique_id(local_bridge.serial)
65  if self.sourcesourcesourcesource == SOURCE_REAUTH:
66  self._abort_if_unique_id_mismatch_abort_if_unique_id_mismatch()
67  return self.async_update_reload_and_abortasync_update_reload_and_abort(
68  self._get_reauth_entry_get_reauth_entry(), data_updates=user_input
69  )
70  if self.sourcesourcesourcesource == SOURCE_RECONFIGURE:
71  self._abort_if_unique_id_mismatch_abort_if_unique_id_mismatch()
72  return self.async_update_reload_and_abortasync_update_reload_and_abort(
73  self._get_reconfigure_entry_get_reconfigure_entry(), data_updates=user_input
74  )
75  self._abort_if_unique_id_configured_abort_if_unique_id_configured()
76  return self.async_create_entryasync_create_entryasync_create_entry(
77  title=NAME,
78  data={**user_input, CONF_WEBHOOK_ID: webhook_generate_id()},
79  )
80 
81  return self.async_show_formasync_show_formasync_show_form(
82  step_id="user",
83  data_schema=vol.Schema(
84  {
85  vol.Required(
86  CONF_HOST,
87  ): str,
88  vol.Required(
89  CONF_LOCAL_ACCESS_TOKEN,
90  ): str,
91  }
92  ),
93  errors=errors,
94  )
95 
96  async def async_step_reauth(
97  self, entry_data: Mapping[str, Any]
98  ) -> ConfigFlowResult:
99  """Perform reauth upon an API authentication error."""
100  return await self.async_step_reauth_confirmasync_step_reauth_confirm()
101 
103  self, user_input: dict[str, Any] | None = None
104  ) -> ConfigFlowResult:
105  """Dialog that informs the user that reauth is required."""
106  if not user_input:
107  return self.async_show_formasync_show_formasync_show_form(
108  step_id="reauth_confirm",
109  data_schema=vol.Schema(
110  {
111  vol.Required(
112  CONF_LOCAL_ACCESS_TOKEN,
113  default=self._get_reauth_entry_get_reauth_entry().data[
114  CONF_LOCAL_ACCESS_TOKEN
115  ],
116  ): str,
117  }
118  ),
119  )
120  return await self.async_step_userasync_step_userasync_step_user(user_input)
121 
123  self, user_input: dict[str, Any] | None = None
124  ) -> ConfigFlowResult:
125  """Perform a reconfiguration."""
126  if not user_input:
127  reconfigure_entry = self._get_reconfigure_entry_get_reconfigure_entry()
128  return self.async_show_formasync_show_formasync_show_form(
129  step_id="reconfigure",
130  data_schema=vol.Schema(
131  {
132  vol.Required(
133  CONF_HOST, default=reconfigure_entry.data[CONF_HOST]
134  ): str,
135  vol.Required(
136  CONF_LOCAL_ACCESS_TOKEN,
137  default=reconfigure_entry.data[CONF_LOCAL_ACCESS_TOKEN],
138  ): str,
139  }
140  ),
141  )
142  return await self.async_step_userasync_step_userasync_step_user(user_input)
ConfigFlowResult async_step_user(self, dict[str, Any]|None user_input=None)
Definition: config_flow.py:39
ConfigFlowResult async_step_reauth_confirm(self, dict[str, Any]|None user_input=None)
Definition: config_flow.py:104
ConfigFlowResult async_step_reconfigure(self, dict[str, Any]|None user_input=None)
Definition: config_flow.py:124
ConfigFlowResult async_step_reauth(self, Mapping[str, Any] entry_data)
Definition: config_flow.py:98
None _abort_if_unique_id_configured(self, dict[str, Any]|None updates=None, bool reload_on_update=True, *str error="already_configured")
ConfigEntry|None async_set_unique_id(self, str|None unique_id=None, *bool raise_on_progress=True)
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_step_user(self, dict[str, Any]|None user_input=None)
None _abort_if_unique_id_mismatch(self, *str reason="unique_id_mismatch", 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)
str|None source(self)
aiohttp.ClientSession async_get_clientsession(HomeAssistant hass, bool verify_ssl=True, socket.AddressFamily family=socket.AF_UNSPEC, ssl_util.SSLCipherList ssl_cipher=ssl_util.SSLCipherList.PYTHON_DEFAULT)