Use NamedTuple - metoffice (#53294)
This commit is contained in:
parent
d98e580c3c
commit
f5480481cd
1 changed files with 122 additions and 73 deletions
|
@ -1,4 +1,8 @@
|
||||||
"""Support for UK Met Office weather service."""
|
"""Support for UK Met Office weather service."""
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
from typing import NamedTuple
|
||||||
|
|
||||||
from homeassistant.components.sensor import SensorEntity
|
from homeassistant.components.sensor import SensorEntity
|
||||||
from homeassistant.const import (
|
from homeassistant.const import (
|
||||||
ATTR_ATTRIBUTION,
|
ATTR_ATTRIBUTION,
|
||||||
|
@ -34,51 +38,102 @@ ATTR_SENSOR_ID = "sensor_id"
|
||||||
ATTR_SITE_ID = "site_id"
|
ATTR_SITE_ID = "site_id"
|
||||||
ATTR_SITE_NAME = "site_name"
|
ATTR_SITE_NAME = "site_name"
|
||||||
|
|
||||||
# Sensor types are defined as:
|
|
||||||
# variable -> [0]title, [1]device_class, [2]units, [3]icon, [4]enabled_by_default
|
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
|
||||||
|
|
||||||
|
|
||||||
SENSOR_TYPES = {
|
SENSOR_TYPES = {
|
||||||
"name": ["Station Name", None, None, "mdi:label-outline", False],
|
"name": MetOfficeSensorMetadata(
|
||||||
"weather": [
|
"Station Name",
|
||||||
|
device_class=None,
|
||||||
|
unit_of_measurement=None,
|
||||||
|
icon="mdi:label-outline",
|
||||||
|
enabled_by_default=False,
|
||||||
|
),
|
||||||
|
"weather": MetOfficeSensorMetadata(
|
||||||
"Weather",
|
"Weather",
|
||||||
None,
|
device_class=None,
|
||||||
None,
|
unit_of_measurement=None,
|
||||||
"mdi:weather-sunny", # but will adapt to current conditions
|
icon="mdi:weather-sunny", # but will adapt to current conditions
|
||||||
True,
|
enabled_by_default=True,
|
||||||
],
|
),
|
||||||
"temperature": ["Temperature", DEVICE_CLASS_TEMPERATURE, TEMP_CELSIUS, None, True],
|
"temperature": MetOfficeSensorMetadata(
|
||||||
"feels_like_temperature": [
|
"Temperature",
|
||||||
|
device_class=DEVICE_CLASS_TEMPERATURE,
|
||||||
|
unit_of_measurement=TEMP_CELSIUS,
|
||||||
|
icon=None,
|
||||||
|
enabled_by_default=True,
|
||||||
|
),
|
||||||
|
"feels_like_temperature": MetOfficeSensorMetadata(
|
||||||
"Feels Like Temperature",
|
"Feels Like Temperature",
|
||||||
DEVICE_CLASS_TEMPERATURE,
|
device_class=DEVICE_CLASS_TEMPERATURE,
|
||||||
TEMP_CELSIUS,
|
unit_of_measurement=TEMP_CELSIUS,
|
||||||
None,
|
icon=None,
|
||||||
False,
|
enabled_by_default=False,
|
||||||
],
|
),
|
||||||
"wind_speed": [
|
"wind_speed": MetOfficeSensorMetadata(
|
||||||
"Wind Speed",
|
"Wind Speed",
|
||||||
None,
|
device_class=None,
|
||||||
SPEED_MILES_PER_HOUR,
|
unit_of_measurement=SPEED_MILES_PER_HOUR,
|
||||||
"mdi:weather-windy",
|
icon="mdi:weather-windy",
|
||||||
True,
|
enabled_by_default=True,
|
||||||
],
|
),
|
||||||
"wind_direction": ["Wind Direction", None, None, "mdi:compass-outline", False],
|
"wind_direction": MetOfficeSensorMetadata(
|
||||||
"wind_gust": ["Wind Gust", None, SPEED_MILES_PER_HOUR, "mdi:weather-windy", False],
|
"Wind Direction",
|
||||||
"visibility": ["Visibility", None, None, "mdi:eye", False],
|
device_class=None,
|
||||||
"visibility_distance": [
|
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(
|
||||||
"Visibility Distance",
|
"Visibility Distance",
|
||||||
None,
|
device_class=None,
|
||||||
LENGTH_KILOMETERS,
|
unit_of_measurement=LENGTH_KILOMETERS,
|
||||||
"mdi:eye",
|
icon="mdi:eye",
|
||||||
False,
|
enabled_by_default=False,
|
||||||
],
|
),
|
||||||
"uv": ["UV Index", None, UV_INDEX, "mdi:weather-sunny-alert", True],
|
"uv": MetOfficeSensorMetadata(
|
||||||
"precipitation": [
|
"UV Index",
|
||||||
|
device_class=None,
|
||||||
|
unit_of_measurement=UV_INDEX,
|
||||||
|
icon="mdi:weather-sunny-alert",
|
||||||
|
enabled_by_default=True,
|
||||||
|
),
|
||||||
|
"precipitation": MetOfficeSensorMetadata(
|
||||||
"Probability of Precipitation",
|
"Probability of Precipitation",
|
||||||
None,
|
device_class=None,
|
||||||
PERCENTAGE,
|
unit_of_measurement=PERCENTAGE,
|
||||||
"mdi:weather-rainy",
|
icon="mdi:weather-rainy",
|
||||||
True,
|
enabled_by_default=True,
|
||||||
],
|
),
|
||||||
"humidity": ["Humidity", DEVICE_CLASS_HUMIDITY, PERCENTAGE, None, False],
|
"humidity": MetOfficeSensorMetadata(
|
||||||
|
"Humidity",
|
||||||
|
device_class=DEVICE_CLASS_HUMIDITY,
|
||||||
|
unit_of_measurement=PERCENTAGE,
|
||||||
|
icon=None,
|
||||||
|
enabled_by_default=False,
|
||||||
|
),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -91,15 +146,23 @@ async def async_setup_entry(
|
||||||
async_add_entities(
|
async_add_entities(
|
||||||
[
|
[
|
||||||
MetOfficeCurrentSensor(
|
MetOfficeCurrentSensor(
|
||||||
hass_data[METOFFICE_HOURLY_COORDINATOR], hass_data, True, sensor_type
|
hass_data[METOFFICE_HOURLY_COORDINATOR],
|
||||||
|
hass_data,
|
||||||
|
True,
|
||||||
|
sensor_type,
|
||||||
|
metadata,
|
||||||
)
|
)
|
||||||
for sensor_type in SENSOR_TYPES
|
for sensor_type, metadata in SENSOR_TYPES.items()
|
||||||
]
|
]
|
||||||
+ [
|
+ [
|
||||||
MetOfficeCurrentSensor(
|
MetOfficeCurrentSensor(
|
||||||
hass_data[METOFFICE_DAILY_COORDINATOR], hass_data, False, sensor_type
|
hass_data[METOFFICE_DAILY_COORDINATOR],
|
||||||
|
hass_data,
|
||||||
|
False,
|
||||||
|
sensor_type,
|
||||||
|
metadata,
|
||||||
)
|
)
|
||||||
for sensor_type in SENSOR_TYPES
|
for sensor_type, metadata in SENSOR_TYPES.items()
|
||||||
],
|
],
|
||||||
False,
|
False,
|
||||||
)
|
)
|
||||||
|
@ -108,33 +171,29 @@ async def async_setup_entry(
|
||||||
class MetOfficeCurrentSensor(CoordinatorEntity, SensorEntity):
|
class MetOfficeCurrentSensor(CoordinatorEntity, SensorEntity):
|
||||||
"""Implementation of a Met Office current weather condition sensor."""
|
"""Implementation of a Met Office current weather condition sensor."""
|
||||||
|
|
||||||
def __init__(self, coordinator, hass_data, use_3hourly, sensor_type):
|
def __init__(
|
||||||
|
self,
|
||||||
|
coordinator,
|
||||||
|
hass_data,
|
||||||
|
use_3hourly,
|
||||||
|
sensor_type,
|
||||||
|
metadata: MetOfficeSensorMetadata,
|
||||||
|
):
|
||||||
"""Initialize the sensor."""
|
"""Initialize the sensor."""
|
||||||
super().__init__(coordinator)
|
super().__init__(coordinator)
|
||||||
|
|
||||||
self._type = sensor_type
|
self._type = sensor_type
|
||||||
|
self._metadata = metadata
|
||||||
mode_label = MODE_3HOURLY_LABEL if use_3hourly else MODE_DAILY_LABEL
|
mode_label = MODE_3HOURLY_LABEL if use_3hourly else MODE_DAILY_LABEL
|
||||||
self._name = (
|
self._attr_name = f"{hass_data[METOFFICE_NAME]} {metadata.title} {mode_label}"
|
||||||
f"{hass_data[METOFFICE_NAME]} {SENSOR_TYPES[self._type][0]} {mode_label}"
|
self._attr_unique_id = f"{metadata.title}_{hass_data[METOFFICE_COORDINATES]}"
|
||||||
)
|
|
||||||
self._unique_id = (
|
|
||||||
f"{SENSOR_TYPES[self._type][0]}_{hass_data[METOFFICE_COORDINATES]}"
|
|
||||||
)
|
|
||||||
if not use_3hourly:
|
if not use_3hourly:
|
||||||
self._unique_id = f"{self._unique_id}_{MODE_DAILY}"
|
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
|
||||||
|
|
||||||
self.use_3hourly = use_3hourly
|
self.use_3hourly = use_3hourly
|
||||||
|
|
||||||
@property
|
|
||||||
def name(self):
|
|
||||||
"""Return the name of the sensor."""
|
|
||||||
return self._name
|
|
||||||
|
|
||||||
@property
|
|
||||||
def unique_id(self):
|
|
||||||
"""Return the unique of the sensor."""
|
|
||||||
return self._unique_id
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def state(self):
|
def state(self):
|
||||||
"""Return the state of the sensor."""
|
"""Return the state of the sensor."""
|
||||||
|
@ -167,15 +226,10 @@ class MetOfficeCurrentSensor(CoordinatorEntity, SensorEntity):
|
||||||
|
|
||||||
return value
|
return value
|
||||||
|
|
||||||
@property
|
|
||||||
def unit_of_measurement(self):
|
|
||||||
"""Return the unit of measurement."""
|
|
||||||
return SENSOR_TYPES[self._type][2]
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def icon(self):
|
def icon(self):
|
||||||
"""Return the icon for the entity card."""
|
"""Return the icon for the entity card."""
|
||||||
value = SENSOR_TYPES[self._type][3]
|
value = self._metadata.icon
|
||||||
if self._type == "weather":
|
if self._type == "weather":
|
||||||
value = self.state
|
value = self.state
|
||||||
if value is None:
|
if value is None:
|
||||||
|
@ -186,11 +240,6 @@ class MetOfficeCurrentSensor(CoordinatorEntity, SensorEntity):
|
||||||
|
|
||||||
return value
|
return value
|
||||||
|
|
||||||
@property
|
|
||||||
def device_class(self):
|
|
||||||
"""Return the device class of the sensor."""
|
|
||||||
return SENSOR_TYPES[self._type][1]
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def extra_state_attributes(self):
|
def extra_state_attributes(self):
|
||||||
"""Return the state attributes of the device."""
|
"""Return the state attributes of the device."""
|
||||||
|
@ -205,4 +254,4 @@ class MetOfficeCurrentSensor(CoordinatorEntity, SensorEntity):
|
||||||
@property
|
@property
|
||||||
def entity_registry_enabled_default(self) -> bool:
|
def entity_registry_enabled_default(self) -> bool:
|
||||||
"""Return if the entity should be enabled when first added to the entity registry."""
|
"""Return if the entity should be enabled when first added to the entity registry."""
|
||||||
return SENSOR_TYPES[self._type][4] and self.use_3hourly
|
return self._metadata.enabled_by_default and self.use_3hourly
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue