Home Assistant Unofficial Reference 2024.12.1
config_flow.py
Go to the documentation of this file.
1 """Config flow for Aseko Pool Live 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 aioaseko import Aseko, AsekoAPIError, AsekoInvalidCredentials
10 import voluptuous as vol
11 
12 from homeassistant.config_entries import SOURCE_REAUTH, ConfigFlow, ConfigFlowResult
13 from homeassistant.const import CONF_EMAIL, CONF_PASSWORD, CONF_UNIQUE_ID
14 
15 from .const import DOMAIN
16 
17 _LOGGER = logging.getLogger(__name__)
18 
19 
20 class AsekoConfigFlow(ConfigFlow, domain=DOMAIN):
21  """Handle a config flow for Aseko Pool Live."""
22 
23  VERSION = 2
24 
25  data_schema = vol.Schema(
26  {
27  vol.Required(CONF_EMAIL): str,
28  vol.Required(CONF_PASSWORD): str,
29  }
30  )
31 
32  async def get_account_info(self, email: str, password: str) -> dict[str, Any]:
33  """Get account info from the mobile API and the web API."""
34  aseko = Aseko(email, password)
35  user = await aseko.login()
36  return {
37  CONF_EMAIL: email,
38  CONF_PASSWORD: password,
39  CONF_UNIQUE_ID: user.user_id,
40  }
41 
42  async def async_step_user(
43  self, user_input: Mapping[str, Any] | None = None
44  ) -> ConfigFlowResult:
45  """Handle the initial step."""
46 
47  errors = {}
48 
49  if user_input is not None:
50  try:
51  info = await self.get_account_infoget_account_info(
52  user_input[CONF_EMAIL], user_input[CONF_PASSWORD]
53  )
54  except AsekoAPIError:
55  errors["base"] = "cannot_connect"
56  except AsekoInvalidCredentials:
57  errors["base"] = "invalid_auth"
58  except Exception:
59  _LOGGER.exception("Unexpected exception")
60  errors["base"] = "unknown"
61  else:
62  return await self.async_store_credentialsasync_store_credentials(info)
63 
64  return self.async_show_formasync_show_formasync_show_form(
65  step_id="user",
66  data_schema=self.data_schemadata_schema,
67  errors=errors,
68  )
69 
70  async def async_store_credentials(self, info: dict[str, Any]) -> ConfigFlowResult:
71  """Store validated credentials."""
72 
73  await self.async_set_unique_idasync_set_unique_id(info[CONF_UNIQUE_ID])
74  if self.sourcesourcesourcesource == SOURCE_REAUTH:
75  self._abort_if_unique_id_mismatch_abort_if_unique_id_mismatch()
76  return self.async_update_reload_and_abortasync_update_reload_and_abort(
77  self._get_reauth_entry_get_reauth_entry(),
78  title=info[CONF_EMAIL],
79  data={
80  CONF_EMAIL: info[CONF_EMAIL],
81  CONF_PASSWORD: info[CONF_PASSWORD],
82  },
83  )
84 
85  self._abort_if_unique_id_configured_abort_if_unique_id_configured()
86 
87  return self.async_create_entryasync_create_entryasync_create_entry(
88  title=info[CONF_EMAIL],
89  data={
90  CONF_EMAIL: info[CONF_EMAIL],
91  CONF_PASSWORD: info[CONF_PASSWORD],
92  },
93  )
94 
95  async def async_step_reauth(
96  self, entry_data: Mapping[str, Any]
97  ) -> ConfigFlowResult:
98  """Perform reauth upon an API authentication error."""
99  return await self.async_step_reauth_confirmasync_step_reauth_confirm()
100 
102  self, user_input: dict[str, Any] | None = None
103  ) -> ConfigFlowResult:
104  """Dialog that informs the user that reauth is required."""
105 
106  errors = {}
107  if user_input is not None:
108  try:
109  info = await self.get_account_infoget_account_info(
110  user_input[CONF_EMAIL], user_input[CONF_PASSWORD]
111  )
112  except AsekoAPIError:
113  errors["base"] = "cannot_connect"
114  except AsekoInvalidCredentials:
115  errors["base"] = "invalid_auth"
116  except Exception:
117  _LOGGER.exception("Unexpected exception")
118  errors["base"] = "unknown"
119  else:
120  return await self.async_store_credentialsasync_store_credentials(info)
121 
122  return self.async_show_formasync_show_formasync_show_form(
123  step_id="reauth_confirm",
124  data_schema=self.data_schemadata_schema,
125  errors=errors,
126  )
ConfigFlowResult async_step_reauth(self, Mapping[str, Any] entry_data)
Definition: config_flow.py:97
ConfigFlowResult async_step_user(self, Mapping[str, Any]|None user_input=None)
Definition: config_flow.py:44
dict[str, Any] get_account_info(self, str email, str password)
Definition: config_flow.py:32
ConfigFlowResult async_step_reauth_confirm(self, dict[str, Any]|None user_input=None)
Definition: config_flow.py:103
ConfigFlowResult async_store_credentials(self, dict[str, Any] info)
Definition: config_flow.py:70
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)
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)