mirror of
https://github.com/jdejaegh/irm-kmi-ha.git
synced 2025-06-27 03:35:56 +02:00
Merge pull request #46 from jdejaegh/daily_midnight_bug
Fix midnight bug for daily forecast
This commit is contained in:
commit
eb036f9d05
4 changed files with 44 additions and 9 deletions
|
@ -155,3 +155,5 @@ IRM_KMI_NAME: Final = {
|
|||
'de': 'Königliche Meteorologische Institut von Belgien',
|
||||
'en': 'Royal Meteorological Institute of Belgium'
|
||||
}
|
||||
|
||||
WEEKDAYS = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']
|
||||
|
|
|
@ -19,7 +19,7 @@ from homeassistant.util import dt
|
|||
from homeassistant.util.dt import utcnow
|
||||
|
||||
from .api import IrmKmiApiClient, IrmKmiApiError
|
||||
from .const import CONF_DARK_MODE, CONF_STYLE, DOMAIN, IRM_KMI_NAME
|
||||
from .const import CONF_DARK_MODE, CONF_STYLE, DOMAIN, IRM_KMI_NAME, WEEKDAYS
|
||||
from .const import IRM_KMI_TO_HA_CONDITION_MAP as CDT_MAP
|
||||
from .const import MAP_WARNING_ID_TO_SLUG as SLUG_MAP
|
||||
from .const import OPTION_STYLE_SATELLITE, OUT_OF_BENELUX, STYLE_TO_PARAM_MAP
|
||||
|
@ -28,7 +28,7 @@ from .data import (AnimationFrameData, CurrentWeatherData, IrmKmiForecast,
|
|||
RadarAnimationData, WarningData)
|
||||
from .pollen import PollenParser
|
||||
from .rain_graph import RainGraph
|
||||
from .utils import disable_from_config, get_config_value, preferred_language
|
||||
from .utils import disable_from_config, get_config_value, preferred_language, next_weekday
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
|
@ -356,10 +356,9 @@ class IrmKmiCoordinator(TimestampDataUpdateCoordinator):
|
|||
return None
|
||||
|
||||
forecasts = list()
|
||||
n_days = 0
|
||||
lang = preferred_language(self.hass, self._config_entry)
|
||||
tz = await dt.async_get_time_zone('Europe/Brussels')
|
||||
now = dt.now(tz)
|
||||
forecast_day = dt.now(tz)
|
||||
|
||||
for (idx, f) in enumerate(data):
|
||||
precipitation = None
|
||||
|
@ -384,9 +383,12 @@ class IrmKmiCoordinator(TimestampDataUpdateCoordinator):
|
|||
pass
|
||||
|
||||
is_daytime = f.get('dayNight', None) == 'd'
|
||||
day_name = f.get('dayName', {}).get('en', None)
|
||||
if day_name in WEEKDAYS:
|
||||
forecast_day = next_weekday(forecast_day, WEEKDAYS.index(day_name))
|
||||
|
||||
forecast = IrmKmiForecast(
|
||||
datetime=(now + timedelta(days=n_days)).strftime('%Y-%m-%d') if is_daytime else now.strftime(
|
||||
'%Y-%m-%d'),
|
||||
datetime=(forecast_day.strftime('%Y-%m-%d')),
|
||||
condition=CDT_MAP.get((f.get('ww1', None), f.get('dayNight', None)), None),
|
||||
native_precipitation=precipitation,
|
||||
native_temperature=f.get('tempMax', None),
|
||||
|
@ -406,8 +408,6 @@ class IrmKmiCoordinator(TimestampDataUpdateCoordinator):
|
|||
(forecast['native_temperature'], forecast['native_templow'])
|
||||
|
||||
forecasts.append(forecast)
|
||||
if is_daytime or idx == 0:
|
||||
n_days += 1
|
||||
|
||||
return forecasts
|
||||
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
import logging
|
||||
from datetime import timedelta
|
||||
from typing import Any
|
||||
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
|
@ -38,3 +39,10 @@ def preferred_language(hass: HomeAssistant, config_entry: ConfigEntry) -> str:
|
|||
return hass.config.language if hass.config.language in LANGS else 'en'
|
||||
|
||||
return get_config_value(config_entry, CONF_LANGUAGE_OVERRIDE)
|
||||
|
||||
|
||||
def next_weekday(current, weekday):
|
||||
days_ahead = weekday - current.weekday()
|
||||
if days_ahead < 0:
|
||||
days_ahead += 7
|
||||
return current + timedelta(days_ahead)
|
||||
|
|
|
@ -86,7 +86,7 @@ async def test_current_weather_nl() -> None:
|
|||
assert expected == result
|
||||
|
||||
|
||||
@freeze_time(datetime.fromisoformat('2023-12-26T18:30:00.028724'))
|
||||
@freeze_time(datetime.fromisoformat('2023-12-26T18:30:00+01:00'))
|
||||
async def test_daily_forecast(
|
||||
hass: HomeAssistant,
|
||||
mock_config_entry: MockConfigEntry
|
||||
|
@ -99,6 +99,8 @@ async def test_daily_forecast(
|
|||
|
||||
assert isinstance(result, list)
|
||||
assert len(result) == 8
|
||||
assert result[0]['datetime'] == '2023-12-26'
|
||||
assert not result[0]['is_daytime']
|
||||
|
||||
expected = IrmKmiForecast(
|
||||
datetime='2023-12-27',
|
||||
|
@ -193,6 +195,29 @@ async def test_hourly_forecast_midnight_bug() -> None:
|
|||
assert result[24]['datetime'] == '2024-06-01T00:00:00+02:00'
|
||||
|
||||
|
||||
@freeze_time(datetime.fromisoformat('2024-05-31T00:10:00+02:00'))
|
||||
async def test_daily_forecast_midnight_bug(
|
||||
hass: HomeAssistant,
|
||||
mock_config_entry: MockConfigEntry
|
||||
) -> None:
|
||||
coordinator = IrmKmiCoordinator(hass, mock_config_entry)
|
||||
|
||||
api_data = get_api_data("midnight-bug-31-05-2024T00-13.json").get('for', {}).get('daily')
|
||||
result = await coordinator.daily_list_to_forecast(api_data)
|
||||
|
||||
assert result[0]['datetime'] == '2024-05-31'
|
||||
assert not result[0]['is_daytime']
|
||||
|
||||
assert result[1]['datetime'] == '2024-05-31'
|
||||
assert result[1]['is_daytime']
|
||||
|
||||
assert result[2]['datetime'] == '2024-06-01'
|
||||
assert result[2]['is_daytime']
|
||||
|
||||
assert result[3]['datetime'] == '2024-06-02'
|
||||
assert result[3]['is_daytime']
|
||||
|
||||
|
||||
async def test_refresh_succeed_even_when_pollen_and_radar_fail(
|
||||
hass: HomeAssistant,
|
||||
mock_config_entry: MockConfigEntry,
|
||||
|
|
Loading…
Add table
Reference in a new issue