Map SMHI weather condition codes once (#98517)

This commit is contained in:
Erik Montnemery 2023-08-16 22:07:12 +02:00 committed by GitHub
parent 5c1c8dc682
commit f643d2de46
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -81,6 +81,11 @@ CONDITION_CLASSES: Final[dict[str, list[int]]] = {
ATTR_CONDITION_WINDY_VARIANT: [], ATTR_CONDITION_WINDY_VARIANT: [],
ATTR_CONDITION_EXCEPTIONAL: [], 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 TIMEOUT = 10
# 5 minutes between retrying connect to API again # 5 minutes between retrying connect to API again
@ -148,7 +153,6 @@ class SmhiWeather(WeatherEntity):
name=name, name=name,
configuration_url="http://opendata.smhi.se/apidocs/metfcst/parameters.html", configuration_url="http://opendata.smhi.se/apidocs/metfcst/parameters.html",
) )
self._attr_native_temperature = None
@property @property
def extra_state_attributes(self) -> Mapping[str, Any] | None: 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_pressure = self._forecast_daily[0].pressure
self._attr_native_wind_gust_speed = self._forecast_daily[0].wind_gust self._attr_native_wind_gust_speed = self._forecast_daily[0].wind_gust
self._attr_cloud_coverage = self._forecast_daily[0].cloudiness self._attr_cloud_coverage = self._forecast_daily[0].cloudiness
self._attr_condition = next( self._attr_condition = CONDITION_MAP.get(self._forecast_daily[0].symbol)
(
k
for k, v in CONDITION_CLASSES.items()
if self._forecast_daily[0].symbol in v
),
None,
)
await self.async_update_listeners(("daily", "hourly")) await self.async_update_listeners(("daily", "hourly"))
async def retry_update(self, _: datetime) -> None: async def retry_update(self, _: datetime) -> None:
@ -208,9 +205,7 @@ class SmhiWeather(WeatherEntity):
data: list[Forecast] = [] data: list[Forecast] = []
for forecast in self._forecast_daily[1:]: for forecast in self._forecast_daily[1:]:
condition = next( condition = CONDITION_MAP.get(forecast.symbol)
(k for k, v in CONDITION_CLASSES.items() if forecast.symbol in v), None
)
data.append( data.append(
{ {
@ -240,9 +235,7 @@ class SmhiWeather(WeatherEntity):
data: list[Forecast] = [] data: list[Forecast] = []
for forecast in forecast_data[1:]: for forecast in forecast_data[1:]:
condition = next( condition = CONDITION_MAP.get(forecast.symbol)
(k for k, v in CONDITION_CLASSES.items() if forecast.symbol in v), None
)
data.append( data.append(
{ {