Skip to content

Commit

Permalink
Fix scanners initialization and destroying
Browse files Browse the repository at this point in the history
  • Loading branch information
myhomeiot committed May 11, 2024
1 parent a2d446e commit 4b25b25
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions custom_components/ble_monitor/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -686,7 +686,7 @@ def process_hci_events(self, data, device_id=None, gateway_id=DOMAIN, proxy=Fals
if self.bluetooth is not None and proxy:
try:
scanner_name = device_id or gateway_id
scanner = self.scanners.get(scanner_name)
scanner, _ = self.scanners.get(scanner_name, (None, None))
if not scanner:
hass = async_get_hass()
device = hass.data["device_registry"].devices.get(device_id) if device_id \
Expand All @@ -695,8 +695,9 @@ def process_hci_events(self, data, device_id=None, gateway_id=DOMAIN, proxy=Fals
source = next((connection[1] for connection in device.connections if
connection[0] in ["mac", "bluetooth"]), gateway_id) if device else gateway_id
scanner = self.bluetooth.BaseHaRemoteScanner(source, gateway_id, None, False)
self.bluetooth.async_register_scanner(hass, scanner)
self.scanners[scanner_name] = scanner
self.scanners[scanner_name] = (scanner, [
self.bluetooth.async_register_scanner(hass, scanner),
scanner.async_setup()])
self.hci_packet_on_advertisement(scanner, data)
except Exception as e:
_LOGGER.error("%s: %s: %s", gateway_id, e, data.hex().upper())
Expand Down Expand Up @@ -853,6 +854,9 @@ def run(self):
_LOGGER.debug("%i HCI events processed for previous period", self.evt_cnt)
self.evt_cnt = 0
self._event_loop.close()
for _, unload_callbacks in self.scanners.values():
for callback in unload_callbacks:
callback()
_LOGGER.debug("HCIdump thread: Run finished")

def join(self, timeout=10):
Expand Down

0 comments on commit 4b25b25

Please sign in to comment.