Skip to content

Commit

Permalink
only loop as much as needed
Browse files Browse the repository at this point in the history
  • Loading branch information
basnijholt committed Feb 3, 2020
1 parent 5de36fd commit 859713c
Showing 1 changed file with 6 additions and 6 deletions.
12 changes: 6 additions & 6 deletions homeassistant/components/derivative/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,6 @@ async def async_added_to_hass(self):
@callback
def calc_derivative(entity, old_state, new_state):
"""Handle the sensor state changes."""

if (
old_state is None
or old_state.state in [STATE_UNKNOWN, STATE_UNAVAILABLE]
Expand All @@ -120,11 +119,12 @@ def calc_derivative(entity, old_state, new_state):
self._state_list.append((now, new_state.state))

# Get indices of tuples that are older than `unit_time`
to_remove = [
i
for i, (timestamp, _) in enumerate(self._state_list)
if (now - timestamp).total_seconds() > self._unit_time
]
to_remove = []
for i, (timestamp, _) in enumerate(self._state_list):
if (now - timestamp).total_seconds() > self._unit_time:
to_remove.append(i)
else:
break
# Delete those tuples from the list
for i in reversed(to_remove):
self._state_list.pop(i)
Expand Down

0 comments on commit 859713c

Please sign in to comment.