diff --git a/tests/test_sensor.py b/tests/test_sensor.py index 1213be0d..d6d9006f 100644 --- a/tests/test_sensor.py +++ b/tests/test_sensor.py @@ -404,7 +404,7 @@ async def async_test_general_analog_input( assert entity._cluster_handler.out_of_service == 0 assert entity._cluster_handler.reliability == 0 assert entity._cluster_handler.status_flags == 0 - assert entity._cluster_handler.application_type == 0x00010000 + assert entity._cluster_handler.application_type == 0x00070100 assert entity._cluster_handler.present_value == 1.0 await send_attributes_report( @@ -612,7 +612,7 @@ async def async_test_general_multistate_input( "resolution": 1.1, "status_flags": 0, "engineering_units": 98, - "application_type": 0x00010000, + "application_type": 0x00070100, }, None, ), @@ -629,7 +629,7 @@ async def async_test_general_multistate_input( "reliability": 0, "status_flags": 0, "engineering_units": 98, - "application_type": 0x00010000, + "application_type": 0x00070100, }, None, ), diff --git a/zha/application/platforms/sensor/__init__.py b/zha/application/platforms/sensor/__init__.py index 69acfd18..70fc5e93 100644 --- a/zha/application/platforms/sensor/__init__.py +++ b/zha/application/platforms/sensor/__init__.py @@ -18,6 +18,7 @@ from zigpy.state import Counter, State from zigpy.zcl.clusters.closures import WindowCovering from zigpy.zcl.clusters.general import Basic +from zigpy.zcl.clusters.general_const import ApplicationType from zha.application import Platform from zha.application.const import ENTITY_METADATA @@ -32,7 +33,7 @@ from zha.application.platforms.helpers import validate_device_class from zha.application.platforms.number.const import UNITS from zha.application.platforms.sensor.const import ( - AnalogInputStateClass, + ANALOG_INPUT_APPTYPE_DEV_CLASS, SensorDeviceClass, SensorStateClass, ) @@ -537,16 +538,14 @@ def __init__( super().__init__(unique_id, cluster_handlers, endpoint, device, **kwargs) if self._cluster_handler.cluster.get("state_text"): self._enum = enum.Enum( # type: ignore [misc] - "state_text", self._cluster_handler.cluster.get("state_text") + "state_text", self._cluster_handler.cluster["state_text"] ) elif self._cluster_handler.cluster.get("number_of_states") is not None: self._enum = enum.Enum( # type: ignore [misc] "state_text", [ (f"state_{i+1}", i + 1) - for i in range( - self._cluster_handler.cluster.get("number_of_states") - ) + for i in range(self._cluster_handler.cluster["number_of_states"]) ], ) @@ -579,8 +578,8 @@ def __init__( def device_class(self) -> str | None: """Return the device class.""" if self._cluster_handler.application_type is not None: - device_type = (self._cluster_handler.application_type >> 16) & 0xFF - return AnalogInputStateClass.device_class(device_type) + app_type = ApplicationType(self._cluster_handler.application_type) + return ANALOG_INPUT_APPTYPE_DEV_CLASS[app_type.type] return None @property diff --git a/zha/application/platforms/sensor/const.py b/zha/application/platforms/sensor/const.py index b9f55cbe..44a900fb 100644 --- a/zha/application/platforms/sensor/const.py +++ b/zha/application/platforms/sensor/const.py @@ -2,6 +2,8 @@ import enum +from zigpy.zcl.clusters.general_const import AnalogInputType + class SensorStateClass(enum.StrEnum): """State class for sensors.""" @@ -391,35 +393,20 @@ class SensorDeviceClass(enum.StrEnum): SensorDeviceClass.TIMESTAMP, } - -class AnalogInputStateClass(enum.Enum): - """State class for AnalogInput Types.""" - - TEMPERATURE = (0x00, SensorDeviceClass.TEMPERATURE) - RELATIVE_HUMIDITY = (0x01, SensorDeviceClass.HUMIDITY) - PRESSURE = (0x02, SensorDeviceClass.PRESSURE) - FLOW = (0x03, SensorDeviceClass.VOLUME_FLOW_RATE) - PERCENTAGE = (0x04, None) - PARTS_PER_MILLION = (0x05, SensorDeviceClass.VOLATILE_ORGANIC_COMPOUNDS_PARTS) - RPM = (0x06, None) - CURRENT = (0x07, SensorDeviceClass.CURRENT) - FREQUENCY = (0x08, SensorDeviceClass.FREQUENCY) - POWER_WATTS = (0x09, SensorDeviceClass.POWER) - POWER_KILOWATTS = (0x0A, SensorDeviceClass.POWER) - ENERGY = (0x0B, SensorDeviceClass.ENERGY) - COUNT = (0x0C, None) - ENTHALPY = (0x0D, None) - TIME_SECONDS = (0x0E, None) - - def __init__(self, type, dev_class): - """Init this enum.""" - self.type = type - self.dev_class = dev_class - - @classmethod - def device_class(cls, type): - """Return the device class given a type.""" - for entry in cls: - if entry.type == type: - return entry.dev_class.value - return None +ANALOG_INPUT_APPTYPE_DEV_CLASS = { + AnalogInputType.Temp_Degrees_C: SensorDeviceClass.TEMPERATURE, + AnalogInputType.Relative_Humidity_Percent: SensorDeviceClass.HUMIDITY, + AnalogInputType.Pressure_Pascal: SensorDeviceClass.PRESSURE, + AnalogInputType.Flow_Liters_Per_Sec: SensorDeviceClass.VOLUME_FLOW_RATE, + AnalogInputType.Percentage: None, + AnalogInputType.Parts_Per_Million: SensorDeviceClass.VOLATILE_ORGANIC_COMPOUNDS_PARTS, + AnalogInputType.Rotational_Speed_RPM: None, + AnalogInputType.Current_Amps: SensorDeviceClass.CURRENT, + AnalogInputType.Frequency_Hz: SensorDeviceClass.FREQUENCY, + AnalogInputType.Power_Watts: SensorDeviceClass.POWER, + AnalogInputType.Power_Kilo_Watts: SensorDeviceClass.POWER, + AnalogInputType.Energy_Kilo_Watt_Hours: SensorDeviceClass.ENERGY, + AnalogInputType.Count: None, + AnalogInputType.Enthalpy_KJoules_Per_Kg: None, + AnalogInputType.Time_Seconds: None, +}