diff --git a/homeassistant/components/esphome/manager.py b/homeassistant/components/esphome/manager.py index f89e79aae2b..dc95952194e 100644 --- a/homeassistant/components/esphome/manager.py +++ b/homeassistant/components/esphome/manager.py @@ -30,7 +30,7 @@ from homeassistant.config_entries import ConfigEntry from homeassistant.const import ( ATTR_DEVICE_ID, CONF_MODE, - EVENT_HOMEASSISTANT_STOP, + EVENT_HOMEASSISTANT_CLOSE, EVENT_LOGGING_CHANGED, ) from homeassistant.core import Event, HomeAssistant, ServiceCall, State, callback @@ -542,9 +542,12 @@ class ESPHomeManager: # 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_STOP, self.on_stop, run_immediately=True + EVENT_HOMEASSISTANT_CLOSE, self.on_stop, run_immediately=True ) ) entry_data.cleanup_callbacks.append(