diff --git a/homeassistant/components/metoffice/const.py b/homeassistant/components/metoffice/const.py index e4843d1235e..8b86784b70b 100644 --- a/homeassistant/components/metoffice/const.py +++ b/homeassistant/components/metoffice/const.py @@ -52,6 +52,11 @@ CONDITION_CLASSES: dict[str, list[str]] = { 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 +} VISIBILITY_CLASSES = { "VP": "Very Poor", diff --git a/homeassistant/components/metoffice/sensor.py b/homeassistant/components/metoffice/sensor.py index fcb8e5b134e..371c396a829 100644 --- a/homeassistant/components/metoffice/sensor.py +++ b/homeassistant/components/metoffice/sensor.py @@ -29,7 +29,7 @@ from homeassistant.helpers.update_coordinator import ( from . import get_device_info from .const import ( ATTRIBUTION, - CONDITION_CLASSES, + CONDITION_MAP, DOMAIN, METOFFICE_COORDINATES, METOFFICE_DAILY_COORDINATOR, @@ -221,11 +221,7 @@ class MetOfficeCurrentSensor( elif self.entity_description.key == "weather" and hasattr( self.coordinator.data.now, self.entity_description.key ): - value = [ - k - for k, v in CONDITION_CLASSES.items() - if self.coordinator.data.now.weather.value in v - ][0] + value = CONDITION_MAP.get(self.coordinator.data.now.weather.value) elif hasattr(self.coordinator.data.now, self.entity_description.key): value = getattr(self.coordinator.data.now, self.entity_description.key) diff --git a/homeassistant/components/metoffice/weather.py b/homeassistant/components/metoffice/weather.py index 8257c8a3c35..0b4672ddec8 100644 --- a/homeassistant/components/metoffice/weather.py +++ b/homeassistant/components/metoffice/weather.py @@ -26,7 +26,7 @@ from homeassistant.helpers.update_coordinator import ( from . import get_device_info from .const import ( ATTRIBUTION, - CONDITION_CLASSES, + CONDITION_MAP, DOMAIN, METOFFICE_COORDINATES, METOFFICE_DAILY_COORDINATOR, @@ -55,7 +55,7 @@ async def async_setup_entry( def _build_forecast_data(timestep: Timestep) -> Forecast: data = Forecast(datetime=timestep.date.isoformat()) if timestep.weather: - data[ATTR_FORECAST_CONDITION] = _get_weather_condition(timestep.weather.value) + data[ATTR_FORECAST_CONDITION] = CONDITION_MAP.get(timestep.weather.value) if timestep.precipitation: data[ATTR_FORECAST_PRECIPITATION_PROBABILITY] = timestep.precipitation.value if timestep.temperature: @@ -67,13 +67,6 @@ def _build_forecast_data(timestep: Timestep) -> Forecast: return data -def _get_weather_condition(metoffice_code: str) -> str | None: - for hass_name, metoffice_codes in CONDITION_CLASSES.items(): - if metoffice_code in metoffice_codes: - return hass_name - return None - - class MetOfficeWeather( CoordinatorEntity[DataUpdateCoordinator[MetOfficeData]], WeatherEntity ): @@ -107,7 +100,7 @@ class MetOfficeWeather( def condition(self) -> str | None: """Return the current condition.""" if self.coordinator.data.now: - return _get_weather_condition(self.coordinator.data.now.weather.value) + return CONDITION_MAP.get(self.coordinator.data.now.weather.value) return None @property