mirror of
https://github.com/home-assistant/core.git
synced 2026-12-02 19:57:03 -04:00
Add current rainfall sensor to irm_kmi (#193427)
This commit is contained in:
@@ -5,10 +5,21 @@ from datetime import datetime, timedelta
|
||||
import logging
|
||||
from typing import TYPE_CHECKING, Final, override
|
||||
|
||||
from irm_kmi_api import IrmKmiApiClientHa, IrmKmiApiError, PollenLevel, PollenName
|
||||
from irm_kmi_api import (
|
||||
IrmKmiApiClientHa,
|
||||
IrmKmiApiError,
|
||||
PollenLevel,
|
||||
PollenName,
|
||||
RadarForecast,
|
||||
)
|
||||
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.const import CONF_LATITUDE, CONF_LOCATION, CONF_LONGITUDE
|
||||
from homeassistant.const import (
|
||||
CONF_LATITUDE,
|
||||
CONF_LOCATION,
|
||||
CONF_LONGITUDE,
|
||||
UnitOfVolumetricFlux,
|
||||
)
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers.update_coordinator import (
|
||||
DataUpdateCoordinator,
|
||||
@@ -26,6 +37,12 @@ _LOGGER = logging.getLogger(__name__)
|
||||
UPDATE_INTERVAL: Final = timedelta(minutes=7)
|
||||
GRACE_PERIOD: Final = 2.5 * UPDATE_INTERVAL
|
||||
|
||||
# Belgian and Luxembourgish radar rates are per 10 minutes, Dutch ones per hour
|
||||
MM_PER_HOUR_FACTOR: Final[dict[str | None, float]] = {
|
||||
"mm/10min": 6,
|
||||
UnitOfVolumetricFlux.MILLIMETERS_PER_HOUR: 1,
|
||||
}
|
||||
|
||||
type IrmKmiConfigEntry = ConfigEntry[IrmKmiRuntimeData]
|
||||
|
||||
|
||||
@@ -37,6 +54,31 @@ class IrmKmiRuntimeData:
|
||||
pollen: IrmKmiPollenCoordinator
|
||||
|
||||
|
||||
def _in_mm_per_hour(forecasts: list[RadarForecast]) -> list[RadarForecast]:
|
||||
"""Convert radar rain rates to mm/h, dropping frames in an unknown unit."""
|
||||
converted: list[RadarForecast] = []
|
||||
for forecast in forecasts:
|
||||
factor = MM_PER_HOUR_FACTOR.get(forecast["unit"])
|
||||
if factor is None:
|
||||
_LOGGER.debug(
|
||||
"Dropping a radar frame reported in unknown unit %s", forecast["unit"]
|
||||
)
|
||||
continue
|
||||
precipitation = forecast["native_precipitation"]
|
||||
converted.append(
|
||||
{
|
||||
**forecast,
|
||||
"native_precipitation": None
|
||||
if precipitation is None
|
||||
else round(precipitation * factor, 2),
|
||||
"rain_forecast_max": round(forecast["rain_forecast_max"] * factor, 2),
|
||||
"rain_forecast_min": round(forecast["rain_forecast_min"] * factor, 2),
|
||||
"unit": UnitOfVolumetricFlux.MILLIMETERS_PER_HOUR,
|
||||
}
|
||||
)
|
||||
return converted
|
||||
|
||||
|
||||
class IrmKmiCoordinator(TimestampDataUpdateCoordinator[ProcessedCoordinatorData]):
|
||||
"""Coordinator to update data from IRM KMI."""
|
||||
|
||||
@@ -107,6 +149,7 @@ class IrmKmiCoordinator(TimestampDataUpdateCoordinator[ProcessedCoordinatorData]
|
||||
hourly_forecast=self._api.get_hourly_forecast(tz),
|
||||
country=self._api.get_country(),
|
||||
warnings=self._api.get_warnings(lang),
|
||||
radar_forecast=_in_mm_per_hour(self._api.get_radar_forecast()),
|
||||
)
|
||||
|
||||
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
|
||||
from dataclasses import dataclass, field
|
||||
|
||||
from irm_kmi_api import CurrentWeatherData, ExtendedForecast, WarningData
|
||||
from irm_kmi_api import CurrentWeatherData, ExtendedForecast, RadarForecast, WarningData
|
||||
|
||||
from homeassistant.components.weather import Forecast
|
||||
|
||||
@@ -14,5 +14,7 @@ class ProcessedCoordinatorData:
|
||||
current_weather: CurrentWeatherData
|
||||
country: str | None
|
||||
warnings: list[WarningData]
|
||||
# In mm/h
|
||||
radar_forecast: list[RadarForecast]
|
||||
hourly_forecast: list[Forecast] = field(default_factory=list)
|
||||
daily_forecast: list[ExtendedForecast] = field(default_factory=list)
|
||||
|
||||
@@ -20,6 +20,7 @@ from homeassistant.const import (
|
||||
UnitOfPressure,
|
||||
UnitOfSpeed,
|
||||
UnitOfTemperature,
|
||||
UnitOfVolumetricFlux,
|
||||
)
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback
|
||||
@@ -29,6 +30,7 @@ from homeassistant.util import dt as dt_util
|
||||
from .coordinator import IrmKmiConfigEntry, IrmKmiCoordinator, IrmKmiPollenCoordinator
|
||||
from .data import ProcessedCoordinatorData
|
||||
from .entity import IrmKmiBaseEntity
|
||||
from .utils import current_radar_frame
|
||||
|
||||
# Coordinator is used to centralize the data updates
|
||||
PARALLEL_UPDATES = 0
|
||||
@@ -63,6 +65,13 @@ class IrmKmiPollenSensorEntityDescription(SensorEntityDescription):
|
||||
pollen: PollenName
|
||||
|
||||
|
||||
def _rainfall(data: ProcessedCoordinatorData) -> float | None:
|
||||
"""Return the rain rate of the radar frame covering now."""
|
||||
if not (radar_forecast := data.radar_forecast):
|
||||
return None
|
||||
return radar_forecast[current_radar_frame(radar_forecast)]["native_precipitation"]
|
||||
|
||||
|
||||
SENSOR_TYPES: tuple[IrmKmiSensorEntityDescription, ...] = (
|
||||
IrmKmiSensorEntityDescription(
|
||||
key="temperature",
|
||||
@@ -114,6 +123,15 @@ SENSOR_TYPES: tuple[IrmKmiSensorEntityDescription, ...] = (
|
||||
device_class=SensorDeviceClass.TIMESTAMP,
|
||||
value_fn=_next_warning,
|
||||
),
|
||||
IrmKmiSensorEntityDescription(
|
||||
key="rainfall",
|
||||
device_class=SensorDeviceClass.PRECIPITATION_INTENSITY,
|
||||
state_class=SensorStateClass.MEASUREMENT,
|
||||
native_unit_of_measurement=UnitOfVolumetricFlux.MILLIMETERS_PER_HOUR,
|
||||
# The radar resolves 0.01 mm/10min, which is 0.06 mm/h
|
||||
suggested_display_precision=2,
|
||||
value_fn=_rainfall,
|
||||
),
|
||||
)
|
||||
|
||||
POLLEN_SENSOR_TYPES: tuple[IrmKmiPollenSensorEntityDescription, ...] = tuple(
|
||||
|
||||
@@ -2,7 +2,10 @@
|
||||
|
||||
from typing import TYPE_CHECKING
|
||||
|
||||
from irm_kmi_api import RadarForecast
|
||||
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.util import dt as dt_util
|
||||
|
||||
from .const import CONF_LANGUAGE_OVERRIDE, LANGS
|
||||
|
||||
@@ -20,3 +23,13 @@ def preferred_language(hass: HomeAssistant, config_entry: IrmKmiConfigEntry) ->
|
||||
if language == "none":
|
||||
return hass.config.language if hass.config.language in LANGS else "en"
|
||||
return language
|
||||
|
||||
|
||||
def current_radar_frame(radar_forecast: list[RadarForecast]) -> int:
|
||||
"""Return the index of the last radar frame at or before now, clamped to 0."""
|
||||
now = dt_util.utcnow()
|
||||
started = sum(
|
||||
dt_util.parse_datetime(forecast["datetime"], raise_on_error=True) <= now
|
||||
for forecast in radar_forecast
|
||||
)
|
||||
return max(started - 1, 0)
|
||||
|
||||
@@ -15,7 +15,7 @@ from homeassistant.const import (
|
||||
CONF_UNIQUE_ID,
|
||||
)
|
||||
|
||||
from .const import CURRENT_WEATHER, POLLEN, WARNINGS
|
||||
from .const import CURRENT_WEATHER, POLLEN, RADAR_FORECAST, WARNINGS
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
@@ -62,6 +62,7 @@ def mock_irm_kmi_api() -> Generator[MagicMock]:
|
||||
irm_kmi.get_daily_forecast.return_value = []
|
||||
irm_kmi.get_hourly_forecast.return_value = []
|
||||
irm_kmi.get_warnings.return_value = WARNINGS
|
||||
irm_kmi.get_radar_forecast.return_value = RADAR_FORECAST
|
||||
irm_kmi.get_pollen.return_value = POLLEN
|
||||
yield irm_kmi
|
||||
|
||||
|
||||
@@ -6,6 +6,7 @@ from irm_kmi_api import (
|
||||
CurrentWeatherData,
|
||||
PollenLevel,
|
||||
PollenName,
|
||||
RadarForecast,
|
||||
WarningData,
|
||||
WarningType,
|
||||
)
|
||||
@@ -16,6 +17,7 @@ WARNING_ENTITY_ID = "binary_sensor.brussels_warning"
|
||||
|
||||
ALDER_POLLEN_ENTITY_ID = "sensor.brussels_alder_pollen"
|
||||
NEXT_WARNING_ENTITY_ID = "sensor.brussels_next_warning"
|
||||
RAINFALL_ENTITY_ID = "sensor.brussels_precipitation_intensity"
|
||||
TEMPERATURE_ENTITY_ID = "sensor.brussels_temperature"
|
||||
WIND_DIRECTION_ENTITY_ID = "sensor.brussels_wind_direction"
|
||||
WIND_GUST_SPEED_ENTITY_ID = "sensor.brussels_wind_gust_speed"
|
||||
@@ -60,3 +62,38 @@ POLLEN = {
|
||||
PollenName.MUGWORT: PollenLevel.ACTIVE,
|
||||
PollenName.OAK: PollenLevel.NONE,
|
||||
}
|
||||
|
||||
RADAR_FORECAST = [
|
||||
RadarForecast(
|
||||
datetime="2023-12-28T15:10:00+01:00",
|
||||
native_precipitation=0.1,
|
||||
rain_forecast_max=0.15,
|
||||
rain_forecast_min=0.05,
|
||||
might_rain=True,
|
||||
unit="mm/10min",
|
||||
),
|
||||
RadarForecast(
|
||||
datetime="2023-12-28T15:20:00+01:00",
|
||||
native_precipitation=0.05,
|
||||
rain_forecast_max=0.1,
|
||||
rain_forecast_min=0.0,
|
||||
might_rain=True,
|
||||
unit="mm/10min",
|
||||
),
|
||||
RadarForecast(
|
||||
datetime="2023-12-28T15:30:00+01:00",
|
||||
native_precipitation=0.4,
|
||||
rain_forecast_max=0.6,
|
||||
rain_forecast_min=0.2,
|
||||
might_rain=True,
|
||||
unit="mm/10min",
|
||||
),
|
||||
RadarForecast(
|
||||
datetime="2023-12-28T15:40:00+01:00",
|
||||
native_precipitation=1.2,
|
||||
rain_forecast_max=1.5,
|
||||
rain_forecast_min=0.9,
|
||||
might_rain=True,
|
||||
unit="mm/10min",
|
||||
),
|
||||
]
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,161 @@
|
||||
# serializer version: 1
|
||||
# name: test_radar_forecast_in_millimeters_per_hour[forecast.json]
|
||||
list([
|
||||
dict({
|
||||
'datetime': '2023-12-26T17:00:00+01:00',
|
||||
'might_rain': False,
|
||||
'native_precipitation': 0,
|
||||
'rain_forecast_max': 0,
|
||||
'rain_forecast_min': 0,
|
||||
'unit': <UnitOfVolumetricFlux.MILLIMETERS_PER_HOUR: 'mm/h'>,
|
||||
}),
|
||||
dict({
|
||||
'datetime': '2023-12-26T17:10:00+01:00',
|
||||
'might_rain': False,
|
||||
'native_precipitation': 0,
|
||||
'rain_forecast_max': 0,
|
||||
'rain_forecast_min': 0,
|
||||
'unit': <UnitOfVolumetricFlux.MILLIMETERS_PER_HOUR: 'mm/h'>,
|
||||
}),
|
||||
dict({
|
||||
'datetime': '2023-12-26T17:20:00+01:00',
|
||||
'might_rain': False,
|
||||
'native_precipitation': 0,
|
||||
'rain_forecast_max': 0,
|
||||
'rain_forecast_min': 0,
|
||||
'unit': <UnitOfVolumetricFlux.MILLIMETERS_PER_HOUR: 'mm/h'>,
|
||||
}),
|
||||
dict({
|
||||
'datetime': '2023-12-26T17:30:00+01:00',
|
||||
'might_rain': False,
|
||||
'native_precipitation': 0,
|
||||
'rain_forecast_max': 0,
|
||||
'rain_forecast_min': 0,
|
||||
'unit': <UnitOfVolumetricFlux.MILLIMETERS_PER_HOUR: 'mm/h'>,
|
||||
}),
|
||||
dict({
|
||||
'datetime': '2023-12-26T17:40:00+01:00',
|
||||
'might_rain': False,
|
||||
'native_precipitation': 0.6,
|
||||
'rain_forecast_max': 0,
|
||||
'rain_forecast_min': 0,
|
||||
'unit': <UnitOfVolumetricFlux.MILLIMETERS_PER_HOUR: 'mm/h'>,
|
||||
}),
|
||||
dict({
|
||||
'datetime': '2023-12-26T17:50:00+01:00',
|
||||
'might_rain': False,
|
||||
'native_precipitation': 0.06,
|
||||
'rain_forecast_max': 0,
|
||||
'rain_forecast_min': 0,
|
||||
'unit': <UnitOfVolumetricFlux.MILLIMETERS_PER_HOUR: 'mm/h'>,
|
||||
}),
|
||||
dict({
|
||||
'datetime': '2023-12-26T18:00:00+01:00',
|
||||
'might_rain': False,
|
||||
'native_precipitation': 0.72,
|
||||
'rain_forecast_max': 0,
|
||||
'rain_forecast_min': 0,
|
||||
'unit': <UnitOfVolumetricFlux.MILLIMETERS_PER_HOUR: 'mm/h'>,
|
||||
}),
|
||||
dict({
|
||||
'datetime': '2023-12-26T18:10:00+01:00',
|
||||
'might_rain': False,
|
||||
'native_precipitation': 7.2,
|
||||
'rain_forecast_max': 0,
|
||||
'rain_forecast_min': 0,
|
||||
'unit': <UnitOfVolumetricFlux.MILLIMETERS_PER_HOUR: 'mm/h'>,
|
||||
}),
|
||||
dict({
|
||||
'datetime': '2023-12-26T18:20:00+01:00',
|
||||
'might_rain': False,
|
||||
'native_precipitation': 12,
|
||||
'rain_forecast_max': 0,
|
||||
'rain_forecast_min': 0,
|
||||
'unit': <UnitOfVolumetricFlux.MILLIMETERS_PER_HOUR: 'mm/h'>,
|
||||
}),
|
||||
dict({
|
||||
'datetime': '2023-12-26T18:30:00+01:00',
|
||||
'might_rain': False,
|
||||
'native_precipitation': 0,
|
||||
'rain_forecast_max': 0,
|
||||
'rain_forecast_min': 0,
|
||||
'unit': <UnitOfVolumetricFlux.MILLIMETERS_PER_HOUR: 'mm/h'>,
|
||||
}),
|
||||
dict({
|
||||
'datetime': '2023-12-26T18:40:00+01:00',
|
||||
'might_rain': False,
|
||||
'native_precipitation': 0,
|
||||
'rain_forecast_max': 0,
|
||||
'rain_forecast_min': 0,
|
||||
'unit': <UnitOfVolumetricFlux.MILLIMETERS_PER_HOUR: 'mm/h'>,
|
||||
}),
|
||||
])
|
||||
# ---
|
||||
# name: test_radar_forecast_in_millimeters_per_hour[forecast_nl_with_rain_on_radar.json]
|
||||
list([
|
||||
dict({
|
||||
'datetime': '2023-12-28T13:50:00+00:00',
|
||||
'might_rain': False,
|
||||
'native_precipitation': 0,
|
||||
'rain_forecast_max': 0,
|
||||
'rain_forecast_min': 0,
|
||||
'unit': <UnitOfVolumetricFlux.MILLIMETERS_PER_HOUR: 'mm/h'>,
|
||||
}),
|
||||
dict({
|
||||
'datetime': '2023-12-28T13:55:00+00:00',
|
||||
'might_rain': False,
|
||||
'native_precipitation': 0,
|
||||
'rain_forecast_max': 0,
|
||||
'rain_forecast_min': 0,
|
||||
'unit': <UnitOfVolumetricFlux.MILLIMETERS_PER_HOUR: 'mm/h'>,
|
||||
}),
|
||||
dict({
|
||||
'datetime': '2023-12-28T14:00:00+00:00',
|
||||
'might_rain': False,
|
||||
'native_precipitation': 0,
|
||||
'rain_forecast_max': 0,
|
||||
'rain_forecast_min': 0,
|
||||
'unit': <UnitOfVolumetricFlux.MILLIMETERS_PER_HOUR: 'mm/h'>,
|
||||
}),
|
||||
dict({
|
||||
'datetime': '2023-12-28T14:05:00+00:00',
|
||||
'might_rain': False,
|
||||
'native_precipitation': 0,
|
||||
'rain_forecast_max': 0,
|
||||
'rain_forecast_min': 0,
|
||||
'unit': <UnitOfVolumetricFlux.MILLIMETERS_PER_HOUR: 'mm/h'>,
|
||||
}),
|
||||
dict({
|
||||
'datetime': '2023-12-28T14:10:00+00:00',
|
||||
'might_rain': False,
|
||||
'native_precipitation': 0,
|
||||
'rain_forecast_max': 0,
|
||||
'rain_forecast_min': 0,
|
||||
'unit': <UnitOfVolumetricFlux.MILLIMETERS_PER_HOUR: 'mm/h'>,
|
||||
}),
|
||||
dict({
|
||||
'datetime': '2023-12-28T14:15:00+00:00',
|
||||
'might_rain': False,
|
||||
'native_precipitation': 0,
|
||||
'rain_forecast_max': 0,
|
||||
'rain_forecast_min': 0,
|
||||
'unit': <UnitOfVolumetricFlux.MILLIMETERS_PER_HOUR: 'mm/h'>,
|
||||
}),
|
||||
dict({
|
||||
'datetime': '2023-12-28T14:20:00+00:00',
|
||||
'might_rain': False,
|
||||
'native_precipitation': 0,
|
||||
'rain_forecast_max': 0,
|
||||
'rain_forecast_min': 0,
|
||||
'unit': <UnitOfVolumetricFlux.MILLIMETERS_PER_HOUR: 'mm/h'>,
|
||||
}),
|
||||
dict({
|
||||
'datetime': '2023-12-28T14:25:00+00:00',
|
||||
'might_rain': False,
|
||||
'native_precipitation': 0.15,
|
||||
'rain_forecast_max': 0,
|
||||
'rain_forecast_min': 0,
|
||||
'unit': <UnitOfVolumetricFlux.MILLIMETERS_PER_HOUR: 'mm/h'>,
|
||||
}),
|
||||
])
|
||||
# ---
|
||||
@@ -607,6 +607,65 @@
|
||||
'state': 'none',
|
||||
})
|
||||
# ---
|
||||
# name: test_sensors[sensor.brussels_precipitation_intensity-entry]
|
||||
EntityRegistryEntrySnapshot({
|
||||
'aliases': list([
|
||||
None,
|
||||
]),
|
||||
'area_id': None,
|
||||
'capabilities': dict({
|
||||
<SensorEntityCapabilityAttribute.STATE_CLASS: 'state_class'>: <SensorStateClass.MEASUREMENT: 'measurement'>,
|
||||
}),
|
||||
'config_entry_id': <ANY>,
|
||||
'config_subentry_id': None,
|
||||
'device_class': None,
|
||||
'device_id': <DeviceEntry identifiers=[('irm_kmi', '01JIRMKMI00000000000000000')]>,
|
||||
'disabled_by': None,
|
||||
'domain': 'sensor',
|
||||
'entity_category': None,
|
||||
'entity_id': 'sensor.brussels_precipitation_intensity',
|
||||
'has_entity_name': True,
|
||||
'hidden_by': None,
|
||||
'icon': None,
|
||||
'id': <ANY>,
|
||||
'labels': set({
|
||||
}),
|
||||
'name': None,
|
||||
'object_id_base': 'Precipitation intensity',
|
||||
'options': dict({
|
||||
'sensor': dict({
|
||||
'suggested_display_precision': 2,
|
||||
}),
|
||||
}),
|
||||
'original_device_class': <SensorDeviceClass.PRECIPITATION_INTENSITY: 'precipitation_intensity'>,
|
||||
'original_icon': None,
|
||||
'original_name': 'Precipitation intensity',
|
||||
'platform': 'irm_kmi',
|
||||
'previous_unique_id': None,
|
||||
'suggested_object_id': None,
|
||||
'supported_features': 0,
|
||||
'translation_key': None,
|
||||
'unique_id': 'brussels be-rainfall',
|
||||
'unit_of_measurement': <UnitOfVolumetricFlux.MILLIMETERS_PER_HOUR: 'mm/h'>,
|
||||
})
|
||||
# ---
|
||||
# name: test_sensors[sensor.brussels_precipitation_intensity-state]
|
||||
StateSnapshot({
|
||||
'attributes': ReadOnlyDict({
|
||||
<EntityStateAttribute.ATTRIBUTION: 'attribution'>: 'Weather data from the Royal Meteorological Institute of Belgium meteo.be',
|
||||
<EntityStateAttribute.DEVICE_CLASS: 'device_class'>: 'precipitation_intensity',
|
||||
<EntityStateAttribute.FRIENDLY_NAME: 'friendly_name'>: 'Brussels Precipitation intensity',
|
||||
<SensorEntityCapabilityAttribute.STATE_CLASS: 'state_class'>: <SensorStateClass.MEASUREMENT: 'measurement'>,
|
||||
<EntityStateAttribute.UNIT_OF_MEASUREMENT: 'unit_of_measurement'>: <UnitOfVolumetricFlux.MILLIMETERS_PER_HOUR: 'mm/h'>,
|
||||
}),
|
||||
'context': <ANY>,
|
||||
'entity_id': 'sensor.brussels_precipitation_intensity',
|
||||
'last_changed': <ANY>,
|
||||
'last_reported': <ANY>,
|
||||
'last_updated': <ANY>,
|
||||
'state': '2.4',
|
||||
})
|
||||
# ---
|
||||
# name: test_sensors[sensor.brussels_temperature-entry]
|
||||
EntityRegistryEntrySnapshot({
|
||||
'aliases': list([
|
||||
|
||||
@@ -0,0 +1,43 @@
|
||||
"""Tests for the IRM KMI coordinator."""
|
||||
|
||||
import pytest
|
||||
from pytest_homeassistant.common import MockConfigEntry
|
||||
from syrupy.assertion import SnapshotAssertion
|
||||
|
||||
from homeassistant.const import UnitOfVolumetricFlux
|
||||
from homeassistant.core import HomeAssistant
|
||||
|
||||
from . import setup_integration
|
||||
from .const import RADAR_FORECAST
|
||||
|
||||
|
||||
@pytest.mark.usefixtures("mock_get_forecasts_coord")
|
||||
@pytest.mark.parametrize(
|
||||
"forecast_fixture", ["forecast.json", "forecast_nl_with_rain_on_radar.json"]
|
||||
)
|
||||
async def test_radar_forecast_in_millimeters_per_hour(
|
||||
hass: HomeAssistant,
|
||||
mock_config_entry: MockConfigEntry,
|
||||
snapshot: SnapshotAssertion,
|
||||
) -> None:
|
||||
"""Test every radar rate the provider reports is converted to mm/h."""
|
||||
await setup_integration(hass, mock_config_entry)
|
||||
|
||||
assert mock_config_entry.runtime_data.weather.data.radar_forecast == snapshot
|
||||
|
||||
|
||||
@pytest.mark.usefixtures("mock_irm_kmi_api")
|
||||
async def test_radar_forecast_bounds_in_millimeters_per_hour(
|
||||
hass: HomeAssistant,
|
||||
mock_config_entry: MockConfigEntry,
|
||||
) -> None:
|
||||
"""Test the rain bounds of a radar frame are converted along with its rate."""
|
||||
await setup_integration(hass, mock_config_entry)
|
||||
|
||||
assert mock_config_entry.runtime_data.weather.data.radar_forecast[2] == {
|
||||
**RADAR_FORECAST[2],
|
||||
"native_precipitation": 2.4,
|
||||
"rain_forecast_max": 3.6,
|
||||
"rain_forecast_min": 1.2,
|
||||
"unit": UnitOfVolumetricFlux.MILLIMETERS_PER_HOUR,
|
||||
}
|
||||
@@ -11,6 +11,7 @@ from irm_kmi_api import (
|
||||
PollenLevel,
|
||||
PollenName,
|
||||
PollenParser,
|
||||
RadarForecast,
|
||||
WarningData,
|
||||
)
|
||||
import pytest
|
||||
@@ -21,7 +22,13 @@ from pytest_homeassistant.common import (
|
||||
)
|
||||
from syrupy.assertion import SnapshotAssertion
|
||||
|
||||
from homeassistant.const import STATE_UNAVAILABLE, STATE_UNKNOWN, Platform
|
||||
from homeassistant.const import (
|
||||
ATTR_UNIT_OF_MEASUREMENT,
|
||||
STATE_UNAVAILABLE,
|
||||
STATE_UNKNOWN,
|
||||
Platform,
|
||||
UnitOfVolumetricFlux,
|
||||
)
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers import entity_registry as er
|
||||
|
||||
@@ -31,6 +38,8 @@ from .const import (
|
||||
CURRENT_WEATHER,
|
||||
NEXT_WARNING_ENTITY_ID,
|
||||
POLLEN,
|
||||
RADAR_FORECAST,
|
||||
RAINFALL_ENTITY_ID,
|
||||
TEMPERATURE_ENTITY_ID,
|
||||
WARNINGS,
|
||||
WIND_DIRECTION_ENTITY_ID,
|
||||
@@ -246,3 +255,143 @@ async def test_next_warning(
|
||||
state = hass.states.get(NEXT_WARNING_ENTITY_ID)
|
||||
assert state
|
||||
assert state.state == expected_state
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("now", "radar_forecast", "expected_state"),
|
||||
[
|
||||
pytest.param(
|
||||
"2023-12-28T15:00:00+01:00",
|
||||
RADAR_FORECAST,
|
||||
"0.6",
|
||||
id="before_the_first_frame",
|
||||
),
|
||||
pytest.param(
|
||||
"2023-12-28T15:10:00+01:00",
|
||||
RADAR_FORECAST,
|
||||
"0.6",
|
||||
id="on_the_first_frame",
|
||||
),
|
||||
pytest.param(
|
||||
"2023-12-28T15:19:59+01:00",
|
||||
RADAR_FORECAST,
|
||||
"0.6",
|
||||
id="one_second_before_the_next_frame",
|
||||
),
|
||||
pytest.param(
|
||||
"2023-12-28T15:20:00+01:00",
|
||||
RADAR_FORECAST,
|
||||
"0.3",
|
||||
id="on_a_frame",
|
||||
),
|
||||
pytest.param(
|
||||
"2023-12-28T15:35:00+01:00",
|
||||
RADAR_FORECAST,
|
||||
"2.4",
|
||||
id="between_two_frames",
|
||||
),
|
||||
pytest.param(
|
||||
"2023-12-28T16:30:00+01:00",
|
||||
RADAR_FORECAST,
|
||||
"7.2",
|
||||
id="after_the_last_frame",
|
||||
),
|
||||
pytest.param(
|
||||
"2023-12-28T15:30:00+01:00",
|
||||
[],
|
||||
STATE_UNKNOWN,
|
||||
id="without_any_frame",
|
||||
),
|
||||
],
|
||||
)
|
||||
async def test_rainfall_frame_covering_now(
|
||||
hass: HomeAssistant,
|
||||
mock_config_entry: MockConfigEntry,
|
||||
mock_irm_kmi_api: MagicMock,
|
||||
freezer: FrozenDateTimeFactory,
|
||||
now: str,
|
||||
radar_forecast: list[RadarForecast],
|
||||
expected_state: str,
|
||||
) -> None:
|
||||
"""Test the sensor reports the last radar frame that already started."""
|
||||
freezer.move_to(now)
|
||||
mock_irm_kmi_api.get_radar_forecast.return_value = radar_forecast
|
||||
|
||||
await setup_integration(hass, mock_config_entry)
|
||||
|
||||
state = hass.states.get(RAINFALL_ENTITY_ID)
|
||||
assert state
|
||||
assert state.state == expected_state
|
||||
|
||||
|
||||
@pytest.mark.usefixtures("mock_get_forecasts_coord")
|
||||
@pytest.mark.parametrize(
|
||||
("forecast_fixture", "now", "expected_state"),
|
||||
[
|
||||
pytest.param(
|
||||
"forecast.json",
|
||||
"2023-12-26T18:15:00+01:00",
|
||||
"7.2",
|
||||
id="belgium",
|
||||
),
|
||||
pytest.param(
|
||||
"forecast_nl_with_rain_on_radar.json",
|
||||
"2023-12-28T15:25:00+01:00",
|
||||
"0.15",
|
||||
id="netherlands",
|
||||
),
|
||||
],
|
||||
)
|
||||
async def test_rainfall_is_always_in_millimeters_per_hour(
|
||||
hass: HomeAssistant,
|
||||
mock_config_entry: MockConfigEntry,
|
||||
freezer: FrozenDateTimeFactory,
|
||||
now: str,
|
||||
expected_state: str,
|
||||
) -> None:
|
||||
"""Test both units the provider reports are converted to mm/h."""
|
||||
freezer.move_to(now)
|
||||
|
||||
await setup_integration(hass, mock_config_entry)
|
||||
|
||||
state = hass.states.get(RAINFALL_ENTITY_ID)
|
||||
assert state
|
||||
assert state.state == expected_state
|
||||
assert (
|
||||
state.attributes[ATTR_UNIT_OF_MEASUREMENT]
|
||||
== UnitOfVolumetricFlux.MILLIMETERS_PER_HOUR
|
||||
)
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"radar_forecast",
|
||||
[
|
||||
pytest.param(
|
||||
[RadarForecast(**{**RADAR_FORECAST[2], "unit": None})],
|
||||
id="without_a_unit",
|
||||
),
|
||||
pytest.param(
|
||||
[RadarForecast(**{**RADAR_FORECAST[2], "unit": "mm/5min"})],
|
||||
id="unknown_unit",
|
||||
),
|
||||
pytest.param(
|
||||
[RadarForecast(**{**RADAR_FORECAST[2], "native_precipitation": None})],
|
||||
id="without_a_rate",
|
||||
),
|
||||
],
|
||||
)
|
||||
@pytest.mark.freeze_time("2023-12-28T15:30:00+01:00")
|
||||
async def test_rainfall_without_a_usable_frame(
|
||||
hass: HomeAssistant,
|
||||
mock_config_entry: MockConfigEntry,
|
||||
mock_irm_kmi_api: MagicMock,
|
||||
radar_forecast: list[RadarForecast],
|
||||
) -> None:
|
||||
"""Test a frame that cannot be expressed in mm/h is unknown."""
|
||||
mock_irm_kmi_api.get_radar_forecast.return_value = radar_forecast
|
||||
|
||||
await setup_integration(hass, mock_config_entry)
|
||||
|
||||
state = hass.states.get(RAINFALL_ENTITY_ID)
|
||||
assert state
|
||||
assert state.state == STATE_UNKNOWN
|
||||
|
||||
Reference in New Issue
Block a user