Home Assistant Unofficial Reference 2024.12.1
config_flow.py
Go to the documentation of this file.
1 """Config flow for PoolSense integration."""
2 
3 import logging
4 from typing import Any
5 
6 from poolsense import PoolSense
7 import voluptuous as vol
8 
9 from homeassistant.config_entries import ConfigFlow, ConfigFlowResult
10 from homeassistant.const import CONF_EMAIL, CONF_PASSWORD
11 from homeassistant.helpers import aiohttp_client
12 
13 from .const import DOMAIN
14 
15 _LOGGER = logging.getLogger(__name__)
16 
17 
18 class PoolSenseConfigFlow(ConfigFlow, domain=DOMAIN):
19  """Handle a config flow for PoolSense."""
20 
21  VERSION = 1
22 
23  async def async_step_user(
24  self, user_input: dict[str, Any] | None = None
25  ) -> ConfigFlowResult:
26  """Handle the initial step."""
27  errors = {}
28 
29  if user_input is not None:
30  await self.async_set_unique_idasync_set_unique_id(user_input[CONF_EMAIL])
31  self._abort_if_unique_id_configured_abort_if_unique_id_configured()
32 
33  _LOGGER.debug(
34  "Configuring user: %s - Password hidden", user_input[CONF_EMAIL]
35  )
36 
37  poolsense = PoolSense(
38  aiohttp_client.async_get_clientsession(self.hass),
39  user_input[CONF_EMAIL],
40  user_input[CONF_PASSWORD],
41  )
42  api_key_valid = await poolsense.test_poolsense_credentials()
43 
44  if not api_key_valid:
45  errors["base"] = "invalid_auth"
46 
47  if not errors:
48  return self.async_create_entryasync_create_entryasync_create_entry(
49  title=user_input[CONF_EMAIL],
50  data={
51  CONF_EMAIL: user_input[CONF_EMAIL],
52  CONF_PASSWORD: user_input[CONF_PASSWORD],
53  },
54  )
55 
56  return self.async_show_formasync_show_formasync_show_form(
57  step_id="user",
58  data_schema=vol.Schema(
59  {vol.Required(CONF_EMAIL): str, vol.Required(CONF_PASSWORD): str}
60  ),
61  errors=errors,
62  )
ConfigFlowResult async_step_user(self, dict[str, Any]|None user_input=None)
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_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)