From ac5e32d6487d603262b7b2b733972da1a2df2e23 Mon Sep 17 00:00:00 2001 From: Erik Montnemery Date: Tue, 26 Oct 2021 14:05:45 +0200 Subject: [PATCH] Corrections for external statistics (#58469) --- .../components/recorder/statistics.py | 18 ++--- tests/components/recorder/test_statistics.py | 76 ++++++++++++++++++- 2 files changed, 81 insertions(+), 13 deletions(-) diff --git a/homeassistant/components/recorder/statistics.py b/homeassistant/components/recorder/statistics.py index 40470c2346d..edac688bdd1 100644 --- a/homeassistant/components/recorder/statistics.py +++ b/homeassistant/components/recorder/statistics.py @@ -182,8 +182,8 @@ def async_setup(hass: HomeAssistant) -> None: entity_id = event.data["entity_id"] with session_scope(hass=hass) as session: session.query(StatisticsMeta).filter( - StatisticsMeta.statistic_id == old_entity_id - and StatisticsMeta.source == DOMAIN + (StatisticsMeta.statistic_id == old_entity_id) + & (StatisticsMeta.source == DOMAIN) ).update({StatisticsMeta.statistic_id: entity_id}) @callback @@ -457,12 +457,12 @@ def _update_statistics( try: session.query(table).filter_by(id=stat_id).update( { - table.mean: statistic["mean"], - table.min: statistic["min"], - table.max: statistic["max"], - table.last_reset: statistic["last_reset"], - table.state: statistic["state"], - table.sum: statistic["sum"], + table.mean: statistic.get("mean"), + table.min: statistic.get("min"), + table.max: statistic.get("max"), + table.last_reset: statistic.get("last_reset"), + table.state: statistic.get("state"), + table.sum: statistic.get("sum"), }, synchronize_session=False, ) @@ -992,7 +992,7 @@ def _statistics_exists( """Return id if a statistics entry already exists.""" result = ( session.query(table.id) - .filter(table.metadata_id == metadata_id and table.start == start) + .filter((table.metadata_id == metadata_id) & (table.start == start)) .first() ) return result["id"] if result else None diff --git a/tests/components/recorder/test_statistics.py b/tests/components/recorder/test_statistics.py index 4682b7fe482..5b93d1f567d 100644 --- a/tests/components/recorder/test_statistics.py +++ b/tests/components/recorder/test_statistics.py @@ -314,13 +314,20 @@ def test_external_statistics(hass_recorder, caplog): zero = dt_util.utcnow() period1 = zero.replace(minute=0, second=0, microsecond=0) + timedelta(hours=1) + period2 = zero.replace(minute=0, second=0, microsecond=0) + timedelta(hours=2) - external_statistics = { + external_statistics1 = { "start": period1, "last_reset": None, "state": 0, "sum": 2, } + external_statistics2 = { + "start": period2, + "last_reset": None, + "state": 1, + "sum": 3, + } external_metadata = { "has_mean": False, @@ -331,7 +338,9 @@ def test_external_statistics(hass_recorder, caplog): "unit_of_measurement": "kWh", } - async_add_external_statistics(hass, external_metadata, (external_statistics,)) + async_add_external_statistics( + hass, external_metadata, (external_statistics1, external_statistics2) + ) wait_recording_done(hass) stats = statistics_during_period(hass, zero, period="hour") assert stats == { @@ -346,7 +355,18 @@ def test_external_statistics(hass_recorder, caplog): "last_reset": None, "state": approx(0.0), "sum": approx(2.0), - } + }, + { + "statistic_id": "test:total_energy_import", + "start": period2.isoformat(), + "end": (period2 + timedelta(hours=1)).isoformat(), + "max": None, + "mean": None, + "min": None, + "last_reset": None, + "state": approx(1.0), + "sum": approx(3.0), + }, ] } statistic_ids = list_statistic_ids(hass) @@ -373,6 +393,43 @@ def test_external_statistics(hass_recorder, caplog): ) } + # Update the previously inserted statistics + external_statistics = { + "start": period1, + "last_reset": None, + "state": 5, + "sum": 6, + } + async_add_external_statistics(hass, external_metadata, (external_statistics,)) + wait_recording_done(hass) + stats = statistics_during_period(hass, zero, period="hour") + assert stats == { + "test:total_energy_import": [ + { + "statistic_id": "test:total_energy_import", + "start": period1.isoformat(), + "end": (period1 + timedelta(hours=1)).isoformat(), + "max": None, + "mean": None, + "min": None, + "last_reset": None, + "state": approx(5.0), + "sum": approx(6.0), + }, + { + "statistic_id": "test:total_energy_import", + "start": period2.isoformat(), + "end": (period2 + timedelta(hours=1)).isoformat(), + "max": None, + "mean": None, + "min": None, + "last_reset": None, + "state": approx(1.0), + "sum": approx(3.0), + }, + ] + } + # Update the previously inserted statistics external_statistics = { "start": period1, @@ -398,7 +455,18 @@ def test_external_statistics(hass_recorder, caplog): "last_reset": None, "state": approx(4.0), "sum": approx(5.0), - } + }, + { + "statistic_id": "test:total_energy_import", + "start": period2.isoformat(), + "end": (period2 + timedelta(hours=1)).isoformat(), + "max": None, + "mean": None, + "min": None, + "last_reset": None, + "state": approx(1.0), + "sum": approx(3.0), + }, ] }