2019-04-03 15:40:03 +00:00
|
|
|
"""Support for UK Met Office weather service."""
|
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
|
|
|
|
2020-06-15 10:02:25 +00:00
|
|
|
# Sensor types are defined as:
|
|
|
|
# variable -> [0]title, [1]device_class, [2]units, [3]icon, [4]enabled_by_default
|
|
|
|
SENSOR_TYPES = {
|
|
|
|
"name": ["Station Name", None, None, "mdi:label-outline", False],
|
|
|
|
"weather": [
|
|
|
|
"Weather",
|
|
|
|
None,
|
|
|
|
None,
|
|
|
|
"mdi:weather-sunny", # but will adapt to current conditions
|
|
|
|
True,
|
|
|
|
],
|
|
|
|
"temperature": ["Temperature", DEVICE_CLASS_TEMPERATURE, TEMP_CELSIUS, None, True],
|
|
|
|
"feels_like_temperature": [
|
|
|
|
"Feels Like Temperature",
|
|
|
|
DEVICE_CLASS_TEMPERATURE,
|
|
|
|
TEMP_CELSIUS,
|
|
|
|
None,
|
|
|
|
False,
|
|
|
|
],
|
|
|
|
"wind_speed": [
|
|
|
|
"Wind Speed",
|
|
|
|
None,
|
|
|
|
SPEED_MILES_PER_HOUR,
|
|
|
|
"mdi:weather-windy",
|
|
|
|
True,
|
|
|
|
],
|
|
|
|
"wind_direction": ["Wind Direction", None, None, "mdi:compass-outline", False],
|
|
|
|
"wind_gust": ["Wind Gust", None, SPEED_MILES_PER_HOUR, "mdi:weather-windy", False],
|
|
|
|
"visibility": ["Visibility", None, None, "mdi:eye", False],
|
|
|
|
"visibility_distance": [
|
|
|
|
"Visibility Distance",
|
|
|
|
None,
|
|
|
|
LENGTH_KILOMETERS,
|
|
|
|
"mdi:eye",
|
|
|
|
False,
|
|
|
|
],
|
|
|
|
"uv": ["UV Index", None, UV_INDEX, "mdi:weather-sunny-alert", True],
|
|
|
|
"precipitation": [
|
|
|
|
"Probability of Precipitation",
|
|
|
|
None,
|
2020-09-05 19:09:14 +00:00
|
|
|
PERCENTAGE,
|
2020-06-15 10:02:25 +00:00
|
|
|
"mdi:weather-rainy",
|
|
|
|
True,
|
|
|
|
],
|
2020-09-05 19:09:14 +00:00
|
|
|
"humidity": ["Humidity", DEVICE_CLASS_HUMIDITY, PERCENTAGE, None, 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(
|
|
|
|
hass_data[METOFFICE_HOURLY_COORDINATOR], hass_data, True, sensor_type
|
|
|
|
)
|
|
|
|
for sensor_type in SENSOR_TYPES
|
|
|
|
]
|
|
|
|
+ [
|
|
|
|
MetOfficeCurrentSensor(
|
|
|
|
hass_data[METOFFICE_DAILY_COORDINATOR], hass_data, False, sensor_type
|
|
|
|
)
|
2020-06-15 10:02:25 +00:00
|
|
|
for sensor_type in SENSOR_TYPES
|
|
|
|
],
|
|
|
|
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-06-27 19:04:42 +00:00
|
|
|
def __init__(self, coordinator, hass_data, use_3hourly, sensor_type):
|
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-06-27 19:04:42 +00:00
|
|
|
mode_label = MODE_3HOURLY_LABEL if use_3hourly else MODE_DAILY_LABEL
|
|
|
|
self._name = (
|
|
|
|
f"{hass_data[METOFFICE_NAME]} {SENSOR_TYPES[self._type][0]} {mode_label}"
|
|
|
|
)
|
|
|
|
self._unique_id = (
|
|
|
|
f"{SENSOR_TYPES[self._type][0]}_{hass_data[METOFFICE_COORDINATES]}"
|
|
|
|
)
|
|
|
|
if not use_3hourly:
|
|
|
|
self._unique_id = f"{self._unique_id}_{MODE_DAILY}"
|
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 name(self):
|
|
|
|
"""Return the name of the sensor."""
|
|
|
|
return self._name
|
2017-03-31 20:03:27 +00:00
|
|
|
|
2020-06-15 10:02:25 +00:00
|
|
|
@property
|
|
|
|
def unique_id(self):
|
|
|
|
"""Return the unique of the sensor."""
|
|
|
|
return self._unique_id
|
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 unit_of_measurement(self):
|
|
|
|
"""Return the unit of measurement."""
|
|
|
|
return SENSOR_TYPES[self._type][2]
|
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."""
|
|
|
|
value = SENSOR_TYPES[self._type][3]
|
|
|
|
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
|
2020-06-15 10:02:25 +00:00
|
|
|
def device_class(self):
|
|
|
|
"""Return the device class of the sensor."""
|
|
|
|
return SENSOR_TYPES[self._type][1]
|
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-06-27 19:04:42 +00:00
|
|
|
return SENSOR_TYPES[self._type][4] and self.use_3hourly
|