From f2d21ea735c80242847972ece7a8902495a58826 Mon Sep 17 00:00:00 2001 From: "David F. Mulcahey" Date: Sun, 6 Sep 2020 15:00:28 -0400 Subject: [PATCH] Update ZHA storage every 10 minutes (#39710) --- homeassistant/components/zha/core/gateway.py | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/homeassistant/components/zha/core/gateway.py b/homeassistant/components/zha/core/gateway.py index 1f58dc65086..bb57d7f03f4 100644 --- a/homeassistant/components/zha/core/gateway.py +++ b/homeassistant/components/zha/core/gateway.py @@ -26,6 +26,7 @@ from homeassistant.helpers.entity_registry import ( async_entries_for_device, async_get_registry as get_ent_reg, ) +from homeassistant.helpers.event import async_track_time_interval from . import discovery, typing as zha_typing from .const import ( @@ -117,6 +118,7 @@ class ZHAGateway: self.debug_enabled = False self._log_relay_handler = LogRelayHandler(hass, self) self._config_entry = config_entry + self._unsubs = [] async def async_initialize(self): """Initialize controller and connect radio.""" @@ -186,6 +188,13 @@ class ZHAGateway: "available" if zha_device.available else "unavailable", delta_msg, ) + # update the last seen time for devices every 10 minutes to avoid thrashing + # writes and shutdown issues where storage isn't updated + self._unsubs.append( + async_track_time_interval( + self._hass, self.async_update_device_storage, timedelta(minutes=10) + ) + ) @callback def async_load_groups(self) -> None: @@ -515,7 +524,7 @@ class ZHAGateway: if device.status is DeviceStatus.INITIALIZED: device.update_available(available) - async def async_update_device_storage(self): + async def async_update_device_storage(self, *_): """Update the devices in the store.""" for device in self.devices.values(): self.zha_storage.async_update_device(device) @@ -626,6 +635,8 @@ class ZHAGateway: async def shutdown(self): """Stop ZHA Controller Application.""" _LOGGER.debug("Shutting down ZHA ControllerApplication") + for unsubscribe in self._unsubs: + unsubscribe() await self.application_controller.shutdown()