Skip to content

Commit 2c5537e

Browse files
committed
Added default "measurement" state_class for all numeric sensors to allow long term statistics.
1 parent d5e779b commit 2c5537e

1 file changed

Lines changed: 13 additions & 1 deletion

File tree

custom_components/enpal_webparser/entity_factory.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,19 @@ def __init__(self, sensor: dict, coordinator: DataUpdateCoordinator):
4949
else:
5050
self._attr_device_class = device_class
5151

52-
# Overrides for state class if specified
52+
# allow explicit state_class from sensor dict
53+
self._attr_state_class = self._sensor.get("state_class")
54+
55+
# measurement state class for numeric sensors if not explicitly set
56+
numeric_device_classes = {
57+
"power", "voltage", "current", "temperature", "frequency",
58+
"battery", "humidity", "pressure"
59+
}
60+
if self._attr_state_class is None:
61+
if device_class in numeric_device_classes or self._attr_native_unit_of_measurement in {"W","kW","V","A","Hz","°C","%"}:
62+
self._attr_state_class = "measurement"
63+
64+
# custom overrides for state class if specified
5365
sensor_id = self._attr_unique_id
5466
if sensor_id in STATE_CLASS_OVERRIDES:
5567
self._attr_state_class = STATE_CLASS_OVERRIDES[sensor_id]

0 commit comments

Comments
 (0)