From 8d37fd08c79f5cc4acbc3e1f5c026b8abcb173f9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20Lov=C3=A9n?= <thomasloven@gmail.com> Date: Wed, 18 Aug 2021 17:59:31 +0200 Subject: [PATCH] Fix integration sensors sometimes not getting device_class or unit_of_measurement (#54802) --- homeassistant/components/integration/sensor.py | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/homeassistant/components/integration/sensor.py b/homeassistant/components/integration/sensor.py index cf91fd46dad..b8e72c3be5c 100644 --- a/homeassistant/components/integration/sensor.py +++ b/homeassistant/components/integration/sensor.py @@ -145,12 +145,6 @@ class IntegrationSensor(RestoreEntity, SensorEntity): """Handle the sensor state changes.""" old_state = event.data.get("old_state") new_state = event.data.get("new_state") - if ( - old_state is None - or old_state.state in (STATE_UNKNOWN, STATE_UNAVAILABLE) - or new_state.state in (STATE_UNKNOWN, STATE_UNAVAILABLE) - ): - return if self._unit_of_measurement is None: unit = new_state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) @@ -162,6 +156,14 @@ class IntegrationSensor(RestoreEntity, SensorEntity): and new_state.attributes.get(ATTR_DEVICE_CLASS) == DEVICE_CLASS_POWER ): self._attr_device_class = DEVICE_CLASS_ENERGY + + if ( + old_state is None + or old_state.state in (STATE_UNKNOWN, STATE_UNAVAILABLE) + or new_state.state in (STATE_UNKNOWN, STATE_UNAVAILABLE) + ): + return + try: # integration as the Riemann integral of previous measures. area = 0 -- GitLab