Add ability to shutdown update coordinator (#91456)

* Add ability to shutdown update coordinator

* Adjust nibe_heatpump

* Add tests

* Use async

* Remove duplicate code in update coordinator

* Adjust

* Revert nibe changes - it can now be done in a follow-up PR

* Adjust

* Fix incorrect merge

* async_fire_time_changed
This commit is contained in:
epenet 2023-04-18 18:56:43 +02:00 committed by GitHub
parent bdffb1f298
commit ae0cbffdd8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 45 additions and 1 deletions

View file

@ -71,6 +71,7 @@ class DataUpdateCoordinator(BaseDataUpdateCoordinatorProtocol, Generic[_T]):
self.name = name
self.update_method = update_method
self.update_interval = update_interval
self._shutdown_requested = False
self.config_entry = config_entries.current_entry.get()
# It's None before the first successful update.
@ -141,6 +142,12 @@ class DataUpdateCoordinator(BaseDataUpdateCoordinatorProtocol, Generic[_T]):
for update_callback, _ in list(self._listeners.values()):
update_callback()
async def async_shutdown(self) -> None:
"""Cancel any scheduled call, and ignore new runs."""
self._shutdown_requested = True
self._async_unsub_refresh()
await self._debounced_refresh.async_shutdown()
@callback
def _unschedule_refresh(self) -> None:
"""Unschedule any pending refresh since there is no longer any listeners."""
@ -237,7 +244,7 @@ class DataUpdateCoordinator(BaseDataUpdateCoordinatorProtocol, Generic[_T]):
self._async_unsub_refresh()
self._debounced_refresh.async_cancel()
if scheduled and self.hass.is_stopping:
if self._shutdown_requested or scheduled and self.hass.is_stopping:
return
if log_timing := self.logger.isEnabledFor(logging.DEBUG):