Add support for manual updating a data coordinator (#42746)
This commit is contained in:
parent
ce16ff27ef
commit
1844e30858
2 changed files with 57 additions and 1 deletions
|
@ -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."""
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue