Merge pull request #50 from jdejaegh/serialize_daily_forecast

Serialize daily forecast
This commit is contained in:
Jules 2024-06-23 13:50:25 +02:00 committed by GitHub
commit 65e31b700d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 21 additions and 21 deletions

View file

@ -212,7 +212,7 @@ class IrmKmiCoordinator(TimestampDataUpdateCoordinator):
or not isinstance(hourly_forecast_data, list) or not isinstance(hourly_forecast_data, list)
or len(hourly_forecast_data) == 0): or len(hourly_forecast_data) == 0):
for current in hourly_forecast_data[:2]: for current in hourly_forecast_data[:4]:
if now.strftime('%H') == current['hour']: if now.strftime('%H') == current['hour']:
now_hourly = current now_hourly = current
break break
@ -278,7 +278,7 @@ class IrmKmiCoordinator(TimestampDataUpdateCoordinator):
if current_weather['condition'] is None: if current_weather['condition'] is None:
try: try:
current_weather['condition'] = CDT_MAP.get((int(now_hourly.get('ww')), now_hourly.get('dayNight'))) current_weather['condition'] = CDT_MAP.get((int(now_hourly.get('ww')), now_hourly.get('dayNight')))
except (TypeError, ValueError): except (TypeError, ValueError, AttributeError):
current_weather['condition'] = None current_weather['condition'] = None
return current_weather return current_weather
@ -440,8 +440,8 @@ class IrmKmiCoordinator(TimestampDataUpdateCoordinator):
wind_bearing=wind_bearing, wind_bearing=wind_bearing,
is_daytime=is_daytime, is_daytime=is_daytime,
text=f.get('text', {}).get(lang, ""), text=f.get('text', {}).get(lang, ""),
sunrise=sunrise, sunrise=sunrise.isoformat() if sunrise is not None else None,
sunset=sunset sunset=sunset.isoformat() if sunset is not None else None
) )
# Swap temperature and templow if needed # Swap temperature and templow if needed
if (forecast['native_templow'] is not None if (forecast['native_templow'] is not None

View file

@ -10,8 +10,8 @@ class IrmKmiForecast(Forecast):
# TODO: add condition_2 as well and evolution to match data from the API? # TODO: add condition_2 as well and evolution to match data from the API?
text: str | None text: str | None
sunrise: datetime | None sunrise: str | None
sunset: datetime | None sunset: str | None
class IrmKmiRadarForecast(Forecast): class IrmKmiRadarForecast(Forecast):

View file

@ -1,5 +1,5 @@
"""Sensor for pollen from the IRM KMI""" """Sensor for pollen from the IRM KMI"""
import datetime from datetime import datetime
import logging import logging
from homeassistant.components import sensor from homeassistant.components import sensor
@ -74,7 +74,7 @@ class IrmKmiNextWarning(CoordinatorEntity, SensorEntity):
self._attr_translation_key = f"next_warning" self._attr_translation_key = f"next_warning"
@property @property
def native_value(self) -> datetime.datetime | None: def native_value(self) -> datetime | None:
"""Return the timestamp for the start of the next warning. Is None when no future warning are available""" """Return the timestamp for the start of the next warning. Is None when no future warning are available"""
if self.coordinator.data.get('warnings') is None: if self.coordinator.data.get('warnings') is None:
return None return None
@ -124,12 +124,13 @@ class IrmKmiNextSunMove(CoordinatorEntity, SensorEntity):
self._attr_icon = 'mdi:weather-sunset-down' if move == 'sunset' else 'mdi:weather-sunset-up' self._attr_icon = 'mdi:weather-sunset-down' if move == 'sunset' else 'mdi:weather-sunset-up'
@property @property
def native_value(self) -> datetime.datetime | None: def native_value(self) -> datetime | None:
"""Return the timestamp for the next sunrise or sunset""" """Return the timestamp for the next sunrise or sunset"""
now = dt.now() now = dt.now()
data: list[IrmKmiForecast] = self.coordinator.data.get('daily_forecast') data: list[IrmKmiForecast] = self.coordinator.data.get('daily_forecast')
upcoming = [f.get(self._move) for f in data if f.get(self._move) >= now] upcoming = [datetime.fromisoformat(f.get(self._move)) for f in data
if f.get(self._move) is not None and datetime.fromisoformat(f.get(self._move)) >= now]
if len(upcoming) > 0: if len(upcoming) > 0:
return upcoming[0] return upcoming[0]

View file

@ -102,7 +102,6 @@ async def test_daily_forecast(
assert len(result) == 8 assert len(result) == 8
assert result[0]['datetime'] == '2023-12-26' assert result[0]['datetime'] == '2023-12-26'
assert not result[0]['is_daytime'] assert not result[0]['is_daytime']
tz = zoneinfo.ZoneInfo(key='Europe/Brussels')
expected = IrmKmiForecast( expected = IrmKmiForecast(
datetime='2023-12-27', datetime='2023-12-27',
condition=ATTR_CONDITION_PARTLYCLOUDY, condition=ATTR_CONDITION_PARTLYCLOUDY,
@ -115,8 +114,8 @@ async def test_daily_forecast(
wind_bearing=180, wind_bearing=180,
is_daytime=True, is_daytime=True,
text='Bar', text='Bar',
sunrise=datetime.fromisoformat("2023-12-27T08:44:00+01:00").astimezone(tz), sunrise="2023-12-27T08:44:00+01:00",
sunset=datetime.fromisoformat("2023-12-27T16:43:00+01:00").astimezone(tz) sunset="2023-12-27T16:43:00+01:00"
) )
assert result[1] == expected assert result[1] == expected
@ -365,14 +364,14 @@ async def test_sunrise_sunset_nl(
coordinator = IrmKmiCoordinator(hass, mock_config_entry) coordinator = IrmKmiCoordinator(hass, mock_config_entry)
result = await coordinator.daily_list_to_forecast(api_data) result = await coordinator.daily_list_to_forecast(api_data)
assert result[0]['sunrise'].isoformat() == '2024-06-09T05:19:28+02:00' assert result[0]['sunrise'] == '2024-06-09T05:19:28+02:00'
assert result[0]['sunset'].isoformat() == '2024-06-09T22:01:09+02:00' assert result[0]['sunset'] == '2024-06-09T22:01:09+02:00'
assert result[1]['sunrise'] is None assert result[1]['sunrise'] is None
assert result[1]['sunset'] is None assert result[1]['sunset'] is None
assert result[2]['sunrise'].isoformat() == '2024-06-10T05:19:08+02:00' assert result[2]['sunrise'] == '2024-06-10T05:19:08+02:00'
assert result[2]['sunset'].isoformat() == '2024-06-10T22:01:53+02:00' assert result[2]['sunset'] == '2024-06-10T22:01:53+02:00'
@freeze_time("2023-12-26T18:30:00+01:00") @freeze_time("2023-12-26T18:30:00+01:00")
@ -385,8 +384,8 @@ async def test_sunrise_sunset_be(
coordinator = IrmKmiCoordinator(hass, mock_config_entry) coordinator = IrmKmiCoordinator(hass, mock_config_entry)
result = await coordinator.daily_list_to_forecast(api_data) result = await coordinator.daily_list_to_forecast(api_data)
assert result[1]['sunrise'].isoformat() == '2023-12-27T08:44:00+01:00' assert result[1]['sunrise'] == '2023-12-27T08:44:00+01:00'
assert result[1]['sunset'].isoformat() == '2023-12-27T16:43:00+01:00' assert result[1]['sunset'] == '2023-12-27T16:43:00+01:00'
assert result[2]['sunrise'].isoformat() == '2023-12-28T08:45:00+01:00' assert result[2]['sunrise'] == '2023-12-28T08:45:00+01:00'
assert result[2]['sunset'].isoformat() == '2023-12-28T16:43:00+01:00' assert result[2]['sunset'] == '2023-12-28T16:43:00+01:00'