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