Home Assistant Unofficial Reference 2024.12.1
config_flow.py
Go to the documentation of this file.
1 """The Things Network's integration config flow."""
2 
3 from collections.abc import Mapping
4 import logging
5 from typing import Any
6 
7 from ttn_client import TTNAuthError, TTNClient
8 import voluptuous as vol
9 
10 from homeassistant.config_entries import SOURCE_REAUTH, ConfigFlow, ConfigFlowResult
11 from homeassistant.const import CONF_API_KEY, CONF_HOST
13  TextSelector,
14  TextSelectorConfig,
15  TextSelectorType,
16 )
17 
18 from .const import CONF_APP_ID, DOMAIN, TTN_API_HOST
19 
20 _LOGGER = logging.getLogger(__name__)
21 
22 
23 class TTNFlowHandler(ConfigFlow, domain=DOMAIN):
24  """Handle a config flow."""
25 
26  VERSION = 1
27 
28  async def async_step_user(
29  self, user_input: Mapping[str, Any] | None = None
30  ) -> ConfigFlowResult:
31  """User initiated config flow."""
32 
33  errors = {}
34  if user_input is not None:
35  client = TTNClient(
36  user_input[CONF_HOST],
37  user_input[CONF_APP_ID],
38  user_input[CONF_API_KEY],
39  0,
40  )
41  try:
42  await client.fetch_data()
43  except TTNAuthError:
44  _LOGGER.exception("Error authenticating with The Things Network")
45  errors["base"] = "invalid_auth"
46  except Exception:
47  _LOGGER.exception("Unknown error occurred")
48  errors["base"] = "unknown"
49 
50  if not errors:
51  # Create entry
52  if self.sourcesourcesourcesource == SOURCE_REAUTH:
53  return self.async_update_reload_and_abortasync_update_reload_and_abort(
54  self._get_reauth_entry_get_reauth_entry(), data=user_input
55  )
56  await self.async_set_unique_idasync_set_unique_id(user_input[CONF_APP_ID])
57  self._abort_if_unique_id_configured_abort_if_unique_id_configured()
58 
59  return self.async_create_entryasync_create_entryasync_create_entry(
60  title=str(user_input[CONF_APP_ID]),
61  data=user_input,
62  )
63 
64  # Show form for user to provide settings
65  if not user_input:
66  if self.sourcesourcesourcesource == SOURCE_REAUTH:
67  user_input = self._get_reauth_entry_get_reauth_entry().data
68  else:
69  user_input = {CONF_HOST: TTN_API_HOST}
70 
71  schema = self.add_suggested_values_to_schemaadd_suggested_values_to_schema(
72  vol.Schema(
73  {
74  vol.Required(CONF_HOST): str,
75  vol.Required(CONF_APP_ID): str,
76  vol.Required(CONF_API_KEY): TextSelector(
78  type=TextSelectorType.PASSWORD, autocomplete="api_key"
79  )
80  ),
81  }
82  ),
83  user_input,
84  )
85  return self.async_show_formasync_show_formasync_show_form(step_id="user", data_schema=schema, errors=errors)
86 
87  async def async_step_reauth(
88  self, entry_data: Mapping[str, Any]
89  ) -> ConfigFlowResult:
90  """Handle a flow initialized by a reauth event."""
91  return await self.async_step_reauth_confirmasync_step_reauth_confirm()
92 
94  self, user_input: dict[str, Any] | None = None
95  ) -> ConfigFlowResult:
96  """Dialog that informs the user that reauth is required."""
97  if user_input is None:
98  return self.async_show_formasync_show_formasync_show_form(step_id="reauth_confirm")
99  return await self.async_step_userasync_step_userasync_step_user()
ConfigFlowResult async_step_reauth(self, Mapping[str, Any] entry_data)
Definition: config_flow.py:89
ConfigFlowResult async_step_user(self, Mapping[str, Any]|None user_input=None)
Definition: config_flow.py:30
ConfigFlowResult async_step_reauth_confirm(self, dict[str, Any]|None user_input=None)
Definition: config_flow.py:95
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)
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)
str
vol.Schema add_suggested_values_to_schema(self, vol.Schema data_schema, Mapping[str, Any]|None suggested_values)
_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)