Clean up state class and device class usage in ZHA (#61049)

* Clean up sensor and device class usage in ZHA

* additional cleanup

* Use EntityCategory
This commit is contained in:
David F. Mulcahey 2021-12-11 11:51:24 -05:00 committed by GitHub
parent f6ac856b8d
commit abbde8f128
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 52 additions and 81 deletions

View File

@ -2,14 +2,7 @@
import functools
from homeassistant.components.binary_sensor import (
DEVICE_CLASS_GAS,
DEVICE_CLASS_MOISTURE,
DEVICE_CLASS_MOTION,
DEVICE_CLASS_MOVING,
DEVICE_CLASS_OCCUPANCY,
DEVICE_CLASS_OPENING,
DEVICE_CLASS_SMOKE,
DEVICE_CLASS_VIBRATION,
BinarySensorDeviceClass,
BinarySensorEntity,
)
from homeassistant.config_entries import ConfigEntry
@ -34,12 +27,12 @@ from .entity import ZhaEntity
# Zigbee Cluster Library Zone Type to Home Assistant device class
CLASS_MAPPING = {
0x000D: DEVICE_CLASS_MOTION,
0x0015: DEVICE_CLASS_OPENING,
0x0028: DEVICE_CLASS_SMOKE,
0x002A: DEVICE_CLASS_MOISTURE,
0x002B: DEVICE_CLASS_GAS,
0x002D: DEVICE_CLASS_VIBRATION,
0x000D: BinarySensorDeviceClass.MOTION,
0x0015: BinarySensorDeviceClass.OPENING,
0x0028: BinarySensorDeviceClass.SMOKE,
0x002A: BinarySensorDeviceClass.MOISTURE,
0x002B: BinarySensorDeviceClass.GAS,
0x002D: BinarySensorDeviceClass.VIBRATION,
}
STRICT_MATCH = functools.partial(ZHA_ENTITIES.strict_match, Platform.BINARY_SENSOR)
@ -67,13 +60,11 @@ class BinarySensor(ZhaEntity, BinarySensorEntity):
"""ZHA BinarySensor."""
SENSOR_ATTR = None
DEVICE_CLASS = None
def __init__(self, unique_id, zha_device, channels, **kwargs):
"""Initialize the ZHA binary sensor."""
super().__init__(unique_id, zha_device, channels, **kwargs)
self._channel = channels[0]
self._device_class = self.DEVICE_CLASS
async def async_added_to_hass(self):
"""Run when about to be added to hass."""
@ -95,11 +86,6 @@ class BinarySensor(ZhaEntity, BinarySensorEntity):
return False
return self._state
@property
def device_class(self) -> str:
"""Return device class from component DEVICE_CLASSES."""
return self._device_class
@callback
def async_set_state(self, attr_id, attr_name, value):
"""Set the state."""
@ -122,7 +108,7 @@ class Accelerometer(BinarySensor):
"""ZHA BinarySensor."""
SENSOR_ATTR = "acceleration"
DEVICE_CLASS = DEVICE_CLASS_MOVING
_attr_device_class: BinarySensorDeviceClass = BinarySensorDeviceClass.MOVING
@STRICT_MATCH(channel_names=CHANNEL_OCCUPANCY)
@ -130,7 +116,7 @@ class Occupancy(BinarySensor):
"""ZHA BinarySensor."""
SENSOR_ATTR = "occupancy"
DEVICE_CLASS = DEVICE_CLASS_OCCUPANCY
_attr_device_class: BinarySensorDeviceClass = BinarySensorDeviceClass.OCCUPANCY
@STRICT_MATCH(channel_names=CHANNEL_ON_OFF)
@ -138,7 +124,7 @@ class Opening(BinarySensor):
"""ZHA BinarySensor."""
SENSOR_ATTR = "on_off"
DEVICE_CLASS = DEVICE_CLASS_OPENING
_attr_device_class: BinarySensorDeviceClass = BinarySensorDeviceClass.OPENING
@STRICT_MATCH(channel_names=CHANNEL_BINARY_INPUT)
@ -164,7 +150,7 @@ class Motion(BinarySensor):
"""ZHA BinarySensor."""
SENSOR_ATTR = "on_off"
DEVICE_CLASS = DEVICE_CLASS_MOTION
_attr_device_class: BinarySensorDeviceClass = BinarySensorDeviceClass.MOTION
@STRICT_MATCH(channel_names=CHANNEL_ZONE)

View File

@ -10,8 +10,7 @@ from zigpy.zcl.foundation import Status
from homeassistant.components.cover import (
ATTR_CURRENT_POSITION,
ATTR_POSITION,
DEVICE_CLASS_DAMPER,
DEVICE_CLASS_SHADE,
CoverDeviceClass,
CoverEntity,
)
from homeassistant.config_entries import ConfigEntry
@ -187,7 +186,7 @@ class ZhaCover(ZhaEntity, CoverEntity):
class Shade(ZhaEntity, CoverEntity):
"""ZHA Shade."""
_attr_device_class = DEVICE_CLASS_SHADE
_attr_device_class = CoverDeviceClass.SHADE
def __init__(
self,
@ -296,7 +295,7 @@ class Shade(ZhaEntity, CoverEntity):
class KeenVent(Shade):
"""Keen vent cover."""
_attr_device_class = DEVICE_CLASS_DAMPER
_attr_device_class = CoverDeviceClass.DAMPER
async def async_open_cover(self, **kwargs):
"""Open the cover."""

View File

@ -13,29 +13,18 @@ from homeassistant.components.climate.const import (
CURRENT_HVAC_OFF,
)
from homeassistant.components.sensor import (
DEVICE_CLASS_BATTERY,
DEVICE_CLASS_CO,
DEVICE_CLASS_CO2,
DEVICE_CLASS_CURRENT,
DEVICE_CLASS_HUMIDITY,
DEVICE_CLASS_ILLUMINANCE,
DEVICE_CLASS_POWER,
DEVICE_CLASS_PRESSURE,
DEVICE_CLASS_TEMPERATURE,
STATE_CLASS_MEASUREMENT,
STATE_CLASS_TOTAL_INCREASING,
SensorDeviceClass,
SensorEntity,
SensorStateClass,
)
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import (
CONCENTRATION_MICROGRAMS_PER_CUBIC_METER,
CONCENTRATION_PARTS_PER_BILLION,
CONCENTRATION_PARTS_PER_MILLION,
DEVICE_CLASS_ENERGY,
ELECTRIC_CURRENT_AMPERE,
ELECTRIC_POTENTIAL_VOLT,
ENERGY_KILO_WATT_HOUR,
ENTITY_CATEGORY_DIAGNOSTIC,
LIGHT_LUX,
PERCENTAGE,
POWER_VOLT_AMPERE,
@ -54,6 +43,7 @@ from homeassistant.const import (
)
from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers.dispatcher import async_dispatcher_connect
from homeassistant.helpers.entity import EntityCategory
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.typing import StateType
@ -128,10 +118,8 @@ class Sensor(ZhaEntity, SensorEntity):
SENSOR_ATTR: int | str | None = None
_decimals: int = 1
_device_class: str | None = None
_divisor: int = 1
_multiplier: int = 1
_state_class: str | None = None
_unit: str | None = None
def __init__(
@ -170,16 +158,6 @@ class Sensor(ZhaEntity, SensorEntity):
self._channel, SIGNAL_ATTR_UPDATED, self.async_set_state
)
@property
def device_class(self) -> str:
"""Return device class from component DEVICE_CLASSES."""
return self._device_class
@property
def state_class(self) -> str | None:
"""Return the state class of this entity, from STATE_CLASSES, if any."""
return self._state_class
@property
def native_unit_of_measurement(self) -> str | None:
"""Return the unit of measurement of this entity."""
@ -225,10 +203,10 @@ class Battery(Sensor):
"""Battery sensor of power configuration cluster."""
SENSOR_ATTR = "battery_percentage_remaining"
_device_class = DEVICE_CLASS_BATTERY
_state_class = STATE_CLASS_MEASUREMENT
_attr_device_class: SensorDeviceClass = SensorDeviceClass.BATTERY
_attr_state_class: SensorStateClass = SensorStateClass.MEASUREMENT
_unit = PERCENTAGE
_attr_entity_category = ENTITY_CATEGORY_DIAGNOSTIC
_attr_entity_category = EntityCategory.DIAGNOSTIC
@classmethod
def create_entity(
@ -276,8 +254,8 @@ class ElectricalMeasurement(Sensor):
"""Active power measurement."""
SENSOR_ATTR = "active_power"
_device_class = DEVICE_CLASS_POWER
_state_class = STATE_CLASS_MEASUREMENT
_attr_device_class: SensorDeviceClass = SensorDeviceClass.POWER
_attr_state_class: SensorStateClass = SensorStateClass.MEASUREMENT
_unit = POWER_WATT
_div_mul_prefix = "ac_power"
@ -322,7 +300,6 @@ class ElectricalMeasurementApparentPower(
"""Apparent power measurement."""
SENSOR_ATTR = "apparent_power"
_device_class = DEVICE_CLASS_POWER
_unit = POWER_VOLT_AMPERE
_div_mul_prefix = "ac_power"
@ -337,7 +314,7 @@ class ElectricalMeasurementRMSCurrent(ElectricalMeasurement, id_suffix="rms_curr
"""RMS current measurement."""
SENSOR_ATTR = "rms_current"
_device_class = DEVICE_CLASS_CURRENT
_attr_device_class: SensorDeviceClass = SensorDeviceClass.CURRENT
_unit = ELECTRIC_CURRENT_AMPERE
_div_mul_prefix = "ac_current"
@ -352,7 +329,7 @@ class ElectricalMeasurementRMSVoltage(ElectricalMeasurement, id_suffix="rms_volt
"""RMS Voltage measurement."""
SENSOR_ATTR = "rms_voltage"
_device_class = DEVICE_CLASS_CURRENT
_attr_device_class: SensorDeviceClass = SensorDeviceClass.CURRENT
_unit = ELECTRIC_POTENTIAL_VOLT
_div_mul_prefix = "ac_voltage"
@ -368,9 +345,9 @@ class Humidity(Sensor):
"""Humidity sensor."""
SENSOR_ATTR = "measured_value"
_device_class = DEVICE_CLASS_HUMIDITY
_attr_device_class: SensorDeviceClass = SensorDeviceClass.HUMIDITY
_attr_state_class: SensorStateClass = SensorStateClass.MEASUREMENT
_divisor = 100
_state_class = STATE_CLASS_MEASUREMENT
_unit = PERCENTAGE
@ -379,9 +356,9 @@ class SoilMoisture(Sensor):
"""Soil Moisture sensor."""
SENSOR_ATTR = "measured_value"
_device_class = DEVICE_CLASS_HUMIDITY
_attr_device_class: SensorDeviceClass = SensorDeviceClass.HUMIDITY
_attr_state_class: SensorStateClass = SensorStateClass.MEASUREMENT
_divisor = 100
_state_class = STATE_CLASS_MEASUREMENT
_unit = PERCENTAGE
@ -390,9 +367,9 @@ class LeafWetness(Sensor):
"""Leaf Wetness sensor."""
SENSOR_ATTR = "measured_value"
_device_class = DEVICE_CLASS_HUMIDITY
_attr_device_class: SensorDeviceClass = SensorDeviceClass.HUMIDITY
_attr_state_class: SensorStateClass = SensorStateClass.MEASUREMENT
_divisor = 100
_state_class = STATE_CLASS_MEASUREMENT
_unit = PERCENTAGE
@ -401,7 +378,8 @@ class Illuminance(Sensor):
"""Illuminance Sensor."""
SENSOR_ATTR = "measured_value"
_device_class = DEVICE_CLASS_ILLUMINANCE
_attr_device_class: SensorDeviceClass = SensorDeviceClass.ILLUMINANCE
_attr_state_class: SensorStateClass = SensorStateClass.MEASUREMENT
_unit = LIGHT_LUX
@staticmethod
@ -415,8 +393,8 @@ class SmartEnergyMetering(Sensor):
"""Metering sensor."""
SENSOR_ATTR: int | str = "instantaneous_demand"
_device_class: str | None = DEVICE_CLASS_POWER
_state_class: str | None = STATE_CLASS_MEASUREMENT
_attr_device_class: SensorDeviceClass = SensorDeviceClass.POWER
_attr_state_class: SensorStateClass = SensorStateClass.MEASUREMENT
unit_of_measure_map = {
0x00: POWER_WATT,
@ -459,8 +437,8 @@ class SmartEnergySummation(SmartEnergyMetering, id_suffix="summation_delivered")
"""Smart Energy Metering summation sensor."""
SENSOR_ATTR: int | str = "current_summ_delivered"
_device_class: str | None = DEVICE_CLASS_ENERGY
_state_class: str = STATE_CLASS_TOTAL_INCREASING
_attr_device_class: SensorDeviceClass = SensorDeviceClass.ENERGY
_attr_state_class: SensorStateClass = SensorStateClass.TOTAL_INCREASING
unit_of_measure_map = {
0x00: ENERGY_KILO_WATT_HOUR,
@ -492,9 +470,9 @@ class Pressure(Sensor):
"""Pressure sensor."""
SENSOR_ATTR = "measured_value"
_device_class = DEVICE_CLASS_PRESSURE
_attr_device_class: SensorDeviceClass = SensorDeviceClass.PRESSURE
_attr_state_class: SensorStateClass = SensorStateClass.MEASUREMENT
_decimals = 0
_state_class = STATE_CLASS_MEASUREMENT
_unit = PRESSURE_HPA
@ -503,9 +481,9 @@ class Temperature(Sensor):
"""Temperature Sensor."""
SENSOR_ATTR = "measured_value"
_device_class = DEVICE_CLASS_TEMPERATURE
_attr_device_class: SensorDeviceClass = SensorDeviceClass.TEMPERATURE
_attr_state_class: SensorStateClass = SensorStateClass.MEASUREMENT
_divisor = 100
_state_class = STATE_CLASS_MEASUREMENT
_unit = TEMP_CELSIUS
@ -514,7 +492,8 @@ class CarbonDioxideConcentration(Sensor):
"""Carbon Dioxide Concentration sensor."""
SENSOR_ATTR = "measured_value"
_device_class = DEVICE_CLASS_CO2
_attr_device_class: SensorDeviceClass = SensorDeviceClass.CO2
_attr_state_class: SensorStateClass = SensorStateClass.MEASUREMENT
_decimals = 0
_multiplier = 1e6
_unit = CONCENTRATION_PARTS_PER_MILLION
@ -525,7 +504,8 @@ class CarbonMonoxideConcentration(Sensor):
"""Carbon Monoxide Concentration sensor."""
SENSOR_ATTR = "measured_value"
_device_class = DEVICE_CLASS_CO
_attr_device_class: SensorDeviceClass = SensorDeviceClass.CO
_attr_state_class: SensorStateClass = SensorStateClass.MEASUREMENT
_decimals = 0
_multiplier = 1e6
_unit = CONCENTRATION_PARTS_PER_MILLION
@ -537,6 +517,8 @@ class VOCLevel(Sensor):
"""VOC Level sensor."""
SENSOR_ATTR = "measured_value"
_attr_device_class: SensorDeviceClass = SensorDeviceClass.VOLATILE_ORGANIC_COMPOUNDS
_attr_state_class: SensorStateClass = SensorStateClass.MEASUREMENT
_decimals = 0
_multiplier = 1e6
_unit = CONCENTRATION_MICROGRAMS_PER_CUBIC_METER
@ -547,6 +529,8 @@ class PPBVOCLevel(Sensor):
"""VOC Level sensor."""
SENSOR_ATTR = "measured_value"
_attr_device_class: SensorDeviceClass = SensorDeviceClass.VOLATILE_ORGANIC_COMPOUNDS
_attr_state_class: SensorStateClass = SensorStateClass.MEASUREMENT
_decimals = 0
_multiplier = 1
_unit = CONCENTRATION_PARTS_PER_BILLION
@ -557,6 +541,7 @@ class FormaldehydeConcentration(Sensor):
"""Formaldehyde Concentration sensor."""
SENSOR_ATTR = "measured_value"
_attr_state_class: SensorStateClass = SensorStateClass.MEASUREMENT
_decimals = 0
_multiplier = 1e6
_unit = CONCENTRATION_PARTS_PER_MILLION

View File

@ -8,6 +8,7 @@ import zigpy.zcl.clusters.homeautomation as homeautomation
import zigpy.zcl.clusters.measurement as measurement
import zigpy.zcl.clusters.smartenergy as smartenergy
from homeassistant.components.sensor import SensorDeviceClass
from homeassistant.components.zha.core.const import ZHA_CHANNEL_READS_PER_REQ
import homeassistant.config as config_util
from homeassistant.const import (
@ -16,7 +17,6 @@ from homeassistant.const import (
CONF_UNIT_SYSTEM,
CONF_UNIT_SYSTEM_IMPERIAL,
CONF_UNIT_SYSTEM_METRIC,
DEVICE_CLASS_ENERGY,
ELECTRIC_CURRENT_AMPERE,
ELECTRIC_POTENTIAL_VOLT,
ENERGY_KILO_WATT_HOUR,
@ -149,7 +149,8 @@ async def async_test_smart_energy_summation(hass, cluster, entity_id):
assert hass.states.get(entity_id).attributes["status"] == "NO_ALARMS"
assert hass.states.get(entity_id).attributes["device_type"] == "Electric Metering"
assert (
hass.states.get(entity_id).attributes[ATTR_DEVICE_CLASS] == DEVICE_CLASS_ENERGY
hass.states.get(entity_id).attributes[ATTR_DEVICE_CLASS]
== SensorDeviceClass.ENERGY
)