Home Assistant Unofficial Reference 2024.12.1
config_flow.py
Go to the documentation of this file.
1 """Config flow for Qingping integration."""
2 
3 from __future__ import annotations
4 
5 from typing import Any
6 
7 from qingping_ble import QingpingBluetoothDeviceData as DeviceData
8 import voluptuous as vol
9 
11  BluetoothScanningMode,
12  BluetoothServiceInfoBleak,
13  async_discovered_service_info,
14  async_process_advertisements,
15 )
16 from homeassistant.config_entries import ConfigFlow, ConfigFlowResult
17 from homeassistant.const import CONF_ADDRESS
18 
19 from .const import DOMAIN
20 
21 # How long to wait for additional advertisement packets if we don't have the right ones
22 ADDITIONAL_DISCOVERY_TIMEOUT = 60
23 
24 
25 class QingpingConfigFlow(ConfigFlow, domain=DOMAIN):
26  """Handle a config flow for qingping."""
27 
28  VERSION = 1
29 
30  def __init__(self) -> None:
31  """Initialize the config flow."""
32  self._discovery_info_discovery_info: BluetoothServiceInfoBleak | None = None
33  self._discovered_device_discovered_device: DeviceData | None = None
34  self._discovered_devices: dict[str, str] = {}
35 
37  self, discovery_info: BluetoothServiceInfoBleak, device: DeviceData
38  ) -> BluetoothServiceInfoBleak:
39  """Wait for the full advertisement.
40 
41  Sometimes the first advertisement we receive is blank or incomplete.
42  """
43  if device.supported(discovery_info):
44  return discovery_info
45  return await async_process_advertisements(
46  self.hass,
47  device.supported,
48  {"address": discovery_info.address},
49  BluetoothScanningMode.ACTIVE,
50  ADDITIONAL_DISCOVERY_TIMEOUT,
51  )
52 
54  self, discovery_info: BluetoothServiceInfoBleak
55  ) -> ConfigFlowResult:
56  """Handle the bluetooth discovery step."""
57  await self.async_set_unique_idasync_set_unique_id(discovery_info.address)
58  self._abort_if_unique_id_configured_abort_if_unique_id_configured()
59  device = DeviceData()
60  try:
61  self._discovery_info_discovery_info = await self._async_wait_for_full_advertisement_async_wait_for_full_advertisement(
62  discovery_info, device
63  )
64  except TimeoutError:
65  return self.async_abortasync_abortasync_abort(reason="not_supported")
66  self._discovery_info_discovery_info = discovery_info
67  self._discovered_device_discovered_device = device
68  return await self.async_step_bluetooth_confirmasync_step_bluetooth_confirm()
69 
71  self, user_input: dict[str, Any] | None = None
72  ) -> ConfigFlowResult:
73  """Confirm discovery."""
74  assert self._discovered_device_discovered_device is not None
75  device = self._discovered_device_discovered_device
76  assert self._discovery_info_discovery_info is not None
77  discovery_info = self._discovery_info_discovery_info
78  title = device.title or device.get_device_name() or discovery_info.name
79  if user_input is not None:
80  return self.async_create_entryasync_create_entryasync_create_entry(title=title, data={})
81 
82  self._set_confirm_only_set_confirm_only()
83  placeholders = {"name": title}
84  self.context["title_placeholders"] = placeholders
85  return self.async_show_formasync_show_formasync_show_form(
86  step_id="bluetooth_confirm", description_placeholders=placeholders
87  )
88 
89  async def async_step_user(
90  self, user_input: dict[str, Any] | None = None
91  ) -> ConfigFlowResult:
92  """Handle the user step to pick discovered device."""
93  if user_input is not None:
94  address = user_input[CONF_ADDRESS]
95  await self.async_set_unique_idasync_set_unique_id(address, raise_on_progress=False)
96  self._abort_if_unique_id_configured_abort_if_unique_id_configured()
97  return self.async_create_entryasync_create_entryasync_create_entry(
98  title=self._discovered_devices[address], data={}
99  )
100 
101  current_addresses = self._async_current_ids_async_current_ids()
102  for discovery_info in async_discovered_service_info(self.hass, False):
103  address = discovery_info.address
104  if address in current_addresses or address in self._discovered_devices:
105  continue
106  device = DeviceData()
107  if device.supported(discovery_info):
108  self._discovered_devices[address] = (
109  device.title or device.get_device_name() or discovery_info.name
110  )
111 
112  if not self._discovered_devices:
113  return self.async_abortasync_abortasync_abort(reason="no_devices_found")
114 
115  return self.async_show_formasync_show_formasync_show_form(
116  step_id="user",
117  data_schema=vol.Schema(
118  {vol.Required(CONF_ADDRESS): vol.In(self._discovered_devices)}
119  ),
120  )
BluetoothServiceInfoBleak _async_wait_for_full_advertisement(self, BluetoothServiceInfoBleak discovery_info, DeviceData device)
Definition: config_flow.py:38
ConfigFlowResult async_step_bluetooth_confirm(self, dict[str, Any]|None user_input=None)
Definition: config_flow.py:72
ConfigFlowResult async_step_user(self, dict[str, Any]|None user_input=None)
Definition: config_flow.py:91
ConfigFlowResult async_step_bluetooth(self, BluetoothServiceInfoBleak discovery_info)
Definition: config_flow.py:55
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
BluetoothServiceInfoBleak async_process_advertisements(HomeAssistant hass, ProcessAdvertisementCallback callback, BluetoothCallbackMatcher match_dict, BluetoothScanningMode mode, int timeout)
Definition: api.py:134