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

This commit is contained in:
J. Nick Koston 2025-01-29 09:09:00 -10:00 committed by GitHub
parent 5286bd8f0c
commit edabf0f8dd
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
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() now_monotonic = monotonic_time_coarse()
connectable_loaded_history: dict[str, BluetoothServiceInfoBleak] = {} connectable_loaded_history: dict[str, BluetoothServiceInfoBleak] = {}
all_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 # Restore local adapters
for address, history in adapters.history.items(): for address, history in adapters.history.items():
@ -50,7 +54,11 @@ def async_load_history_from_system(
BluetoothServiceInfoBleak.from_device_and_advertisement_data( BluetoothServiceInfoBleak.from_device_and_advertisement_data(
history.device, history.device,
history.advertisement_data, 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, now_monotonic,
True, True,
) )

View File

@ -426,7 +426,7 @@ async def test_restore_history_from_dbus(
address: AdvertisementHistory( address: AdvertisementHistory(
ble_device, ble_device,
generate_advertisement_data(local_name="name"), 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() await hass.async_block_till_done()
assert bluetooth.async_ble_device_from_address(hass, address) is ble_device 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") @pytest.mark.usefixtures("one_adapter")