Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix nws blocking startup #117094

Merged
merged 6 commits into from
May 8, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
70 changes: 48 additions & 22 deletions homeassistant/components/nws/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

from __future__ import annotations

from collections.abc import Awaitable, Callable
from dataclasses import dataclass
import datetime
from functools import partial
import logging

from pynws import SimpleNWS, call_with_retry
Expand Down Expand Up @@ -58,36 +60,49 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
nws_data = SimpleNWS(latitude, longitude, api_key, client_session)
await nws_data.set_station(station)

async def update_observation() -> None:
"""Retrieve recent observations."""
await call_with_retry(
nws_data.update_observation,
RETRY_INTERVAL,
RETRY_STOP,
start_time=utcnow() - UPDATE_TIME_PERIOD,
)

async def update_forecast() -> None:
"""Retrieve twice-daily forecsat."""
await call_with_retry(
def async_setup_update_observation(
retry_interval: datetime.timedelta | float,
retry_stop: datetime.timedelta | float,
) -> Callable[[], Awaitable[None]]:
async def update_observation() -> None:
"""Retrieve recent observations."""
await call_with_retry(
nws_data.update_observation,
retry_interval,
retry_stop,
start_time=utcnow() - UPDATE_TIME_PERIOD,
)

return update_observation

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,
RETRY_INTERVAL,
RETRY_STOP,
retry_interval,
retry_stop,
)

async def update_forecast_hourly() -> None:
"""Retrieve hourly forecast."""
await call_with_retry(
def async_setup_update_forecast_hourly(
retry_interval: datetime.timedelta | float,
retry_stop: datetime.timedelta | float,
) -> Callable[[], Awaitable[None]]:
return partial(
call_with_retry,
nws_data.update_forecast_hourly,
RETRY_INTERVAL,
RETRY_STOP,
retry_interval,
retry_stop,
)

# Don't use retries in setup
coordinator_observation = TimestampDataUpdateCoordinator(
hass,
_LOGGER,
name=f"NWS observation station {station}",
update_method=update_observation,
update_method=async_setup_update_observation(0, 0),
update_interval=DEFAULT_SCAN_INTERVAL,
request_refresh_debouncer=debounce.Debouncer(
hass, _LOGGER, cooldown=DEBOUNCE_TIME, immediate=True
Expand All @@ -98,7 +113,7 @@ async def update_forecast_hourly() -> None:
hass,
_LOGGER,
name=f"NWS forecast station {station}",
update_method=update_forecast,
update_method=async_setup_update_forecast(0, 0),
update_interval=DEFAULT_SCAN_INTERVAL,
request_refresh_debouncer=debounce.Debouncer(
hass, _LOGGER, cooldown=DEBOUNCE_TIME, immediate=True
Expand All @@ -109,7 +124,7 @@ async def update_forecast_hourly() -> None:
hass,
_LOGGER,
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,
request_refresh_debouncer=debounce.Debouncer(
hass, _LOGGER, cooldown=DEBOUNCE_TIME, immediate=True
Expand All @@ -128,6 +143,17 @@ async def update_forecast_hourly() -> None:
await coordinator_forecast.async_refresh()
Copy link
Member

@bdraco bdraco May 8, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For the future: Sometimes its faster to do an asyncio.gather here (with create_eager_task)

It will depend on if more connections is slower or faster with the remote server
example

create_eager_task(async_setup_component(hass, domain, config))

If the server doesn't like more connections at once it can be slower though so it has to be tested to see the result

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I had to guess, NWS won't like multiple connections, but I can look into testing.

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)

return True
Expand Down