From f9225bad5fd2395afe036113adbb3c8b8ee9ccdf Mon Sep 17 00:00:00 2001 From: Erik Montnemery Date: Tue, 31 Aug 2021 10:45:17 +0200 Subject: [PATCH] Make new cycles for sensor sum statistics start with 0 as zero-point (#55473) --- homeassistant/components/sensor/recorder.py | 7 ++----- tests/components/sensor/test_recorder.py | 20 ++++++++++---------- 2 files changed, 12 insertions(+), 15 deletions(-) diff --git a/homeassistant/components/sensor/recorder.py b/homeassistant/components/sensor/recorder.py index 6ab75f88dbd..cc1b6865d81 100644 --- a/homeassistant/components/sensor/recorder.py +++ b/homeassistant/components/sensor/recorder.py @@ -403,11 +403,8 @@ def compile_statistics( # ..and update the starting point new_state = fstate old_last_reset = last_reset - # Force a new cycle for STATE_CLASS_TOTAL_INCREASING to start at 0 - if ( - state_class == STATE_CLASS_TOTAL_INCREASING - and old_state is not None - ): + # Force a new cycle for an existing sensor to start at 0 + if old_state is not None: old_state = 0.0 else: old_state = new_state diff --git a/tests/components/sensor/test_recorder.py b/tests/components/sensor/test_recorder.py index 6c4c899eb14..b3f0ab075c6 100644 --- a/tests/components/sensor/test_recorder.py +++ b/tests/components/sensor/test_recorder.py @@ -258,7 +258,7 @@ def test_compile_hourly_sum_statistics_amount( "min": None, "last_reset": process_timestamp_to_utc_isoformat(four), "state": approx(factor * seq[5]), - "sum": approx(factor * 10.0), + "sum": approx(factor * 40.0), }, { "statistic_id": "sensor.test1", @@ -268,7 +268,7 @@ def test_compile_hourly_sum_statistics_amount( "min": None, "last_reset": process_timestamp_to_utc_isoformat(four), "state": approx(factor * seq[8]), - "sum": approx(factor * 40.0), + "sum": approx(factor * 70.0), }, ] } @@ -512,7 +512,7 @@ def test_compile_hourly_energy_statistics_unsupported(hass_recorder, caplog): "min": None, "last_reset": process_timestamp_to_utc_isoformat(four), "state": approx(40.0), - "sum": approx(10.0), + "sum": approx(40.0), }, { "statistic_id": "sensor.test1", @@ -522,7 +522,7 @@ def test_compile_hourly_energy_statistics_unsupported(hass_recorder, caplog): "min": None, "last_reset": process_timestamp_to_utc_isoformat(four), "state": approx(70.0), - "sum": approx(40.0), + "sum": approx(70.0), }, ] } @@ -595,7 +595,7 @@ def test_compile_hourly_energy_statistics_multiple(hass_recorder, caplog): "min": None, "last_reset": process_timestamp_to_utc_isoformat(four), "state": approx(40.0), - "sum": approx(10.0), + "sum": approx(40.0), }, { "statistic_id": "sensor.test1", @@ -605,7 +605,7 @@ def test_compile_hourly_energy_statistics_multiple(hass_recorder, caplog): "min": None, "last_reset": process_timestamp_to_utc_isoformat(four), "state": approx(70.0), - "sum": approx(40.0), + "sum": approx(70.0), }, ], "sensor.test2": [ @@ -627,7 +627,7 @@ def test_compile_hourly_energy_statistics_multiple(hass_recorder, caplog): "min": None, "last_reset": process_timestamp_to_utc_isoformat(four), "state": approx(45.0), - "sum": approx(-95.0), + "sum": approx(-65.0), }, { "statistic_id": "sensor.test2", @@ -637,7 +637,7 @@ def test_compile_hourly_energy_statistics_multiple(hass_recorder, caplog): "min": None, "last_reset": process_timestamp_to_utc_isoformat(four), "state": approx(75.0), - "sum": approx(-65.0), + "sum": approx(-35.0), }, ], "sensor.test3": [ @@ -659,7 +659,7 @@ def test_compile_hourly_energy_statistics_multiple(hass_recorder, caplog): "min": None, "last_reset": process_timestamp_to_utc_isoformat(four), "state": approx(50.0 / 1000), - "sum": approx(30.0 / 1000), + "sum": approx(60.0 / 1000), }, { "statistic_id": "sensor.test3", @@ -669,7 +669,7 @@ def test_compile_hourly_energy_statistics_multiple(hass_recorder, caplog): "min": None, "last_reset": process_timestamp_to_utc_isoformat(four), "state": approx(90.0 / 1000), - "sum": approx(70.0 / 1000), + "sum": approx(100.0 / 1000), }, ], }