From 5d1a193ecac14d7549d1a1a30b80626190e1d4f4 Mon Sep 17 00:00:00 2001 From: Erik Montnemery Date: Tue, 31 Aug 2021 19:15:22 +0200 Subject: [PATCH] Improve log for sum statistics (#55502) --- homeassistant/components/sensor/recorder.py | 17 +++++++++++++++-- tests/components/sensor/test_recorder.py | 6 ++++++ 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/homeassistant/components/sensor/recorder.py b/homeassistant/components/sensor/recorder.py index cc1b6865d81..1c8bbf89b89 100644 --- a/homeassistant/components/sensor/recorder.py +++ b/homeassistant/components/sensor/recorder.py @@ -282,7 +282,7 @@ def reset_detected( return state < 0.9 * previous_state -def compile_statistics( +def compile_statistics( # noqa: C901 hass: HomeAssistant, start: datetime.datetime, end: datetime.datetime ) -> dict: """Compile statistics for all entities during start-end. @@ -376,6 +376,19 @@ def compile_statistics( and (last_reset := state.attributes.get("last_reset")) != old_last_reset ): + if old_state is None: + _LOGGER.info( + "Compiling initial sum statistics for %s, zero point set to %s", + entity_id, + fstate, + ) + else: + _LOGGER.info( + "Detected new cycle for %s, last_reset set to %s (old last_reset %s)", + entity_id, + last_reset, + old_last_reset, + ) reset = True elif old_state is None and last_reset is None: reset = True @@ -390,7 +403,7 @@ def compile_statistics( ): reset = True _LOGGER.info( - "Detected new cycle for %s, zero point set to %s (old zero point %s)", + "Detected new cycle for %s, value dropped from %s to %s", entity_id, fstate, new_state, diff --git a/tests/components/sensor/test_recorder.py b/tests/components/sensor/test_recorder.py index b3f0ab075c6..d26ecbc1c71 100644 --- a/tests/components/sensor/test_recorder.py +++ b/tests/components/sensor/test_recorder.py @@ -273,6 +273,9 @@ def test_compile_hourly_sum_statistics_amount( ] } assert "Error while processing event StatisticsTask" not in caplog.text + assert "Detected new cycle for sensor.test1, last_reset set to" in caplog.text + assert "Compiling initial sum statistics for sensor.test1" in caplog.text + assert "Detected new cycle for sensor.test1, value dropped" not in caplog.text @pytest.mark.parametrize( @@ -353,6 +356,9 @@ def test_compile_hourly_sum_statistics_total_increasing( ] } assert "Error while processing event StatisticsTask" not in caplog.text + assert "Detected new cycle for sensor.test1, last_reset set to" not in caplog.text + assert "Compiling initial sum statistics for sensor.test1" in caplog.text + assert "Detected new cycle for sensor.test1, value dropped" in caplog.text @pytest.mark.parametrize(