Fix state_class for deCONZ power sensors (#56586)

* Fix state_class for power sensors
Rewrite entity descriptions for binary sensor and sensor platforms

* Remove icon if device_class is specified
pull/56661/head
Robert Svensson 2021-09-25 20:54:55 +02:00 committed by GitHub
parent d4ebcf2ba5
commit 8db0bd3c0e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 81 additions and 59 deletions

View File

@ -11,6 +11,7 @@ from homeassistant.components.binary_sensor import (
DEVICE_CLASS_VIBRATION,
DOMAIN,
BinarySensorEntity,
BinarySensorEntityDescription,
)
from homeassistant.const import ATTR_TEMPERATURE
from homeassistant.core import callback
@ -24,13 +25,31 @@ ATTR_ORIENTATION = "orientation"
ATTR_TILTANGLE = "tiltangle"
ATTR_VIBRATIONSTRENGTH = "vibrationstrength"
DEVICE_CLASS = {
CarbonMonoxide: DEVICE_CLASS_GAS,
Fire: DEVICE_CLASS_SMOKE,
OpenClose: DEVICE_CLASS_OPENING,
Presence: DEVICE_CLASS_MOTION,
Vibration: DEVICE_CLASS_VIBRATION,
Water: DEVICE_CLASS_MOISTURE,
ENTITY_DESCRIPTIONS = {
CarbonMonoxide: BinarySensorEntityDescription(
key="carbonmonoxide",
device_class=DEVICE_CLASS_GAS,
),
Fire: BinarySensorEntityDescription(
key="fire",
device_class=DEVICE_CLASS_SMOKE,
),
OpenClose: BinarySensorEntityDescription(
key="openclose",
device_class=DEVICE_CLASS_OPENING,
),
Presence: BinarySensorEntityDescription(
key="presence",
device_class=DEVICE_CLASS_MOTION,
),
Vibration: BinarySensorEntityDescription(
key="vibration",
device_class=DEVICE_CLASS_VIBRATION,
),
Water: BinarySensorEntityDescription(
key="water",
device_class=DEVICE_CLASS_MOISTURE,
),
}
@ -84,7 +103,9 @@ class DeconzBinarySensor(DeconzDevice, BinarySensorEntity):
def __init__(self, device, gateway):
"""Initialize deCONZ binary sensor."""
super().__init__(device, gateway)
self._attr_device_class = DEVICE_CLASS.get(type(self._device))
if entity_description := ENTITY_DESCRIPTIONS.get(type(device)):
self.entity_description = entity_description
@callback
def async_update_callback(self, force_update=False):

View File

@ -59,14 +59,6 @@ class DeconzDevice(DeconzBase, Entity):
self._attr_name = self._device.name
@property
def entity_registry_enabled_default(self) -> bool:
"""Return if the entity should be enabled when first added to the entity registry.
Daylight is a virtual sensor from deCONZ that should never be enabled by default.
"""
return self._device.type != "Daylight"
async def async_added_to_hass(self):
"""Subscribe to device events."""
self._device.register_callback(self.async_update_callback)

View File

@ -19,6 +19,7 @@ from homeassistant.components.sensor import (
STATE_CLASS_MEASUREMENT,
STATE_CLASS_TOTAL_INCREASING,
SensorEntity,
SensorEntityDescription,
)
from homeassistant.const import (
ATTR_TEMPERATURE,
@ -52,35 +53,53 @@ ATTR_POWER = "power"
ATTR_DAYLIGHT = "daylight"
ATTR_EVENT_ID = "event_id"
DEVICE_CLASS = {
Consumption: DEVICE_CLASS_ENERGY,
Humidity: DEVICE_CLASS_HUMIDITY,
LightLevel: DEVICE_CLASS_ILLUMINANCE,
Power: DEVICE_CLASS_POWER,
Pressure: DEVICE_CLASS_PRESSURE,
Temperature: DEVICE_CLASS_TEMPERATURE,
}
ICON = {
Daylight: "mdi:white-balance-sunny",
Pressure: "mdi:gauge",
Temperature: "mdi:thermometer",
}
STATE_CLASS = {
Consumption: STATE_CLASS_TOTAL_INCREASING,
Humidity: STATE_CLASS_MEASUREMENT,
Pressure: STATE_CLASS_MEASUREMENT,
Temperature: STATE_CLASS_MEASUREMENT,
}
UNIT_OF_MEASUREMENT = {
Consumption: ENERGY_KILO_WATT_HOUR,
Humidity: PERCENTAGE,
LightLevel: LIGHT_LUX,
Power: POWER_WATT,
Pressure: PRESSURE_HPA,
Temperature: TEMP_CELSIUS,
ENTITY_DESCRIPTIONS = {
Battery: SensorEntityDescription(
key="battery",
device_class=DEVICE_CLASS_BATTERY,
state_class=STATE_CLASS_MEASUREMENT,
unit_of_measurement=PERCENTAGE,
),
Consumption: SensorEntityDescription(
key="consumption",
device_class=DEVICE_CLASS_ENERGY,
state_class=STATE_CLASS_TOTAL_INCREASING,
unit_of_measurement=ENERGY_KILO_WATT_HOUR,
),
Daylight: SensorEntityDescription(
key="daylight",
icon="mdi:white-balance-sunny",
entity_registry_enabled_default=False,
),
Humidity: SensorEntityDescription(
key="humidity",
device_class=DEVICE_CLASS_HUMIDITY,
state_class=STATE_CLASS_MEASUREMENT,
unit_of_measurement=PERCENTAGE,
),
LightLevel: SensorEntityDescription(
key="lightlevel",
device_class=DEVICE_CLASS_ILLUMINANCE,
unit_of_measurement=LIGHT_LUX,
),
Power: SensorEntityDescription(
key="power",
device_class=DEVICE_CLASS_POWER,
state_class=STATE_CLASS_MEASUREMENT,
unit_of_measurement=POWER_WATT,
),
Pressure: SensorEntityDescription(
key="pressure",
device_class=DEVICE_CLASS_PRESSURE,
state_class=STATE_CLASS_MEASUREMENT,
unit_of_measurement=PRESSURE_HPA,
),
Temperature: SensorEntityDescription(
key="temperature",
device_class=DEVICE_CLASS_TEMPERATURE,
state_class=STATE_CLASS_MEASUREMENT,
unit_of_measurement=TEMP_CELSIUS,
),
}
@ -157,12 +176,8 @@ class DeconzSensor(DeconzDevice, SensorEntity):
"""Initialize deCONZ binary sensor."""
super().__init__(device, gateway)
self._attr_device_class = DEVICE_CLASS.get(type(self._device))
self._attr_icon = ICON.get(type(self._device))
self._attr_state_class = STATE_CLASS.get(type(self._device))
self._attr_native_unit_of_measurement = UNIT_OF_MEASUREMENT.get(
type(self._device)
)
if entity_description := ENTITY_DESCRIPTIONS.get(type(device)):
self.entity_description = entity_description
@callback
def async_update_callback(self, force_update=False):
@ -214,16 +229,13 @@ class DeconzTemperature(DeconzDevice, SensorEntity):
Extra temperature sensor on certain Xiaomi devices.
"""
_attr_device_class = DEVICE_CLASS_TEMPERATURE
_attr_state_class = STATE_CLASS_MEASUREMENT
_attr_native_unit_of_measurement = TEMP_CELSIUS
TYPE = DOMAIN
def __init__(self, device, gateway):
"""Initialize deCONZ temperature sensor."""
super().__init__(device, gateway)
self.entity_description = ENTITY_DESCRIPTIONS[Temperature]
self._attr_name = f"{self._device.name} Temperature"
@property
@ -247,16 +259,13 @@ class DeconzTemperature(DeconzDevice, SensorEntity):
class DeconzBattery(DeconzDevice, SensorEntity):
"""Battery class for when a device is only represented as an event."""
_attr_device_class = DEVICE_CLASS_BATTERY
_attr_state_class = STATE_CLASS_MEASUREMENT
_attr_native_unit_of_measurement = PERCENTAGE
TYPE = DOMAIN
def __init__(self, device, gateway):
"""Initialize deCONZ battery level sensor."""
super().__init__(device, gateway)
self.entity_description = ENTITY_DESCRIPTIONS[Battery]
self._attr_name = f"{self._device.name} Battery Level"
@callback