diff --git a/README.md b/README.md index 113612b..4800149 100644 --- a/README.md +++ b/README.md @@ -87,6 +87,7 @@ Each element in the list has the following attributes: * `text: str`: language specific additional information about the warning * `starts_at: datetime`: time at which the warning starts being relevant * `ends_at: datetime`: time at which the warning stops being relevant + * `is_active: bool`: `true` if `starts_at` < now < `ends_at` The following table summarizes the different known warning types. Other warning types may be returned and will have `unknown` as slug. Feel free to open an issue with the id and the English friendly name to have it added to this integration. @@ -104,6 +105,9 @@ The following table summarizes the different known warning types. Other warning | storm_surge | 15 | Storm surge, Marée forte, Stormtij, Sturmflut | | coldspell | 17 | Coldspell, Vague de froid, Koude, Koude | +The sensor has an attribute called `active_warnings_friendly_names`, holding a comma separated list of the friendly names +of the currently active warnings (e.g. `Fog, Ice or snow`). There is no particular order for the list. + ## Disclaimer This is a personal project and isn't in any way affiliated with, sponsored or endorsed by [The Royal Meteorological diff --git a/custom_components/irm_kmi/binary_sensor.py b/custom_components/irm_kmi/binary_sensor.py index 5442512..1e490d3 100644 --- a/custom_components/irm_kmi/binary_sensor.py +++ b/custom_components/irm_kmi/binary_sensor.py @@ -59,4 +59,12 @@ class IrmKmiWarning(CoordinatorEntity, BinarySensorEntity): def extra_state_attributes(self) -> dict: """Return the camera state attributes.""" attrs = {"warnings": self.coordinator.data.get('warnings', [])} + + now = datetime.datetime.now(tz=pytz.timezone(self.hass.config.time_zone)) + for warning in attrs['warnings']: + warning['is_active'] = warning.get('starts_at') < now < warning.get('ends_at') + + attrs["active_warnings_friendly_names"] = ", ".join([warning['friendly_name'] for warning in attrs['warnings'] + if warning['is_active']]) + return attrs diff --git a/custom_components/irm_kmi/coordinator.py b/custom_components/irm_kmi/coordinator.py index f7cfd39..b344e41 100644 --- a/custom_components/irm_kmi/coordinator.py +++ b/custom_components/irm_kmi/coordinator.py @@ -347,10 +347,10 @@ class IrmKmiCoordinator(DataUpdateCoordinator): dark_mode=self._dark_mode, tz=self.hass.config.time_zone) - def warnings_from_data(self, warning_data: list | None) -> List[WarningData] | None: + def warnings_from_data(self, warning_data: list | None) -> List[WarningData]: """Create a list of warning data instances based on the api data""" if warning_data is None or not isinstance(warning_data, list) or len(warning_data) == 0: - return None + return [] result = list() for data in warning_data: @@ -379,4 +379,4 @@ class IrmKmiCoordinator(DataUpdateCoordinator): ) ) - return result if len(result) > 0 else None + return result if len(result) > 0 else [] diff --git a/custom_components/irm_kmi/data.py b/custom_components/irm_kmi/data.py index 4a045b1..6267ce3 100644 --- a/custom_components/irm_kmi/data.py +++ b/custom_components/irm_kmi/data.py @@ -63,4 +63,4 @@ class ProcessedCoordinatorData(TypedDict, total=False): hourly_forecast: List[Forecast] | None daily_forecast: List[IrmKmiForecast] | None animation: RadarAnimationData - warnings: List[WarningData] | None + warnings: List[WarningData] diff --git a/tests/test_binary_sensor.py b/tests/test_binary_sensor.py index 276c82a..7f320a6 100644 --- a/tests/test_binary_sensor.py +++ b/tests/test_binary_sensor.py @@ -25,3 +25,8 @@ async def test_warning_data( assert warning.is_on assert len(warning.extra_state_attributes['warnings']) == 2 + + for w in warning.extra_state_attributes['warnings']: + assert w['is_active'] + + assert warning.extra_state_attributes['active_warnings_friendly_names'] == "Fog, Ice or snow"