Skip to content

Commit

Permalink
Remove constants, update tests and Analog Input
Browse files Browse the repository at this point in the history
  • Loading branch information
prairiesnpr committed Sep 17, 2024
1 parent 21381db commit a76bb19
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 42 deletions.
6 changes: 3 additions & 3 deletions tests/test_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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,
),
Expand All @@ -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,
),
Expand Down
13 changes: 6 additions & 7 deletions zha/application/platforms/sensor/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
)
Expand Down Expand Up @@ -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"])
],
)

Expand Down Expand Up @@ -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
Expand Down
51 changes: 19 additions & 32 deletions zha/application/platforms/sensor/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import enum

from zigpy.zcl.clusters.general_const import AnalogInputType


class SensorStateClass(enum.StrEnum):
"""State class for sensors."""
Expand Down Expand Up @@ -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,
}

0 comments on commit a76bb19

Please sign in to comment.