Home Assistant Unofficial Reference 2024.12.1
config_flow.py
Go to the documentation of this file.
1 """Config flow for Palazzetti."""
2 
3 from typing import Any
4 
5 from pypalazzetti.client import PalazzettiClient
6 from pypalazzetti.exceptions import CommunicationError
7 import voluptuous as vol
8 
9 from homeassistant.components import dhcp
10 from homeassistant.config_entries import ConfigFlow, ConfigFlowResult
11 from homeassistant.const import CONF_HOST
12 from homeassistant.helpers import device_registry as dr
13 
14 from .const import DOMAIN, LOGGER
15 
16 
17 class PalazzettiConfigFlow(ConfigFlow, domain=DOMAIN):
18  """Palazzetti config flow."""
19 
20  _discovered_device: PalazzettiClient
21 
22  async def async_step_user(
23  self, user_input: dict[str, Any] | None = None
24  ) -> ConfigFlowResult:
25  """User configuration step."""
26  errors: dict[str, str] = {}
27  if user_input is not None:
28  host = user_input[CONF_HOST]
29  client = PalazzettiClient(hostname=host)
30  try:
31  await client.connect()
32  except CommunicationError:
33  LOGGER.exception("Communication error")
34  errors["base"] = "cannot_connect"
35  else:
36  formatted_mac = dr.format_mac(client.mac)
37 
38  # Assign a unique ID to the flow
39  await self.async_set_unique_idasync_set_unique_id(formatted_mac)
40 
41  # Abort the flow if a config entry with the same unique ID exists
42  self._abort_if_unique_id_configured_abort_if_unique_id_configured()
43 
44  return self.async_create_entryasync_create_entryasync_create_entry(
45  title=client.name,
46  data=user_input,
47  )
48 
49  return self.async_show_formasync_show_formasync_show_form(
50  step_id="user",
51  data_schema=vol.Schema({vol.Required(CONF_HOST): str}),
52  errors=errors,
53  )
54 
55  async def async_step_dhcp(
56  self, discovery_info: dhcp.DhcpServiceInfo
57  ) -> ConfigFlowResult:
58  """Handle DHCP discovery."""
59 
60  LOGGER.debug(
61  "DHCP discovery detected Palazzetti: %s", discovery_info.macaddress
62  )
63 
64  await self.async_set_unique_idasync_set_unique_id(dr.format_mac(discovery_info.macaddress))
65  self._abort_if_unique_id_configured_abort_if_unique_id_configured()
66  self._discovered_device_discovered_device = PalazzettiClient(hostname=discovery_info.ip)
67  try:
68  await self._discovered_device_discovered_device.connect()
69  except CommunicationError:
70  return self.async_abortasync_abortasync_abort(reason="cannot_connect")
71 
72  return await self.async_step_discovery_confirm()
73 
74  async def async_step_discovery_confirm(
75  self, user_input: dict[str, Any] | None = None
76  ) -> ConfigFlowResult:
77  """Confirm discovery."""
78  if user_input is not None:
79  return self.async_create_entryasync_create_entryasync_create_entry(
80  title=self._discovered_device_discovered_device.name,
81  data={CONF_HOST: self._discovered_device_discovered_device.host},
82  )
83 
84  self._set_confirm_only_set_confirm_only()
85  return self.async_show_formasync_show_formasync_show_form(
86  step_id="discovery_confirm",
87  description_placeholders={
88  "name": self._discovered_device_discovered_device.name,
89  "host": self._discovered_device_discovered_device.host,
90  },
91  )
ConfigFlowResult async_step_user(self, dict[str, Any]|None user_input=None)
Definition: config_flow.py:24
None _abort_if_unique_id_configured(self, dict[str, Any]|None updates=None, bool reload_on_update=True, *str error="already_configured")
ConfigFlowResult async_step_dhcp(self, DhcpServiceInfo discovery_info)
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_abort(self, *str reason, Mapping[str, str]|None description_placeholders=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)
_FlowResultT async_abort(self, *str reason, Mapping[str, str]|None description_placeholders=None)