Update ZHA storage every 10 minutes (#39710)

This commit is contained in:
David F. Mulcahey 2020-09-06 15:00:28 -04:00 committed by GitHub
parent 8af64456ec
commit f2d21ea735
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -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()