Home Assistant Unofficial Reference 2024.12.1
util.py
Go to the documentation of this file.
1 """The bluetooth integration utilities."""
2 
3 from __future__ import annotations
4 
5 from bluetooth_adapters import (
6  ADAPTER_ADDRESS,
7  ADAPTER_MANUFACTURER,
8  ADAPTER_PRODUCT,
9  AdapterDetails,
10  BluetoothAdapters,
11  adapter_unique_name,
12 )
13 from bluetooth_data_tools import monotonic_time_coarse
14 
15 from homeassistant.core import callback
16 
17 from .models import BluetoothServiceInfoBleak
18 from .storage import BluetoothStorage
19 
20 
21 @callback
23  adapters: BluetoothAdapters, storage: BluetoothStorage
24 ) -> tuple[dict[str, BluetoothServiceInfoBleak], dict[str, BluetoothServiceInfoBleak]]:
25  """Load the device and advertisement_data history.
26 
27  Only loads if available on the current system.
28  """
29  now_monotonic = monotonic_time_coarse()
30  connectable_loaded_history: dict[str, BluetoothServiceInfoBleak] = {}
31  all_loaded_history: dict[str, BluetoothServiceInfoBleak] = {}
32 
33  # Restore local adapters
34  for address, history in adapters.history.items():
35  if (
36  not (existing_all := connectable_loaded_history.get(address))
37  or history.advertisement_data.rssi > existing_all.rssi
38  ):
39  connectable_loaded_history[address] = all_loaded_history[address] = (
40  BluetoothServiceInfoBleak.from_device_and_advertisement_data(
41  history.device,
42  history.advertisement_data,
43  history.source,
44  now_monotonic,
45  True,
46  )
47  )
48 
49  # Restore remote adapters
50  for scanner in storage.scanners():
51  if not (adv_history := storage.async_get_advertisement_history(scanner)):
52  continue
53 
54  connectable = adv_history.connectable
55  discovered_device_timestamps = adv_history.discovered_device_timestamps
56  for (
57  address,
58  (device, advertisement_data),
59  ) in adv_history.discovered_device_advertisement_datas.items():
60  service_info = BluetoothServiceInfoBleak.from_device_and_advertisement_data(
61  device,
62  advertisement_data,
63  scanner,
64  discovered_device_timestamps[address],
65  connectable,
66  )
67  if (
68  not (existing_all := all_loaded_history.get(address))
69  or service_info.rssi > existing_all.rssi
70  ):
71  all_loaded_history[address] = service_info
72  if connectable and (
73  not (existing_connectable := connectable_loaded_history.get(address))
74  or service_info.rssi > existing_connectable.rssi
75  ):
76  connectable_loaded_history[address] = service_info
77 
78  return all_loaded_history, connectable_loaded_history
79 
80 
81 @callback
82 def adapter_title(adapter: str, details: AdapterDetails) -> str:
83  """Return the adapter title."""
84  unique_name = adapter_unique_name(adapter, details[ADAPTER_ADDRESS])
85  model = details.get(ADAPTER_PRODUCT, "Unknown")
86  manufacturer = details[ADAPTER_MANUFACTURER] or "Unknown"
87  return f"{manufacturer} {model} ({unique_name})"
tuple[dict[str, BluetoothServiceInfoBleak], dict[str, BluetoothServiceInfoBleak]] async_load_history_from_system(BluetoothAdapters adapters, BluetoothStorage storage)
Definition: util.py:24
str adapter_title(str adapter, AdapterDetails details)
Definition: util.py:82