mirror of
https://github.com/home-assistant/core.git
synced 2025-07-19 03:07:37 +00:00
Improve typing of nws (#98485)
* Improve typing of nws * Address review comments
This commit is contained in:
parent
b1053e8077
commit
827e06a5c8
@ -2,6 +2,7 @@
|
|||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
from collections.abc import Awaitable, Callable
|
from collections.abc import Awaitable, Callable
|
||||||
|
from dataclasses import dataclass
|
||||||
import datetime
|
import datetime
|
||||||
import logging
|
import logging
|
||||||
from typing import TYPE_CHECKING
|
from typing import TYPE_CHECKING
|
||||||
@ -18,15 +19,7 @@ from homeassistant.helpers.event import async_track_point_in_utc_time
|
|||||||
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator
|
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator
|
||||||
from homeassistant.util.dt import utcnow
|
from homeassistant.util.dt import utcnow
|
||||||
|
|
||||||
from .const import (
|
from .const import CONF_STATION, DOMAIN, UPDATE_TIME_PERIOD
|
||||||
CONF_STATION,
|
|
||||||
COORDINATOR_FORECAST,
|
|
||||||
COORDINATOR_FORECAST_HOURLY,
|
|
||||||
COORDINATOR_OBSERVATION,
|
|
||||||
DOMAIN,
|
|
||||||
NWS_DATA,
|
|
||||||
UPDATE_TIME_PERIOD,
|
|
||||||
)
|
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
@ -42,6 +35,16 @@ def base_unique_id(latitude: float, longitude: float) -> str:
|
|||||||
return f"{latitude}_{longitude}"
|
return f"{latitude}_{longitude}"
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass
|
||||||
|
class NWSData:
|
||||||
|
"""Data for the National Weather Service integration."""
|
||||||
|
|
||||||
|
api: SimpleNWS
|
||||||
|
coordinator_observation: NwsDataUpdateCoordinator
|
||||||
|
coordinator_forecast: NwsDataUpdateCoordinator
|
||||||
|
coordinator_forecast_hourly: NwsDataUpdateCoordinator
|
||||||
|
|
||||||
|
|
||||||
class NwsDataUpdateCoordinator(DataUpdateCoordinator[None]):
|
class NwsDataUpdateCoordinator(DataUpdateCoordinator[None]):
|
||||||
"""NWS data update coordinator.
|
"""NWS data update coordinator.
|
||||||
|
|
||||||
@ -150,12 +153,12 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
|||||||
),
|
),
|
||||||
)
|
)
|
||||||
nws_hass_data = hass.data.setdefault(DOMAIN, {})
|
nws_hass_data = hass.data.setdefault(DOMAIN, {})
|
||||||
nws_hass_data[entry.entry_id] = {
|
nws_hass_data[entry.entry_id] = NWSData(
|
||||||
NWS_DATA: nws_data,
|
nws_data,
|
||||||
COORDINATOR_OBSERVATION: coordinator_observation,
|
coordinator_observation,
|
||||||
COORDINATOR_FORECAST: coordinator_forecast,
|
coordinator_forecast,
|
||||||
COORDINATOR_FORECAST_HOURLY: coordinator_forecast_hourly,
|
coordinator_forecast_hourly,
|
||||||
}
|
)
|
||||||
|
|
||||||
# Fetch initial data so we have data when entities subscribe
|
# Fetch initial data so we have data when entities subscribe
|
||||||
await coordinator_observation.async_refresh()
|
await coordinator_observation.async_refresh()
|
||||||
|
@ -74,11 +74,6 @@ CONDITION_CLASSES: dict[str, list[str]] = {
|
|||||||
DAYNIGHT = "daynight"
|
DAYNIGHT = "daynight"
|
||||||
HOURLY = "hourly"
|
HOURLY = "hourly"
|
||||||
|
|
||||||
NWS_DATA = "nws data"
|
|
||||||
COORDINATOR_OBSERVATION = "coordinator_observation"
|
|
||||||
COORDINATOR_FORECAST = "coordinator_forecast"
|
|
||||||
COORDINATOR_FORECAST_HOURLY = "coordinator_forecast_hourly"
|
|
||||||
|
|
||||||
OBSERVATION_VALID_TIME = timedelta(minutes=20)
|
OBSERVATION_VALID_TIME = timedelta(minutes=20)
|
||||||
FORECAST_VALID_TIME = timedelta(minutes=45)
|
FORECAST_VALID_TIME = timedelta(minutes=45)
|
||||||
# A lot of stations update once hourly plus some wiggle room
|
# A lot of stations update once hourly plus some wiggle room
|
||||||
|
@ -5,8 +5,6 @@ from dataclasses import dataclass
|
|||||||
from types import MappingProxyType
|
from types import MappingProxyType
|
||||||
from typing import Any
|
from typing import Any
|
||||||
|
|
||||||
from pynws import SimpleNWS
|
|
||||||
|
|
||||||
from homeassistant.components.sensor import (
|
from homeassistant.components.sensor import (
|
||||||
SensorDeviceClass,
|
SensorDeviceClass,
|
||||||
SensorEntity,
|
SensorEntity,
|
||||||
@ -36,15 +34,8 @@ from homeassistant.util.unit_conversion import (
|
|||||||
)
|
)
|
||||||
from homeassistant.util.unit_system import US_CUSTOMARY_SYSTEM
|
from homeassistant.util.unit_system import US_CUSTOMARY_SYSTEM
|
||||||
|
|
||||||
from . import NwsDataUpdateCoordinator, base_unique_id, device_info
|
from . import NWSData, NwsDataUpdateCoordinator, base_unique_id, device_info
|
||||||
from .const import (
|
from .const import ATTRIBUTION, CONF_STATION, DOMAIN, OBSERVATION_VALID_TIME
|
||||||
ATTRIBUTION,
|
|
||||||
CONF_STATION,
|
|
||||||
COORDINATOR_OBSERVATION,
|
|
||||||
DOMAIN,
|
|
||||||
NWS_DATA,
|
|
||||||
OBSERVATION_VALID_TIME,
|
|
||||||
)
|
|
||||||
|
|
||||||
PARALLEL_UPDATES = 0
|
PARALLEL_UPDATES = 0
|
||||||
|
|
||||||
@ -152,14 +143,14 @@ async def async_setup_entry(
|
|||||||
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
|
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Set up the NWS weather platform."""
|
"""Set up the NWS weather platform."""
|
||||||
hass_data = hass.data[DOMAIN][entry.entry_id]
|
nws_data: NWSData = hass.data[DOMAIN][entry.entry_id]
|
||||||
station = entry.data[CONF_STATION]
|
station = entry.data[CONF_STATION]
|
||||||
|
|
||||||
async_add_entities(
|
async_add_entities(
|
||||||
NWSSensor(
|
NWSSensor(
|
||||||
hass=hass,
|
hass=hass,
|
||||||
entry_data=entry.data,
|
entry_data=entry.data,
|
||||||
hass_data=hass_data,
|
nws_data=nws_data,
|
||||||
description=description,
|
description=description,
|
||||||
station=station,
|
station=station,
|
||||||
)
|
)
|
||||||
@ -177,13 +168,13 @@ class NWSSensor(CoordinatorEntity[NwsDataUpdateCoordinator], SensorEntity):
|
|||||||
self,
|
self,
|
||||||
hass: HomeAssistant,
|
hass: HomeAssistant,
|
||||||
entry_data: MappingProxyType[str, Any],
|
entry_data: MappingProxyType[str, Any],
|
||||||
hass_data: dict[str, Any],
|
nws_data: NWSData,
|
||||||
description: NWSSensorEntityDescription,
|
description: NWSSensorEntityDescription,
|
||||||
station: str,
|
station: str,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Initialise the platform with a data instance."""
|
"""Initialise the platform with a data instance."""
|
||||||
super().__init__(hass_data[COORDINATOR_OBSERVATION])
|
super().__init__(nws_data.coordinator_observation)
|
||||||
self._nws: SimpleNWS = hass_data[NWS_DATA]
|
self._nws = nws_data.api
|
||||||
self._latitude = entry_data[CONF_LATITUDE]
|
self._latitude = entry_data[CONF_LATITUDE]
|
||||||
self._longitude = entry_data[CONF_LONGITUDE]
|
self._longitude = entry_data[CONF_LONGITUDE]
|
||||||
self.entity_description = description
|
self.entity_description = description
|
||||||
|
@ -35,19 +35,15 @@ from homeassistant.util.dt import utcnow
|
|||||||
from homeassistant.util.unit_conversion import SpeedConverter, TemperatureConverter
|
from homeassistant.util.unit_conversion import SpeedConverter, TemperatureConverter
|
||||||
from homeassistant.util.unit_system import UnitSystem
|
from homeassistant.util.unit_system import UnitSystem
|
||||||
|
|
||||||
from . import base_unique_id, device_info
|
from . import NWSData, base_unique_id, device_info
|
||||||
from .const import (
|
from .const import (
|
||||||
ATTR_FORECAST_DETAILED_DESCRIPTION,
|
ATTR_FORECAST_DETAILED_DESCRIPTION,
|
||||||
ATTRIBUTION,
|
ATTRIBUTION,
|
||||||
CONDITION_CLASSES,
|
CONDITION_CLASSES,
|
||||||
COORDINATOR_FORECAST,
|
|
||||||
COORDINATOR_FORECAST_HOURLY,
|
|
||||||
COORDINATOR_OBSERVATION,
|
|
||||||
DAYNIGHT,
|
DAYNIGHT,
|
||||||
DOMAIN,
|
DOMAIN,
|
||||||
FORECAST_VALID_TIME,
|
FORECAST_VALID_TIME,
|
||||||
HOURLY,
|
HOURLY,
|
||||||
NWS_DATA,
|
|
||||||
OBSERVATION_VALID_TIME,
|
OBSERVATION_VALID_TIME,
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -84,12 +80,12 @@ async def async_setup_entry(
|
|||||||
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
|
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Set up the NWS weather platform."""
|
"""Set up the NWS weather platform."""
|
||||||
hass_data = hass.data[DOMAIN][entry.entry_id]
|
nws_data: NWSData = hass.data[DOMAIN][entry.entry_id]
|
||||||
|
|
||||||
async_add_entities(
|
async_add_entities(
|
||||||
[
|
[
|
||||||
NWSWeather(entry.data, hass_data, DAYNIGHT, hass.config.units),
|
NWSWeather(entry.data, nws_data, DAYNIGHT, hass.config.units),
|
||||||
NWSWeather(entry.data, hass_data, HOURLY, hass.config.units),
|
NWSWeather(entry.data, nws_data, HOURLY, hass.config.units),
|
||||||
],
|
],
|
||||||
False,
|
False,
|
||||||
)
|
)
|
||||||
@ -112,19 +108,19 @@ class NWSWeather(WeatherEntity):
|
|||||||
def __init__(
|
def __init__(
|
||||||
self,
|
self,
|
||||||
entry_data: MappingProxyType[str, Any],
|
entry_data: MappingProxyType[str, Any],
|
||||||
hass_data: dict[str, Any],
|
nws_data: NWSData,
|
||||||
mode: str,
|
mode: str,
|
||||||
units: UnitSystem,
|
units: UnitSystem,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Initialise the platform with a data instance and station name."""
|
"""Initialise the platform with a data instance and station name."""
|
||||||
self.nws = hass_data[NWS_DATA]
|
self.nws = nws_data.api
|
||||||
self.latitude = entry_data[CONF_LATITUDE]
|
self.latitude = entry_data[CONF_LATITUDE]
|
||||||
self.longitude = entry_data[CONF_LONGITUDE]
|
self.longitude = entry_data[CONF_LONGITUDE]
|
||||||
self.coordinator_observation = hass_data[COORDINATOR_OBSERVATION]
|
self.coordinator_observation = nws_data.coordinator_observation
|
||||||
if mode == DAYNIGHT:
|
if mode == DAYNIGHT:
|
||||||
self.coordinator_forecast = hass_data[COORDINATOR_FORECAST]
|
self.coordinator_forecast = nws_data.coordinator_forecast
|
||||||
else:
|
else:
|
||||||
self.coordinator_forecast = hass_data[COORDINATOR_FORECAST_HOURLY]
|
self.coordinator_forecast = nws_data.coordinator_forecast_hourly
|
||||||
self.station = self.nws.station
|
self.station = self.nws.station
|
||||||
|
|
||||||
self.mode = mode
|
self.mode = mode
|
||||||
|
Loading…
x
Reference in New Issue
Block a user