2019-04-03 15:40:03 +00:00
|
|
|
"""Support for UK Met Office weather service."""
|
2021-07-22 05:25:38 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
from typing import NamedTuple
|
|
|
|
|
2021-03-22 18:59:03 +00:00
|
|
|
from homeassistant.components.sensor import SensorEntity
|
2017-03-31 20:03:27 +00:00
|
|
|
from homeassistant.const import (
|
2019-07-31 19:25:30 +00:00
|
|
|
ATTR_ATTRIBUTION,
|
2020-06-15 10:02:25 +00:00
|
|
|
DEVICE_CLASS_HUMIDITY,
|
|
|
|
DEVICE_CLASS_TEMPERATURE,
|
2020-04-11 00:12:39 +00:00
|
|
|
LENGTH_KILOMETERS,
|
2020-09-05 19:09:14 +00:00
|
|
|
PERCENTAGE,
|
2020-02-25 01:52:14 +00:00
|
|
|
SPEED_MILES_PER_HOUR,
|
2019-07-31 19:25:30 +00:00
|
|
|
TEMP_CELSIUS,
|
2020-04-21 17:45:53 +00:00
|
|
|
UV_INDEX,
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2021-06-27 19:04:42 +00:00
|
|
|
from homeassistant.core import HomeAssistant
|
2021-04-22 21:53:37 +00:00
|
|
|
from homeassistant.helpers.typing import ConfigType
|
2021-06-27 19:04:42 +00:00
|
|
|
from homeassistant.helpers.update_coordinator import CoordinatorEntity
|
2020-06-15 10:02:25 +00:00
|
|
|
|
|
|
|
from .const import (
|
|
|
|
ATTRIBUTION,
|
|
|
|
CONDITION_CLASSES,
|
|
|
|
DOMAIN,
|
2021-06-27 19:04:42 +00:00
|
|
|
METOFFICE_COORDINATES,
|
|
|
|
METOFFICE_DAILY_COORDINATOR,
|
|
|
|
METOFFICE_HOURLY_COORDINATOR,
|
2020-06-15 10:02:25 +00:00
|
|
|
METOFFICE_NAME,
|
2021-06-27 19:04:42 +00:00
|
|
|
MODE_3HOURLY_LABEL,
|
|
|
|
MODE_DAILY,
|
|
|
|
MODE_DAILY_LABEL,
|
2020-06-15 10:02:25 +00:00
|
|
|
VISIBILITY_CLASSES,
|
|
|
|
VISIBILITY_DISTANCE_CLASSES,
|
|
|
|
)
|
2017-03-31 20:03:27 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
ATTR_LAST_UPDATE = "last_update"
|
|
|
|
ATTR_SENSOR_ID = "sensor_id"
|
|
|
|
ATTR_SITE_ID = "site_id"
|
|
|
|
ATTR_SITE_NAME = "site_name"
|
2018-01-06 20:53:14 +00:00
|
|
|
|
2021-07-22 05:25:38 +00:00
|
|
|
|
|
|
|
class MetOfficeSensorMetadata(NamedTuple):
|
|
|
|
"""Sensor metadata for an individual NWS sensor."""
|
|
|
|
|
|
|
|
title: str
|
|
|
|
device_class: str | None
|
|
|
|
unit_of_measurement: str | None
|
|
|
|
icon: str | None
|
|
|
|
enabled_by_default: bool
|
|
|
|
|
|
|
|
|
2020-06-15 10:02:25 +00:00
|
|
|
SENSOR_TYPES = {
|
2021-07-22 05:25:38 +00:00
|
|
|
"name": MetOfficeSensorMetadata(
|
|
|
|
"Station Name",
|
|
|
|
device_class=None,
|
|
|
|
unit_of_measurement=None,
|
|
|
|
icon="mdi:label-outline",
|
|
|
|
enabled_by_default=False,
|
|
|
|
),
|
|
|
|
"weather": MetOfficeSensorMetadata(
|
2020-06-15 10:02:25 +00:00
|
|
|
"Weather",
|
2021-07-22 05:25:38 +00:00
|
|
|
device_class=None,
|
|
|
|
unit_of_measurement=None,
|
|
|
|
icon="mdi:weather-sunny", # but will adapt to current conditions
|
|
|
|
enabled_by_default=True,
|
|
|
|
),
|
|
|
|
"temperature": MetOfficeSensorMetadata(
|
|
|
|
"Temperature",
|
|
|
|
device_class=DEVICE_CLASS_TEMPERATURE,
|
|
|
|
unit_of_measurement=TEMP_CELSIUS,
|
|
|
|
icon=None,
|
|
|
|
enabled_by_default=True,
|
|
|
|
),
|
|
|
|
"feels_like_temperature": MetOfficeSensorMetadata(
|
2020-06-15 10:02:25 +00:00
|
|
|
"Feels Like Temperature",
|
2021-07-22 05:25:38 +00:00
|
|
|
device_class=DEVICE_CLASS_TEMPERATURE,
|
|
|
|
unit_of_measurement=TEMP_CELSIUS,
|
|
|
|
icon=None,
|
|
|
|
enabled_by_default=False,
|
|
|
|
),
|
|
|
|
"wind_speed": MetOfficeSensorMetadata(
|
2020-06-15 10:02:25 +00:00
|
|
|
"Wind Speed",
|
2021-07-22 05:25:38 +00:00
|
|
|
device_class=None,
|
|
|
|
unit_of_measurement=SPEED_MILES_PER_HOUR,
|
|
|
|
icon="mdi:weather-windy",
|
|
|
|
enabled_by_default=True,
|
|
|
|
),
|
|
|
|
"wind_direction": MetOfficeSensorMetadata(
|
|
|
|
"Wind Direction",
|
|
|
|
device_class=None,
|
|
|
|
unit_of_measurement=None,
|
|
|
|
icon="mdi:compass-outline",
|
|
|
|
enabled_by_default=False,
|
|
|
|
),
|
|
|
|
"wind_gust": MetOfficeSensorMetadata(
|
|
|
|
"Wind Gust",
|
|
|
|
device_class=None,
|
|
|
|
unit_of_measurement=SPEED_MILES_PER_HOUR,
|
|
|
|
icon="mdi:weather-windy",
|
|
|
|
enabled_by_default=False,
|
|
|
|
),
|
|
|
|
"visibility": MetOfficeSensorMetadata(
|
|
|
|
"Visibility",
|
|
|
|
device_class=None,
|
|
|
|
unit_of_measurement=None,
|
|
|
|
icon="mdi:eye",
|
|
|
|
enabled_by_default=False,
|
|
|
|
),
|
|
|
|
"visibility_distance": MetOfficeSensorMetadata(
|
2020-06-15 10:02:25 +00:00
|
|
|
"Visibility Distance",
|
2021-07-22 05:25:38 +00:00
|
|
|
device_class=None,
|
|
|
|
unit_of_measurement=LENGTH_KILOMETERS,
|
|
|
|
icon="mdi:eye",
|
|
|
|
enabled_by_default=False,
|
|
|
|
),
|
|
|
|
"uv": MetOfficeSensorMetadata(
|
|
|
|
"UV Index",
|
|
|
|
device_class=None,
|
|
|
|
unit_of_measurement=UV_INDEX,
|
|
|
|
icon="mdi:weather-sunny-alert",
|
|
|
|
enabled_by_default=True,
|
|
|
|
),
|
|
|
|
"precipitation": MetOfficeSensorMetadata(
|
2020-06-15 10:02:25 +00:00
|
|
|
"Probability of Precipitation",
|
2021-07-22 05:25:38 +00:00
|
|
|
device_class=None,
|
|
|
|
unit_of_measurement=PERCENTAGE,
|
|
|
|
icon="mdi:weather-rainy",
|
|
|
|
enabled_by_default=True,
|
|
|
|
),
|
|
|
|
"humidity": MetOfficeSensorMetadata(
|
|
|
|
"Humidity",
|
|
|
|
device_class=DEVICE_CLASS_HUMIDITY,
|
|
|
|
unit_of_measurement=PERCENTAGE,
|
|
|
|
icon=None,
|
|
|
|
enabled_by_default=False,
|
|
|
|
),
|
2017-03-31 20:03:27 +00:00
|
|
|
}
|
|
|
|
|
2018-01-06 20:53:14 +00:00
|
|
|
|
2020-06-15 10:02:25 +00:00
|
|
|
async def async_setup_entry(
|
2021-04-22 21:53:37 +00:00
|
|
|
hass: HomeAssistant, entry: ConfigType, async_add_entities
|
2020-06-15 10:02:25 +00:00
|
|
|
) -> None:
|
|
|
|
"""Set up the Met Office weather sensor platform."""
|
|
|
|
hass_data = hass.data[DOMAIN][entry.entry_id]
|
2017-07-14 03:01:25 +00:00
|
|
|
|
2020-06-15 10:02:25 +00:00
|
|
|
async_add_entities(
|
|
|
|
[
|
2021-06-27 19:04:42 +00:00
|
|
|
MetOfficeCurrentSensor(
|
2021-07-22 05:25:38 +00:00
|
|
|
hass_data[METOFFICE_HOURLY_COORDINATOR],
|
|
|
|
hass_data,
|
|
|
|
True,
|
|
|
|
sensor_type,
|
|
|
|
metadata,
|
2021-06-27 19:04:42 +00:00
|
|
|
)
|
2021-07-22 05:25:38 +00:00
|
|
|
for sensor_type, metadata in SENSOR_TYPES.items()
|
2021-06-27 19:04:42 +00:00
|
|
|
]
|
|
|
|
+ [
|
|
|
|
MetOfficeCurrentSensor(
|
2021-07-22 05:25:38 +00:00
|
|
|
hass_data[METOFFICE_DAILY_COORDINATOR],
|
|
|
|
hass_data,
|
|
|
|
False,
|
|
|
|
sensor_type,
|
|
|
|
metadata,
|
2021-06-27 19:04:42 +00:00
|
|
|
)
|
2021-07-22 05:25:38 +00:00
|
|
|
for sensor_type, metadata in SENSOR_TYPES.items()
|
2020-06-15 10:02:25 +00:00
|
|
|
],
|
|
|
|
False,
|
|
|
|
)
|
2017-03-31 20:03:27 +00:00
|
|
|
|
|
|
|
|
2021-06-27 19:04:42 +00:00
|
|
|
class MetOfficeCurrentSensor(CoordinatorEntity, SensorEntity):
|
2020-06-15 10:02:25 +00:00
|
|
|
"""Implementation of a Met Office current weather condition sensor."""
|
2017-03-31 20:03:27 +00:00
|
|
|
|
2021-07-22 05:25:38 +00:00
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
coordinator,
|
|
|
|
hass_data,
|
|
|
|
use_3hourly,
|
|
|
|
sensor_type,
|
|
|
|
metadata: MetOfficeSensorMetadata,
|
|
|
|
):
|
2020-06-15 10:02:25 +00:00
|
|
|
"""Initialize the sensor."""
|
2021-06-27 19:04:42 +00:00
|
|
|
super().__init__(coordinator)
|
2017-03-31 20:03:27 +00:00
|
|
|
|
2020-06-15 10:02:25 +00:00
|
|
|
self._type = sensor_type
|
2021-07-22 05:25:38 +00:00
|
|
|
self._metadata = metadata
|
2021-06-27 19:04:42 +00:00
|
|
|
mode_label = MODE_3HOURLY_LABEL if use_3hourly else MODE_DAILY_LABEL
|
2021-07-22 05:25:38 +00:00
|
|
|
self._attr_name = f"{hass_data[METOFFICE_NAME]} {metadata.title} {mode_label}"
|
|
|
|
self._attr_unique_id = f"{metadata.title}_{hass_data[METOFFICE_COORDINATES]}"
|
2021-06-27 19:04:42 +00:00
|
|
|
if not use_3hourly:
|
2021-07-22 05:25:38 +00:00
|
|
|
self._attr_unique_id = f"{self._attr_unique_id}_{MODE_DAILY}"
|
|
|
|
self._attr_device_class = metadata.device_class
|
|
|
|
self._attr_unit_of_measurement = metadata.unit_of_measurement
|
2018-01-06 20:53:14 +00:00
|
|
|
|
2021-06-27 19:04:42 +00:00
|
|
|
self.use_3hourly = use_3hourly
|
2017-03-31 20:03:27 +00:00
|
|
|
|
2020-06-15 10:02:25 +00:00
|
|
|
@property
|
|
|
|
def state(self):
|
|
|
|
"""Return the state of the sensor."""
|
|
|
|
value = None
|
2017-03-31 20:03:27 +00:00
|
|
|
|
2020-06-15 10:02:25 +00:00
|
|
|
if self._type == "visibility_distance" and hasattr(
|
2021-06-27 19:04:42 +00:00
|
|
|
self.coordinator.data.now, "visibility"
|
2020-06-15 10:02:25 +00:00
|
|
|
):
|
2021-06-27 19:04:42 +00:00
|
|
|
value = VISIBILITY_DISTANCE_CLASSES.get(
|
|
|
|
self.coordinator.data.now.visibility.value
|
|
|
|
)
|
2017-03-31 20:03:27 +00:00
|
|
|
|
2021-06-27 19:04:42 +00:00
|
|
|
if self._type == "visibility" and hasattr(
|
|
|
|
self.coordinator.data.now, "visibility"
|
|
|
|
):
|
|
|
|
value = VISIBILITY_CLASSES.get(self.coordinator.data.now.visibility.value)
|
2018-01-06 20:53:14 +00:00
|
|
|
|
2021-06-27 19:04:42 +00:00
|
|
|
elif self._type == "weather" and hasattr(self.coordinator.data.now, self._type):
|
2020-06-15 10:02:25 +00:00
|
|
|
value = [
|
|
|
|
k
|
|
|
|
for k, v in CONDITION_CLASSES.items()
|
2021-06-27 19:04:42 +00:00
|
|
|
if self.coordinator.data.now.weather.value in v
|
2020-06-15 10:02:25 +00:00
|
|
|
][0]
|
2017-03-31 20:03:27 +00:00
|
|
|
|
2021-06-27 19:04:42 +00:00
|
|
|
elif hasattr(self.coordinator.data.now, self._type):
|
|
|
|
value = getattr(self.coordinator.data.now, self._type)
|
2017-03-31 20:03:27 +00:00
|
|
|
|
2020-06-15 10:02:25 +00:00
|
|
|
if not isinstance(value, int):
|
|
|
|
value = value.value
|
2017-03-31 20:03:27 +00:00
|
|
|
|
2020-06-15 10:02:25 +00:00
|
|
|
return value
|
2017-03-31 20:03:27 +00:00
|
|
|
|
|
|
|
@property
|
2020-06-15 10:02:25 +00:00
|
|
|
def icon(self):
|
|
|
|
"""Return the icon for the entity card."""
|
2021-07-22 05:25:38 +00:00
|
|
|
value = self._metadata.icon
|
2020-06-15 10:02:25 +00:00
|
|
|
if self._type == "weather":
|
|
|
|
value = self.state
|
|
|
|
if value is None:
|
|
|
|
value = "sunny"
|
|
|
|
elif value == "partlycloudy":
|
|
|
|
value = "partly-cloudy"
|
|
|
|
value = f"mdi:weather-{value}"
|
|
|
|
|
|
|
|
return value
|
2017-03-31 20:03:27 +00:00
|
|
|
|
|
|
|
@property
|
2021-03-11 19:11:25 +00:00
|
|
|
def extra_state_attributes(self):
|
2017-03-31 20:03:27 +00:00
|
|
|
"""Return the state attributes of the device."""
|
2020-06-15 10:02:25 +00:00
|
|
|
return {
|
|
|
|
ATTR_ATTRIBUTION: ATTRIBUTION,
|
2021-06-27 19:04:42 +00:00
|
|
|
ATTR_LAST_UPDATE: self.coordinator.data.now.date,
|
2020-06-15 10:02:25 +00:00
|
|
|
ATTR_SENSOR_ID: self._type,
|
2021-06-27 19:04:42 +00:00
|
|
|
ATTR_SITE_ID: self.coordinator.data.site.id,
|
|
|
|
ATTR_SITE_NAME: self.coordinator.data.site.name,
|
2020-06-15 10:02:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@property
|
|
|
|
def entity_registry_enabled_default(self) -> bool:
|
|
|
|
"""Return if the entity should be enabled when first added to the entity registry."""
|
2021-07-22 05:25:38 +00:00
|
|
|
return self._metadata.enabled_by_default and self.use_3hourly
|