Home Assistant Unofficial Reference 2024.12.1
config_flow.py
Go to the documentation of this file.
1 """Config flow for Nobø Ecohub integration."""
2 
3 from __future__ import annotations
4 
5 import socket
6 from typing import TYPE_CHECKING, Any
7 
8 from pynobo import nobo
9 import voluptuous as vol
10 
11 from homeassistant.config_entries import (
12  ConfigEntry,
13  ConfigFlow,
14  ConfigFlowResult,
15  OptionsFlow,
16 )
17 from homeassistant.const import CONF_IP_ADDRESS
18 from homeassistant.core import callback
19 from homeassistant.exceptions import HomeAssistantError
20 
21 from .const import (
22  CONF_AUTO_DISCOVERED,
23  CONF_OVERRIDE_TYPE,
24  CONF_SERIAL,
25  DOMAIN,
26  OVERRIDE_TYPE_CONSTANT,
27  OVERRIDE_TYPE_NOW,
28 )
29 
30 DATA_NOBO_HUB_IMPL = "nobo_hub_flow_implementation"
31 DEVICE_INPUT = "device_input"
32 
33 
34 class NoboHubConfigFlow(ConfigFlow, domain=DOMAIN):
35  """Handle a config flow for Nobø Ecohub."""
36 
37  VERSION = 1
38 
39  def __init__(self) -> None:
40  """Initialize the config flow."""
41  self._discovered_hubs_discovered_hubs: dict[str, Any] | None = None
42  self._hub_hub: str | None = None
43 
44  async def async_step_user(
45  self, user_input: dict[str, Any] | None = None
46  ) -> ConfigFlowResult:
47  """Handle the initial step."""
48  if self._discovered_hubs_discovered_hubs is None:
49  self._discovered_hubs_discovered_hubs = dict(await nobo.async_discover_hubs())
50 
51  if not self._discovered_hubs_discovered_hubs:
52  # No hubs auto discovered
53  return await self.async_step_manualasync_step_manual()
54 
55  if user_input is not None:
56  if user_input["device"] == "manual":
57  return await self.async_step_manualasync_step_manual()
58  self._hub_hub = user_input["device"]
59  return await self.async_step_selectedasync_step_selected()
60 
61  hubs = self._hubs_hubs()
62  hubs["manual"] = "Manual"
63  data_schema = vol.Schema(
64  {
65  vol.Required("device"): vol.In(hubs),
66  }
67  )
68  return self.async_show_formasync_show_formasync_show_form(
69  step_id="user",
70  data_schema=data_schema,
71  )
72 
74  self, user_input: dict[str, Any] | None = None
75  ) -> ConfigFlowResult:
76  """Handle configuration of a selected discovered device."""
77  errors = {}
78  if TYPE_CHECKING:
79  assert self._discovered_hubs_discovered_hubs
80  assert self._hub_hub
81  if user_input is not None:
82  serial_prefix = self._discovered_hubs_discovered_hubs[self._hub_hub]
83  serial_suffix = user_input["serial_suffix"]
84  serial = f"{serial_prefix}{serial_suffix}"
85  try:
86  return await self._create_configuration_create_configuration(serial, self._hub_hub, True)
87  except NoboHubConnectError as error:
88  errors["base"] = error.msg
89 
90  user_input = user_input or {}
91  return self.async_show_formasync_show_formasync_show_form(
92  step_id="selected",
93  data_schema=vol.Schema(
94  {
95  vol.Required(
96  "serial_suffix", default=user_input.get("serial_suffix")
97  ): str,
98  }
99  ),
100  errors=errors,
101  description_placeholders={
102  "hub": self._format_hub_format_hub(self._hub_hub, self._discovered_hubs_discovered_hubs[self._hub_hub])
103  },
104  )
105 
106  async def async_step_manual(
107  self, user_input: dict[str, Any] | None = None
108  ) -> ConfigFlowResult:
109  """Handle configuration of an undiscovered device."""
110  errors = {}
111  if user_input is not None:
112  serial = user_input[CONF_SERIAL]
113  ip_address = user_input[CONF_IP_ADDRESS]
114  try:
115  return await self._create_configuration_create_configuration(serial, ip_address, False)
116  except NoboHubConnectError as error:
117  errors["base"] = error.msg
118 
119  user_input = user_input or {}
120  return self.async_show_formasync_show_formasync_show_form(
121  step_id="manual",
122  data_schema=vol.Schema(
123  {
124  vol.Required(CONF_SERIAL, default=user_input.get(CONF_SERIAL)): str,
125  vol.Required(
126  CONF_IP_ADDRESS, default=user_input.get(CONF_IP_ADDRESS)
127  ): str,
128  }
129  ),
130  errors=errors,
131  )
132 
134  self, serial: str, ip_address: str, auto_discovered: bool
135  ) -> ConfigFlowResult:
136  await self.async_set_unique_idasync_set_unique_id(serial)
137  self._abort_if_unique_id_configured_abort_if_unique_id_configured()
138  name = await self._test_connection_test_connection(serial, ip_address)
139  return self.async_create_entryasync_create_entryasync_create_entry(
140  title=name,
141  data={
142  CONF_SERIAL: serial,
143  CONF_IP_ADDRESS: ip_address,
144  CONF_AUTO_DISCOVERED: auto_discovered,
145  },
146  )
147 
148  async def _test_connection(self, serial: str, ip_address: str) -> str:
149  if not len(serial) == 12 or not serial.isdigit():
150  raise NoboHubConnectError("invalid_serial")
151  try:
152  socket.inet_aton(ip_address)
153  except OSError as err:
154  raise NoboHubConnectError("invalid_ip") from err
155  hub = nobo(serial=serial, ip=ip_address, discover=False, synchronous=False)
156  if not await hub.async_connect_hub(ip_address, serial):
157  raise NoboHubConnectError("cannot_connect")
158  name = hub.hub_info["name"]
159  await hub.close()
160  return name
161 
162  @staticmethod
163  def _format_hub(ip, serial_prefix):
164  return f"{serial_prefix}XXX ({ip})"
165 
166  def _hubs(self):
167  return {
168  ip: self._format_hub_format_hub(ip, serial_prefix)
169  for ip, serial_prefix in self._discovered_hubs_discovered_hubs.items()
170  }
171 
172  @staticmethod
173  @callback
175  config_entry: ConfigEntry,
176  ) -> OptionsFlow:
177  """Get the options flow for this handler."""
178  return OptionsFlowHandler()
179 
180 
182  """Error with connecting to Nobø Ecohub."""
183 
184  def __init__(self, msg) -> None:
185  """Instantiate error."""
186  super().__init__()
187  self.msgmsg = msg
188 
189 
191  """Handles options flow for the component."""
192 
193  async def async_step_init(self, user_input=None) -> ConfigFlowResult:
194  """Manage the options."""
195 
196  if user_input is not None:
197  data = {
198  CONF_OVERRIDE_TYPE: user_input.get(CONF_OVERRIDE_TYPE),
199  }
200  return self.async_create_entryasync_create_entry(title="", data=data)
201 
202  override_type = self.config_entryconfig_entryconfig_entry.options.get(
203  CONF_OVERRIDE_TYPE, OVERRIDE_TYPE_CONSTANT
204  )
205 
206  schema = vol.Schema(
207  {
208  vol.Required(CONF_OVERRIDE_TYPE, default=override_type): vol.In(
209  [OVERRIDE_TYPE_CONSTANT, OVERRIDE_TYPE_NOW]
210  ),
211  }
212  )
213 
214  return self.async_show_formasync_show_form(step_id="init", data_schema=schema)
ConfigFlowResult async_step_user(self, dict[str, Any]|None user_input=None)
Definition: config_flow.py:46
str _test_connection(self, str serial, str ip_address)
Definition: config_flow.py:148
ConfigFlowResult _create_configuration(self, str serial, str ip_address, bool auto_discovered)
Definition: config_flow.py:135
ConfigFlowResult async_step_manual(self, dict[str, Any]|None user_input=None)
Definition: config_flow.py:108
OptionsFlow async_get_options_flow(ConfigEntry config_entry)
Definition: config_flow.py:176
ConfigFlowResult async_step_selected(self, dict[str, Any]|None user_input=None)
Definition: config_flow.py:75
ConfigFlowResult async_step_init(self, user_input=None)
Definition: config_flow.py:193
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)
None config_entry(self, ConfigEntry value)
_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)