From a2e619155a25864c0860a03a4c58881db01e7f26 Mon Sep 17 00:00:00 2001 From: Erik Montnemery Date: Wed, 16 Aug 2023 15:01:54 +0200 Subject: [PATCH] Map ipma weather condition codes once (#98512) --- homeassistant/components/ipma/const.py | 9 ++++++++- homeassistant/components/ipma/weather.py | 7 ++----- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/homeassistant/components/ipma/const.py b/homeassistant/components/ipma/const.py index c7482770f48..26fdee779b6 100644 --- a/homeassistant/components/ipma/const.py +++ b/homeassistant/components/ipma/const.py @@ -1,4 +1,6 @@ """Constants for IPMA component.""" +from __future__ import annotations + from datetime import timedelta from homeassistant.components.weather import ( @@ -31,7 +33,7 @@ ENTITY_ID_SENSOR_FORMAT_HOME = f"{WEATHER_DOMAIN}.ipma_{HOME_LOCATION_NAME}" MIN_TIME_BETWEEN_UPDATES = timedelta(minutes=30) -CONDITION_CLASSES = { +CONDITION_CLASSES: dict[str, list[int]] = { ATTR_CONDITION_CLOUDY: [4, 5, 24, 25, 27], ATTR_CONDITION_FOG: [16, 17, 26], ATTR_CONDITION_HAIL: [21, 22], @@ -48,5 +50,10 @@ CONDITION_CLASSES = { ATTR_CONDITION_EXCEPTIONAL: [], ATTR_CONDITION_CLEAR_NIGHT: [-1], } +CONDITION_MAP = { + cond_code: cond_ha + for cond_ha, cond_codes in CONDITION_CLASSES.items() + for cond_code in cond_codes +} ATTRIBUTION = "Instituto Português do Mar e Atmosfera" diff --git a/homeassistant/components/ipma/weather.py b/homeassistant/components/ipma/weather.py index d4d11aa26e8..1f948bcc4e1 100644 --- a/homeassistant/components/ipma/weather.py +++ b/homeassistant/components/ipma/weather.py @@ -37,7 +37,7 @@ from homeassistant.util import Throttle from .const import ( ATTRIBUTION, - CONDITION_CLASSES, + CONDITION_MAP, DATA_API, DATA_LOCATION, DOMAIN, @@ -135,10 +135,7 @@ class IPMAWeather(WeatherEntity, IPMADevice): if identifier == 1 and not is_up(self.hass, forecast_dt): identifier = -identifier - return next( - (k for k, v in CONDITION_CLASSES.items() if identifier in v), - None, - ) + return CONDITION_MAP.get(identifier) @property def condition(self):