Home Assistant Unofficial Reference 2024.12.1
config_flow.py
Go to the documentation of this file.
1 """Config flow for the Rova platform."""
2 
3 from typing import Any
4 
5 from requests.exceptions import ConnectTimeout, HTTPError
6 from rova.rova import Rova
7 import voluptuous as vol
8 
9 from homeassistant.config_entries import ConfigFlow, ConfigFlowResult
10 
11 from .const import CONF_HOUSE_NUMBER, CONF_HOUSE_NUMBER_SUFFIX, CONF_ZIP_CODE, DOMAIN
12 
13 
14 class RovaConfigFlow(ConfigFlow, domain=DOMAIN):
15  """Handle Rova config flow."""
16 
17  VERSION = 1
18 
19  async def async_step_user(
20  self, user_input: dict[str, Any] | None = None
21  ) -> ConfigFlowResult:
22  """Step when user initializes a integration."""
23  errors: dict[str, str] = {}
24 
25  if user_input is not None:
26  # generate unique name for rova integration
27  zip_code = user_input[CONF_ZIP_CODE]
28  number = user_input[CONF_HOUSE_NUMBER]
29  suffix = user_input[CONF_HOUSE_NUMBER_SUFFIX]
30 
31  await self.async_set_unique_idasync_set_unique_id(f"{zip_code}{number}{suffix}".strip())
32  self._abort_if_unique_id_configured_abort_if_unique_id_configured()
33 
34  api = Rova(zip_code, number, suffix)
35 
36  try:
37  if not await self.hass.async_add_executor_job(api.is_rova_area):
38  errors = {"base": "invalid_rova_area"}
39  except (ConnectTimeout, HTTPError):
40  errors = {"base": "cannot_connect"}
41 
42  if not errors:
43  return self.async_create_entryasync_create_entryasync_create_entry(
44  title=f"{zip_code} {number} {suffix}".strip(),
45  data=user_input,
46  )
47 
48  return self.async_show_formasync_show_formasync_show_form(
49  step_id="user",
50  data_schema=self.add_suggested_values_to_schemaadd_suggested_values_to_schema(
51  vol.Schema(
52  {
53  vol.Required(CONF_ZIP_CODE): str,
54  vol.Required(CONF_HOUSE_NUMBER): str,
55  vol.Optional(CONF_HOUSE_NUMBER_SUFFIX, default=""): str,
56  }
57  ),
58  user_input,
59  ),
60  errors=errors,
61  )
ConfigFlowResult async_step_user(self, dict[str, Any]|None user_input=None)
Definition: config_flow.py:21
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)
vol.Schema add_suggested_values_to_schema(self, vol.Schema data_schema, Mapping[str, Any]|None suggested_values)
_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)