Include has_mean + has_sum in statistics metadata WS response (#68546)

* Include has_mean + has_sum in statistics metadata WS response

* Don't include has_mean/has_sum in history/list_statistic_ids

* Adjust tests

* Do include has_mean/has_sum in history/list_statistic_ids
This commit is contained in:
Erik Montnemery 2022-03-24 10:12:01 +01:00 committed by GitHub
parent 4736470915
commit 61cc8e32f3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 97 additions and 1 deletions

View file

@ -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"],

View file

@ -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,
}

View file

@ -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",

View file

@ -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,

View file

@ -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",

View file

@ -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,

View file

@ -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",