Fix nws blocking startup (#117094)

Co-authored-by: J. Nick Koston <nick@koston.org>
This commit is contained in:
MatthewFlamm 2024-05-08 15:16:20 -04:00 committed by GitHub
parent 84a91a86a9
commit 6b3ffad77a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -2,8 +2,10 @@
from __future__ import annotations from __future__ import annotations
from collections.abc import Awaitable, Callable
from dataclasses import dataclass from dataclasses import dataclass
import datetime import datetime
from functools import partial
import logging import logging
from pynws import SimpleNWS, call_with_retry from pynws import SimpleNWS, call_with_retry
@ -58,36 +60,49 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
nws_data = SimpleNWS(latitude, longitude, api_key, client_session) nws_data = SimpleNWS(latitude, longitude, api_key, client_session)
await nws_data.set_station(station) await nws_data.set_station(station)
async def update_observation() -> None: def async_setup_update_observation(
"""Retrieve recent observations.""" retry_interval: datetime.timedelta | float,
await call_with_retry( retry_stop: datetime.timedelta | float,
nws_data.update_observation, ) -> Callable[[], Awaitable[None]]:
RETRY_INTERVAL, async def update_observation() -> None:
RETRY_STOP, """Retrieve recent observations."""
start_time=utcnow() - UPDATE_TIME_PERIOD, await call_with_retry(
) nws_data.update_observation,
retry_interval,
retry_stop,
start_time=utcnow() - UPDATE_TIME_PERIOD,
)
async def update_forecast() -> None: return update_observation
"""Retrieve twice-daily forecsat."""
await call_with_retry( def async_setup_update_forecast(
retry_interval: datetime.timedelta | float,
retry_stop: datetime.timedelta | float,
) -> Callable[[], Awaitable[None]]:
return partial(
call_with_retry,
nws_data.update_forecast, nws_data.update_forecast,
RETRY_INTERVAL, retry_interval,
RETRY_STOP, retry_stop,
) )
async def update_forecast_hourly() -> None: def async_setup_update_forecast_hourly(
"""Retrieve hourly forecast.""" retry_interval: datetime.timedelta | float,
await call_with_retry( retry_stop: datetime.timedelta | float,
) -> Callable[[], Awaitable[None]]:
return partial(
call_with_retry,
nws_data.update_forecast_hourly, nws_data.update_forecast_hourly,
RETRY_INTERVAL, retry_interval,
RETRY_STOP, retry_stop,
) )
# Don't use retries in setup
coordinator_observation = TimestampDataUpdateCoordinator( coordinator_observation = TimestampDataUpdateCoordinator(
hass, hass,
_LOGGER, _LOGGER,
name=f"NWS observation station {station}", name=f"NWS observation station {station}",
update_method=update_observation, update_method=async_setup_update_observation(0, 0),
update_interval=DEFAULT_SCAN_INTERVAL, update_interval=DEFAULT_SCAN_INTERVAL,
request_refresh_debouncer=debounce.Debouncer( request_refresh_debouncer=debounce.Debouncer(
hass, _LOGGER, cooldown=DEBOUNCE_TIME, immediate=True hass, _LOGGER, cooldown=DEBOUNCE_TIME, immediate=True
@ -98,7 +113,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
hass, hass,
_LOGGER, _LOGGER,
name=f"NWS forecast station {station}", name=f"NWS forecast station {station}",
update_method=update_forecast, update_method=async_setup_update_forecast(0, 0),
update_interval=DEFAULT_SCAN_INTERVAL, update_interval=DEFAULT_SCAN_INTERVAL,
request_refresh_debouncer=debounce.Debouncer( request_refresh_debouncer=debounce.Debouncer(
hass, _LOGGER, cooldown=DEBOUNCE_TIME, immediate=True hass, _LOGGER, cooldown=DEBOUNCE_TIME, immediate=True
@ -109,7 +124,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
hass, hass,
_LOGGER, _LOGGER,
name=f"NWS forecast hourly station {station}", name=f"NWS forecast hourly station {station}",
update_method=update_forecast_hourly, update_method=async_setup_update_forecast_hourly(0, 0),
update_interval=DEFAULT_SCAN_INTERVAL, update_interval=DEFAULT_SCAN_INTERVAL,
request_refresh_debouncer=debounce.Debouncer( request_refresh_debouncer=debounce.Debouncer(
hass, _LOGGER, cooldown=DEBOUNCE_TIME, immediate=True hass, _LOGGER, cooldown=DEBOUNCE_TIME, immediate=True
@ -128,6 +143,17 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
await coordinator_forecast.async_refresh() await coordinator_forecast.async_refresh()
await coordinator_forecast_hourly.async_refresh() await coordinator_forecast_hourly.async_refresh()
# Use retries
coordinator_observation.update_method = async_setup_update_observation(
RETRY_INTERVAL, RETRY_STOP
)
coordinator_forecast.update_method = async_setup_update_forecast(
RETRY_INTERVAL, RETRY_STOP
)
coordinator_forecast_hourly.update_method = async_setup_update_forecast_hourly(
RETRY_INTERVAL, RETRY_STOP
)
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS) await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
return True return True