From 2924f4605bc62f5b14c68d3cc5ec1ea7b6ab80d8 Mon Sep 17 00:00:00 2001 From: Thomas Dietrich Date: Mon, 8 Nov 2021 23:25:19 +0100 Subject: [PATCH] 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 * Switch test case logic to remove sensor last Co-authored-by: Erik Montnemery --- homeassistant/components/statistics/sensor.py | 2 +- tests/components/statistics/test_sensor.py | 7 +++++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/homeassistant/components/statistics/sensor.py b/homeassistant/components/statistics/sensor.py index 6bf882c861d..3d5857e2b03 100644 --- a/homeassistant/components/statistics/sensor.py +++ b/homeassistant/components/statistics/sensor.py @@ -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: diff --git a/tests/components/statistics/test_sensor.py b/tests/components/statistics/test_sensor.py index 41ec88deaec..a2eaa9add03 100644 --- a/tests/components/statistics/test_sensor.py +++ b/tests/components/statistics/test_sensor.py @@ -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(