Fix daily forecast sunset sunrise type

Fix #49
This commit is contained in:
Jules 2024-06-23 12:43:45 +02:00
parent 2a5f122dae
commit 5f53d16ce2
Signed by: jdejaegh
GPG key ID: 99D6D184CA66933A
4 changed files with 19 additions and 19 deletions

View file

@ -440,8 +440,8 @@ class IrmKmiCoordinator(TimestampDataUpdateCoordinator):
wind_bearing=wind_bearing,
is_daytime=is_daytime,
text=f.get('text', {}).get(lang, ""),
sunrise=sunrise,
sunset=sunset
sunrise=sunrise.isoformat() if sunrise is not None else None,
sunset=sunset.isoformat() if sunset is not None else None
)
# Swap temperature and templow if needed
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?
text: str | None
sunrise: datetime | None
sunset: datetime | None
sunrise: str | None
sunset: str | None
class IrmKmiRadarForecast(Forecast):

View file

@ -1,5 +1,5 @@
"""Sensor for pollen from the IRM KMI"""
import datetime
from datetime import datetime
import logging
from homeassistant.components import sensor
@ -74,7 +74,7 @@ class IrmKmiNextWarning(CoordinatorEntity, SensorEntity):
self._attr_translation_key = f"next_warning"
@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"""
if self.coordinator.data.get('warnings') is 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'
@property
def native_value(self) -> datetime.datetime | None:
def native_value(self) -> datetime | None:
"""Return the timestamp for the next sunrise or sunset"""
now = dt.now()
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:
return upcoming[0]

View file

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