From 28652345bd925d429312c021befd5278f8dd8d0d Mon Sep 17 00:00:00 2001 From: epenet <6771947+epenet@users.noreply.github.com> Date: Tue, 18 Apr 2023 01:07:58 +0200 Subject: [PATCH] Remove duplicate code in update coordinator (#91573) --- homeassistant/helpers/update_coordinator.py | 25 +++++++++------------ 1 file changed, 10 insertions(+), 15 deletions(-) diff --git a/homeassistant/helpers/update_coordinator.py b/homeassistant/helpers/update_coordinator.py index e9de580bad1..cd6393ae694 100644 --- a/homeassistant/helpers/update_coordinator.py +++ b/homeassistant/helpers/update_coordinator.py @@ -144,10 +144,7 @@ class DataUpdateCoordinator(BaseDataUpdateCoordinatorProtocol, Generic[_T]): @callback def _unschedule_refresh(self) -> None: """Unschedule any pending refresh since there is no longer any listeners.""" - if self._unsub_refresh: - self._unsub_refresh() - self._unsub_refresh = None - + self._async_unsub_refresh() self._debounced_refresh.async_cancel() def async_contexts(self) -> Generator[Any, None, None]: @@ -156,6 +153,12 @@ class DataUpdateCoordinator(BaseDataUpdateCoordinatorProtocol, Generic[_T]): context for _, context in self._listeners.values() if context is not None ) + def _async_unsub_refresh(self) -> None: + """Cancel any scheduled call.""" + if self._unsub_refresh: + self._unsub_refresh() + self._unsub_refresh = None + @callback def _schedule_refresh(self) -> None: """Schedule a refresh.""" @@ -167,9 +170,7 @@ class DataUpdateCoordinator(BaseDataUpdateCoordinatorProtocol, Generic[_T]): # We do not cancel the debouncer here. If the refresh interval is shorter # than the debouncer cooldown, this would cause the debounce to never be called - if self._unsub_refresh: - self._unsub_refresh() - self._unsub_refresh = None + self._async_unsub_refresh() # We _floor_ utcnow to create a schedule on a rounded second, # minimizing the time between the point and the real activation. @@ -233,10 +234,7 @@ class DataUpdateCoordinator(BaseDataUpdateCoordinatorProtocol, Generic[_T]): raise_on_entry_error: bool = False, ) -> None: """Refresh data.""" - if self._unsub_refresh: - self._unsub_refresh() - self._unsub_refresh = None - + self._async_unsub_refresh() self._debounced_refresh.async_cancel() if scheduled and self.hass.is_stopping: @@ -352,10 +350,7 @@ class DataUpdateCoordinator(BaseDataUpdateCoordinatorProtocol, Generic[_T]): @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._async_unsub_refresh() self._debounced_refresh.async_cancel() self.data = data