Home Assistant Unofficial Reference 2024.12.1
config_flow.py
Go to the documentation of this file.
1 """Config flow for Suez Water integration."""
2 
3 from __future__ import annotations
4 
5 import logging
6 from typing import Any
7 
8 from pysuez import PySuezError, SuezClient
9 import voluptuous as vol
10 
11 from homeassistant.config_entries import ConfigFlow, ConfigFlowResult
12 from homeassistant.const import CONF_PASSWORD, CONF_USERNAME
13 from homeassistant.exceptions import HomeAssistantError
14 
15 from .const import CONF_COUNTER_ID, DOMAIN
16 
17 _LOGGER = logging.getLogger(__name__)
18 
19 STEP_USER_DATA_SCHEMA = vol.Schema(
20  {
21  vol.Required(CONF_USERNAME): str,
22  vol.Required(CONF_PASSWORD): str,
23  vol.Optional(CONF_COUNTER_ID): str,
24  }
25 )
26 
27 
28 async def validate_input(data: dict[str, Any]) -> None:
29  """Validate the user input allows us to connect.
30 
31  Data has the keys from STEP_USER_DATA_SCHEMA with values provided by the user.
32  """
33  try:
34  counter_id = data.get(CONF_COUNTER_ID)
35  client = SuezClient(
36  data[CONF_USERNAME],
37  data[CONF_PASSWORD],
38  counter_id,
39  )
40  if not await client.check_credentials():
41  raise InvalidAuth
42  except PySuezError as ex:
43  raise CannotConnect from ex
44 
45  if counter_id is None:
46  try:
47  data[CONF_COUNTER_ID] = await client.find_counter()
48  except PySuezError as ex:
49  raise CounterNotFound from ex
50 
51 
52 class SuezWaterConfigFlow(ConfigFlow, domain=DOMAIN):
53  """Handle a config flow for Suez Water."""
54 
55  VERSION = 1
56 
57  async def async_step_user(
58  self, user_input: dict[str, Any] | None = None
59  ) -> ConfigFlowResult:
60  """Handle the initial step."""
61  errors: dict[str, str] = {}
62  if user_input is not None:
63  await self.async_set_unique_idasync_set_unique_id(user_input[CONF_USERNAME])
64  self._abort_if_unique_id_configured_abort_if_unique_id_configured()
65  try:
66  await validate_input(user_input)
67  except CannotConnect:
68  errors["base"] = "cannot_connect"
69  except InvalidAuth:
70  errors["base"] = "invalid_auth"
71  except CounterNotFound:
72  errors["base"] = "counter_not_found"
73  except Exception:
74  _LOGGER.exception("Unexpected exception")
75  errors["base"] = "unknown"
76  else:
77  return self.async_create_entryasync_create_entryasync_create_entry(
78  title=user_input[CONF_USERNAME], data=user_input
79  )
80 
81  return self.async_show_formasync_show_formasync_show_form(
82  step_id="user", data_schema=STEP_USER_DATA_SCHEMA, errors=errors
83  )
84 
85 
87  """Error to indicate we cannot connect."""
88 
89 
91  """Error to indicate there is invalid auth."""
92 
93 
95  """Error to indicate we cannot automatically found the counter id."""
ConfigFlowResult async_step_user(self, dict[str, Any]|None user_input=None)
Definition: config_flow.py:59
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)