aemet: move forecast timestamp sensor to lambda

Signed-off-by: Álvaro Fernández Rojas <noltari@gmail.com>
This commit is contained in:
Álvaro Fernández Rojas
2021-12-26 17:33:58 +01:00
parent 521526f818
commit 1f012c9adc
2 changed files with 23 additions and 13 deletions

View File

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

View File

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