Fix incorrect Bluetooth source address when restoring data from D-Bus (#136862)

pull/137390/head
J. Nick Koston 2025-01-29 09:09:00 -10:00 committed by Paulus Schoutsen
parent 0f97747d27
commit 9c0fa327a6
2 changed files with 12 additions and 2 deletions

View File

@ -39,6 +39,10 @@ def async_load_history_from_system(
now_monotonic = monotonic_time_coarse()
connectable_loaded_history: dict[str, BluetoothServiceInfoBleak] = {}
all_loaded_history: dict[str, BluetoothServiceInfoBleak] = {}
adapter_to_source_address = {
adapter: details[ADAPTER_ADDRESS]
for adapter, details in adapters.adapters.items()
}
# Restore local adapters
for address, history in adapters.history.items():
@ -50,7 +54,11 @@ def async_load_history_from_system(
BluetoothServiceInfoBleak.from_device_and_advertisement_data(
history.device,
history.advertisement_data,
history.source,
# history.source is really the adapter name
# for historical compatibility since BlueZ
# does not know the MAC address of the adapter
# so we need to convert it to the source address (MAC)
adapter_to_source_address.get(history.source, history.source),
now_monotonic,
True,
)

View File

@ -426,7 +426,7 @@ async def test_restore_history_from_dbus(
address: AdvertisementHistory(
ble_device,
generate_advertisement_data(local_name="name"),
HCI0_SOURCE_ADDRESS,
"hci0",
)
}
@ -438,6 +438,8 @@ async def test_restore_history_from_dbus(
await hass.async_block_till_done()
assert bluetooth.async_ble_device_from_address(hass, address) is ble_device
info = bluetooth.async_last_service_info(hass, address, False)
assert info.source == "00:00:00:00:00:01"
@pytest.mark.usefixtures("one_adapter")