Map metoffice weather condition codes once (#98515)

This commit is contained in:
Erik Montnemery 2023-08-16 22:09:06 +02:00 committed by GitHub
parent f135c42524
commit 227d4a590d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 10 additions and 16 deletions

View File

@ -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",

View File

@ -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)

View File

@ -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