Fix statistics startup error for None value states (#59199)

* Catch statistics startup error for None value states, fix #49254

* Add test for statistics None handling

* Update tests/components/statistics/test_sensor.py

Co-authored-by: Erik Montnemery <erik@montnemery.com>

* Switch test case logic to remove sensor last

Co-authored-by: Erik Montnemery <erik@montnemery.com>
This commit is contained in:
Thomas Dietrich 2021-11-08 23:25:19 +01:00 committed by GitHub
parent 20b93132dd
commit 2924f4605b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 8 additions and 1 deletions

View file

@ -175,7 +175,7 @@ class StatisticsSensor(SensorEntity):
def _add_state_to_queue(self, new_state):
"""Add the state to the queue."""
if new_state.state in (STATE_UNKNOWN, STATE_UNAVAILABLE):
if new_state.state in (STATE_UNKNOWN, STATE_UNAVAILABLE, None):
return
try:

View file

@ -135,6 +135,13 @@ class TestStatisticsSensor(unittest.TestCase):
new_state = self.hass.states.get("sensor.test")
assert state == new_state
# Source sensor is removed, unit and state should not change
# This is equal to a None value being published
self.hass.states.remove("sensor.test_monitored")
self.hass.block_till_done()
new_state = self.hass.states.get("sensor.test")
assert state == new_state
def test_sampling_size(self):
"""Test rotation."""
assert setup_component(