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.""" """Constant values for the AEMET OpenData component."""
from __future__ import annotations from __future__ import annotations
from collections.abc import Callable
from dataclasses import dataclass
from datetime import date
from homeassistant.components.sensor import ( from homeassistant.components.sensor import (
SensorDeviceClass, SensorDeviceClass,
SensorEntityDescription, SensorEntityDescription,
@@ -35,6 +39,7 @@ from homeassistant.const import (
TEMP_CELSIUS, TEMP_CELSIUS,
Platform, Platform,
) )
from homeassistant.util import dt as dt_util
ATTRIBUTION = "Powered by AEMET OpenData" ATTRIBUTION = "Powered by AEMET OpenData"
CONF_STATION_UPDATES = "station_updates" CONF_STATION_UPDATES = "station_updates"
@@ -200,44 +205,53 @@ FORECAST_MODE_ATTR_API = {
FORECAST_MODE_HOURLY: ATTR_API_FORECAST_HOURLY, 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, key=ATTR_FORECAST_CONDITION,
name="Condition", name="Condition",
), ),
SensorEntityDescription( ForecastSensorEntityDescription(
key=ATTR_FORECAST_PRECIPITATION, key=ATTR_FORECAST_PRECIPITATION,
name="Precipitation", name="Precipitation",
native_unit_of_measurement=PRECIPITATION_MILLIMETERS_PER_HOUR, native_unit_of_measurement=PRECIPITATION_MILLIMETERS_PER_HOUR,
), ),
SensorEntityDescription( ForecastSensorEntityDescription(
key=ATTR_FORECAST_PRECIPITATION_PROBABILITY, key=ATTR_FORECAST_PRECIPITATION_PROBABILITY,
name="Precipitation probability", name="Precipitation probability",
native_unit_of_measurement=PERCENTAGE, native_unit_of_measurement=PERCENTAGE,
), ),
SensorEntityDescription( ForecastSensorEntityDescription(
key=ATTR_FORECAST_TEMP, key=ATTR_FORECAST_TEMP,
name="Temperature", name="Temperature",
native_unit_of_measurement=TEMP_CELSIUS, native_unit_of_measurement=TEMP_CELSIUS,
device_class=SensorDeviceClass.TEMPERATURE, device_class=SensorDeviceClass.TEMPERATURE,
), ),
SensorEntityDescription( ForecastSensorEntityDescription(
key=ATTR_FORECAST_TEMP_LOW, key=ATTR_FORECAST_TEMP_LOW,
name="Temperature Low", name="Temperature Low",
native_unit_of_measurement=TEMP_CELSIUS, native_unit_of_measurement=TEMP_CELSIUS,
device_class=SensorDeviceClass.TEMPERATURE, device_class=SensorDeviceClass.TEMPERATURE,
), ),
SensorEntityDescription( ForecastSensorEntityDescription(
key=ATTR_FORECAST_TIME, key=ATTR_FORECAST_TIME,
name="Time", name="Time",
device_class=SensorDeviceClass.TIMESTAMP, device_class=SensorDeviceClass.TIMESTAMP,
value=lambda value: dt_util.parse_datetime(value),
), ),
SensorEntityDescription( ForecastSensorEntityDescription(
key=ATTR_FORECAST_WIND_BEARING, key=ATTR_FORECAST_WIND_BEARING,
name="Wind bearing", name="Wind bearing",
native_unit_of_measurement=DEGREE, native_unit_of_measurement=DEGREE,
), ),
SensorEntityDescription( ForecastSensorEntityDescription(
key=ATTR_FORECAST_WIND_SPEED, key=ATTR_FORECAST_WIND_SPEED,
name="Wind speed", name="Wind speed",
native_unit_of_measurement=SPEED_KILOMETERS_PER_HOUR, 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.components.sensor import SensorEntity, SensorEntityDescription
from homeassistant.const import ATTR_ATTRIBUTION from homeassistant.const import ATTR_ATTRIBUTION
from homeassistant.helpers.update_coordinator import CoordinatorEntity from homeassistant.helpers.update_coordinator import CoordinatorEntity
from homeassistant.util import dt as dt_util
from .const import ( from .const import (
ATTR_FORECAST_TIME,
ATTRIBUTION, ATTRIBUTION,
DOMAIN, DOMAIN,
ENTRY_NAME, ENTRY_NAME,
@@ -132,6 +130,4 @@ class AemetForecastSensor(AbstractAemetSensor):
) )
if forecasts: if forecasts:
forecast = forecasts[0].get(self.entity_description.key) forecast = forecasts[0].get(self.entity_description.key)
if self.entity_description.key == ATTR_FORECAST_TIME:
forecast = dt_util.parse_datetime(forecast)
return forecast return forecast