Add support for manual updating a data coordinator (#42746)

This commit is contained in:
Paulus Schoutsen 2020-11-02 09:54:08 +01:00 committed by GitHub
parent ce16ff27ef
commit 1844e30858
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 57 additions and 1 deletions

View file

@ -138,9 +138,9 @@ class DataUpdateCoordinator(Generic[T]):
self._unsub_refresh = None
self._debounced_refresh.async_cancel()
start = monotonic()
try:
start = monotonic()
self.data = await self._async_update_data()
except (asyncio.TimeoutError, requests.exceptions.Timeout):
@ -192,6 +192,28 @@ class DataUpdateCoordinator(Generic[T]):
for update_callback in self._listeners:
update_callback()
@callback
def async_set_updated_data(self, data: T) -> None:
"""Manually update data, notify listeners and reset refresh interval."""
if self._unsub_refresh:
self._unsub_refresh()
self._unsub_refresh = None
self._debounced_refresh.async_cancel()
self.data = data
self.last_update_success = True
self.logger.debug(
"Manually updated %s data",
self.name,
)
if self._listeners:
self._schedule_refresh()
for update_callback in self._listeners:
update_callback()
class CoordinatorEntity(entity.Entity):
"""A class for entities using DataUpdateCoordinator."""