Home Assistant Unofficial Reference 2024.12.1
config_flow.py
Go to the documentation of this file.
1 """Config flow to configure the SmartTub integration."""
2 
3 from __future__ import annotations
4 
5 from collections.abc import Mapping
6 from typing import Any
7 
8 from smarttub import LoginFailed
9 import voluptuous as vol
10 
11 from homeassistant.config_entries import SOURCE_REAUTH, ConfigFlow, ConfigFlowResult
12 from homeassistant.const import CONF_EMAIL, CONF_PASSWORD
13 
14 from .const import DOMAIN
15 from .controller import SmartTubController
16 
17 DATA_SCHEMA = vol.Schema(
18  {vol.Required(CONF_EMAIL): str, vol.Required(CONF_PASSWORD): str}
19 )
20 
21 
22 class SmartTubConfigFlow(ConfigFlow, domain=DOMAIN):
23  """SmartTub configuration flow."""
24 
25  VERSION = 1
26 
27  async def async_step_user(
28  self, user_input: dict[str, Any] | None = None
29  ) -> ConfigFlowResult:
30  """Handle a flow initiated by the user."""
31  errors = {}
32 
33  if user_input is not None:
34  controller = SmartTubController(self.hass)
35  try:
36  account = await controller.login(
37  user_input[CONF_EMAIL], user_input[CONF_PASSWORD]
38  )
39 
40  except LoginFailed:
41  errors["base"] = "invalid_auth"
42  else:
43  await self.async_set_unique_idasync_set_unique_id(account.id)
44 
45  if self.sourcesourcesource != SOURCE_REAUTH:
46  self._abort_if_unique_id_configured_abort_if_unique_id_configured()
47  return self.async_create_entryasync_create_entryasync_create_entry(
48  title=user_input[CONF_EMAIL], data=user_input
49  )
50 
51  # this is a reauth attempt
52  self._abort_if_unique_id_mismatch_abort_if_unique_id_mismatch(reason="already_configured")
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 
57  return self.async_show_formasync_show_formasync_show_form(
58  step_id="user", data_schema=DATA_SCHEMA, errors=errors
59  )
60 
61  async def async_step_reauth(
62  self, entry_data: Mapping[str, Any]
63  ) -> ConfigFlowResult:
64  """Get new credentials if the current ones don't work anymore."""
65  return await self.async_step_reauth_confirmasync_step_reauth_confirm()
66 
68  self, user_input: dict[str, str] | None = None
69  ) -> ConfigFlowResult:
70  """Dialog that informs the user that reauth is required."""
71  if user_input is None:
72  # same as DATA_SCHEMA but with default email
73  data_schema = vol.Schema(
74  {
75  vol.Required(
76  CONF_EMAIL,
77  default=self._get_reauth_entry_get_reauth_entry().data.get(CONF_EMAIL),
78  ): str,
79  vol.Required(CONF_PASSWORD): str,
80  }
81  )
82  return self.async_show_formasync_show_formasync_show_form(
83  step_id="reauth_confirm",
84  data_schema=data_schema,
85  )
86  return await self.async_step_userasync_step_userasync_step_user(user_input)
ConfigFlowResult async_step_reauth_confirm(self, dict[str, str]|None user_input=None)
Definition: config_flow.py:69
ConfigFlowResult async_step_user(self, dict[str, Any]|None user_input=None)
Definition: config_flow.py:29
ConfigFlowResult async_step_reauth(self, Mapping[str, Any] entry_data)
Definition: config_flow.py:63
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)