fix: follow Bluetooth adapters that change their hci index - #1609
Open
dingausmwald wants to merge 1 commit into
Open
dingausmwald wants to merge 1 commit into
dingausmwald wants to merge 1 commit into
Conversation
A Bluetooth dongle that is reset by the kernel can come back under a different hci index. HCIdump kept using the index resolved at setup and did not recover until Home Assistant Core was restarted. Look up the current hci index of the configured adapters by MAC address at the start of each scan period. Also stop using BT_INTERFACES[iface] in the power cycle log, which raised a KeyError for an index that was not present at import time. Fixes custom-components#830 Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #830
Problem
When the kernel resets a Bluetooth dongle, the dongle can come back under a different hci index. This happens for example after a
command tx timeouton a Realtek RTL8761BU. After that, BLE monitor keeps using the old index and never recovers until Home Assistant Core is restarted. Reloading the integration does not help.The log from a Raspberry Pi 4 on 14.2.0, where the dongle
00:E0:42:A7:E9:03moved from hci2 to hci0:The last three lines repeat every minute. In this case they ran for 8.5 hours until Core was restarted by hand.
Cause
The configured MAC address is resolved to an hci index only once, in
async_setup_entry, and the lookup table it uses (BT_INTERFACES) is built once when the module is imported. After that,HCIdumpandreset_bluetooth()only work with the stored index, even though the MAC address is known.Fix
HCIdumplooks up the current index of each configured adapter by its MAC address and uses that index. When an index changes, a warning is logged. If an adapter is currently missing, it keeps its old index, so the existing power-cycle recovery behaves as before.BT_INTERFACES[iface]. That lookup raised aKeyErrorin the HCIdump thread when the new index was not present at import time.bt_helpers.hci_get_all_mac()returns all available adapters.hci_get_mac()now uses it and behaves as before.If
hci_interfaceis configured in YAML and no MAC address is known for it, nothing changes.Tests
test/test_hcidump_interfaces.pycovers a renumbered adapter, an unchanged adapter, a missing adapter, two adapters swapping indexes, disabled Bluetooth, an index without a known MAC address, andhci_get_mac/hci_get_all_mac.pyteston Python 3.14, usingrequirements_test.txt.🤖 Generated with Claude Code