From 338938a38e88c3e7862f70b88763a99adde0024c Mon Sep 17 00:00:00 2001 From: Simone Chemelli Date: Wed, 30 Dec 2020 10:29:54 +0100 Subject: [PATCH] Fix shelly shutdown AttributeError (#44172) * Additional check for clean shutdown * Changed approach * Remover leftover * Added callback key * Moved to listen once --- homeassistant/components/shelly/__init__.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/homeassistant/components/shelly/__init__.py b/homeassistant/components/shelly/__init__.py index 298c7e111b2..cd08b625a5d 100644 --- a/homeassistant/components/shelly/__init__.py +++ b/homeassistant/components/shelly/__init__.py @@ -143,6 +143,8 @@ class ShellyDeviceWrapper(update_coordinator.DataUpdateCoordinator): ) self._last_input_events_count = dict() + hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STOP, self._handle_ha_stop) + @callback def _async_input_events_handler(self): """Handle device input events.""" @@ -184,6 +186,7 @@ class ShellyDeviceWrapper(update_coordinator.DataUpdateCoordinator): async def _async_update_data(self): """Fetch data.""" + _LOGGER.debug("Polling Shelly Device - %s", self.name) try: async with async_timeout.timeout( @@ -206,6 +209,7 @@ class ShellyDeviceWrapper(update_coordinator.DataUpdateCoordinator): async def async_setup(self): """Set up the wrapper.""" + dev_reg = await device_registry.async_get_registry(self.hass) model_type = self.device.settings["device"]["type"] entry = dev_reg.async_get_or_create( @@ -225,6 +229,12 @@ class ShellyDeviceWrapper(update_coordinator.DataUpdateCoordinator): self.device.shutdown() self._async_remove_input_events_handler() + @callback + def _handle_ha_stop(self, _): + """Handle Home Assistant stopping.""" + _LOGGER.debug("Stopping ShellyDeviceWrapper for %s", self.name) + self.shutdown() + class ShellyDeviceRestWrapper(update_coordinator.DataUpdateCoordinator): """Rest Wrapper for a Shelly device with Home Assistant specific functions."""