From b1053e8077291527402191d2ca5418631b725c50 Mon Sep 17 00:00:00 2001 From: Erik Montnemery Date: Wed, 16 Aug 2023 20:20:47 +0200 Subject: [PATCH] Map accuweather weather condition codes once (#98509) Map accuweather condition codes once --- homeassistant/components/accuweather/const.py | 5 +++++ homeassistant/components/accuweather/weather.py | 15 +++------------ 2 files changed, 8 insertions(+), 12 deletions(-) diff --git a/homeassistant/components/accuweather/const.py b/homeassistant/components/accuweather/const.py index 87bc8eaef89..2e18977d112 100644 --- a/homeassistant/components/accuweather/const.py +++ b/homeassistant/components/accuweather/const.py @@ -50,3 +50,8 @@ CONDITION_CLASSES: Final[dict[str, list[int]]] = { ATTR_CONDITION_SUNNY: [1, 2, 5], ATTR_CONDITION_WINDY: [32], } +CONDITION_MAP = { + cond_code: cond_ha + for cond_ha, cond_codes in CONDITION_CLASSES.items() + for cond_code in cond_codes +} diff --git a/homeassistant/components/accuweather/weather.py b/homeassistant/components/accuweather/weather.py index c2889bae102..518714b3874 100644 --- a/homeassistant/components/accuweather/weather.py +++ b/homeassistant/components/accuweather/weather.py @@ -40,7 +40,7 @@ from .const import ( ATTR_SPEED, ATTR_VALUE, ATTRIBUTION, - CONDITION_CLASSES, + CONDITION_MAP, DOMAIN, ) @@ -80,14 +80,7 @@ class AccuWeatherEntity( @property def condition(self) -> str | None: """Return the current condition.""" - try: - return [ - k - for k, v in CONDITION_CLASSES.items() - if self.coordinator.data["WeatherIcon"] in v - ][0] - except IndexError: - return None + return CONDITION_MAP.get(self.coordinator.data["WeatherIcon"]) @property def cloud_coverage(self) -> float: @@ -177,9 +170,7 @@ class AccuWeatherEntity( ], ATTR_FORECAST_UV_INDEX: item["UVIndex"][ATTR_VALUE], ATTR_FORECAST_WIND_BEARING: item["WindDay"][ATTR_DIRECTION]["Degrees"], - ATTR_FORECAST_CONDITION: [ - k for k, v in CONDITION_CLASSES.items() if item["IconDay"] in v - ][0], + ATTR_FORECAST_CONDITION: CONDITION_MAP.get(item["IconDay"]), } for item in self.coordinator.data[ATTR_FORECAST] ]