Skip to content

Commit

Permalink
Fix incorrect Bluetooth source address when restoring data from D-Bus (
Browse files Browse the repository at this point in the history
  • Loading branch information
bdraco authored Jan 29, 2025
1 parent 5286bd8 commit edabf0f
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
10 changes: 9 additions & 1 deletion homeassistant/components/bluetooth/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand All @@ -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,
)
Expand Down
4 changes: 3 additions & 1 deletion tests/components/bluetooth/test_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
)
}

Expand All @@ -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")
Expand Down

0 comments on commit edabf0f

Please sign in to comment.