Home Assistant Unofficial Reference 2024.12.1
config_flow.py
Go to the documentation of this file.
1 """Config flow for Aranet integration."""
2 
3 from __future__ import annotations
4 
5 from typing import Any
6 
7 from aranet4.client import Aranet4Advertisement, Version as AranetVersion
8 from bluetooth_data_tools import human_readable_name
9 import voluptuous as vol
10 
12  BluetoothServiceInfoBleak,
13  async_discovered_service_info,
14 )
15 from homeassistant.config_entries import ConfigFlow, ConfigFlowResult
16 from homeassistant.const import CONF_ADDRESS
17 from homeassistant.data_entry_flow import AbortFlow
18 
19 from .const import DOMAIN
20 
21 MIN_VERSION = AranetVersion(1, 2, 0)
22 
23 
24 def _title(discovery_info: BluetoothServiceInfoBleak) -> str:
25  return discovery_info.device.name or human_readable_name(
26  None, "Aranet", discovery_info.address
27  )
28 
29 
30 class AranetConfigFlow(ConfigFlow, domain=DOMAIN):
31  """Handle a config flow for Aranet."""
32 
33  VERSION = 1
34 
35  def __init__(self) -> None:
36  """Set up a new config flow for Aranet."""
37  self._discovery_info_discovery_info: BluetoothServiceInfoBleak | None = None
38  self._discovered_device_discovered_device: Aranet4Advertisement | None = None
39  self._discovered_devices: dict[str, tuple[str, Aranet4Advertisement]] = {}
40 
41  def _raise_for_advertisement_errors(self, adv: Aranet4Advertisement) -> None:
42  """Raise any configuration errors that apply to an advertisement."""
43  # Old versions of firmware don't expose sensor data in advertisements.
44  if not adv.manufacturer_data or adv.manufacturer_data.version < MIN_VERSION:
45  raise AbortFlow("outdated_version")
46 
47  # If integrations are disabled, we get no sensor data.
48  if not adv.manufacturer_data.integrations:
49  raise AbortFlow("integrations_disabled")
50 
52  self, discovery_info: BluetoothServiceInfoBleak
53  ) -> ConfigFlowResult:
54  """Handle the Bluetooth discovery step."""
55  await self.async_set_unique_idasync_set_unique_id(discovery_info.address)
56  self._abort_if_unique_id_configured_abort_if_unique_id_configured()
57  adv = Aranet4Advertisement(discovery_info.device, discovery_info.advertisement)
58  self._raise_for_advertisement_errors_raise_for_advertisement_errors(adv)
59 
60  self._discovery_info_discovery_info = discovery_info
61  self._discovered_device_discovered_device = adv
62  return await self.async_step_bluetooth_confirmasync_step_bluetooth_confirm()
63 
65  self, user_input: dict[str, Any] | None = None
66  ) -> ConfigFlowResult:
67  """Confirm discovery."""
68  assert self._discovery_info_discovery_info is not None
69  title = _title(self._discovery_info_discovery_info)
70  if user_input is not None:
71  return self.async_create_entryasync_create_entryasync_create_entry(title=title, data={})
72 
73  self._set_confirm_only_set_confirm_only()
74  placeholders = {"name": title}
75  self.context["title_placeholders"] = placeholders
76  return self.async_show_formasync_show_formasync_show_form(
77  step_id="bluetooth_confirm", description_placeholders=placeholders
78  )
79 
80  async def async_step_user(
81  self, user_input: dict[str, Any] | None = None
82  ) -> ConfigFlowResult:
83  """Handle the user step to pick discovered device."""
84  if user_input is not None:
85  address = user_input[CONF_ADDRESS]
86  adv = self._discovered_devices[address][1]
87  self._raise_for_advertisement_errors_raise_for_advertisement_errors(adv)
88 
89  await self.async_set_unique_idasync_set_unique_id(address, raise_on_progress=False)
90  self._abort_if_unique_id_configured_abort_if_unique_id_configured()
91  return self.async_create_entryasync_create_entryasync_create_entry(
92  title=self._discovered_devices[address][0], data={}
93  )
94 
95  current_addresses = self._async_current_ids_async_current_ids()
96  for discovery_info in async_discovered_service_info(self.hass, False):
97  address = discovery_info.address
98  if address in current_addresses or address in self._discovered_devices:
99  continue
100 
101  adv = Aranet4Advertisement(
102  discovery_info.device, discovery_info.advertisement
103  )
104  if adv.manufacturer_data:
105  self._discovered_devices[address] = (_title(discovery_info), adv)
106 
107  if not self._discovered_devices:
108  return self.async_abortasync_abortasync_abort(reason="no_devices_found")
109 
110  return self.async_show_formasync_show_formasync_show_form(
111  step_id="user",
112  data_schema=vol.Schema(
113  {
114  vol.Required(CONF_ADDRESS): vol.In(
115  {
116  addr: dev[0]
117  for (addr, dev) in self._discovered_devices.items()
118  }
119  )
120  }
121  ),
122  )
None _raise_for_advertisement_errors(self, Aranet4Advertisement adv)
Definition: config_flow.py:41
ConfigFlowResult async_step_bluetooth_confirm(self, dict[str, Any]|None user_input=None)
Definition: config_flow.py:66
ConfigFlowResult async_step_user(self, dict[str, Any]|None user_input=None)
Definition: config_flow.py:82
ConfigFlowResult async_step_bluetooth(self, BluetoothServiceInfoBleak discovery_info)
Definition: config_flow.py:53
None _abort_if_unique_id_configured(self, dict[str, Any]|None updates=None, bool reload_on_update=True, *str error="already_configured")
set[str|None] _async_current_ids(self, bool include_ignore=True)
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)
str _title(BluetoothServiceInfoBleak discovery_info)
Definition: config_flow.py:24
Iterable[BluetoothServiceInfoBleak] async_discovered_service_info(HomeAssistant hass, bool connectable=True)
Definition: api.py:72
str human_readable_name(str hostname, str vendor, str mac_address)
Definition: __init__.py:50