diff --git a/homeassistant/components/recorder/statistics.py b/homeassistant/components/recorder/statistics.py index 50e987a5533..67b2568fc53 100644 --- a/homeassistant/components/recorder/statistics.py +++ b/homeassistant/components/recorder/statistics.py @@ -753,7 +753,7 @@ def list_statistic_ids( hass: HomeAssistant, statistic_ids: list[str] | tuple[str] | None = None, statistic_type: Literal["mean"] | Literal["sum"] | None = None, -) -> list[dict | None]: +) -> list[dict]: """Return all statistic_ids (or filtered one) and unit of measurement. Queries the database for existing statistic_ids, as well as integrations with @@ -777,6 +777,8 @@ def list_statistic_ids( result = { meta["statistic_id"]: { + "has_mean": meta["has_mean"], + "has_sum": meta["has_sum"], "name": meta["name"], "source": meta["source"], "unit_of_measurement": meta["unit_of_measurement"], @@ -805,6 +807,8 @@ def list_statistic_ids( return [ { "statistic_id": _id, + "has_mean": info["has_mean"], + "has_sum": info["has_sum"], "name": info.get("name"), "source": info["source"], "unit_of_measurement": info["unit_of_measurement"], diff --git a/homeassistant/components/sensor/recorder.py b/homeassistant/components/sensor/recorder.py index 75e117d9834..4afc514d457 100644 --- a/homeassistant/components/sensor/recorder.py +++ b/homeassistant/components/sensor/recorder.py @@ -627,6 +627,8 @@ def list_statistic_ids( if device_class not in UNIT_CONVERSIONS: result[state.entity_id] = { + "has_mean": "mean" in provided_statistics, + "has_sum": "sum" in provided_statistics, "source": RECORDER_DOMAIN, "unit_of_measurement": native_unit, } @@ -637,6 +639,8 @@ def list_statistic_ids( statistics_unit = DEVICE_CLASS_UNITS[device_class] result[state.entity_id] = { + "has_mean": "mean" in provided_statistics, + "has_sum": "sum" in provided_statistics, "source": RECORDER_DOMAIN, "unit_of_measurement": statistics_unit, } diff --git a/tests/components/demo/test_init.py b/tests/components/demo/test_init.py index f25d1a57b34..beffa3ba332 100644 --- a/tests/components/demo/test_init.py +++ b/tests/components/demo/test_init.py @@ -57,12 +57,16 @@ async def test_demo_statistics(hass, recorder_mock): list_statistic_ids, hass ) assert { + "has_mean": True, + "has_sum": False, "name": None, "source": "demo", "statistic_id": "demo:temperature_outdoor", "unit_of_measurement": "°C", } in statistic_ids assert { + "has_mean": False, + "has_sum": True, "name": None, "source": "demo", "statistic_id": "demo:energy_consumption", diff --git a/tests/components/history/test_init.py b/tests/components/history/test_init.py index 1590dcf1ed0..f9fb7b49fb0 100644 --- a/tests/components/history/test_init.py +++ b/tests/components/history/test_init.py @@ -1038,6 +1038,8 @@ async def test_list_statistic_ids(hass, hass_ws_client, units, attributes, unit) assert response["result"] == [ { "statistic_id": "sensor.test", + "has_mean": True, + "has_sum": False, "name": None, "source": "recorder", "unit_of_measurement": unit, @@ -1056,6 +1058,8 @@ async def test_list_statistic_ids(hass, hass_ws_client, units, attributes, unit) assert response["result"] == [ { "statistic_id": "sensor.test", + "has_mean": True, + "has_sum": False, "name": None, "source": "recorder", "unit_of_measurement": unit, @@ -1076,6 +1080,8 @@ async def test_list_statistic_ids(hass, hass_ws_client, units, attributes, unit) assert response["result"] == [ { "statistic_id": "sensor.test", + "has_mean": True, + "has_sum": False, "name": None, "source": "recorder", "unit_of_measurement": unit, diff --git a/tests/components/recorder/test_statistics.py b/tests/components/recorder/test_statistics.py index 29853c2cc0e..2e8635c6c6e 100644 --- a/tests/components/recorder/test_statistics.py +++ b/tests/components/recorder/test_statistics.py @@ -401,6 +401,8 @@ async def test_external_statistics(hass, hass_ws_client, caplog): statistic_ids = list_statistic_ids(hass) assert statistic_ids == [ { + "has_mean": False, + "has_sum": True, "statistic_id": "test:total_energy_import", "name": "Total imported energy", "source": "test", diff --git a/tests/components/recorder/test_websocket_api.py b/tests/components/recorder/test_websocket_api.py index 33478b76bcb..3b09350417b 100644 --- a/tests/components/recorder/test_websocket_api.py +++ b/tests/components/recorder/test_websocket_api.py @@ -230,6 +230,8 @@ async def test_update_statistics_metadata(hass, hass_ws_client, new_unit): assert response["result"] == [ { "statistic_id": "sensor.test", + "has_mean": True, + "has_sum": False, "name": None, "source": "recorder", "unit_of_measurement": "W", @@ -254,6 +256,8 @@ async def test_update_statistics_metadata(hass, hass_ws_client, new_unit): assert response["result"] == [ { "statistic_id": "sensor.test", + "has_mean": True, + "has_sum": False, "name": None, "source": "recorder", "unit_of_measurement": new_unit, @@ -525,6 +529,8 @@ async def test_get_statistics_metadata(hass, hass_ws_client, units, attributes, assert response["result"] == [ { "statistic_id": "sensor.test", + "has_mean": False, + "has_sum": True, "name": None, "source": "recorder", "unit_of_measurement": unit, @@ -549,6 +555,8 @@ async def test_get_statistics_metadata(hass, hass_ws_client, units, attributes, assert response["result"] == [ { "statistic_id": "sensor.test", + "has_mean": False, + "has_sum": True, "name": None, "source": "recorder", "unit_of_measurement": unit, diff --git a/tests/components/sensor/test_recorder.py b/tests/components/sensor/test_recorder.py index f26b5e7ead3..33550495589 100644 --- a/tests/components/sensor/test_recorder.py +++ b/tests/components/sensor/test_recorder.py @@ -122,6 +122,8 @@ def test_compile_hourly_statistics( assert statistic_ids == [ { "statistic_id": "sensor.test1", + "has_mean": True, + "has_sum": False, "name": None, "source": "recorder", "unit_of_measurement": native_unit, @@ -185,6 +187,8 @@ def test_compile_hourly_statistics_purged_state_changes( assert statistic_ids == [ { "statistic_id": "sensor.test1", + "has_mean": True, + "has_sum": False, "name": None, "source": "recorder", "unit_of_measurement": native_unit, @@ -251,18 +255,24 @@ def test_compile_hourly_statistics_unsupported(hass_recorder, caplog, attributes assert statistic_ids == [ { "statistic_id": "sensor.test1", + "has_mean": True, + "has_sum": False, "name": None, "source": "recorder", "unit_of_measurement": "°C", }, { "statistic_id": "sensor.test6", + "has_mean": True, + "has_sum": False, "name": None, "source": "recorder", "unit_of_measurement": "°C", }, { "statistic_id": "sensor.test7", + "has_mean": True, + "has_sum": False, "name": None, "source": "recorder", "unit_of_measurement": "°C", @@ -386,6 +396,8 @@ async def test_compile_hourly_sum_statistics_amount( assert statistic_ids == [ { "statistic_id": "sensor.test1", + "has_mean": False, + "has_sum": True, "name": None, "source": "recorder", "unit_of_measurement": display_unit, @@ -565,6 +577,8 @@ def test_compile_hourly_sum_statistics_amount_reset_every_state_change( assert statistic_ids == [ { "statistic_id": "sensor.test1", + "has_mean": False, + "has_sum": True, "name": None, "source": "recorder", "unit_of_measurement": native_unit, @@ -654,6 +668,8 @@ def test_compile_hourly_sum_statistics_amount_invalid_last_reset( assert statistic_ids == [ { "statistic_id": "sensor.test1", + "has_mean": False, + "has_sum": True, "name": None, "source": "recorder", "unit_of_measurement": native_unit, @@ -727,6 +743,8 @@ def test_compile_hourly_sum_statistics_nan_inf_state( assert statistic_ids == [ { "statistic_id": "sensor.test1", + "has_mean": False, + "has_sum": True, "name": None, "source": "recorder", "unit_of_measurement": native_unit, @@ -837,6 +855,8 @@ def test_compile_hourly_sum_statistics_negative_state( statistic_ids = list_statistic_ids(hass) assert { "name": None, + "has_mean": False, + "has_sum": True, "source": "recorder", "statistic_id": entity_id, "unit_of_measurement": native_unit, @@ -914,6 +934,8 @@ def test_compile_hourly_sum_statistics_total_no_reset( assert statistic_ids == [ { "statistic_id": "sensor.test1", + "has_mean": False, + "has_sum": True, "name": None, "source": "recorder", "unit_of_measurement": native_unit, @@ -1006,6 +1028,8 @@ def test_compile_hourly_sum_statistics_total_increasing( assert statistic_ids == [ { "statistic_id": "sensor.test1", + "has_mean": False, + "has_sum": True, "name": None, "source": "recorder", "unit_of_measurement": native_unit, @@ -1109,6 +1133,8 @@ def test_compile_hourly_sum_statistics_total_increasing_small_dip( assert statistic_ids == [ { "statistic_id": "sensor.test1", + "has_mean": False, + "has_sum": True, "name": None, "source": "recorder", "unit_of_measurement": native_unit, @@ -1200,6 +1226,8 @@ def test_compile_hourly_energy_statistics_unsupported(hass_recorder, caplog): assert statistic_ids == [ { "statistic_id": "sensor.test1", + "has_mean": False, + "has_sum": True, "name": None, "source": "recorder", "unit_of_measurement": "kWh", @@ -1289,18 +1317,24 @@ def test_compile_hourly_energy_statistics_multiple(hass_recorder, caplog): assert statistic_ids == [ { "statistic_id": "sensor.test1", + "has_mean": False, + "has_sum": True, "name": None, "source": "recorder", "unit_of_measurement": "kWh", }, { "statistic_id": "sensor.test2", + "has_mean": False, + "has_sum": True, "name": None, "source": "recorder", "unit_of_measurement": "kWh", }, { "statistic_id": "sensor.test3", + "has_mean": False, + "has_sum": True, "name": None, "source": "recorder", "unit_of_measurement": "kWh", @@ -1622,6 +1656,8 @@ def test_list_statistic_ids( assert statistic_ids == [ { "statistic_id": "sensor.test1", + "has_mean": statistic_type == "mean", + "has_sum": statistic_type == "sum", "name": None, "source": "recorder", "unit_of_measurement": native_unit, @@ -1633,6 +1669,8 @@ def test_list_statistic_ids( assert statistic_ids == [ { "statistic_id": "sensor.test1", + "has_mean": statistic_type == "mean", + "has_sum": statistic_type == "sum", "name": None, "source": "recorder", "unit_of_measurement": native_unit, @@ -1710,6 +1748,8 @@ def test_compile_hourly_statistics_changing_units_1( assert statistic_ids == [ { "statistic_id": "sensor.test1", + "has_mean": True, + "has_sum": False, "name": None, "source": "recorder", "unit_of_measurement": native_unit, @@ -1742,6 +1782,8 @@ def test_compile_hourly_statistics_changing_units_1( assert statistic_ids == [ { "statistic_id": "sensor.test1", + "has_mean": True, + "has_sum": False, "name": None, "source": "recorder", "unit_of_measurement": native_unit, @@ -1805,6 +1847,8 @@ def test_compile_hourly_statistics_changing_units_2( assert statistic_ids == [ { "statistic_id": "sensor.test1", + "has_mean": True, + "has_sum": False, "name": None, "source": "recorder", "unit_of_measurement": "cats", @@ -1858,6 +1902,8 @@ def test_compile_hourly_statistics_changing_units_3( assert statistic_ids == [ { "statistic_id": "sensor.test1", + "has_mean": True, + "has_sum": False, "name": None, "source": "recorder", "unit_of_measurement": native_unit, @@ -1888,6 +1934,8 @@ def test_compile_hourly_statistics_changing_units_3( assert statistic_ids == [ { "statistic_id": "sensor.test1", + "has_mean": True, + "has_sum": False, "name": None, "source": "recorder", "unit_of_measurement": native_unit, @@ -1941,6 +1989,8 @@ def test_compile_hourly_statistics_changing_device_class_1( assert statistic_ids == [ { "statistic_id": "sensor.test1", + "has_mean": True, + "has_sum": False, "name": None, "source": "recorder", "unit_of_measurement": state_unit, @@ -1987,6 +2037,8 @@ def test_compile_hourly_statistics_changing_device_class_1( assert statistic_ids == [ { "statistic_id": "sensor.test1", + "has_mean": True, + "has_sum": False, "name": None, "source": "recorder", "unit_of_measurement": state_unit, @@ -2041,6 +2093,8 @@ def test_compile_hourly_statistics_changing_device_class_2( assert statistic_ids == [ { "statistic_id": "sensor.test1", + "has_mean": True, + "has_sum": False, "name": None, "source": "recorder", "unit_of_measurement": statistic_unit, @@ -2087,6 +2141,8 @@ def test_compile_hourly_statistics_changing_device_class_2( assert statistic_ids == [ { "statistic_id": "sensor.test1", + "has_mean": True, + "has_sum": False, "name": None, "source": "recorder", "unit_of_measurement": statistic_unit, @@ -2144,6 +2200,8 @@ def test_compile_hourly_statistics_changing_statistics( assert statistic_ids == [ { "statistic_id": "sensor.test1", + "has_mean": True, + "has_sum": False, "name": None, "source": "recorder", "unit_of_measurement": None, @@ -2176,6 +2234,8 @@ def test_compile_hourly_statistics_changing_statistics( assert statistic_ids == [ { "statistic_id": "sensor.test1", + "has_mean": False, + "has_sum": True, "name": None, "source": "recorder", "unit_of_measurement": None, @@ -2372,24 +2432,32 @@ def test_compile_statistics_hourly_daily_monthly_summary( assert statistic_ids == [ { "statistic_id": "sensor.test1", + "has_mean": True, + "has_sum": False, "name": None, "source": "recorder", "unit_of_measurement": "%", }, { "statistic_id": "sensor.test2", + "has_mean": True, + "has_sum": False, "name": None, "source": "recorder", "unit_of_measurement": "%", }, { "statistic_id": "sensor.test3", + "has_mean": True, + "has_sum": False, "name": None, "source": "recorder", "unit_of_measurement": "%", }, { "statistic_id": "sensor.test4", + "has_mean": False, + "has_sum": True, "name": None, "source": "recorder", "unit_of_measurement": "EUR",