Use EntityDescription - tellduslive (#55928)

This commit is contained in:
Marc Mueller 2021-09-27 12:28:58 +02:00 committed by GitHub
parent f2debf5c01
commit 83b1b3e92c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,6 +1,8 @@
"""Support for Tellstick Net/Telstick Live sensors.""" """Support for Tellstick Net/Telstick Live sensors."""
from __future__ import annotations
from homeassistant.components import sensor, tellduslive from homeassistant.components import sensor, tellduslive
from homeassistant.components.sensor import SensorEntity from homeassistant.components.sensor import SensorEntity, SensorEntityDescription
from homeassistant.const import ( from homeassistant.const import (
DEVICE_CLASS_HUMIDITY, DEVICE_CLASS_HUMIDITY,
DEVICE_CLASS_ILLUMINANCE, DEVICE_CLASS_ILLUMINANCE,
@ -31,29 +33,72 @@ SENSOR_TYPE_LUMINANCE = "lum"
SENSOR_TYPE_DEW_POINT = "dewp" SENSOR_TYPE_DEW_POINT = "dewp"
SENSOR_TYPE_BAROMETRIC_PRESSURE = "barpress" SENSOR_TYPE_BAROMETRIC_PRESSURE = "barpress"
SENSOR_TYPES = { SENSOR_TYPES: dict[str, SensorEntityDescription] = {
SENSOR_TYPE_TEMPERATURE: [ SENSOR_TYPE_TEMPERATURE: SensorEntityDescription(
"Temperature", key=SENSOR_TYPE_TEMPERATURE,
TEMP_CELSIUS, name="Temperature",
None, native_unit_of_measurement=TEMP_CELSIUS,
DEVICE_CLASS_TEMPERATURE, device_class=DEVICE_CLASS_TEMPERATURE,
], ),
SENSOR_TYPE_HUMIDITY: ["Humidity", PERCENTAGE, None, DEVICE_CLASS_HUMIDITY], SENSOR_TYPE_HUMIDITY: SensorEntityDescription(
SENSOR_TYPE_RAINRATE: [ key=SENSOR_TYPE_HUMIDITY,
"Rain rate", name="Humidity",
PRECIPITATION_MILLIMETERS_PER_HOUR, native_unit_of_measurement=PERCENTAGE,
"mdi:water", device_class=DEVICE_CLASS_HUMIDITY,
None, ),
], SENSOR_TYPE_RAINRATE: SensorEntityDescription(
SENSOR_TYPE_RAINTOTAL: ["Rain total", LENGTH_MILLIMETERS, "mdi:water", None], key=SENSOR_TYPE_RAINRATE,
SENSOR_TYPE_WINDDIRECTION: ["Wind direction", "", "", None], name="Rain rate",
SENSOR_TYPE_WINDAVERAGE: ["Wind average", SPEED_METERS_PER_SECOND, "", None], native_unit_of_measurement=PRECIPITATION_MILLIMETERS_PER_HOUR,
SENSOR_TYPE_WINDGUST: ["Wind gust", SPEED_METERS_PER_SECOND, "", None], icon="mdi:water",
SENSOR_TYPE_UV: ["UV", UV_INDEX, "", None], ),
SENSOR_TYPE_WATT: ["Power", POWER_WATT, "", None], SENSOR_TYPE_RAINTOTAL: SensorEntityDescription(
SENSOR_TYPE_LUMINANCE: ["Luminance", LIGHT_LUX, None, DEVICE_CLASS_ILLUMINANCE], key=SENSOR_TYPE_RAINTOTAL,
SENSOR_TYPE_DEW_POINT: ["Dew Point", TEMP_CELSIUS, None, DEVICE_CLASS_TEMPERATURE], name="Rain total",
SENSOR_TYPE_BAROMETRIC_PRESSURE: ["Barometric Pressure", "kPa", "", None], native_unit_of_measurement=LENGTH_MILLIMETERS,
icon="mdi:water",
),
SENSOR_TYPE_WINDDIRECTION: SensorEntityDescription(
key=SENSOR_TYPE_WINDDIRECTION,
name="Wind direction",
),
SENSOR_TYPE_WINDAVERAGE: SensorEntityDescription(
key=SENSOR_TYPE_WINDAVERAGE,
name="Wind average",
native_unit_of_measurement=SPEED_METERS_PER_SECOND,
),
SENSOR_TYPE_WINDGUST: SensorEntityDescription(
key=SENSOR_TYPE_WINDGUST,
name="Wind gust",
native_unit_of_measurement=SPEED_METERS_PER_SECOND,
),
SENSOR_TYPE_UV: SensorEntityDescription(
key=SENSOR_TYPE_UV,
name="UV",
native_unit_of_measurement=UV_INDEX,
),
SENSOR_TYPE_WATT: SensorEntityDescription(
key=SENSOR_TYPE_WATT,
name="Power",
native_unit_of_measurement=POWER_WATT,
),
SENSOR_TYPE_LUMINANCE: SensorEntityDescription(
key=SENSOR_TYPE_LUMINANCE,
name="Luminance",
native_unit_of_measurement=LIGHT_LUX,
device_class=DEVICE_CLASS_ILLUMINANCE,
),
SENSOR_TYPE_DEW_POINT: SensorEntityDescription(
key=SENSOR_TYPE_DEW_POINT,
name="Dew Point",
native_unit_of_measurement=TEMP_CELSIUS,
device_class=DEVICE_CLASS_TEMPERATURE,
),
SENSOR_TYPE_BAROMETRIC_PRESSURE: SensorEntityDescription(
key=SENSOR_TYPE_BAROMETRIC_PRESSURE,
name="Barometric Pressure",
native_unit_of_measurement="kPa",
),
} }
@ -75,6 +120,12 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
class TelldusLiveSensor(TelldusLiveEntity, SensorEntity): class TelldusLiveSensor(TelldusLiveEntity, SensorEntity):
"""Representation of a Telldus Live sensor.""" """Representation of a Telldus Live sensor."""
def __init__(self, client, device_id):
"""Initialize TelldusLiveSensor."""
super().__init__(client, device_id)
if desc := SENSOR_TYPES.get(self._type):
self.entity_description = desc
@property @property
def device_id(self): def device_id(self):
"""Return id of the device.""" """Return id of the device."""
@ -108,7 +159,10 @@ class TelldusLiveSensor(TelldusLiveEntity, SensorEntity):
@property @property
def name(self): def name(self):
"""Return the name of the sensor.""" """Return the name of the sensor."""
return "{} {}".format(super().name, self.quantity_name or "").strip() quantity_name = (
self.entity_description.name if hasattr(self, "entity_description") else ""
)
return "{} {}".format(super().name, quantity_name or "").strip()
@property @property
def native_value(self): def native_value(self):
@ -123,26 +177,6 @@ class TelldusLiveSensor(TelldusLiveEntity, SensorEntity):
return self._value_as_luminance return self._value_as_luminance
return self._value return self._value
@property
def quantity_name(self):
"""Name of quantity."""
return SENSOR_TYPES[self._type][0] if self._type in SENSOR_TYPES else None
@property
def native_unit_of_measurement(self):
"""Return the unit of measurement."""
return SENSOR_TYPES[self._type][1] if self._type in SENSOR_TYPES else None
@property
def icon(self):
"""Return the icon."""
return SENSOR_TYPES[self._type][2] if self._type in SENSOR_TYPES else None
@property
def device_class(self):
"""Return the device class."""
return SENSOR_TYPES[self._type][3] if self._type in SENSOR_TYPES else None
@property @property
def unique_id(self) -> str: def unique_id(self) -> str:
"""Return a unique ID.""" """Return a unique ID."""