Home Assistant Unofficial Reference 2024.12.1
config_flow.py
Go to the documentation of this file.
1 """Config flow for Skybell integration."""
2 
3 from __future__ import annotations
4 
5 from collections.abc import Mapping
6 from typing import Any
7 
8 from aioskybell import Skybell, exceptions
9 import voluptuous as vol
10 
11 from homeassistant.config_entries import ConfigFlow, ConfigFlowResult
12 from homeassistant.const import CONF_EMAIL, CONF_PASSWORD
13 from homeassistant.helpers.aiohttp_client import async_get_clientsession
14 
15 from .const import DOMAIN
16 
17 
18 class SkybellFlowHandler(ConfigFlow, domain=DOMAIN):
19  """Handle a config flow for Skybell."""
20 
21  reauth_email: str
22 
23  async def async_step_reauth(
24  self, entry_data: Mapping[str, Any]
25  ) -> ConfigFlowResult:
26  """Handle a reauthorization flow request."""
27  self.reauth_emailreauth_email = entry_data[CONF_EMAIL]
28  return await self.async_step_reauth_confirm()
29 
30  async def async_step_reauth_confirm(
31  self, user_input: dict[str, str] | None = None
32  ) -> ConfigFlowResult:
33  """Handle user's reauth credentials."""
34  errors = {}
35  if user_input:
36  password = user_input[CONF_PASSWORD]
37  _, error = await self._async_validate_input(self.reauth_emailreauth_email, password)
38  if error is None:
39  return self.async_update_reload_and_abortasync_update_reload_and_abort(
40  self._get_reauth_entry_get_reauth_entry(), data_updates=user_input
41  )
42 
43  errors["base"] = error
44  return self.async_show_formasync_show_formasync_show_form(
45  step_id="reauth_confirm",
46  data_schema=vol.Schema({vol.Required(CONF_PASSWORD): str}),
47  description_placeholders={CONF_EMAIL: self.reauth_emailreauth_email},
48  errors=errors,
49  )
50 
51  async def async_step_user(
52  self, user_input: dict[str, Any] | None = None
53  ) -> ConfigFlowResult:
54  """Handle a flow initiated by the user."""
55  errors = {}
56 
57  if user_input is not None:
58  email = user_input[CONF_EMAIL].lower()
59  password = user_input[CONF_PASSWORD]
60 
61  self._async_abort_entries_match_async_abort_entries_match({CONF_EMAIL: email})
62  user_id, error = await self._async_validate_input(email, password)
63  if error is None:
64  await self.async_set_unique_idasync_set_unique_id(user_id)
65  self._abort_if_unique_id_configured_abort_if_unique_id_configured()
66  return self.async_create_entryasync_create_entryasync_create_entry(
67  title=email,
68  data={CONF_EMAIL: email, CONF_PASSWORD: password},
69  )
70  errors["base"] = error
71 
72  user_input = user_input or {}
73  return self.async_show_formasync_show_formasync_show_form(
74  step_id="user",
75  data_schema=vol.Schema(
76  {
77  vol.Required(CONF_EMAIL, default=user_input.get(CONF_EMAIL)): str,
78  vol.Required(CONF_PASSWORD): str,
79  }
80  ),
81  errors=errors,
82  )
83 
84  async def _async_validate_input(self, email: str, password: str) -> tuple:
85  """Validate login credentials."""
86  skybell = Skybell(
87  username=email,
88  password=password,
89  disable_cache=True,
90  session=async_get_clientsession(self.hass),
91  )
92  try:
93  await skybell.async_initialize()
94  except exceptions.SkybellAuthenticationException:
95  return None, "invalid_auth"
96  except exceptions.SkybellException:
97  return None, "cannot_connect"
98  except Exception: # noqa: BLE001
99  return None, "unknown"
100  return skybell.user_id, None
ConfigFlowResult async_step_reauth(self, Mapping[str, Any] entry_data)
Definition: config_flow.py:25
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 _async_abort_entries_match(self, dict[str, Any]|None match_dict=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)
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)