From cf71742b2fb5ae31d5b332ea62a55267575f6d1c Mon Sep 17 00:00:00 2001 From: Jules Dejaeghere Date: Sat, 10 May 2025 21:45:29 +0200 Subject: [PATCH] Breaking: Update return of get_forecasts_radar_service() --- custom_components/irm_kmi/weather.py | 12 +++++------- tests/test_weather.py | 25 +++---------------------- 2 files changed, 8 insertions(+), 29 deletions(-) diff --git a/custom_components/irm_kmi/weather.py b/custom_components/irm_kmi/weather.py index c8a5f8d..2a5bd16 100644 --- a/custom_components/irm_kmi/weather.py +++ b/custom_components/irm_kmi/weather.py @@ -1,7 +1,7 @@ """Support for IRM KMI weather.""" import logging from datetime import datetime -from typing import List +from typing import List, Dict import voluptuous as vol from homeassistant.components.weather import (Forecast, WeatherEntity, @@ -152,20 +152,18 @@ class IrmKmiWeather(CoordinatorEntity, WeatherEntity): return [f for f in data if f.get('is_daytime')] - def get_forecasts_radar_service(self, include_past_forecasts: bool = False) -> List[Forecast] | None: + def get_forecasts_radar_service(self, include_past_forecasts: bool = False) -> Dict[str, List[Forecast]]: """ Forecast service based on data from the radar. Only contains datetime and precipitation amount. The result always include the current 10 minutes interval, even if include_past_forecast is false. :param include_past_forecasts: whether to include data points that are in the past - :return: ordered list of forecasts + :return: ordered list of forecasts, under the 'forecast' key as other HA services """ now = dt.now() now = now.replace(minute=(now.minute // 10) * 10, second=0, microsecond=0) - # TODO adapt the return value to match the weather.get_forecasts in next breaking change release - # return { 'forecast': [...] } - return [f for f in self.coordinator.data.get('radar_forecast') - if include_past_forecasts or datetime.fromisoformat(f.get('datetime')) >= now] + return {'forecast': [f for f in self.coordinator.data.get('radar_forecast') + if include_past_forecasts or datetime.fromisoformat(f.get('datetime')) >= now]} # TODO remove on next breaking changes @property diff --git a/tests/test_weather.py b/tests/test_weather.py index 5b19d4f..a79efea 100644 --- a/tests/test_weather.py +++ b/tests/test_weather.py @@ -61,25 +61,6 @@ async def test_weather_higher_temp_at_night( assert f['native_temperature'] >= f['native_templow'] -@freeze_time(datetime.fromisoformat("2023-12-26T18:30:00+01:00")) -async def test_forecast_attribute_same_as_service_call( - hass: HomeAssistant, - mock_config_entry_with_deprecated: MockConfigEntry -) -> None: - coordinator = IrmKmiCoordinator(hass, mock_config_entry_with_deprecated) - forecast = json.loads(load_fixture("forecast.json")) - coordinator._api._api_data = forecast - - coordinator.data = await coordinator.process_api_data() - - weather = IrmKmiWeather(coordinator, mock_config_entry_with_deprecated) - - result_service: List[Forecast] = await weather.async_forecast_twice_daily() - result_forecast: List[Forecast] = weather.extra_state_attributes['forecast'] - - assert result_service == result_forecast - - @freeze_time(datetime.fromisoformat("2023-12-26T17:58:03+01:00")) async def test_radar_forecast_service( hass: HomeAssistant, @@ -98,7 +79,7 @@ async def test_radar_forecast_service( result_service: List[Forecast] = weather.get_forecasts_radar_service(False) - expected = [ + l = [ IrmKmiRadarForecast(datetime="2023-12-26T17:00:00+01:00", native_precipitation=0, might_rain=False, rain_forecast_max=0, rain_forecast_min=0, unit='mm/10min'), IrmKmiRadarForecast(datetime="2023-12-26T17:10:00+01:00", native_precipitation=0, might_rain=False, @@ -123,8 +104,8 @@ async def test_radar_forecast_service( rain_forecast_max=0, rain_forecast_min=0, unit='mm/10min') ] - assert result_service == expected[5:] + assert result_service == {'forecast': l[5:]} result_service: List[Forecast] = weather.get_forecasts_radar_service(True) - assert result_service == expected + assert result_service == {'forecast': l}