Fix integration sensors sometimes not getting device_class or unit_of_measurement (#54802)

This commit is contained in:
Thomas Lovén 2021-08-18 17:59:31 +02:00 committed by GitHub
parent bce7c73925
commit 8d37fd08c7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -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