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-12-08 18:57:36 +00:00
|
|
|
from homeassistant.components.sensor import SensorDeviceClass, SensorEntity
|
2021-05-19 08:37:16 +00:00
|
|
|
from homeassistant.config_entries import ConfigEntry
|
2021-12-08 18:57:36 +00:00
|
|
|
from homeassistant.const import CONF_NAME
|
2021-06-20 21:53:08 +00:00
|
|
|
from homeassistant.core import HomeAssistant, callback
|
2021-11-22 17:14:15 +00:00
|
|
|
from homeassistant.helpers.device_registry import DeviceEntryType
|
2021-10-22 14:35:39 +00:00
|
|
|
from homeassistant.helpers.entity import DeviceInfo
|
2021-05-19 08:37:16 +00:00
|
|
|
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,
|
2020-08-02 14:22:51 +00:00
|
|
|
ATTR_FORECAST,
|
|
|
|
ATTRIBUTION,
|
|
|
|
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,
|
|
|
|
)
|
2021-07-28 08:26:14 +00:00
|
|
|
from .model import AccuWeatherSensorDescription
|
2020-08-02 14:22:51 +00:00
|
|
|
|
|
|
|
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-06-21 04:40:04 +00:00
|
|
|
coordinator: AccuWeatherDataUpdateCoordinator = hass.data[DOMAIN][entry.entry_id]
|
2020-08-02 14:22:51 +00:00
|
|
|
|
2021-05-19 08:37:16 +00:00
|
|
|
sensors: list[AccuWeatherSensor] = []
|
2021-07-28 08:26:14 +00:00
|
|
|
for description in SENSOR_TYPES:
|
|
|
|
sensors.append(AccuWeatherSensor(name, coordinator, description))
|
2020-08-02 14:22:51 +00:00
|
|
|
|
|
|
|
if coordinator.forecast:
|
2021-07-28 08:26:14 +00:00
|
|
|
for description 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.
|
2021-07-28 08:26:14 +00:00
|
|
|
if description.key in coordinator.data[ATTR_FORECAST][0]:
|
2020-08-02 14:22:51 +00:00
|
|
|
sensors.append(
|
2021-07-28 08:26:14 +00:00
|
|
|
AccuWeatherSensor(
|
|
|
|
name, coordinator, description, forecast_day=day
|
|
|
|
)
|
2020-08-02 14:22:51 +00:00
|
|
|
)
|
|
|
|
|
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-10-11 21:15:32 +00:00
|
|
|
_attr_attribution = ATTRIBUTION
|
2021-05-19 08:37:16 +00:00
|
|
|
coordinator: AccuWeatherDataUpdateCoordinator
|
2021-07-28 08:26:14 +00:00
|
|
|
entity_description: AccuWeatherSensorDescription
|
2021-05-19 08:37:16 +00:00
|
|
|
|
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
name: str,
|
|
|
|
coordinator: AccuWeatherDataUpdateCoordinator,
|
2021-07-28 08:26:14 +00:00
|
|
|
description: AccuWeatherSensorDescription,
|
2021-05-19 08:37:16 +00:00
|
|
|
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-07-28 08:26:14 +00:00
|
|
|
self.entity_description = description
|
|
|
|
self._sensor_data = _get_sensor_data(
|
|
|
|
coordinator.data, forecast_day, description.key
|
|
|
|
)
|
2021-10-11 21:15:32 +00:00
|
|
|
self._attrs: dict[str, Any] = {}
|
2021-07-28 08:26:14 +00:00
|
|
|
if forecast_day is not None:
|
|
|
|
self._attr_name = f"{name} {description.name} {forecast_day}d"
|
2021-07-05 09:36:04 +00:00
|
|
|
self._attr_unique_id = (
|
2021-07-28 08:26:14 +00:00
|
|
|
f"{coordinator.location_key}-{description.key}-{forecast_day}".lower()
|
2021-07-05 09:36:04 +00:00
|
|
|
)
|
|
|
|
else:
|
2021-07-28 08:26:14 +00:00
|
|
|
self._attr_name = f"{name} {description.name}"
|
|
|
|
self._attr_unique_id = (
|
|
|
|
f"{coordinator.location_key}-{description.key}".lower()
|
|
|
|
)
|
2021-07-05 09:36:04 +00:00
|
|
|
if coordinator.is_metric:
|
2021-07-28 08:26:14 +00:00
|
|
|
self._unit_system = API_METRIC
|
2021-08-11 08:45:05 +00:00
|
|
|
self._attr_native_unit_of_measurement = description.unit_metric
|
2021-07-05 09:36:04 +00:00
|
|
|
else:
|
2021-07-28 08:26:14 +00:00
|
|
|
self._unit_system = API_IMPERIAL
|
2021-08-11 08:45:05 +00:00
|
|
|
self._attr_native_unit_of_measurement = description.unit_imperial
|
2021-10-22 14:35:39 +00:00
|
|
|
self._attr_device_info = DeviceInfo(
|
2021-11-22 17:14:15 +00:00
|
|
|
entry_type=DeviceEntryType.SERVICE,
|
2021-10-22 14:35:39 +00:00
|
|
|
identifiers={(DOMAIN, coordinator.location_key)},
|
|
|
|
manufacturer=MANUFACTURER,
|
|
|
|
name=NAME,
|
|
|
|
)
|
2021-07-28 08:26:14 +00:00
|
|
|
self.forecast_day = forecast_day
|
2020-08-05 10:50:34 +00:00
|
|
|
|
2020-08-02 14:22:51 +00:00
|
|
|
@property
|
2021-08-11 08:45:05 +00:00
|
|
|
def native_value(self) -> StateType:
|
2020-08-02 14:22:51 +00:00
|
|
|
"""Return the state."""
|
|
|
|
if self.forecast_day is not None:
|
2021-12-08 18:57:36 +00:00
|
|
|
if self.entity_description.device_class == SensorDeviceClass.TEMPERATURE:
|
2021-05-19 08:37:16 +00:00
|
|
|
return cast(float, self._sensor_data["Value"])
|
2021-07-28 08:26:14 +00:00
|
|
|
if self.entity_description.key == "UVIndex":
|
2021-05-19 08:37:16 +00:00
|
|
|
return cast(int, self._sensor_data["Value"])
|
2021-07-28 08:26:14 +00:00
|
|
|
if self.entity_description.key in ("Grass", "Mold", "Ragweed", "Tree", "Ozone"):
|
2021-05-19 08:37:16 +00:00
|
|
|
return cast(int, self._sensor_data["Value"])
|
2021-07-28 08:26:14 +00:00
|
|
|
if self.entity_description.key == "Ceiling":
|
2021-05-19 08:37:16 +00:00
|
|
|
return round(self._sensor_data[self._unit_system]["Value"])
|
2021-07-28 08:26:14 +00:00
|
|
|
if self.entity_description.key == "PressureTendency":
|
2021-05-19 08:37:16 +00:00
|
|
|
return cast(str, self._sensor_data["LocalizedText"].lower())
|
2021-12-08 18:57:36 +00:00
|
|
|
if self.entity_description.device_class == SensorDeviceClass.TEMPERATURE:
|
2021-05-19 08:37:16 +00:00
|
|
|
return cast(float, self._sensor_data[self._unit_system]["Value"])
|
2021-07-28 08:26:14 +00:00
|
|
|
if self.entity_description.key == "Precipitation":
|
2021-05-19 08:37:16 +00:00
|
|
|
return cast(float, self._sensor_data[self._unit_system]["Value"])
|
2021-07-28 08:26:14 +00:00
|
|
|
if self.entity_description.key in ("Wind", "WindGust"):
|
2021-05-19 08:37:16 +00:00
|
|
|
return cast(float, self._sensor_data["Speed"][self._unit_system]["Value"])
|
2021-07-28 08:26:14 +00:00
|
|
|
if self.entity_description.key in (
|
|
|
|
"WindDay",
|
|
|
|
"WindNight",
|
|
|
|
"WindGustDay",
|
|
|
|
"WindGustNight",
|
|
|
|
):
|
2021-05-19 08:37:16 +00:00
|
|
|
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 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:
|
2021-07-28 08:26:14 +00:00
|
|
|
if self.entity_description.key in (
|
|
|
|
"WindDay",
|
|
|
|
"WindNight",
|
|
|
|
"WindGustDay",
|
|
|
|
"WindGustNight",
|
|
|
|
):
|
2021-05-19 08:37:16 +00:00
|
|
|
self._attrs["direction"] = self._sensor_data["Direction"]["English"]
|
2021-07-28 08:26:14 +00:00
|
|
|
elif self.entity_description.key in (
|
|
|
|
"Grass",
|
|
|
|
"Mold",
|
|
|
|
"Ozone",
|
|
|
|
"Ragweed",
|
|
|
|
"Tree",
|
|
|
|
"UVIndex",
|
|
|
|
):
|
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
|
2021-07-28 08:26:14 +00:00
|
|
|
if self.entity_description.key == "UVIndex":
|
2020-08-02 14:22:51 +00:00
|
|
|
self._attrs["level"] = self.coordinator.data["UVIndexText"]
|
2021-07-28 08:26:14 +00:00
|
|
|
elif self.entity_description.key == "Precipitation":
|
2020-08-02 14:22:51 +00:00
|
|
|
self._attrs["type"] = self.coordinator.data["PrecipitationType"]
|
|
|
|
return self._attrs
|
|
|
|
|
2021-06-20 21:53:08 +00:00
|
|
|
@callback
|
|
|
|
def _handle_coordinator_update(self) -> None:
|
|
|
|
"""Handle data update."""
|
|
|
|
self._sensor_data = _get_sensor_data(
|
2021-07-28 08:26:14 +00:00
|
|
|
self.coordinator.data, self.forecast_day, self.entity_description.key
|
2021-06-20 21:53:08 +00:00
|
|
|
)
|
|
|
|
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]
|