2021-02-13 20:53:28 +00:00
|
|
|
"""Support for the AEMET OpenData service."""
|
2021-09-06 08:12:09 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
from homeassistant.components.sensor import SensorEntity, SensorEntityDescription
|
2022-01-03 18:18:00 +00:00
|
|
|
from homeassistant.config_entries import ConfigEntry
|
|
|
|
from homeassistant.core import HomeAssistant
|
|
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
2021-05-14 11:28:48 +00:00
|
|
|
from homeassistant.helpers.update_coordinator import CoordinatorEntity
|
2022-01-16 11:31:55 +00:00
|
|
|
from homeassistant.util import dt as dt_util
|
2021-05-14 11:28:48 +00:00
|
|
|
|
2021-02-13 20:53:28 +00:00
|
|
|
from .const import (
|
2022-07-06 14:22:45 +00:00
|
|
|
ATTR_API_FORECAST_TIME,
|
2021-05-14 11:28:48 +00:00
|
|
|
ATTRIBUTION,
|
2021-02-13 20:53:28 +00:00
|
|
|
DOMAIN,
|
|
|
|
ENTRY_NAME,
|
|
|
|
ENTRY_WEATHER_COORDINATOR,
|
|
|
|
FORECAST_MODE_ATTR_API,
|
|
|
|
FORECAST_MODE_DAILY,
|
|
|
|
FORECAST_MODES,
|
|
|
|
FORECAST_MONITORED_CONDITIONS,
|
|
|
|
FORECAST_SENSOR_TYPES,
|
|
|
|
MONITORED_CONDITIONS,
|
|
|
|
WEATHER_SENSOR_TYPES,
|
|
|
|
)
|
|
|
|
from .weather_update_coordinator import WeatherUpdateCoordinator
|
|
|
|
|
|
|
|
|
2022-01-03 18:18:00 +00:00
|
|
|
async def async_setup_entry(
|
|
|
|
hass: HomeAssistant,
|
|
|
|
config_entry: ConfigEntry,
|
|
|
|
async_add_entities: AddEntitiesCallback,
|
|
|
|
) -> None:
|
2021-02-13 20:53:28 +00:00
|
|
|
"""Set up AEMET OpenData sensor entities based on a config entry."""
|
|
|
|
domain_data = hass.data[DOMAIN][config_entry.entry_id]
|
|
|
|
name = domain_data[ENTRY_NAME]
|
|
|
|
weather_coordinator = domain_data[ENTRY_WEATHER_COORDINATOR]
|
|
|
|
|
2021-09-06 08:12:09 +00:00
|
|
|
unique_id = config_entry.unique_id
|
|
|
|
entities: list[AbstractAemetSensor] = [
|
|
|
|
AemetSensor(name, unique_id, weather_coordinator, description)
|
|
|
|
for description in WEATHER_SENSOR_TYPES
|
|
|
|
if description.key in MONITORED_CONDITIONS
|
|
|
|
]
|
|
|
|
entities.extend(
|
|
|
|
[
|
|
|
|
AemetForecastSensor(
|
2022-07-06 14:22:45 +00:00
|
|
|
f"{domain_data[ENTRY_NAME]} {mode} Forecast",
|
|
|
|
f"{unique_id}-forecast-{mode}",
|
2021-02-13 20:53:28 +00:00
|
|
|
weather_coordinator,
|
2021-09-06 08:12:09 +00:00
|
|
|
mode,
|
|
|
|
description,
|
2021-02-13 20:53:28 +00:00
|
|
|
)
|
2021-09-06 08:12:09 +00:00
|
|
|
for mode in FORECAST_MODES
|
|
|
|
for description in FORECAST_SENSOR_TYPES
|
|
|
|
if description.key in FORECAST_MONITORED_CONDITIONS
|
|
|
|
]
|
|
|
|
)
|
2021-02-13 20:53:28 +00:00
|
|
|
|
|
|
|
async_add_entities(entities)
|
|
|
|
|
|
|
|
|
2022-03-21 14:49:39 +00:00
|
|
|
class AbstractAemetSensor(CoordinatorEntity[WeatherUpdateCoordinator], SensorEntity):
|
2021-05-14 11:28:48 +00:00
|
|
|
"""Abstract class for an AEMET OpenData sensor."""
|
|
|
|
|
2022-08-04 06:13:20 +00:00
|
|
|
_attr_attribution = ATTRIBUTION
|
2021-07-05 05:53:16 +00:00
|
|
|
|
2021-05-14 11:28:48 +00:00
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
name,
|
|
|
|
unique_id,
|
|
|
|
coordinator: WeatherUpdateCoordinator,
|
2021-09-06 08:12:09 +00:00
|
|
|
description: SensorEntityDescription,
|
2021-05-14 11:28:48 +00:00
|
|
|
):
|
|
|
|
"""Initialize the sensor."""
|
|
|
|
super().__init__(coordinator)
|
2021-09-06 08:12:09 +00:00
|
|
|
self.entity_description = description
|
|
|
|
self._attr_name = f"{name} {description.name}"
|
|
|
|
self._attr_unique_id = unique_id
|
2021-05-14 11:28:48 +00:00
|
|
|
|
|
|
|
|
2021-03-27 21:33:24 +00:00
|
|
|
class AemetSensor(AbstractAemetSensor):
|
2021-02-13 20:53:28 +00:00
|
|
|
"""Implementation of an AEMET OpenData sensor."""
|
|
|
|
|
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
name,
|
2022-07-06 14:22:45 +00:00
|
|
|
unique_id_prefix,
|
2021-02-13 20:53:28 +00:00
|
|
|
weather_coordinator: WeatherUpdateCoordinator,
|
2021-09-06 08:12:09 +00:00
|
|
|
description: SensorEntityDescription,
|
2021-02-13 20:53:28 +00:00
|
|
|
):
|
|
|
|
"""Initialize the sensor."""
|
|
|
|
super().__init__(
|
2021-09-06 08:12:09 +00:00
|
|
|
name=name,
|
2022-07-06 14:22:45 +00:00
|
|
|
unique_id=f"{unique_id_prefix}-{description.key}",
|
2021-09-06 08:12:09 +00:00
|
|
|
coordinator=weather_coordinator,
|
|
|
|
description=description,
|
2021-02-13 20:53:28 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
@property
|
2021-08-11 08:45:05 +00:00
|
|
|
def native_value(self):
|
2021-02-13 20:53:28 +00:00
|
|
|
"""Return the state of the device."""
|
2021-09-06 08:12:09 +00:00
|
|
|
return self.coordinator.data.get(self.entity_description.key)
|
2021-02-13 20:53:28 +00:00
|
|
|
|
|
|
|
|
2021-03-27 21:33:24 +00:00
|
|
|
class AemetForecastSensor(AbstractAemetSensor):
|
2021-02-13 20:53:28 +00:00
|
|
|
"""Implementation of an AEMET OpenData forecast sensor."""
|
|
|
|
|
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
name,
|
2022-07-06 14:22:45 +00:00
|
|
|
unique_id_prefix,
|
2021-02-13 20:53:28 +00:00
|
|
|
weather_coordinator: WeatherUpdateCoordinator,
|
|
|
|
forecast_mode,
|
2021-09-06 08:12:09 +00:00
|
|
|
description: SensorEntityDescription,
|
2021-02-13 20:53:28 +00:00
|
|
|
):
|
|
|
|
"""Initialize the sensor."""
|
|
|
|
super().__init__(
|
2021-09-06 08:12:09 +00:00
|
|
|
name=name,
|
2022-07-06 14:22:45 +00:00
|
|
|
unique_id=f"{unique_id_prefix}-{description.key}",
|
2021-09-06 08:12:09 +00:00
|
|
|
coordinator=weather_coordinator,
|
|
|
|
description=description,
|
2021-02-13 20:53:28 +00:00
|
|
|
)
|
|
|
|
self._forecast_mode = forecast_mode
|
2021-07-05 05:53:16 +00:00
|
|
|
self._attr_entity_registry_enabled_default = (
|
|
|
|
self._forecast_mode == FORECAST_MODE_DAILY
|
|
|
|
)
|
2021-02-13 20:53:28 +00:00
|
|
|
|
|
|
|
@property
|
2021-08-11 08:45:05 +00:00
|
|
|
def native_value(self):
|
2021-02-13 20:53:28 +00:00
|
|
|
"""Return the state of the device."""
|
2021-03-27 21:33:24 +00:00
|
|
|
forecast = None
|
2021-09-06 08:12:09 +00:00
|
|
|
forecasts = self.coordinator.data.get(
|
2021-02-13 20:53:28 +00:00
|
|
|
FORECAST_MODE_ATTR_API[self._forecast_mode]
|
|
|
|
)
|
|
|
|
if forecasts:
|
2021-09-06 08:12:09 +00:00
|
|
|
forecast = forecasts[0].get(self.entity_description.key)
|
2022-07-06 14:22:45 +00:00
|
|
|
if self.entity_description.key == ATTR_API_FORECAST_TIME:
|
2022-01-16 11:31:55 +00:00
|
|
|
forecast = dt_util.parse_datetime(forecast)
|
2021-03-27 21:33:24 +00:00
|
|
|
return forecast
|