From f135c42524587c7c311f968711a1616b9924720b Mon Sep 17 00:00:00 2001 From: Erik Montnemery Date: Wed, 16 Aug 2023 22:08:17 +0200 Subject: [PATCH] Map openweathermap weather condition codes once (#98516) --- homeassistant/components/openweathermap/const.py | 5 +++++ .../components/openweathermap/weather_update_coordinator.py | 4 ++-- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/homeassistant/components/openweathermap/const.py b/homeassistant/components/openweathermap/const.py index d53fbc136b2..1420b1170ca 100644 --- a/homeassistant/components/openweathermap/const.py +++ b/homeassistant/components/openweathermap/const.py @@ -157,3 +157,8 @@ CONDITION_CLASSES = { 904, ], } +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/openweathermap/weather_update_coordinator.py b/homeassistant/components/openweathermap/weather_update_coordinator.py index 732557363d8..cf0c941f0df 100644 --- a/homeassistant/components/openweathermap/weather_update_coordinator.py +++ b/homeassistant/components/openweathermap/weather_update_coordinator.py @@ -46,7 +46,7 @@ from .const import ( ATTR_API_WIND_BEARING, ATTR_API_WIND_GUST, ATTR_API_WIND_SPEED, - CONDITION_CLASSES, + CONDITION_MAP, DOMAIN, FORECAST_MODE_DAILY, FORECAST_MODE_HOURLY, @@ -267,7 +267,7 @@ class WeatherUpdateCoordinator(DataUpdateCoordinator): return ATTR_CONDITION_SUNNY return ATTR_CONDITION_CLEAR_NIGHT - return [k for k, v in CONDITION_CLASSES.items() if weather_code in v][0] + return CONDITION_MAP.get(weather_code) class LegacyWeather: