From c4b277b6ab6a3b9ca0bed68316ff18a1dc00a9a2 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Tue, 25 Jun 2024 19:40:04 +0200 Subject: [PATCH] Small cleanups to ESPHome manager reconnect shutdown (#120401) --- homeassistant/components/esphome/manager.py | 33 ++++++++++----------- 1 file changed, 15 insertions(+), 18 deletions(-) diff --git a/homeassistant/components/esphome/manager.py b/homeassistant/components/esphome/manager.py index 5ab0265c1d4..e8d002fba9d 100644 --- a/homeassistant/components/esphome/manager.py +++ b/homeassistant/components/esphome/manager.py @@ -586,23 +586,6 @@ class ESPHomeManager: if entry.options.get(CONF_ALLOW_SERVICE_CALLS, DEFAULT_ALLOW_SERVICE_CALLS): async_delete_issue(hass, DOMAIN, self.services_issue) - # Use async_listen instead of async_listen_once so that we don't deregister - # the callback twice when shutting down Home Assistant. - # "Unable to remove unknown listener - # .onetime_listener>" - # We only close the connection at the last possible moment - # when the CLOSE event is fired so anything using a Bluetooth - # proxy has a chance to shut down properly. - entry_data.cleanup_callbacks.append( - hass.bus.async_listen(EVENT_HOMEASSISTANT_CLOSE, self.on_stop) - ) - entry_data.cleanup_callbacks.append( - hass.bus.async_listen( - EVENT_LOGGING_CHANGED, - self._async_handle_logging_changed, - ) - ) - reconnect_logic = ReconnectLogic( client=self.cli, on_connect=self.on_connect, @@ -613,6 +596,21 @@ class ESPHomeManager: ) self.reconnect_logic = reconnect_logic + # Use async_listen instead of async_listen_once so that we don't deregister + # the callback twice when shutting down Home Assistant. + # "Unable to remove unknown listener + # .onetime_listener>" + # We only close the connection at the last possible moment + # when the CLOSE event is fired so anything using a Bluetooth + # proxy has a chance to shut down properly. + bus = hass.bus + cleanups = ( + bus.async_listen(EVENT_HOMEASSISTANT_CLOSE, self.on_stop), + bus.async_listen(EVENT_LOGGING_CHANGED, self._async_handle_logging_changed), + reconnect_logic.stop_callback, + ) + entry_data.cleanup_callbacks.extend(cleanups) + infos, services = await entry_data.async_load_from_store() if entry.unique_id: await entry_data.async_update_static_infos( @@ -628,7 +626,6 @@ class ESPHomeManager: ) await reconnect_logic.start() - entry_data.cleanup_callbacks.append(reconnect_logic.stop_callback) entry.async_on_unload( entry.add_update_listener(entry_data.async_update_listener)