Home Assistant Unofficial Reference 2024.12.1
config_flow.py
Go to the documentation of this file.
1 """Config flow for IronOS integration."""
2 
3 from __future__ import annotations
4 
5 from typing import Any
6 
7 from habluetooth import BluetoothServiceInfoBleak
8 import voluptuous as vol
9 
10 from homeassistant.components.bluetooth.api import async_discovered_service_info
11 from homeassistant.config_entries import ConfigFlow, ConfigFlowResult
12 from homeassistant.const import CONF_ADDRESS
13 
14 from .const import DISCOVERY_SVC_UUID, DOMAIN
15 
16 
17 class IronOSConfigFlow(ConfigFlow, domain=DOMAIN):
18  """Handle a config flow for IronOS."""
19 
20  def __init__(self) -> None:
21  """Initialize the config flow."""
22  self._discovery_info_discovery_info: BluetoothServiceInfoBleak | None = None
23  self._discovered_devices: dict[str, str] = {}
24 
26  self, discovery_info: BluetoothServiceInfoBleak
27  ) -> ConfigFlowResult:
28  """Handle bluetooth discovery step."""
29  await self.async_set_unique_idasync_set_unique_id(discovery_info.address)
30  self._abort_if_unique_id_configured_abort_if_unique_id_configured()
31  self._discovery_info_discovery_info = discovery_info
32 
33  return await self.async_step_bluetooth_confirmasync_step_bluetooth_confirm()
34 
36  self, user_input: dict[str, Any] | None = None
37  ) -> ConfigFlowResult:
38  """Confirm discovery."""
39  assert self._discovery_info_discovery_info is not None
40  discovery_info = self._discovery_info_discovery_info
41  title = discovery_info.name
42 
43  if user_input is not None:
44  return self.async_create_entryasync_create_entryasync_create_entry(title=title, data={})
45 
46  self._set_confirm_only_set_confirm_only()
47  placeholders = {"name": title}
48  self.context["title_placeholders"] = placeholders
49  return self.async_show_formasync_show_formasync_show_form(
50  step_id="bluetooth_confirm", description_placeholders=placeholders
51  )
52 
53  async def async_step_user(
54  self, user_input: dict[str, Any] | None = None
55  ) -> ConfigFlowResult:
56  """Handle the user step to pick discovered device."""
57  if user_input is not None:
58  address = user_input[CONF_ADDRESS]
59  title = self._discovered_devices[address]
60  await self.async_set_unique_idasync_set_unique_id(address, raise_on_progress=False)
61  self._abort_if_unique_id_configured_abort_if_unique_id_configured()
62  return self.async_create_entryasync_create_entryasync_create_entry(title=title, data={})
63 
64  current_addresses = self._async_current_ids_async_current_ids()
65  for discovery_info in async_discovered_service_info(self.hass, True):
66  address = discovery_info.address
67  if (
68  DISCOVERY_SVC_UUID not in discovery_info.service_uuids
69  or address in current_addresses
70  or address in self._discovered_devices
71  ):
72  continue
73  self._discovered_devices[address] = discovery_info.name
74 
75  if not self._discovered_devices:
76  return self.async_abortasync_abortasync_abort(reason="no_devices_found")
77 
78  return self.async_show_formasync_show_formasync_show_form(
79  step_id="user",
80  data_schema=vol.Schema(
81  {vol.Required(CONF_ADDRESS): vol.In(self._discovered_devices)}
82  ),
83  )
ConfigFlowResult async_step_user(self, dict[str, Any]|None user_input=None)
Definition: config_flow.py:55
ConfigFlowResult async_step_bluetooth(self, BluetoothServiceInfoBleak discovery_info)
Definition: config_flow.py:27
ConfigFlowResult async_step_bluetooth_confirm(self, dict[str, Any]|None user_input=None)
Definition: config_flow.py:37
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)
Iterable[BluetoothServiceInfoBleak] async_discovered_service_info(HomeAssistant hass, bool connectable=True)
Definition: api.py:72