Map metoffice weather condition codes once (#98515)
This commit is contained in:
parent
f135c42524
commit
227d4a590d
3 changed files with 10 additions and 16 deletions
|
@ -52,6 +52,11 @@ CONDITION_CLASSES: dict[str, list[str]] = {
|
||||||
ATTR_CONDITION_WINDY_VARIANT: [],
|
ATTR_CONDITION_WINDY_VARIANT: [],
|
||||||
ATTR_CONDITION_EXCEPTIONAL: [],
|
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 = {
|
VISIBILITY_CLASSES = {
|
||||||
"VP": "Very Poor",
|
"VP": "Very Poor",
|
||||||
|
|
|
@ -29,7 +29,7 @@ from homeassistant.helpers.update_coordinator import (
|
||||||
from . import get_device_info
|
from . import get_device_info
|
||||||
from .const import (
|
from .const import (
|
||||||
ATTRIBUTION,
|
ATTRIBUTION,
|
||||||
CONDITION_CLASSES,
|
CONDITION_MAP,
|
||||||
DOMAIN,
|
DOMAIN,
|
||||||
METOFFICE_COORDINATES,
|
METOFFICE_COORDINATES,
|
||||||
METOFFICE_DAILY_COORDINATOR,
|
METOFFICE_DAILY_COORDINATOR,
|
||||||
|
@ -221,11 +221,7 @@ class MetOfficeCurrentSensor(
|
||||||
elif self.entity_description.key == "weather" and hasattr(
|
elif self.entity_description.key == "weather" and hasattr(
|
||||||
self.coordinator.data.now, self.entity_description.key
|
self.coordinator.data.now, self.entity_description.key
|
||||||
):
|
):
|
||||||
value = [
|
value = CONDITION_MAP.get(self.coordinator.data.now.weather.value)
|
||||||
k
|
|
||||||
for k, v in CONDITION_CLASSES.items()
|
|
||||||
if self.coordinator.data.now.weather.value in v
|
|
||||||
][0]
|
|
||||||
|
|
||||||
elif hasattr(self.coordinator.data.now, self.entity_description.key):
|
elif hasattr(self.coordinator.data.now, self.entity_description.key):
|
||||||
value = getattr(self.coordinator.data.now, self.entity_description.key)
|
value = getattr(self.coordinator.data.now, self.entity_description.key)
|
||||||
|
|
|
@ -26,7 +26,7 @@ from homeassistant.helpers.update_coordinator import (
|
||||||
from . import get_device_info
|
from . import get_device_info
|
||||||
from .const import (
|
from .const import (
|
||||||
ATTRIBUTION,
|
ATTRIBUTION,
|
||||||
CONDITION_CLASSES,
|
CONDITION_MAP,
|
||||||
DOMAIN,
|
DOMAIN,
|
||||||
METOFFICE_COORDINATES,
|
METOFFICE_COORDINATES,
|
||||||
METOFFICE_DAILY_COORDINATOR,
|
METOFFICE_DAILY_COORDINATOR,
|
||||||
|
@ -55,7 +55,7 @@ async def async_setup_entry(
|
||||||
def _build_forecast_data(timestep: Timestep) -> Forecast:
|
def _build_forecast_data(timestep: Timestep) -> Forecast:
|
||||||
data = Forecast(datetime=timestep.date.isoformat())
|
data = Forecast(datetime=timestep.date.isoformat())
|
||||||
if timestep.weather:
|
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:
|
if timestep.precipitation:
|
||||||
data[ATTR_FORECAST_PRECIPITATION_PROBABILITY] = timestep.precipitation.value
|
data[ATTR_FORECAST_PRECIPITATION_PROBABILITY] = timestep.precipitation.value
|
||||||
if timestep.temperature:
|
if timestep.temperature:
|
||||||
|
@ -67,13 +67,6 @@ def _build_forecast_data(timestep: Timestep) -> Forecast:
|
||||||
return data
|
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(
|
class MetOfficeWeather(
|
||||||
CoordinatorEntity[DataUpdateCoordinator[MetOfficeData]], WeatherEntity
|
CoordinatorEntity[DataUpdateCoordinator[MetOfficeData]], WeatherEntity
|
||||||
):
|
):
|
||||||
|
@ -107,7 +100,7 @@ class MetOfficeWeather(
|
||||||
def condition(self) -> str | None:
|
def condition(self) -> str | None:
|
||||||
"""Return the current condition."""
|
"""Return the current condition."""
|
||||||
if self.coordinator.data.now:
|
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
|
return None
|
||||||
|
|
||||||
@property
|
@property
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue