Home Assistant Unofficial Reference 2024.12.1
config_flow.py
Go to the documentation of this file.
1 """Config flow for A. O. Smith integration."""
2 
3 from __future__ import annotations
4 
5 from collections.abc import Mapping
6 import logging
7 from typing import Any
8 
9 from py_aosmith import AOSmithAPIClient, AOSmithInvalidCredentialsException
10 import voluptuous as vol
11 
12 from homeassistant.config_entries import ConfigFlow, ConfigFlowResult
13 from homeassistant.const import CONF_EMAIL, CONF_PASSWORD
14 from homeassistant.helpers import aiohttp_client
15 
16 from .const import DOMAIN
17 
18 _LOGGER = logging.getLogger(__name__)
19 
20 
21 class AOSmithConfigFlow(ConfigFlow, domain=DOMAIN):
22  """Handle a config flow for A. O. Smith."""
23 
24  VERSION = 1
25 
26  _reauth_email: str
27 
29  self, email: str, password: str
30  ) -> str | None:
31  """Validate the credentials. Return an error string, or None if successful."""
32  session = aiohttp_client.async_get_clientsession(self.hass)
33  client = AOSmithAPIClient(email, password, session)
34 
35  try:
36  await client.get_devices()
37  except AOSmithInvalidCredentialsException:
38  return "invalid_auth"
39  except Exception:
40  _LOGGER.exception("Unexpected exception")
41  return "unknown"
42 
43  return None
44 
45  async def async_step_user(
46  self, user_input: dict[str, Any] | None = None
47  ) -> ConfigFlowResult:
48  """Handle the initial step."""
49  errors: dict[str, str] = {}
50  if user_input is not None:
51  unique_id = user_input[CONF_EMAIL].lower()
52  await self.async_set_unique_idasync_set_unique_id(unique_id)
53  self._abort_if_unique_id_configured_abort_if_unique_id_configured()
54 
55  error = await self._async_validate_credentials_async_validate_credentials(
56  user_input[CONF_EMAIL], user_input[CONF_PASSWORD]
57  )
58  if error is None:
59  return self.async_create_entryasync_create_entryasync_create_entry(
60  title=user_input[CONF_EMAIL], data=user_input
61  )
62 
63  errors["base"] = error
64 
65  return self.async_show_formasync_show_formasync_show_form(
66  step_id="user",
67  data_schema=vol.Schema(
68  {
69  vol.Required(CONF_EMAIL): str,
70  vol.Required(CONF_PASSWORD): str,
71  }
72  ),
73  errors=errors,
74  )
75 
76  async def async_step_reauth(
77  self, entry_data: Mapping[str, Any]
78  ) -> ConfigFlowResult:
79  """Perform reauth if the user credentials have changed."""
80  self._reauth_email_reauth_email = entry_data[CONF_EMAIL]
81  return await self.async_step_reauth_confirmasync_step_reauth_confirm()
82 
84  self, user_input: dict[str, Any] | None = None
85  ) -> ConfigFlowResult:
86  """Handle user's reauth credentials."""
87  errors: dict[str, str] = {}
88  if user_input:
89  password = user_input[CONF_PASSWORD]
90 
91  error = await self._async_validate_credentials_async_validate_credentials(self._reauth_email_reauth_email, password)
92  if error is None:
93  return self.async_update_reload_and_abortasync_update_reload_and_abort(
94  self._get_reauth_entry_get_reauth_entry(),
95  data_updates=user_input,
96  )
97  errors["base"] = error
98 
99  return self.async_show_formasync_show_formasync_show_form(
100  step_id="reauth_confirm",
101  data_schema=vol.Schema({vol.Required(CONF_PASSWORD): str}),
102  description_placeholders={CONF_EMAIL: self._reauth_email_reauth_email},
103  errors=errors,
104  )
ConfigFlowResult async_step_reauth_confirm(self, dict[str, Any]|None user_input=None)
Definition: config_flow.py:85
ConfigFlowResult async_step_reauth(self, Mapping[str, Any] entry_data)
Definition: config_flow.py:78
ConfigFlowResult async_step_user(self, dict[str, Any]|None user_input=None)
Definition: config_flow.py:47
str|None _async_validate_credentials(self, str email, str password)
Definition: config_flow.py:30
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_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)