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
This commit is contained in:
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, DEVICE_CLASS_VIBRATION,
DOMAIN, DOMAIN,
BinarySensorEntity, BinarySensorEntity,
BinarySensorEntityDescription,
) )
from homeassistant.const import ATTR_TEMPERATURE from homeassistant.const import ATTR_TEMPERATURE
from homeassistant.core import callback from homeassistant.core import callback
@ -24,13 +25,31 @@ ATTR_ORIENTATION = "orientation"
ATTR_TILTANGLE = "tiltangle" ATTR_TILTANGLE = "tiltangle"
ATTR_VIBRATIONSTRENGTH = "vibrationstrength" ATTR_VIBRATIONSTRENGTH = "vibrationstrength"
DEVICE_CLASS = { ENTITY_DESCRIPTIONS = {
CarbonMonoxide: DEVICE_CLASS_GAS, CarbonMonoxide: BinarySensorEntityDescription(
Fire: DEVICE_CLASS_SMOKE, key="carbonmonoxide",
OpenClose: DEVICE_CLASS_OPENING, device_class=DEVICE_CLASS_GAS,
Presence: DEVICE_CLASS_MOTION, ),
Vibration: DEVICE_CLASS_VIBRATION, Fire: BinarySensorEntityDescription(
Water: DEVICE_CLASS_MOISTURE, 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): def __init__(self, device, gateway):
"""Initialize deCONZ binary sensor.""" """Initialize deCONZ binary sensor."""
super().__init__(device, gateway) 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 @callback
def async_update_callback(self, force_update=False): def async_update_callback(self, force_update=False):

View File

@ -59,14 +59,6 @@ class DeconzDevice(DeconzBase, Entity):
self._attr_name = self._device.name 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): async def async_added_to_hass(self):
"""Subscribe to device events.""" """Subscribe to device events."""
self._device.register_callback(self.async_update_callback) self._device.register_callback(self.async_update_callback)

View File

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