Skip to content

Commit

Permalink
Fix integration sensors sometimes not getting device_class or unit_of…
Browse files Browse the repository at this point in the history
…_measurement (home-assistant#54802)
  • Loading branch information
thomasloven authored Aug 18, 2021
1 parent bce7c73 commit 8d37fd0
Showing 1 changed file with 8 additions and 6 deletions.
14 changes: 8 additions & 6 deletions homeassistant/components/integration/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,12 +145,6 @@ def calc_integration(event):
"""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)
Expand All @@ -162,6 +156,14 @@ def calc_integration(event):
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
Expand Down

0 comments on commit 8d37fd0

Please sign in to comment.