diff --git a/homeassistant/components/aemet/const.py b/homeassistant/components/aemet/const.py index 4be90011f5a..ae884e041b6 100644 --- a/homeassistant/components/aemet/const.py +++ b/homeassistant/components/aemet/const.py @@ -1,6 +1,10 @@ """Constant values for the AEMET OpenData component.""" from __future__ import annotations +from collections.abc import Callable +from dataclasses import dataclass +from datetime import date + from homeassistant.components.sensor import ( SensorDeviceClass, SensorEntityDescription, @@ -35,6 +39,7 @@ from homeassistant.const import ( TEMP_CELSIUS, Platform, ) +from homeassistant.util import dt as dt_util ATTRIBUTION = "Powered by AEMET OpenData" CONF_STATION_UPDATES = "station_updates" @@ -200,44 +205,53 @@ FORECAST_MODE_ATTR_API = { FORECAST_MODE_HOURLY: ATTR_API_FORECAST_HOURLY, } -FORECAST_SENSOR_TYPES: tuple[SensorEntityDescription, ...] = ( - SensorEntityDescription( + +@dataclass +class ForecastSensorEntityDescription(SensorEntityDescription): + """Class describing Aemet forecast sensor entities.""" + + value: Callable = date + + +FORECAST_SENSOR_TYPES: tuple[ForecastSensorEntityDescription, ...] = ( + ForecastSensorEntityDescription( key=ATTR_FORECAST_CONDITION, name="Condition", ), - SensorEntityDescription( + ForecastSensorEntityDescription( key=ATTR_FORECAST_PRECIPITATION, name="Precipitation", native_unit_of_measurement=PRECIPITATION_MILLIMETERS_PER_HOUR, ), - SensorEntityDescription( + ForecastSensorEntityDescription( key=ATTR_FORECAST_PRECIPITATION_PROBABILITY, name="Precipitation probability", native_unit_of_measurement=PERCENTAGE, ), - SensorEntityDescription( + ForecastSensorEntityDescription( key=ATTR_FORECAST_TEMP, name="Temperature", native_unit_of_measurement=TEMP_CELSIUS, device_class=SensorDeviceClass.TEMPERATURE, ), - SensorEntityDescription( + ForecastSensorEntityDescription( key=ATTR_FORECAST_TEMP_LOW, name="Temperature Low", native_unit_of_measurement=TEMP_CELSIUS, device_class=SensorDeviceClass.TEMPERATURE, ), - SensorEntityDescription( + ForecastSensorEntityDescription( key=ATTR_FORECAST_TIME, name="Time", device_class=SensorDeviceClass.TIMESTAMP, + value=lambda value: dt_util.parse_datetime(value), ), - SensorEntityDescription( + ForecastSensorEntityDescription( key=ATTR_FORECAST_WIND_BEARING, name="Wind bearing", native_unit_of_measurement=DEGREE, ), - SensorEntityDescription( + ForecastSensorEntityDescription( key=ATTR_FORECAST_WIND_SPEED, name="Wind speed", native_unit_of_measurement=SPEED_KILOMETERS_PER_HOUR, diff --git a/homeassistant/components/aemet/sensor.py b/homeassistant/components/aemet/sensor.py index 8d8deb4a430..685e9fb200b 100644 --- a/homeassistant/components/aemet/sensor.py +++ b/homeassistant/components/aemet/sensor.py @@ -4,10 +4,8 @@ from __future__ import annotations from homeassistant.components.sensor import SensorEntity, SensorEntityDescription from homeassistant.const import ATTR_ATTRIBUTION from homeassistant.helpers.update_coordinator import CoordinatorEntity -from homeassistant.util import dt as dt_util from .const import ( - ATTR_FORECAST_TIME, ATTRIBUTION, DOMAIN, ENTRY_NAME, @@ -132,6 +130,4 @@ class AemetForecastSensor(AbstractAemetSensor): ) if forecasts: forecast = forecasts[0].get(self.entity_description.key) - if self.entity_description.key == ATTR_FORECAST_TIME: - forecast = dt_util.parse_datetime(forecast) return forecast