Skip to content

Commit

Permalink
fix: regression detecting local usb adaptor as scanner
Browse files Browse the repository at this point in the history
  • Loading branch information
agittins committed Nov 13, 2024
1 parent e7f9949 commit 8c50b57
Showing 1 changed file with 20 additions and 7 deletions.
27 changes: 20 additions & 7 deletions custom_components/bermuda/coordinator.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

import voluptuous as vol
import yaml
from habluetooth import BaseHaRemoteScanner, BaseHaScanner
from homeassistant.components import bluetooth
from homeassistant.components.bluetooth import (
MONOTONIC_TIME,
Expand Down Expand Up @@ -150,7 +151,9 @@ def __init__(
update_interval=timedelta(seconds=UPDATE_INTERVAL),
)

self._manager: HomeAssistantBluetoothManager = _get_manager(hass)
self._manager: HomeAssistantBluetoothManager = _get_manager(hass) # instance of the bluetooth manager
self._hascanners: set[BaseHaScanner] # Links to the backend scanners
self._hascanner_timestamps: dict[str, dict[str, float]] # scanner_address, device_address, stamp

self._entity_registry = er.async_get(self.hass)
self._device_registry = dr.async_get(self.hass)
Expand Down Expand Up @@ -1112,22 +1115,32 @@ def _refresh_scanners(self, scanners: list[BluetoothScannerDevice] | None = None
# scanner_b: BermudaDevice entry
#
# Evil: We're acessing private members of bt manager to do it since there's no API call for it.
_allscanners = self._manager._connectable_scanners | self._manager._non_connectable_scanners # noqa: SLF001
for scanner_ha in _allscanners:
scanner_address = format_mac(scanner_ha.source).lower()
scanner_devreg = self._device_registry.async_get_device(connections={("mac", scanner_address)})
self._hascanners = self._manager._connectable_scanners | self._manager._non_connectable_scanners # noqa: SLF001
for hascanner in self._hascanners:
scanner_address = format_mac(hascanner.source).lower()
scanner_devreg = self._device_registry.async_get_device(
connections={
("mac", scanner_address), # Matches ESPHome proxies, Shellys etc
("bluetooth", scanner_address), # Matches local USB Bluetooth (hci0..)
}
)
if scanner_devreg is None:
_LOGGER_SPAM_LESS.error(
"scanner_not_in_devreg",
"Failed to find scanner %s (%s) in Device Registry",
scanner_ha.name,
scanner_ha.source,
hascanner.name,
hascanner.source,
)
continue
# _LOGGER.info("Great! Found scanner: %s (%s)", scanner_ha.name, scanner_ha.source)
# Since this scanner still exists, we won't purge it
if scanner_address in _purge_scanners:
_purge_scanners.remove(scanner_address)

# Populate the local copy of timestamps, if applicable
if isinstance(hascanner, BaseHaRemoteScanner):
self._hascanner_timestamps[hascanner.source.lower()] = hascanner._discovered_device_timestamps # noqa: SLF001

scanner_b = self._get_device(scanner_address)
if scanner_b is None:
# It's a new scanner, we will need to update our saved config.
Expand Down

0 comments on commit 8c50b57

Please sign in to comment.