Add state_class measurements in wiffi integration (#54279)

* add support for state_class measurements in wiffi integration

* use new STATE_CLASS_TOTAL_INCREASING for metered entities

like
- amount of rainfall per hour/day
- rainfall hours per day
- sunshine hours per day

* Update homeassistant/components/wiffi/sensor.py

Co-authored-by: Greg <greg.diehl.gtd@gmail.com>

Co-authored-by: Greg <greg.diehl.gtd@gmail.com>
This commit is contained in:
Steffen Zimmermann 2021-09-27 18:28:20 +02:00 committed by GitHub
parent 4d7e3cde5a
commit e5642a8648
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 18 additions and 0 deletions

View File

@ -222,3 +222,11 @@ class WiffiEntity(Entity):
):
self._value = None
self.async_write_ha_state()
def _is_measurement_entity(self):
"""Measurement entities have a value in present time."""
return not self._name.endswith("_gestern") and not self._is_metered_entity()
def _is_metered_entity(self):
"""Metered entities have a value that keeps increasing until reset."""
return self._name.endswith("_pro_h") or self._name.endswith("_heute")

View File

@ -5,6 +5,8 @@ from homeassistant.components.sensor import (
DEVICE_CLASS_ILLUMINANCE,
DEVICE_CLASS_PRESSURE,
DEVICE_CLASS_TEMPERATURE,
STATE_CLASS_MEASUREMENT,
STATE_CLASS_TOTAL_INCREASING,
SensorEntity,
)
from homeassistant.const import DEGREE, PRESSURE_MBAR, TEMP_CELSIUS
@ -70,6 +72,12 @@ class NumberEntity(WiffiEntity, SensorEntity):
metric.unit_of_measurement, metric.unit_of_measurement
)
self._value = metric.value
if self._is_measurement_entity():
self._attr_state_class = STATE_CLASS_MEASUREMENT
elif self._is_metered_entity():
self._attr_state_class = STATE_CLASS_TOTAL_INCREASING
self.reset_expiration_date()
@property
@ -97,7 +105,9 @@ class NumberEntity(WiffiEntity, SensorEntity):
self._unit_of_measurement = UOM_MAP.get(
metric.unit_of_measurement, metric.unit_of_measurement
)
self._value = metric.value
self.async_write_ha_state()