From f643d2de46de31ab2542de25638b2688c068a13b Mon Sep 17 00:00:00 2001 From: Erik Montnemery Date: Wed, 16 Aug 2023 22:07:12 +0200 Subject: [PATCH] Map SMHI weather condition codes once (#98517) --- homeassistant/components/smhi/weather.py | 23 ++++++++--------------- 1 file changed, 8 insertions(+), 15 deletions(-) diff --git a/homeassistant/components/smhi/weather.py b/homeassistant/components/smhi/weather.py index 5b71d92b25f..c8ff9127ba8 100644 --- a/homeassistant/components/smhi/weather.py +++ b/homeassistant/components/smhi/weather.py @@ -81,6 +81,11 @@ CONDITION_CLASSES: Final[dict[str, list[int]]] = { ATTR_CONDITION_WINDY_VARIANT: [], ATTR_CONDITION_EXCEPTIONAL: [], } +CONDITION_MAP = { + cond_code: cond_ha + for cond_ha, cond_codes in CONDITION_CLASSES.items() + for cond_code in cond_codes +} TIMEOUT = 10 # 5 minutes between retrying connect to API again @@ -148,7 +153,6 @@ class SmhiWeather(WeatherEntity): name=name, configuration_url="http://opendata.smhi.se/apidocs/metfcst/parameters.html", ) - self._attr_native_temperature = None @property def extra_state_attributes(self) -> Mapping[str, Any] | None: @@ -183,14 +187,7 @@ class SmhiWeather(WeatherEntity): self._attr_native_pressure = self._forecast_daily[0].pressure self._attr_native_wind_gust_speed = self._forecast_daily[0].wind_gust self._attr_cloud_coverage = self._forecast_daily[0].cloudiness - self._attr_condition = next( - ( - k - for k, v in CONDITION_CLASSES.items() - if self._forecast_daily[0].symbol in v - ), - None, - ) + self._attr_condition = CONDITION_MAP.get(self._forecast_daily[0].symbol) await self.async_update_listeners(("daily", "hourly")) async def retry_update(self, _: datetime) -> None: @@ -208,9 +205,7 @@ class SmhiWeather(WeatherEntity): data: list[Forecast] = [] for forecast in self._forecast_daily[1:]: - condition = next( - (k for k, v in CONDITION_CLASSES.items() if forecast.symbol in v), None - ) + condition = CONDITION_MAP.get(forecast.symbol) data.append( { @@ -240,9 +235,7 @@ class SmhiWeather(WeatherEntity): data: list[Forecast] = [] for forecast in forecast_data[1:]: - condition = next( - (k for k, v in CONDITION_CLASSES.items() if forecast.symbol in v), None - ) + condition = CONDITION_MAP.get(forecast.symbol) data.append( {