From a401bc172ab155fe26ba6313afc4685259f19b7c Mon Sep 17 00:00:00 2001 From: Jules Dejaeghere Date: Sat, 20 Jan 2024 14:39:42 +0100 Subject: [PATCH] Remove text_fr and text_nl from forecast and add text instead (auto-detect instance language) --- custom_components/irm_kmi/coordinator.py | 8 +++----- custom_components/irm_kmi/data.py | 4 +--- tests/fixtures/forecast.json | 3 ++- tests/test_coordinator.py | 12 ++++++++---- 4 files changed, 14 insertions(+), 13 deletions(-) diff --git a/custom_components/irm_kmi/coordinator.py b/custom_components/irm_kmi/coordinator.py index b344e41..bf2246d 100644 --- a/custom_components/irm_kmi/coordinator.py +++ b/custom_components/irm_kmi/coordinator.py @@ -130,7 +130,7 @@ class IrmKmiCoordinator(DataUpdateCoordinator): """From the API data, create the object that will be used in the entities""" return ProcessedCoordinatorData( current_weather=IrmKmiCoordinator.current_weather_from_data(api_data), - daily_forecast=IrmKmiCoordinator.daily_list_to_forecast(api_data.get('for', {}).get('daily')), + daily_forecast=self.daily_list_to_forecast(api_data.get('for', {}).get('daily')), hourly_forecast=IrmKmiCoordinator.hourly_list_to_forecast(api_data.get('for', {}).get('hourly')), animation=await self._async_animation_data(api_data=api_data), warnings=self.warnings_from_data(api_data.get('for', {}).get('warning')) @@ -257,8 +257,7 @@ class IrmKmiCoordinator(DataUpdateCoordinator): return forecasts - @staticmethod - def daily_list_to_forecast(data: List[dict] | None) -> List[Forecast] | None: + def daily_list_to_forecast(self, data: List[dict] | None) -> List[Forecast] | None: """Parse data from the API to create a list of daily forecasts""" if data is None or not isinstance(data, list) or len(data) == 0: return None @@ -295,8 +294,7 @@ class IrmKmiCoordinator(DataUpdateCoordinator): precipitation_probability=f.get('precipChance', None), wind_bearing=f.get('wind', {}).get('dirText', {}).get('en'), is_daytime=is_daytime, - text_fr=f.get('text', {}).get('fr'), - text_nl=f.get('text', {}).get('nl') + text=f.get('text', {}).get(self.hass.config.language, ""), ) forecasts.append(forecast) if is_daytime or idx == 0: diff --git a/custom_components/irm_kmi/data.py b/custom_components/irm_kmi/data.py index 6267ce3..02b3989 100644 --- a/custom_components/irm_kmi/data.py +++ b/custom_components/irm_kmi/data.py @@ -9,9 +9,7 @@ class IrmKmiForecast(Forecast): """Forecast class with additional attributes for IRM KMI""" # TODO: add condition_2 as well and evolution to match data from the API? - # TODO: remove the _fr and _nl to have only one 'text' attribute - text_fr: str | None - text_nl: str | None + text: str | None class CurrentWeatherData(TypedDict, total=False): diff --git a/tests/fixtures/forecast.json b/tests/fixtures/forecast.json index c9c118d..de81f9e 100644 --- a/tests/fixtures/forecast.json +++ b/tests/fixtures/forecast.json @@ -66,7 +66,8 @@ "dayNight": "d", "text": { "nl": "Foo", - "fr": "Bar" + "fr": "Bar", + "en": "Hey!" }, "dawnRiseSeconds": "31440", "dawnSetSeconds": "60180", diff --git a/tests/test_coordinator.py b/tests/test_coordinator.py index 76374da..5d786bc 100644 --- a/tests/test_coordinator.py +++ b/tests/test_coordinator.py @@ -82,9 +82,14 @@ def test_current_weather_nl() -> None: @freeze_time(datetime.fromisoformat('2023-12-26T18:30:00.028724')) -def test_daily_forecast() -> None: +async def test_daily_forecast( + hass: HomeAssistant, + mock_config_entry: MockConfigEntry +) -> None: api_data = get_api_data("forecast.json").get('for', {}).get('daily') - result = IrmKmiCoordinator.daily_list_to_forecast(api_data) + + coordinator = IrmKmiCoordinator(hass, mock_config_entry) + result = coordinator.daily_list_to_forecast(api_data) assert isinstance(result, list) assert len(result) == 8 @@ -100,8 +105,7 @@ def test_daily_forecast() -> None: precipitation_probability=0, wind_bearing='S', is_daytime=True, - text_fr='Bar', - text_nl='Foo' + text='Hey!', ) assert result[1] == expected