mirror of
https://github.com/home-assistant/core.git
synced 2025-07-15 17:27:10 +00:00
Use EntityDescription - metoffice (#53555)
This commit is contained in:
parent
e3df4f8795
commit
268ade6b76
@ -1,9 +1,7 @@
|
|||||||
"""Support for UK Met Office weather service."""
|
"""Support for UK Met Office weather service."""
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
from typing import NamedTuple
|
from homeassistant.components.sensor import SensorEntity, SensorEntityDescription
|
||||||
|
|
||||||
from homeassistant.components.sensor import SensorEntity
|
|
||||||
from homeassistant.const import (
|
from homeassistant.const import (
|
||||||
ATTR_ATTRIBUTION,
|
ATTR_ATTRIBUTION,
|
||||||
DEVICE_CLASS_HUMIDITY,
|
DEVICE_CLASS_HUMIDITY,
|
||||||
@ -39,102 +37,104 @@ ATTR_SITE_ID = "site_id"
|
|||||||
ATTR_SITE_NAME = "site_name"
|
ATTR_SITE_NAME = "site_name"
|
||||||
|
|
||||||
|
|
||||||
class MetOfficeSensorMetadata(NamedTuple):
|
SENSOR_TYPES: tuple[SensorEntityDescription, ...] = (
|
||||||
"""Sensor metadata for an individual NWS sensor."""
|
SensorEntityDescription(
|
||||||
|
key="name",
|
||||||
title: str
|
name="Station Name",
|
||||||
device_class: str | None
|
|
||||||
unit_of_measurement: str | None
|
|
||||||
icon: str | None
|
|
||||||
enabled_by_default: bool
|
|
||||||
|
|
||||||
|
|
||||||
SENSOR_TYPES = {
|
|
||||||
"name": MetOfficeSensorMetadata(
|
|
||||||
"Station Name",
|
|
||||||
device_class=None,
|
device_class=None,
|
||||||
unit_of_measurement=None,
|
unit_of_measurement=None,
|
||||||
icon="mdi:label-outline",
|
icon="mdi:label-outline",
|
||||||
enabled_by_default=False,
|
entity_registry_enabled_default=False,
|
||||||
),
|
),
|
||||||
"weather": MetOfficeSensorMetadata(
|
SensorEntityDescription(
|
||||||
"Weather",
|
key="weather",
|
||||||
|
name="Weather",
|
||||||
device_class=None,
|
device_class=None,
|
||||||
unit_of_measurement=None,
|
unit_of_measurement=None,
|
||||||
icon="mdi:weather-sunny", # but will adapt to current conditions
|
icon="mdi:weather-sunny", # but will adapt to current conditions
|
||||||
enabled_by_default=True,
|
entity_registry_enabled_default=True,
|
||||||
),
|
),
|
||||||
"temperature": MetOfficeSensorMetadata(
|
SensorEntityDescription(
|
||||||
"Temperature",
|
key="temperature",
|
||||||
|
name="Temperature",
|
||||||
device_class=DEVICE_CLASS_TEMPERATURE,
|
device_class=DEVICE_CLASS_TEMPERATURE,
|
||||||
unit_of_measurement=TEMP_CELSIUS,
|
unit_of_measurement=TEMP_CELSIUS,
|
||||||
icon=None,
|
icon=None,
|
||||||
enabled_by_default=True,
|
entity_registry_enabled_default=True,
|
||||||
),
|
),
|
||||||
"feels_like_temperature": MetOfficeSensorMetadata(
|
SensorEntityDescription(
|
||||||
"Feels Like Temperature",
|
key="feels_like_temperature",
|
||||||
|
name="Feels Like Temperature",
|
||||||
device_class=DEVICE_CLASS_TEMPERATURE,
|
device_class=DEVICE_CLASS_TEMPERATURE,
|
||||||
unit_of_measurement=TEMP_CELSIUS,
|
unit_of_measurement=TEMP_CELSIUS,
|
||||||
icon=None,
|
icon=None,
|
||||||
enabled_by_default=False,
|
entity_registry_enabled_default=False,
|
||||||
),
|
),
|
||||||
"wind_speed": MetOfficeSensorMetadata(
|
SensorEntityDescription(
|
||||||
"Wind Speed",
|
key="wind_speed",
|
||||||
|
name="Wind Speed",
|
||||||
device_class=None,
|
device_class=None,
|
||||||
unit_of_measurement=SPEED_MILES_PER_HOUR,
|
unit_of_measurement=SPEED_MILES_PER_HOUR,
|
||||||
icon="mdi:weather-windy",
|
icon="mdi:weather-windy",
|
||||||
enabled_by_default=True,
|
entity_registry_enabled_default=True,
|
||||||
),
|
),
|
||||||
"wind_direction": MetOfficeSensorMetadata(
|
SensorEntityDescription(
|
||||||
"Wind Direction",
|
key="wind_direction",
|
||||||
|
name="Wind Direction",
|
||||||
device_class=None,
|
device_class=None,
|
||||||
unit_of_measurement=None,
|
unit_of_measurement=None,
|
||||||
icon="mdi:compass-outline",
|
icon="mdi:compass-outline",
|
||||||
enabled_by_default=False,
|
entity_registry_enabled_default=False,
|
||||||
),
|
),
|
||||||
"wind_gust": MetOfficeSensorMetadata(
|
SensorEntityDescription(
|
||||||
"Wind Gust",
|
key="wind_gust",
|
||||||
|
name="Wind Gust",
|
||||||
device_class=None,
|
device_class=None,
|
||||||
unit_of_measurement=SPEED_MILES_PER_HOUR,
|
unit_of_measurement=SPEED_MILES_PER_HOUR,
|
||||||
icon="mdi:weather-windy",
|
icon="mdi:weather-windy",
|
||||||
enabled_by_default=False,
|
entity_registry_enabled_default=False,
|
||||||
),
|
),
|
||||||
"visibility": MetOfficeSensorMetadata(
|
SensorEntityDescription(
|
||||||
"Visibility",
|
key="visibility",
|
||||||
|
name="Visibility",
|
||||||
device_class=None,
|
device_class=None,
|
||||||
unit_of_measurement=None,
|
unit_of_measurement=None,
|
||||||
icon="mdi:eye",
|
icon="mdi:eye",
|
||||||
enabled_by_default=False,
|
entity_registry_enabled_default=False,
|
||||||
),
|
),
|
||||||
"visibility_distance": MetOfficeSensorMetadata(
|
SensorEntityDescription(
|
||||||
"Visibility Distance",
|
key="visibility_distance",
|
||||||
|
name="Visibility Distance",
|
||||||
device_class=None,
|
device_class=None,
|
||||||
unit_of_measurement=LENGTH_KILOMETERS,
|
unit_of_measurement=LENGTH_KILOMETERS,
|
||||||
icon="mdi:eye",
|
icon="mdi:eye",
|
||||||
enabled_by_default=False,
|
entity_registry_enabled_default=False,
|
||||||
),
|
),
|
||||||
"uv": MetOfficeSensorMetadata(
|
SensorEntityDescription(
|
||||||
"UV Index",
|
key="uv",
|
||||||
|
name="UV Index",
|
||||||
device_class=None,
|
device_class=None,
|
||||||
unit_of_measurement=UV_INDEX,
|
unit_of_measurement=UV_INDEX,
|
||||||
icon="mdi:weather-sunny-alert",
|
icon="mdi:weather-sunny-alert",
|
||||||
enabled_by_default=True,
|
entity_registry_enabled_default=True,
|
||||||
),
|
),
|
||||||
"precipitation": MetOfficeSensorMetadata(
|
SensorEntityDescription(
|
||||||
"Probability of Precipitation",
|
key="precipitation",
|
||||||
|
name="Probability of Precipitation",
|
||||||
device_class=None,
|
device_class=None,
|
||||||
unit_of_measurement=PERCENTAGE,
|
unit_of_measurement=PERCENTAGE,
|
||||||
icon="mdi:weather-rainy",
|
icon="mdi:weather-rainy",
|
||||||
enabled_by_default=True,
|
entity_registry_enabled_default=True,
|
||||||
),
|
),
|
||||||
"humidity": MetOfficeSensorMetadata(
|
SensorEntityDescription(
|
||||||
"Humidity",
|
key="humidity",
|
||||||
|
name="Humidity",
|
||||||
device_class=DEVICE_CLASS_HUMIDITY,
|
device_class=DEVICE_CLASS_HUMIDITY,
|
||||||
unit_of_measurement=PERCENTAGE,
|
unit_of_measurement=PERCENTAGE,
|
||||||
icon=None,
|
icon=None,
|
||||||
enabled_by_default=False,
|
entity_registry_enabled_default=False,
|
||||||
),
|
),
|
||||||
}
|
)
|
||||||
|
|
||||||
|
|
||||||
async def async_setup_entry(
|
async def async_setup_entry(
|
||||||
@ -149,20 +149,18 @@ async def async_setup_entry(
|
|||||||
hass_data[METOFFICE_HOURLY_COORDINATOR],
|
hass_data[METOFFICE_HOURLY_COORDINATOR],
|
||||||
hass_data,
|
hass_data,
|
||||||
True,
|
True,
|
||||||
sensor_type,
|
description,
|
||||||
metadata,
|
|
||||||
)
|
)
|
||||||
for sensor_type, metadata in SENSOR_TYPES.items()
|
for description in SENSOR_TYPES
|
||||||
]
|
]
|
||||||
+ [
|
+ [
|
||||||
MetOfficeCurrentSensor(
|
MetOfficeCurrentSensor(
|
||||||
hass_data[METOFFICE_DAILY_COORDINATOR],
|
hass_data[METOFFICE_DAILY_COORDINATOR],
|
||||||
hass_data,
|
hass_data,
|
||||||
False,
|
False,
|
||||||
sensor_type,
|
description,
|
||||||
metadata,
|
|
||||||
)
|
)
|
||||||
for sensor_type, metadata in SENSOR_TYPES.items()
|
for description in SENSOR_TYPES
|
||||||
],
|
],
|
||||||
False,
|
False,
|
||||||
)
|
)
|
||||||
@ -176,21 +174,17 @@ class MetOfficeCurrentSensor(CoordinatorEntity, SensorEntity):
|
|||||||
coordinator,
|
coordinator,
|
||||||
hass_data,
|
hass_data,
|
||||||
use_3hourly,
|
use_3hourly,
|
||||||
sensor_type,
|
description: SensorEntityDescription,
|
||||||
metadata: MetOfficeSensorMetadata,
|
|
||||||
):
|
):
|
||||||
"""Initialize the sensor."""
|
"""Initialize the sensor."""
|
||||||
super().__init__(coordinator)
|
super().__init__(coordinator)
|
||||||
|
|
||||||
self._type = sensor_type
|
self.entity_description = description
|
||||||
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._attr_name = f"{hass_data[METOFFICE_NAME]} {metadata.title} {mode_label}"
|
self._attr_name = f"{hass_data[METOFFICE_NAME]} {description.name} {mode_label}"
|
||||||
self._attr_unique_id = f"{metadata.title}_{hass_data[METOFFICE_COORDINATES]}"
|
self._attr_unique_id = f"{description.name}_{hass_data[METOFFICE_COORDINATES]}"
|
||||||
if not use_3hourly:
|
if not use_3hourly:
|
||||||
self._attr_unique_id = f"{self._attr_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
|
||||||
|
|
||||||
@ -199,27 +193,29 @@ class MetOfficeCurrentSensor(CoordinatorEntity, SensorEntity):
|
|||||||
"""Return the state of the sensor."""
|
"""Return the state of the sensor."""
|
||||||
value = None
|
value = None
|
||||||
|
|
||||||
if self._type == "visibility_distance" and hasattr(
|
if self.entity_description.key == "visibility_distance" and hasattr(
|
||||||
self.coordinator.data.now, "visibility"
|
self.coordinator.data.now, "visibility"
|
||||||
):
|
):
|
||||||
value = VISIBILITY_DISTANCE_CLASSES.get(
|
value = VISIBILITY_DISTANCE_CLASSES.get(
|
||||||
self.coordinator.data.now.visibility.value
|
self.coordinator.data.now.visibility.value
|
||||||
)
|
)
|
||||||
|
|
||||||
if self._type == "visibility" and hasattr(
|
if self.entity_description.key == "visibility" and hasattr(
|
||||||
self.coordinator.data.now, "visibility"
|
self.coordinator.data.now, "visibility"
|
||||||
):
|
):
|
||||||
value = VISIBILITY_CLASSES.get(self.coordinator.data.now.visibility.value)
|
value = VISIBILITY_CLASSES.get(self.coordinator.data.now.visibility.value)
|
||||||
|
|
||||||
elif self._type == "weather" and hasattr(self.coordinator.data.now, self._type):
|
elif self.entity_description.key == "weather" and hasattr(
|
||||||
|
self.coordinator.data.now, self.entity_description.key
|
||||||
|
):
|
||||||
value = [
|
value = [
|
||||||
k
|
k
|
||||||
for k, v in CONDITION_CLASSES.items()
|
for k, v in CONDITION_CLASSES.items()
|
||||||
if self.coordinator.data.now.weather.value in v
|
if self.coordinator.data.now.weather.value in v
|
||||||
][0]
|
][0]
|
||||||
|
|
||||||
elif hasattr(self.coordinator.data.now, self._type):
|
elif hasattr(self.coordinator.data.now, self.entity_description.key):
|
||||||
value = getattr(self.coordinator.data.now, self._type)
|
value = getattr(self.coordinator.data.now, self.entity_description.key)
|
||||||
|
|
||||||
if hasattr(value, "value"):
|
if hasattr(value, "value"):
|
||||||
value = value.value
|
value = value.value
|
||||||
@ -229,8 +225,8 @@ class MetOfficeCurrentSensor(CoordinatorEntity, SensorEntity):
|
|||||||
@property
|
@property
|
||||||
def icon(self):
|
def icon(self):
|
||||||
"""Return the icon for the entity card."""
|
"""Return the icon for the entity card."""
|
||||||
value = self._metadata.icon
|
value = self.entity_description.icon
|
||||||
if self._type == "weather":
|
if self.entity_description.key == "weather":
|
||||||
value = self.state
|
value = self.state
|
||||||
if value is None:
|
if value is None:
|
||||||
value = "sunny"
|
value = "sunny"
|
||||||
@ -246,7 +242,7 @@ class MetOfficeCurrentSensor(CoordinatorEntity, SensorEntity):
|
|||||||
return {
|
return {
|
||||||
ATTR_ATTRIBUTION: ATTRIBUTION,
|
ATTR_ATTRIBUTION: ATTRIBUTION,
|
||||||
ATTR_LAST_UPDATE: self.coordinator.data.now.date,
|
ATTR_LAST_UPDATE: self.coordinator.data.now.date,
|
||||||
ATTR_SENSOR_ID: self._type,
|
ATTR_SENSOR_ID: self.entity_description.key,
|
||||||
ATTR_SITE_ID: self.coordinator.data.site.id,
|
ATTR_SITE_ID: self.coordinator.data.site.id,
|
||||||
ATTR_SITE_NAME: self.coordinator.data.site.name,
|
ATTR_SITE_NAME: self.coordinator.data.site.name,
|
||||||
}
|
}
|
||||||
@ -254,4 +250,6 @@ 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 self._metadata.enabled_by_default and self.use_3hourly
|
return (
|
||||||
|
self.entity_description.entity_registry_enabled_default and self.use_3hourly
|
||||||
|
)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user