Skip to content

Commit cf3b5e6

Browse files
authored
Fix missing state_class for quirk v2 sensors (#341)
* Fix missing state_class for quirk v2 sensors * Update test with invalid state class * Assert log message
1 parent 2d15013 commit cf3b5e6

File tree

2 files changed

+71
-1
lines changed

2 files changed

+71
-1
lines changed

tests/test_sensor.py

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
from zha.application.gateway import Gateway
3737
from zha.application.platforms import PlatformEntity, sensor
3838
from zha.application.platforms.sensor import DanfossSoftwareErrorCode, UnitOfMass
39-
from zha.application.platforms.sensor.const import SensorDeviceClass
39+
from zha.application.platforms.sensor.const import SensorDeviceClass, SensorStateClass
4040
from zha.units import PERCENTAGE, UnitOfEnergy, UnitOfPressure, UnitOfVolume
4141
from zha.zigbee.device import Device
4242

@@ -1245,6 +1245,34 @@ def __init__(self, *args, **kwargs) -> None:
12451245
translation_key="last_feeding_size",
12461246
fallback_name="Last feeding size",
12471247
)
1248+
.sensor(
1249+
"power",
1250+
OppleCluster.cluster_id,
1251+
state_class=SensorStateClass.MEASUREMENT,
1252+
device_class=SensorDeviceClass.POWER,
1253+
fallback_name="Measurement",
1254+
)
1255+
.sensor(
1256+
"energy",
1257+
OppleCluster.cluster_id,
1258+
state_class=SensorStateClass.TOTAL,
1259+
device_class=SensorDeviceClass.ENERGY,
1260+
fallback_name="Measurement",
1261+
)
1262+
.sensor(
1263+
"energy_delivered",
1264+
OppleCluster.cluster_id,
1265+
state_class=SensorStateClass.TOTAL_INCREASING,
1266+
device_class=SensorDeviceClass.ENERGY,
1267+
fallback_name="Measurement",
1268+
)
1269+
.sensor(
1270+
"energy_invalid_state_class",
1271+
OppleCluster.cluster_id,
1272+
state_class=SensorDeviceClass.ENERGY,
1273+
device_class=SensorDeviceClass.ENERGY,
1274+
fallback_name="Measurement",
1275+
)
12481276
.add_to_registry()
12491277
)
12501278

@@ -1292,6 +1320,29 @@ async def test_last_feeding_size_sensor_v2(zha_gateway: Gateway) -> None:
12921320
assert_state(entity, 5.0, "g")
12931321

12941322

1323+
async def test_state_class(
1324+
zha_gateway: Gateway, caplog: pytest.LogCaptureFixture
1325+
) -> None:
1326+
"""Test quirks defined sensor."""
1327+
1328+
zha_device, cluster = await zigpy_device_aqara_sensor_v2_mock(zha_gateway)
1329+
assert isinstance(zha_device.device, CustomDeviceV2)
1330+
power_entity = get_entity(zha_device, platform=Platform.SENSOR, qualifier="power")
1331+
energy_entity = get_entity(zha_device, platform=Platform.SENSOR, qualifier="energy")
1332+
energy_delivered_entity = get_entity(
1333+
zha_device, platform=Platform.SENSOR, qualifier="energy_delivered"
1334+
)
1335+
energy_invalid_state_class = get_entity(
1336+
zha_device, platform=Platform.SENSOR, qualifier="energy_invalid_state_class"
1337+
)
1338+
1339+
assert power_entity.state_class == SensorStateClass.MEASUREMENT
1340+
assert energy_entity.state_class == SensorStateClass.TOTAL
1341+
assert energy_delivered_entity.state_class == SensorStateClass.TOTAL_INCREASING
1342+
assert energy_invalid_state_class.state_class is None
1343+
assert "Quirks provided an invalid state class: energy" in caplog.text
1344+
1345+
12951346
async def test_device_counter_sensors(zha_gateway: Gateway) -> None:
12961347
"""Test coordinator counter sensor."""
12971348

zha/application/platforms/sensor/__init__.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,21 @@ def __init__(
201201
self.handle_cluster_handler_attribute_updated,
202202
)
203203

204+
def _validate_state_class(
205+
self,
206+
state_class_value: SensorStateClass,
207+
) -> SensorStateClass | None:
208+
"""Validate and return a state class."""
209+
try:
210+
return SensorStateClass(state_class_value.value)
211+
except ValueError as ex:
212+
_LOGGER.warning(
213+
"Quirks provided an invalid state class: %s: %s",
214+
state_class_value,
215+
ex,
216+
)
217+
return None
218+
204219
def _init_from_quirks_metadata(self, entity_metadata: ZCLSensorMetadata) -> None:
205220
"""Init this entity from the quirks metadata."""
206221
super()._init_from_quirks_metadata(entity_metadata)
@@ -216,6 +231,10 @@ def _init_from_quirks_metadata(self, entity_metadata: ZCLSensorMetadata) -> None
216231
Platform.SENSOR.value,
217232
_LOGGER,
218233
)
234+
if entity_metadata.state_class is not None:
235+
self._attr_state_class = self._validate_state_class(
236+
entity_metadata.state_class
237+
)
219238
if entity_metadata.unit is not None:
220239
self._attr_native_unit_of_measurement = validate_unit(
221240
entity_metadata.unit

0 commit comments

Comments
 (0)