Home Assistant Unofficial Reference 2024.12.1
config_flow.py
Go to the documentation of this file.
1 """Config flow for ProgettiHWSW Automation integration."""
2 
3 from typing import TYPE_CHECKING, Any
4 
5 from ProgettiHWSW.ProgettiHWSWAPI import ProgettiHWSWAPI
6 import voluptuous as vol
7 
8 from homeassistant.config_entries import ConfigFlow, ConfigFlowResult
9 from homeassistant.core import HomeAssistant
10 from homeassistant.exceptions import HomeAssistantError
11 
12 from .const import DOMAIN
13 
14 DATA_SCHEMA = vol.Schema(
15  {vol.Required("host"): str, vol.Required("port", default=80): int}
16 )
17 
18 
19 async def validate_input(hass: HomeAssistant, data):
20  """Validate the user host input."""
21 
22  api_instance = ProgettiHWSWAPI(f'{data["host"]}:{data["port"]}')
23  is_valid = await api_instance.check_board()
24 
25  if not is_valid:
26  raise CannotConnect
27 
28  return {
29  "title": is_valid["title"],
30  "relay_count": is_valid["relay_count"],
31  "input_count": is_valid["input_count"],
32  "is_old": is_valid["is_old"],
33  }
34 
35 
36 class ProgettiHWSWConfigFlow(ConfigFlow, domain=DOMAIN):
37  """Handle a config flow for ProgettiHWSW Automation."""
38 
39  VERSION = 1
40 
41  def __init__(self) -> None:
42  """Initialize class variables."""
43  self.s1_ins1_in: dict[str, Any] | None = None
44 
46  self, user_input: dict[str, str] | None = None
47  ) -> ConfigFlowResult:
48  """Manage relay modes step."""
49  errors: dict[str, str] = {}
50  if TYPE_CHECKING:
51  assert self.s1_ins1_in is not None
52  if user_input is not None:
53  whole_data = user_input
54  whole_data.update(self.s1_ins1_in)
55 
56  return self.async_create_entryasync_create_entryasync_create_entry(title=whole_data["title"], data=whole_data)
57 
58  relay_modes_schema = {}
59  for i in range(1, int(self.s1_ins1_in["relay_count"]) + 1):
60  relay_modes_schema[vol.Required(f"relay_{i!s}", default="bistable")] = (
61  vol.In(
62  {
63  "bistable": "Bistable (ON/OFF Mode)",
64  "monostable": "Monostable (Timer Mode)",
65  }
66  )
67  )
68 
69  return self.async_show_formasync_show_formasync_show_form(
70  step_id="relay_modes",
71  data_schema=vol.Schema(relay_modes_schema),
72  errors=errors,
73  )
74 
75  async def async_step_user(
76  self, user_input: dict[str, Any] | None = None
77  ) -> ConfigFlowResult:
78  """Handle the initial step."""
79  errors = {}
80  if user_input is not None:
81  self._async_abort_entries_match_async_abort_entries_match(
82  {"host": user_input["host"], "port": user_input["port"]}
83  )
84 
85  try:
86  info = await validate_input(self.hass, user_input)
87  except CannotConnect:
88  errors["base"] = "cannot_connect"
89  except Exception: # noqa: BLE001
90  errors["base"] = "unknown"
91  else:
92  user_input.update(info)
93  self.s1_ins1_in = user_input
94  return await self.async_step_relay_modesasync_step_relay_modes()
95 
96  return self.async_show_formasync_show_formasync_show_form(
97  step_id="user", data_schema=DATA_SCHEMA, errors=errors
98  )
99 
100 
102  """Error to indicate we cannot identify host."""
103 
104 
105 class WrongInfo(HomeAssistantError):
106  """Error to indicate we cannot validate relay modes input."""
107 
108 
110  """Error to indicate we cannot validate relay modes input."""
ConfigFlowResult async_step_user(self, dict[str, Any]|None user_input=None)
Definition: config_flow.py:77
ConfigFlowResult async_step_relay_modes(self, dict[str, str]|None user_input=None)
Definition: config_flow.py:47
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)
None _async_abort_entries_match(self, dict[str, Any]|None match_dict=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)
def validate_input(HomeAssistant hass, data)
Definition: config_flow.py:19