From e5642a86484949f24d5dd764d00cc2e2b6771100 Mon Sep 17 00:00:00 2001 From: Steffen Zimmermann Date: Mon, 27 Sep 2021 18:28:20 +0200 Subject: [PATCH] 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 Co-authored-by: Greg --- homeassistant/components/wiffi/__init__.py | 8 ++++++++ homeassistant/components/wiffi/sensor.py | 10 ++++++++++ 2 files changed, 18 insertions(+) diff --git a/homeassistant/components/wiffi/__init__.py b/homeassistant/components/wiffi/__init__.py index 55b13921c1c..e155a48fd72 100644 --- a/homeassistant/components/wiffi/__init__.py +++ b/homeassistant/components/wiffi/__init__.py @@ -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") diff --git a/homeassistant/components/wiffi/sensor.py b/homeassistant/components/wiffi/sensor.py index b9bcd317b46..c16ae3c6aca 100644 --- a/homeassistant/components/wiffi/sensor.py +++ b/homeassistant/components/wiffi/sensor.py @@ -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()