From 5e8265d8a44c2c8fc73412faf4af8bcf9e70a2c9 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Sat, 16 Mar 2024 19:03:14 -1000 Subject: [PATCH] Add run_immediately to bluetooth listeners (#113659) --- homeassistant/components/bluetooth/__init__.py | 8 ++++++-- homeassistant/components/bluetooth/manager.py | 6 ++++-- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/homeassistant/components/bluetooth/__init__.py b/homeassistant/components/bluetooth/__init__.py index 5a59858093c..1e091ec32cc 100644 --- a/homeassistant/components/bluetooth/__init__.py +++ b/homeassistant/components/bluetooth/__init__.py @@ -166,7 +166,9 @@ async def _async_start_adapter_discovery( """Shutdown debouncer.""" discovery_debouncer.async_shutdown() - hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STOP, _async_shutdown_debouncer) + hass.bus.async_listen_once( + EVENT_HOMEASSISTANT_STOP, _async_shutdown_debouncer, run_immediately=True + ) async def _async_call_debouncer(now: datetime.datetime) -> None: """Call the debouncer at a later time.""" @@ -197,7 +199,9 @@ async def _async_start_adapter_discovery( cancel = usb.async_register_scan_request_callback(hass, _async_trigger_discovery) hass.bus.async_listen_once( - EVENT_HOMEASSISTANT_STOP, hass_callback(lambda event: cancel()) + EVENT_HOMEASSISTANT_STOP, + hass_callback(lambda event: cancel()), + run_immediately=True, ) diff --git a/homeassistant/components/bluetooth/manager.py b/homeassistant/components/bluetooth/manager.py index 2eb07c5133f..3a240e9f01e 100644 --- a/homeassistant/components/bluetooth/manager.py +++ b/homeassistant/components/bluetooth/manager.py @@ -135,9 +135,11 @@ class HomeAssistantBluetoothManager(BluetoothManager): self._bluetooth_adapters, self.storage ) self._cancel_logging_listener = self.hass.bus.async_listen( - EVENT_LOGGING_CHANGED, self._async_logging_changed + EVENT_LOGGING_CHANGED, self._async_logging_changed, run_immediately=True + ) + self.hass.bus.async_listen_once( + EVENT_HOMEASSISTANT_STOP, self.async_stop, run_immediately=True ) - self.hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STOP, self.async_stop) seen: set[str] = set() for address, service_info in itertools.chain( self._connectable_history.items(), self._all_history.items()