1 """The bluetooth integration utilities."""
3 from __future__
import annotations
5 from bluetooth_adapters
import (
13 from bluetooth_data_tools
import monotonic_time_coarse
17 from .models
import BluetoothServiceInfoBleak
18 from .storage
import BluetoothStorage
23 adapters: BluetoothAdapters, storage: BluetoothStorage
24 ) -> tuple[dict[str, BluetoothServiceInfoBleak], dict[str, BluetoothServiceInfoBleak]]:
25 """Load the device and advertisement_data history.
27 Only loads if available on the current system.
29 now_monotonic = monotonic_time_coarse()
30 connectable_loaded_history: dict[str, BluetoothServiceInfoBleak] = {}
31 all_loaded_history: dict[str, BluetoothServiceInfoBleak] = {}
34 for address, history
in adapters.history.items():
36 not (existing_all := connectable_loaded_history.get(address))
37 or history.advertisement_data.rssi > existing_all.rssi
39 connectable_loaded_history[address] = all_loaded_history[address] = (
40 BluetoothServiceInfoBleak.from_device_and_advertisement_data(
42 history.advertisement_data,
50 for scanner
in storage.scanners():
51 if not (adv_history := storage.async_get_advertisement_history(scanner)):
54 connectable = adv_history.connectable
55 discovered_device_timestamps = adv_history.discovered_device_timestamps
58 (device, advertisement_data),
59 )
in adv_history.discovered_device_advertisement_datas.items():
60 service_info = BluetoothServiceInfoBleak.from_device_and_advertisement_data(
64 discovered_device_timestamps[address],
68 not (existing_all := all_loaded_history.get(address))
69 or service_info.rssi > existing_all.rssi
71 all_loaded_history[address] = service_info
73 not (existing_connectable := connectable_loaded_history.get(address))
74 or service_info.rssi > existing_connectable.rssi
76 connectable_loaded_history[address] = service_info
78 return all_loaded_history, connectable_loaded_history
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)
str adapter_title(str adapter, AdapterDetails details)