Determine how to run listeners at setup time instead of execution time (#41304)

This commit is contained in:
J. Nick Koston 2020-10-07 09:51:50 -05:00 committed by GitHub
parent 8d94dff75c
commit 9e1461da62
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 216 additions and 119 deletions

View file

@ -9,7 +9,7 @@ import urllib.error
import aiohttp
import requests
from homeassistant.core import CALLBACK_TYPE, HomeAssistant, callback
from homeassistant.core import CALLBACK_TYPE, HassJob, HomeAssistant, callback
from homeassistant.helpers import entity, event
from homeassistant.util.dt import utcnow
@ -48,6 +48,7 @@ class DataUpdateCoordinator(Generic[T]):
self.data: Optional[T] = None
self._listeners: List[CALLBACK_TYPE] = []
self._job = HassJob(self._handle_refresh_interval)
self._unsub_refresh: Optional[CALLBACK_TYPE] = None
self._request_refresh_task: Optional[asyncio.TimerHandle] = None
self.last_update_success = True
@ -108,7 +109,7 @@ class DataUpdateCoordinator(Generic[T]):
# as long as the update process takes less than a second
self._unsub_refresh = event.async_track_point_in_utc_time(
self.hass,
self._handle_refresh_interval,
self._job,
utcnow().replace(microsecond=0) + self.update_interval,
)