2020-08-02 14:22:51 +00:00
|
|
|
"""Support for the AccuWeather service."""
|
2021-05-19 08:37:16 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
from typing import Any, cast
|
|
|
|
|
2021-03-22 11:37:16 +00:00
|
|
|
from homeassistant.components.sensor import SensorEntity
|
2021-05-19 08:37:16 +00:00
|
|
|
from homeassistant.config_entries import ConfigEntry
|
2021-05-20 07:29:10 +00:00
|
|
|
from homeassistant.const import (
|
|
|
|
ATTR_ATTRIBUTION,
|
|
|
|
ATTR_DEVICE_CLASS,
|
|
|
|
ATTR_ICON,
|
|
|
|
CONF_NAME,
|
|
|
|
DEVICE_CLASS_TEMPERATURE,
|
|
|
|
)
|
2021-06-20 21:53:08 +00:00
|
|
|
from homeassistant.core import HomeAssistant, callback
|
2021-05-19 08:37:16 +00:00
|
|
|
from homeassistant.helpers.entity import DeviceInfo
|
|
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
|
|
|
from homeassistant.helpers.typing import StateType
|
2020-08-30 14:06:38 +00:00
|
|
|
from homeassistant.helpers.update_coordinator import CoordinatorEntity
|
2020-08-02 14:22:51 +00:00
|
|
|
|
2021-05-19 08:37:16 +00:00
|
|
|
from . import AccuWeatherDataUpdateCoordinator
|
2020-08-02 14:22:51 +00:00
|
|
|
from .const import (
|
2021-05-20 07:29:10 +00:00
|
|
|
API_IMPERIAL,
|
|
|
|
API_METRIC,
|
|
|
|
ATTR_ENABLED,
|
2020-08-02 14:22:51 +00:00
|
|
|
ATTR_FORECAST,
|
2021-05-20 07:29:10 +00:00
|
|
|
ATTR_LABEL,
|
|
|
|
ATTR_UNIT_IMPERIAL,
|
|
|
|
ATTR_UNIT_METRIC,
|
2020-08-02 14:22:51 +00:00
|
|
|
ATTRIBUTION,
|
|
|
|
COORDINATOR,
|
|
|
|
DOMAIN,
|
|
|
|
FORECAST_SENSOR_TYPES,
|
2020-08-05 10:50:34 +00:00
|
|
|
MANUFACTURER,
|
2021-05-19 08:37:16 +00:00
|
|
|
MAX_FORECAST_DAYS,
|
2020-08-05 10:50:34 +00:00
|
|
|
NAME,
|
2020-08-02 14:22:51 +00:00
|
|
|
SENSOR_TYPES,
|
|
|
|
)
|
|
|
|
|
|
|
|
PARALLEL_UPDATES = 1
|
|
|
|
|
|
|
|
|
2021-05-19 08:37:16 +00:00
|
|
|
async def async_setup_entry(
|
|
|
|
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
|
|
|
|
) -> None:
|
2020-08-02 14:22:51 +00:00
|
|
|
"""Add AccuWeather entities from a config_entry."""
|
2021-05-19 08:37:16 +00:00
|
|
|
name: str = entry.data[CONF_NAME]
|
2020-08-02 14:22:51 +00:00
|
|
|
|
2021-05-19 08:37:16 +00:00
|
|
|
coordinator: AccuWeatherDataUpdateCoordinator = hass.data[DOMAIN][entry.entry_id][
|
|
|
|
COORDINATOR
|
|
|
|
]
|
2020-08-02 14:22:51 +00:00
|
|
|
|
2021-05-19 08:37:16 +00:00
|
|
|
sensors: list[AccuWeatherSensor] = []
|
2020-08-02 14:22:51 +00:00
|
|
|
for sensor in SENSOR_TYPES:
|
|
|
|
sensors.append(AccuWeatherSensor(name, sensor, coordinator))
|
|
|
|
|
|
|
|
if coordinator.forecast:
|
|
|
|
for sensor in FORECAST_SENSOR_TYPES:
|
2021-05-19 08:37:16 +00:00
|
|
|
for day in range(MAX_FORECAST_DAYS + 1):
|
2020-08-02 14:22:51 +00:00
|
|
|
# Some air quality/allergy sensors are only available for certain
|
|
|
|
# locations.
|
|
|
|
if sensor in coordinator.data[ATTR_FORECAST][0]:
|
|
|
|
sensors.append(
|
|
|
|
AccuWeatherSensor(name, sensor, coordinator, forecast_day=day)
|
|
|
|
)
|
|
|
|
|
2021-05-19 08:37:16 +00:00
|
|
|
async_add_entities(sensors)
|
2020-08-02 14:22:51 +00:00
|
|
|
|
|
|
|
|
2021-03-22 19:05:13 +00:00
|
|
|
class AccuWeatherSensor(CoordinatorEntity, SensorEntity):
|
2020-08-02 14:22:51 +00:00
|
|
|
"""Define an AccuWeather entity."""
|
|
|
|
|
2021-05-19 08:37:16 +00:00
|
|
|
coordinator: AccuWeatherDataUpdateCoordinator
|
|
|
|
|
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
name: str,
|
|
|
|
kind: str,
|
|
|
|
coordinator: AccuWeatherDataUpdateCoordinator,
|
|
|
|
forecast_day: int | None = None,
|
|
|
|
) -> None:
|
2020-08-02 14:22:51 +00:00
|
|
|
"""Initialize."""
|
2020-08-30 14:06:38 +00:00
|
|
|
super().__init__(coordinator)
|
2021-06-20 21:53:08 +00:00
|
|
|
self._sensor_data = _get_sensor_data(coordinator.data, forecast_day, kind)
|
2021-05-19 08:37:16 +00:00
|
|
|
if forecast_day is None:
|
|
|
|
self._description = SENSOR_TYPES[kind]
|
|
|
|
else:
|
|
|
|
self._description = FORECAST_SENSOR_TYPES[kind]
|
2021-05-20 07:29:10 +00:00
|
|
|
self._unit_system = API_METRIC if coordinator.is_metric else API_IMPERIAL
|
2020-08-02 14:22:51 +00:00
|
|
|
self._name = name
|
|
|
|
self.kind = kind
|
|
|
|
self._device_class = None
|
|
|
|
self._attrs = {ATTR_ATTRIBUTION: ATTRIBUTION}
|
|
|
|
self.forecast_day = forecast_day
|
|
|
|
|
|
|
|
@property
|
2021-05-19 08:37:16 +00:00
|
|
|
def name(self) -> str:
|
2020-08-02 14:22:51 +00:00
|
|
|
"""Return the name."""
|
|
|
|
if self.forecast_day is not None:
|
2021-05-20 07:29:10 +00:00
|
|
|
return f"{self._name} {self._description[ATTR_LABEL]} {self.forecast_day}d"
|
|
|
|
return f"{self._name} {self._description[ATTR_LABEL]}"
|
2020-08-02 14:22:51 +00:00
|
|
|
|
|
|
|
@property
|
2021-05-19 08:37:16 +00:00
|
|
|
def unique_id(self) -> str:
|
2020-08-02 14:22:51 +00:00
|
|
|
"""Return a unique_id for this entity."""
|
|
|
|
if self.forecast_day is not None:
|
|
|
|
return f"{self.coordinator.location_key}-{self.kind}-{self.forecast_day}".lower()
|
|
|
|
return f"{self.coordinator.location_key}-{self.kind}".lower()
|
|
|
|
|
2020-08-05 10:50:34 +00:00
|
|
|
@property
|
2021-05-19 08:37:16 +00:00
|
|
|
def device_info(self) -> DeviceInfo:
|
2020-08-05 10:50:34 +00:00
|
|
|
"""Return the device info."""
|
|
|
|
return {
|
|
|
|
"identifiers": {(DOMAIN, self.coordinator.location_key)},
|
|
|
|
"name": NAME,
|
|
|
|
"manufacturer": MANUFACTURER,
|
|
|
|
"entry_type": "service",
|
|
|
|
}
|
|
|
|
|
2020-08-02 14:22:51 +00:00
|
|
|
@property
|
2021-05-19 08:37:16 +00:00
|
|
|
def state(self) -> StateType:
|
2020-08-02 14:22:51 +00:00
|
|
|
"""Return the state."""
|
|
|
|
if self.forecast_day is not None:
|
2021-05-19 08:37:16 +00:00
|
|
|
if self._description["device_class"] == DEVICE_CLASS_TEMPERATURE:
|
|
|
|
return cast(float, self._sensor_data["Value"])
|
|
|
|
if self.kind == "UVIndex":
|
|
|
|
return cast(int, self._sensor_data["Value"])
|
|
|
|
if self.kind in ["Grass", "Mold", "Ragweed", "Tree", "Ozone"]:
|
|
|
|
return cast(int, self._sensor_data["Value"])
|
2020-08-02 14:22:51 +00:00
|
|
|
if self.kind == "Ceiling":
|
2021-05-19 08:37:16 +00:00
|
|
|
return round(self._sensor_data[self._unit_system]["Value"])
|
2020-08-02 14:22:51 +00:00
|
|
|
if self.kind == "PressureTendency":
|
2021-05-19 08:37:16 +00:00
|
|
|
return cast(str, self._sensor_data["LocalizedText"].lower())
|
|
|
|
if self._description["device_class"] == DEVICE_CLASS_TEMPERATURE:
|
|
|
|
return cast(float, self._sensor_data[self._unit_system]["Value"])
|
2020-08-02 14:22:51 +00:00
|
|
|
if self.kind == "Precipitation":
|
2021-05-19 08:37:16 +00:00
|
|
|
return cast(float, self._sensor_data[self._unit_system]["Value"])
|
2020-12-19 15:10:02 +00:00
|
|
|
if self.kind in ["Wind", "WindGust"]:
|
2021-05-19 08:37:16 +00:00
|
|
|
return cast(float, self._sensor_data["Speed"][self._unit_system]["Value"])
|
|
|
|
if self.kind in ["WindDay", "WindNight", "WindGustDay", "WindGustNight"]:
|
|
|
|
return cast(StateType, self._sensor_data["Speed"]["Value"])
|
|
|
|
return cast(StateType, self._sensor_data)
|
2020-08-02 14:22:51 +00:00
|
|
|
|
|
|
|
@property
|
2021-05-19 08:37:16 +00:00
|
|
|
def icon(self) -> str | None:
|
2020-08-02 14:22:51 +00:00
|
|
|
"""Return the icon."""
|
2021-05-20 07:29:10 +00:00
|
|
|
return self._description[ATTR_ICON]
|
2020-08-02 14:22:51 +00:00
|
|
|
|
|
|
|
@property
|
2021-05-19 08:37:16 +00:00
|
|
|
def device_class(self) -> str | None:
|
2020-08-02 14:22:51 +00:00
|
|
|
"""Return the device_class."""
|
2021-05-20 07:29:10 +00:00
|
|
|
return self._description[ATTR_DEVICE_CLASS]
|
2020-08-02 14:22:51 +00:00
|
|
|
|
|
|
|
@property
|
2021-05-19 08:37:16 +00:00
|
|
|
def unit_of_measurement(self) -> str | None:
|
2020-08-02 14:22:51 +00:00
|
|
|
"""Return the unit the value is expressed in."""
|
2021-05-19 08:37:16 +00:00
|
|
|
if self.coordinator.is_metric:
|
2021-05-20 07:29:10 +00:00
|
|
|
return self._description[ATTR_UNIT_METRIC]
|
|
|
|
return self._description[ATTR_UNIT_IMPERIAL]
|
2020-08-02 14:22:51 +00:00
|
|
|
|
|
|
|
@property
|
2021-05-19 08:37:16 +00:00
|
|
|
def extra_state_attributes(self) -> dict[str, Any]:
|
2020-08-02 14:22:51 +00:00
|
|
|
"""Return the state attributes."""
|
|
|
|
if self.forecast_day is not None:
|
2020-12-19 15:10:02 +00:00
|
|
|
if self.kind in ["WindDay", "WindNight", "WindGustDay", "WindGustNight"]:
|
2021-05-19 08:37:16 +00:00
|
|
|
self._attrs["direction"] = self._sensor_data["Direction"]["English"]
|
2020-08-02 14:22:51 +00:00
|
|
|
elif self.kind in ["Grass", "Mold", "Ragweed", "Tree", "UVIndex", "Ozone"]:
|
2021-05-19 08:37:16 +00:00
|
|
|
self._attrs["level"] = self._sensor_data["Category"]
|
2020-08-02 14:22:51 +00:00
|
|
|
return self._attrs
|
|
|
|
if self.kind == "UVIndex":
|
|
|
|
self._attrs["level"] = self.coordinator.data["UVIndexText"]
|
|
|
|
elif self.kind == "Precipitation":
|
|
|
|
self._attrs["type"] = self.coordinator.data["PrecipitationType"]
|
|
|
|
return self._attrs
|
|
|
|
|
|
|
|
@property
|
2021-05-19 08:37:16 +00:00
|
|
|
def entity_registry_enabled_default(self) -> bool:
|
2020-08-02 14:22:51 +00:00
|
|
|
"""Return if the entity should be enabled when first added to the entity registry."""
|
2021-05-20 07:29:10 +00:00
|
|
|
return self._description[ATTR_ENABLED]
|
2021-06-20 21:53:08 +00:00
|
|
|
|
|
|
|
@callback
|
|
|
|
def _handle_coordinator_update(self) -> None:
|
|
|
|
"""Handle data update."""
|
|
|
|
self._sensor_data = _get_sensor_data(
|
|
|
|
self.coordinator.data, self.forecast_day, self.kind
|
|
|
|
)
|
|
|
|
self.async_write_ha_state()
|
|
|
|
|
|
|
|
|
|
|
|
def _get_sensor_data(
|
|
|
|
sensors: dict[str, Any], forecast_day: int | None, kind: str
|
|
|
|
) -> Any:
|
|
|
|
"""Get sensor data."""
|
|
|
|
if forecast_day is not None:
|
|
|
|
return sensors[ATTR_FORECAST][forecast_day][kind]
|
|
|
|
|
|
|
|
if kind == "Precipitation":
|
|
|
|
return sensors["PrecipitationSummary"][kind]
|
|
|
|
|
|
|
|
return sensors[kind]
|