From 2fccbd6e4758faba2b9a43d5c84c02707aa30239 Mon Sep 17 00:00:00 2001 From: Felix Eckhofer Date: Wed, 25 Feb 2026 18:16:44 +0100 Subject: [PATCH] dwd_weather_warnings: Filter expired warnings (#163096) --- .../components/dwd_weather_warnings/sensor.py | 18 +++++- .../dwd_weather_warnings/test_init.py | 63 +++++++++++++++++++ 2 files changed, 79 insertions(+), 2 deletions(-) diff --git a/homeassistant/components/dwd_weather_warnings/sensor.py b/homeassistant/components/dwd_weather_warnings/sensor.py index 1c2817350a5..6069fdc6a2f 100644 --- a/homeassistant/components/dwd_weather_warnings/sensor.py +++ b/homeassistant/components/dwd_weather_warnings/sensor.py @@ -11,6 +11,7 @@ Wetterwarnungen (Stufe 1) from __future__ import annotations +from datetime import UTC, datetime from typing import Any from homeassistant.components.sensor import SensorEntity, SensorEntityDescription @@ -95,13 +96,25 @@ class DwdWeatherWarningsSensor( entry_type=DeviceEntryType.SERVICE, ) + def _filter_expired_warnings( + self, warnings: list[dict[str, Any]] | None + ) -> list[dict[str, Any]]: + if warnings is None: + return [] + + now = datetime.now(UTC) + return [warning for warning in warnings if warning[API_ATTR_WARNING_END] > now] + @property def native_value(self) -> int | None: """Return the state of the sensor.""" if self.entity_description.key == CURRENT_WARNING_SENSOR: - return self.coordinator.api.current_warning_level + warnings = self.coordinator.api.current_warnings + else: + warnings = self.coordinator.api.expected_warnings - return self.coordinator.api.expected_warning_level + warnings = self._filter_expired_warnings(warnings) + return max((w.get(API_ATTR_WARNING_LEVEL, 0) for w in warnings), default=0) @property def extra_state_attributes(self) -> dict[str, Any]: @@ -117,6 +130,7 @@ class DwdWeatherWarningsSensor( else: searched_warnings = self.coordinator.api.expected_warnings + searched_warnings = self._filter_expired_warnings(searched_warnings) data[ATTR_WARNING_COUNT] = len(searched_warnings) for i, warning in enumerate(searched_warnings, 1): diff --git a/tests/components/dwd_weather_warnings/test_init.py b/tests/components/dwd_weather_warnings/test_init.py index 54f57ead77c..0a94555e85a 100644 --- a/tests/components/dwd_weather_warnings/test_init.py +++ b/tests/components/dwd_weather_warnings/test_init.py @@ -1,9 +1,23 @@ """Tests for Deutscher Wetterdienst (DWD) Weather Warnings integration.""" +from datetime import UTC, datetime, timedelta from unittest.mock import MagicMock from homeassistant.components.dwd_weather_warnings.const import ( + API_ATTR_WARNING_COLOR, + API_ATTR_WARNING_DESCRIPTION, + API_ATTR_WARNING_END, + API_ATTR_WARNING_HEADLINE, + API_ATTR_WARNING_INSTRUCTION, + API_ATTR_WARNING_LEVEL, + API_ATTR_WARNING_NAME, + API_ATTR_WARNING_PARAMETERS, + API_ATTR_WARNING_START, + API_ATTR_WARNING_TYPE, + ATTR_WARNING_COUNT, CONF_REGION_DEVICE_TRACKER, + CONF_REGION_IDENTIFIER, + CURRENT_WARNING_SENSOR, DOMAIN, ) from homeassistant.components.dwd_weather_warnings.coordinator import ( @@ -20,6 +34,22 @@ from . import init_integration from tests.common import MockConfigEntry +def _warning(level: int, end_time: datetime) -> dict[str, object]: + """Return a warning payload for tests.""" + return { + API_ATTR_WARNING_NAME: f"Warning {level}", + API_ATTR_WARNING_TYPE: level, + API_ATTR_WARNING_LEVEL: level, + API_ATTR_WARNING_HEADLINE: f"Headline {level}", + API_ATTR_WARNING_DESCRIPTION: "Description", + API_ATTR_WARNING_INSTRUCTION: "Instruction", + API_ATTR_WARNING_START: end_time - timedelta(hours=1), + API_ATTR_WARNING_END: end_time, + API_ATTR_WARNING_PARAMETERS: {}, + API_ATTR_WARNING_COLOR: "#ffffff", + } + + async def test_load_unload_entry( hass: HomeAssistant, mock_identifier_entry: MockConfigEntry, @@ -136,3 +166,36 @@ async def test_load_valid_device_tracker( assert mock_tracker_entry.state is ConfigEntryState.LOADED assert isinstance(mock_tracker_entry.runtime_data, DwdWeatherWarningsCoordinator) + + +async def test_filter_expired_warnings( + hass: HomeAssistant, entity_registry: er.EntityRegistry, mock_dwdwfsapi: MagicMock +) -> None: + """Test expired-warning filtering.""" + now = datetime.now(UTC) + mock_dwdwfsapi.data_valid = True + mock_dwdwfsapi.warncell_id = "803000000" + mock_dwdwfsapi.warncell_name = "Test region" + mock_dwdwfsapi.current_warnings = [ + _warning(4, now - timedelta(minutes=30)), + _warning(2, now + timedelta(minutes=30)), + ] + mock_dwdwfsapi.expected_warnings = [] + + entry = MockConfigEntry( + domain=DOMAIN, + data={CONF_REGION_IDENTIFIER: "803000000"}, + unique_id="803000000", + ) + await init_integration(hass, entry) + + entity_id = entity_registry.async_get_entity_id( + "sensor", DOMAIN, f"{entry.unique_id}-{CURRENT_WARNING_SENSOR}" + ) + assert entity_id is not None + + state = hass.states.get(entity_id) + assert state is not None + assert state.state == "2" + assert state.attributes[ATTR_WARNING_COUNT] == 1 + assert "warning_2" not in state.attributes