Home Assistant Unofficial Reference 2024.12.1
config_flow.py
Go to the documentation of this file.
1 """Config flow for eQ-3 Bluetooth Smart thermostats."""
2 
3 from typing import Any
4 
5 from homeassistant.components.bluetooth import BluetoothServiceInfoBleak
6 from homeassistant.config_entries import ConfigFlow, ConfigFlowResult
7 from homeassistant.const import CONF_MAC
8 from homeassistant.helpers.device_registry import format_mac
9 from homeassistant.util import slugify
10 
11 from .const import DOMAIN
12 from .schemas import SCHEMA_MAC
13 
14 
15 class EQ3ConfigFlow(ConfigFlow, domain=DOMAIN):
16  """Config flow for eQ-3 Bluetooth Smart thermostats."""
17 
18  def __init__(self) -> None:
19  """Initialize the config flow."""
20 
21  self.mac_addressmac_address: str = ""
22 
23  async def async_step_user(
24  self, user_input: dict[str, Any] | None = None
25  ) -> ConfigFlowResult:
26  """Handle a flow initialized by the user."""
27 
28  errors: dict[str, str] = {}
29  if user_input is None:
30  return self.async_show_formasync_show_formasync_show_form(
31  step_id="user",
32  data_schema=SCHEMA_MAC,
33  errors=errors,
34  )
35 
36  mac_address = format_mac(user_input[CONF_MAC])
37 
38  if not validate_mac(mac_address):
39  errors[CONF_MAC] = "invalid_mac_address"
40  return self.async_show_formasync_show_formasync_show_form(
41  step_id="user",
42  data_schema=SCHEMA_MAC,
43  errors=errors,
44  )
45 
46  await self.async_set_unique_idasync_set_unique_id(mac_address)
47  self._abort_if_unique_id_configured_abort_if_unique_id_configured(updates=user_input)
48 
49  # We can not validate if this mac actually is an eQ-3 thermostat,
50  # since the thermostat probably is not advertising right now.
51  return self.async_create_entryasync_create_entryasync_create_entry(title=slugify(mac_address), data={})
52 
54  self, discovery_info: BluetoothServiceInfoBleak
55  ) -> ConfigFlowResult:
56  """Handle bluetooth discovery."""
57 
58  self.mac_addressmac_address = format_mac(discovery_info.address)
59 
60  await self.async_set_unique_idasync_set_unique_id(self.mac_addressmac_address)
61  self._abort_if_unique_id_configured_abort_if_unique_id_configured()
62 
63  self.context.update({"title_placeholders": {CONF_MAC: self.mac_addressmac_address}})
64 
65  return await self.async_step_initasync_step_init()
66 
67  async def async_step_init(
68  self, user_input: dict[str, Any] | None = None
69  ) -> ConfigFlowResult:
70  """Handle flow start."""
71 
72  if user_input is None:
73  return self.async_show_formasync_show_formasync_show_form(
74  step_id="init",
75  description_placeholders={CONF_MAC: self.mac_addressmac_address},
76  )
77 
78  await self.async_set_unique_idasync_set_unique_id(self.mac_addressmac_address)
79  self._abort_if_unique_id_configured_abort_if_unique_id_configured()
80 
81  return self.async_create_entryasync_create_entryasync_create_entry(
82  title=slugify(self.mac_addressmac_address),
83  data={},
84  )
85 
86 
87 def validate_mac(mac: str) -> bool:
88  """Return whether or not given value is a valid MAC address."""
89 
90  return bool(
91  mac
92  and len(mac) == 17
93  and mac.count(":") == 5
94  and all(int(part, 16) < 256 for part in mac.split(":") if part)
95  )
ConfigFlowResult async_step_bluetooth(self, BluetoothServiceInfoBleak discovery_info)
Definition: config_flow.py:55
ConfigFlowResult async_step_user(self, dict[str, Any]|None user_input=None)
Definition: config_flow.py:25
ConfigFlowResult async_step_init(self, dict[str, Any]|None user_input=None)
Definition: config_flow.py:69
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)
_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)
IssData update(pyiss.ISS iss)
Definition: __init__.py:33