Home Assistant Unofficial Reference 2024.12.1
config_flow.py
Go to the documentation of this file.
1 """Config flow for Bryant Evolution integration."""
2 
3 from __future__ import annotations
4 
5 import logging
6 from typing import Any
7 
8 from evolutionhttp import BryantEvolutionLocalClient
9 import voluptuous as vol
10 
11 from homeassistant.config_entries import ConfigFlow, ConfigFlowResult
12 from homeassistant.const import CONF_FILENAME
13 
14 from .const import CONF_SYSTEM_ZONE, DOMAIN
15 
16 _LOGGER = logging.getLogger(__name__)
17 
18 STEP_USER_DATA_SCHEMA = vol.Schema(
19  {
20  vol.Required(CONF_FILENAME, default="/dev/ttyUSB0"): str,
21  }
22 )
23 
24 
25 async def _enumerate_sz(tty: str) -> list[tuple[int, int]]:
26  """Return (system, zone) tuples for each system+zone accessible through tty."""
27  return [
28  (system_id, zone.zone_id)
29  for system_id in (1, 2)
30  for zone in await BryantEvolutionLocalClient.enumerate_zones(system_id, tty)
31  ]
32 
33 
34 class BryantConfigFlow(ConfigFlow, domain=DOMAIN):
35  """Handle a config flow for Bryant Evolution."""
36 
37  async def async_step_user(
38  self, user_input: dict[str, Any] | None = None
39  ) -> ConfigFlowResult:
40  """Handle the initial step."""
41  errors: dict[str, str] = {}
42  if user_input is not None:
43  try:
44  system_zone = await _enumerate_sz(user_input[CONF_FILENAME])
45  except FileNotFoundError:
46  _LOGGER.error("Could not open %s: not found", user_input[CONF_FILENAME])
47  errors["base"] = "cannot_connect"
48  else:
49  if len(system_zone) != 0:
50  return self.async_create_entryasync_create_entryasync_create_entry(
51  title=f"SAM at {user_input[CONF_FILENAME]}",
52  data={
53  CONF_FILENAME: user_input[CONF_FILENAME],
54  CONF_SYSTEM_ZONE: system_zone,
55  },
56  )
57  errors["base"] = "cannot_connect"
58  return self.async_show_formasync_show_formasync_show_form(
59  step_id="user", data_schema=STEP_USER_DATA_SCHEMA, errors=errors
60  )
61 
62  async def async_step_reconfigure(
63  self, user_input: dict[str, Any] | None = None
64  ) -> ConfigFlowResult:
65  """Handle integration reconfiguration."""
66  errors: dict[str, str] = {}
67  if user_input is not None:
68  system_zone = await _enumerate_sz(user_input[CONF_FILENAME])
69  if len(system_zone) != 0:
70  return self.async_update_reload_and_abortasync_update_reload_and_abort(
71  self._get_reconfigure_entry_get_reconfigure_entry(),
72  data={
73  CONF_FILENAME: user_input[CONF_FILENAME],
74  CONF_SYSTEM_ZONE: system_zone,
75  },
76  )
77  errors["base"] = "cannot_connect"
78  return self.async_show_formasync_show_formasync_show_form(
79  step_id="reconfigure",
80  data_schema=STEP_USER_DATA_SCHEMA,
81  errors=errors,
82  )
ConfigFlowResult async_step_user(self, dict[str, Any]|None user_input=None)
Definition: config_flow.py:39
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)
list[tuple[int, int]] _enumerate_sz(str tty)
Definition: config_flow.py:25