Skip to content

Commit 4b25b25

Browse files
committed
Fix scanners initialization and destroying
1 parent a2d446e commit 4b25b25

File tree

1 file changed

+7
-3
lines changed

1 file changed

+7
-3
lines changed

custom_components/ble_monitor/__init__.py

+7-3
Original file line numberDiff line numberDiff line change
@@ -686,7 +686,7 @@ def process_hci_events(self, data, device_id=None, gateway_id=DOMAIN, proxy=Fals
686686
if self.bluetooth is not None and proxy:
687687
try:
688688
scanner_name = device_id or gateway_id
689-
scanner = self.scanners.get(scanner_name)
689+
scanner, _ = self.scanners.get(scanner_name, (None, None))
690690
if not scanner:
691691
hass = async_get_hass()
692692
device = hass.data["device_registry"].devices.get(device_id) if device_id \
@@ -695,8 +695,9 @@ def process_hci_events(self, data, device_id=None, gateway_id=DOMAIN, proxy=Fals
695695
source = next((connection[1] for connection in device.connections if
696696
connection[0] in ["mac", "bluetooth"]), gateway_id) if device else gateway_id
697697
scanner = self.bluetooth.BaseHaRemoteScanner(source, gateway_id, None, False)
698-
self.bluetooth.async_register_scanner(hass, scanner)
699-
self.scanners[scanner_name] = scanner
698+
self.scanners[scanner_name] = (scanner, [
699+
self.bluetooth.async_register_scanner(hass, scanner),
700+
scanner.async_setup()])
700701
self.hci_packet_on_advertisement(scanner, data)
701702
except Exception as e:
702703
_LOGGER.error("%s: %s: %s", gateway_id, e, data.hex().upper())
@@ -853,6 +854,9 @@ def run(self):
853854
_LOGGER.debug("%i HCI events processed for previous period", self.evt_cnt)
854855
self.evt_cnt = 0
855856
self._event_loop.close()
857+
for _, unload_callbacks in self.scanners.values():
858+
for callback in unload_callbacks:
859+
callback()
856860
_LOGGER.debug("HCIdump thread: Run finished")
857861

858862
def join(self, timeout=10):

0 commit comments

Comments
 (0)