Remove duplicate code in update coordinator (#91573)

This commit is contained in:
epenet 2023-04-18 01:07:58 +02:00 committed by GitHub
parent c6b4c88355
commit 28652345bd
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

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