mirror of https://github.com/home-assistant/core
Use EntityDescription - meteoclimatic (#55792)
This commit is contained in:
parent
96db04213b
commit
a4dae0c1e1
|
@ -1,9 +1,11 @@
|
|||
"""Meteoclimatic component constants."""
|
||||
from __future__ import annotations
|
||||
|
||||
from datetime import timedelta
|
||||
|
||||
from meteoclimatic import Condition
|
||||
|
||||
from homeassistant.components.sensor import SensorEntityDescription
|
||||
from homeassistant.components.weather import (
|
||||
ATTR_CONDITION_CLEAR_NIGHT,
|
||||
ATTR_CONDITION_CLOUDY,
|
||||
|
@ -45,77 +47,86 @@ CONF_STATION_CODE = "station_code"
|
|||
|
||||
DEFAULT_WEATHER_CARD = True
|
||||
|
||||
SENSOR_TYPE_NAME = "name"
|
||||
SENSOR_TYPE_UNIT = "unit"
|
||||
SENSOR_TYPE_ICON = "icon"
|
||||
SENSOR_TYPE_CLASS = "device_class"
|
||||
SENSOR_TYPES = {
|
||||
"temp_current": {
|
||||
SENSOR_TYPE_NAME: "Temperature",
|
||||
SENSOR_TYPE_UNIT: TEMP_CELSIUS,
|
||||
SENSOR_TYPE_CLASS: DEVICE_CLASS_TEMPERATURE,
|
||||
},
|
||||
"temp_max": {
|
||||
SENSOR_TYPE_NAME: "Daily Max Temperature",
|
||||
SENSOR_TYPE_UNIT: TEMP_CELSIUS,
|
||||
SENSOR_TYPE_CLASS: DEVICE_CLASS_TEMPERATURE,
|
||||
},
|
||||
"temp_min": {
|
||||
SENSOR_TYPE_NAME: "Daily Min Temperature",
|
||||
SENSOR_TYPE_UNIT: TEMP_CELSIUS,
|
||||
SENSOR_TYPE_CLASS: DEVICE_CLASS_TEMPERATURE,
|
||||
},
|
||||
"humidity_current": {
|
||||
SENSOR_TYPE_NAME: "Humidity",
|
||||
SENSOR_TYPE_UNIT: PERCENTAGE,
|
||||
SENSOR_TYPE_CLASS: DEVICE_CLASS_HUMIDITY,
|
||||
},
|
||||
"humidity_max": {
|
||||
SENSOR_TYPE_NAME: "Daily Max Humidity",
|
||||
SENSOR_TYPE_UNIT: PERCENTAGE,
|
||||
SENSOR_TYPE_CLASS: DEVICE_CLASS_HUMIDITY,
|
||||
},
|
||||
"humidity_min": {
|
||||
SENSOR_TYPE_NAME: "Daily Min Humidity",
|
||||
SENSOR_TYPE_UNIT: PERCENTAGE,
|
||||
SENSOR_TYPE_CLASS: DEVICE_CLASS_HUMIDITY,
|
||||
},
|
||||
"pressure_current": {
|
||||
SENSOR_TYPE_NAME: "Pressure",
|
||||
SENSOR_TYPE_UNIT: PRESSURE_HPA,
|
||||
SENSOR_TYPE_CLASS: DEVICE_CLASS_PRESSURE,
|
||||
},
|
||||
"pressure_max": {
|
||||
SENSOR_TYPE_NAME: "Daily Max Pressure",
|
||||
SENSOR_TYPE_UNIT: PRESSURE_HPA,
|
||||
SENSOR_TYPE_CLASS: DEVICE_CLASS_PRESSURE,
|
||||
},
|
||||
"pressure_min": {
|
||||
SENSOR_TYPE_NAME: "Daily Min Pressure",
|
||||
SENSOR_TYPE_UNIT: PRESSURE_HPA,
|
||||
SENSOR_TYPE_CLASS: DEVICE_CLASS_PRESSURE,
|
||||
},
|
||||
"wind_current": {
|
||||
SENSOR_TYPE_NAME: "Wind Speed",
|
||||
SENSOR_TYPE_UNIT: SPEED_KILOMETERS_PER_HOUR,
|
||||
SENSOR_TYPE_ICON: "mdi:weather-windy",
|
||||
},
|
||||
"wind_max": {
|
||||
SENSOR_TYPE_NAME: "Daily Max Wind Speed",
|
||||
SENSOR_TYPE_UNIT: SPEED_KILOMETERS_PER_HOUR,
|
||||
SENSOR_TYPE_ICON: "mdi:weather-windy",
|
||||
},
|
||||
"wind_bearing": {
|
||||
SENSOR_TYPE_NAME: "Wind Bearing",
|
||||
SENSOR_TYPE_UNIT: DEGREE,
|
||||
SENSOR_TYPE_ICON: "mdi:weather-windy",
|
||||
},
|
||||
"rain": {
|
||||
SENSOR_TYPE_NAME: "Daily Precipitation",
|
||||
SENSOR_TYPE_UNIT: LENGTH_MILLIMETERS,
|
||||
SENSOR_TYPE_ICON: "mdi:cup-water",
|
||||
},
|
||||
}
|
||||
SENSOR_TYPES: tuple[SensorEntityDescription, ...] = (
|
||||
SensorEntityDescription(
|
||||
key="temp_current",
|
||||
name="Temperature",
|
||||
native_unit_of_measurement=TEMP_CELSIUS,
|
||||
device_class=DEVICE_CLASS_TEMPERATURE,
|
||||
),
|
||||
SensorEntityDescription(
|
||||
key="temp_max",
|
||||
name="Daily Max Temperature",
|
||||
native_unit_of_measurement=TEMP_CELSIUS,
|
||||
device_class=DEVICE_CLASS_TEMPERATURE,
|
||||
),
|
||||
SensorEntityDescription(
|
||||
key="temp_min",
|
||||
name="Daily Min Temperature",
|
||||
native_unit_of_measurement=TEMP_CELSIUS,
|
||||
device_class=DEVICE_CLASS_TEMPERATURE,
|
||||
),
|
||||
SensorEntityDescription(
|
||||
key="humidity_current",
|
||||
name="Humidity",
|
||||
native_unit_of_measurement=PERCENTAGE,
|
||||
device_class=DEVICE_CLASS_HUMIDITY,
|
||||
),
|
||||
SensorEntityDescription(
|
||||
key="humidity_max",
|
||||
name="Daily Max Humidity",
|
||||
native_unit_of_measurement=PERCENTAGE,
|
||||
device_class=DEVICE_CLASS_HUMIDITY,
|
||||
),
|
||||
SensorEntityDescription(
|
||||
key="humidity_min",
|
||||
name="Daily Min Humidity",
|
||||
native_unit_of_measurement=PERCENTAGE,
|
||||
device_class=DEVICE_CLASS_HUMIDITY,
|
||||
),
|
||||
SensorEntityDescription(
|
||||
key="pressure_current",
|
||||
name="Pressure",
|
||||
native_unit_of_measurement=PRESSURE_HPA,
|
||||
device_class=DEVICE_CLASS_PRESSURE,
|
||||
),
|
||||
SensorEntityDescription(
|
||||
key="pressure_max",
|
||||
name="Daily Max Pressure",
|
||||
native_unit_of_measurement=PRESSURE_HPA,
|
||||
device_class=DEVICE_CLASS_PRESSURE,
|
||||
),
|
||||
SensorEntityDescription(
|
||||
key="pressure_min",
|
||||
name="Daily Min Pressure",
|
||||
native_unit_of_measurement=PRESSURE_HPA,
|
||||
device_class=DEVICE_CLASS_PRESSURE,
|
||||
),
|
||||
SensorEntityDescription(
|
||||
key="wind_current",
|
||||
name="Wind Speed",
|
||||
native_unit_of_measurement=SPEED_KILOMETERS_PER_HOUR,
|
||||
device_class="mdi:weather-windy",
|
||||
),
|
||||
SensorEntityDescription(
|
||||
key="wind_max",
|
||||
name="Daily Max Wind Speed",
|
||||
native_unit_of_measurement=SPEED_KILOMETERS_PER_HOUR,
|
||||
device_class="mdi:weather-windy",
|
||||
),
|
||||
SensorEntityDescription(
|
||||
key="wind_bearing",
|
||||
name="Wind Bearing",
|
||||
native_unit_of_measurement=DEGREE,
|
||||
device_class="mdi:weather-windy",
|
||||
),
|
||||
SensorEntityDescription(
|
||||
key="rain",
|
||||
name="Daily Precipitation",
|
||||
native_unit_of_measurement=LENGTH_MILLIMETERS,
|
||||
device_class="mdi:cup-water",
|
||||
),
|
||||
)
|
||||
|
||||
CONDITION_CLASSES = {
|
||||
ATTR_CONDITION_CLEAR_NIGHT: [Condition.moon, Condition.hazemoon],
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
"""Support for Meteoclimatic sensor."""
|
||||
import logging
|
||||
|
||||
from homeassistant.components.sensor import SensorEntity
|
||||
from homeassistant.components.sensor import SensorEntity, SensorEntityDescription
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.const import ATTR_ATTRIBUTION
|
||||
from homeassistant.core import HomeAssistant
|
||||
|
@ -10,17 +10,7 @@ from homeassistant.helpers.update_coordinator import (
|
|||
DataUpdateCoordinator,
|
||||
)
|
||||
|
||||
from .const import (
|
||||
ATTRIBUTION,
|
||||
DOMAIN,
|
||||
MANUFACTURER,
|
||||
MODEL,
|
||||
SENSOR_TYPE_CLASS,
|
||||
SENSOR_TYPE_ICON,
|
||||
SENSOR_TYPE_NAME,
|
||||
SENSOR_TYPE_UNIT,
|
||||
SENSOR_TYPES,
|
||||
)
|
||||
from .const import ATTRIBUTION, DOMAIN, MANUFACTURER, MODEL, SENSOR_TYPES
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
|
@ -32,7 +22,7 @@ async def async_setup_entry(
|
|||
coordinator = hass.data[DOMAIN][entry.entry_id]
|
||||
|
||||
async_add_entities(
|
||||
[MeteoclimaticSensor(sensor_type, coordinator) for sensor_type in SENSOR_TYPES],
|
||||
[MeteoclimaticSensor(coordinator, description) for description in SENSOR_TYPES],
|
||||
False,
|
||||
)
|
||||
|
||||
|
@ -40,20 +30,17 @@ async def async_setup_entry(
|
|||
class MeteoclimaticSensor(CoordinatorEntity, SensorEntity):
|
||||
"""Representation of a Meteoclimatic sensor."""
|
||||
|
||||
def __init__(self, sensor_type: str, coordinator: DataUpdateCoordinator) -> None:
|
||||
_attr_extra_state_attributes = {ATTR_ATTRIBUTION: ATTRIBUTION}
|
||||
|
||||
def __init__(
|
||||
self, coordinator: DataUpdateCoordinator, description: SensorEntityDescription
|
||||
) -> None:
|
||||
"""Initialize the Meteoclimatic sensor."""
|
||||
super().__init__(coordinator)
|
||||
self._type = sensor_type
|
||||
self.entity_description = description
|
||||
station = self.coordinator.data["station"]
|
||||
self._attr_device_class = SENSOR_TYPES[sensor_type].get(SENSOR_TYPE_CLASS)
|
||||
self._attr_icon = SENSOR_TYPES[sensor_type].get(SENSOR_TYPE_ICON)
|
||||
self._attr_name = (
|
||||
f"{station.name} {SENSOR_TYPES[sensor_type][SENSOR_TYPE_NAME]}"
|
||||
)
|
||||
self._attr_unique_id = f"{station.code}_{sensor_type}"
|
||||
self._attr_native_unit_of_measurement = SENSOR_TYPES[sensor_type].get(
|
||||
SENSOR_TYPE_UNIT
|
||||
)
|
||||
self._attr_name = f"{station.name} {description.name}"
|
||||
self._attr_unique_id = f"{station.code}_{description.key}"
|
||||
|
||||
@property
|
||||
def device_info(self):
|
||||
|
@ -70,12 +57,7 @@ class MeteoclimaticSensor(CoordinatorEntity, SensorEntity):
|
|||
def native_value(self):
|
||||
"""Return the state of the sensor."""
|
||||
return (
|
||||
getattr(self.coordinator.data["weather"], self._type)
|
||||
getattr(self.coordinator.data["weather"], self.entity_description.key)
|
||||
if self.coordinator.data
|
||||
else None
|
||||
)
|
||||
|
||||
@property
|
||||
def extra_state_attributes(self):
|
||||
"""Return the state attributes."""
|
||||
return {ATTR_ATTRIBUTION: ATTRIBUTION}
|
||||
|
|
Loading…
Reference in New Issue