Improve statistics metadata WS API (#77209)

This commit is contained in:
Erik Montnemery 2022-08-31 11:30:45 +02:00 committed by GitHub
parent 7c585bd380
commit 008ac8d10d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 360 additions and 207 deletions

View file

@ -826,27 +826,28 @@ def list_statistic_ids(
a recorder platform for statistic_ids which will be added in the next statistics a recorder platform for statistic_ids which will be added in the next statistics
period. period.
""" """
units = hass.config.units
result = {} result = {}
def _display_unit(hass: HomeAssistant, unit: str | None) -> str | None:
if unit is None:
return None
return _configured_unit(unit, hass.config.units)
# Query the database # Query the database
with session_scope(hass=hass) as session: with session_scope(hass=hass) as session:
metadata = get_metadata_with_session( metadata = get_metadata_with_session(
hass, session, statistic_type=statistic_type, statistic_ids=statistic_ids hass, session, statistic_type=statistic_type, statistic_ids=statistic_ids
) )
for _, meta in metadata.values():
if (unit := meta["unit_of_measurement"]) is not None:
# Display unit according to user settings
unit = _configured_unit(unit, units)
meta["unit_of_measurement"] = unit
result = { result = {
meta["statistic_id"]: { meta["statistic_id"]: {
"has_mean": meta["has_mean"], "has_mean": meta["has_mean"],
"has_sum": meta["has_sum"], "has_sum": meta["has_sum"],
"name": meta["name"], "name": meta["name"],
"source": meta["source"], "source": meta["source"],
"display_unit_of_measurement": _display_unit(
hass, meta["unit_of_measurement"]
),
"unit_of_measurement": meta["unit_of_measurement"], "unit_of_measurement": meta["unit_of_measurement"],
} }
for _, meta in metadata.values() for _, meta in metadata.values()
@ -860,14 +861,19 @@ def list_statistic_ids(
hass, statistic_ids=statistic_ids, statistic_type=statistic_type hass, statistic_ids=statistic_ids, statistic_type=statistic_type
) )
for statistic_id, info in platform_statistic_ids.items(): for key, meta in platform_statistic_ids.items():
if (unit := info["unit_of_measurement"]) is not None: if key in result:
# Display unit according to user settings continue
unit = _configured_unit(unit, units) result[key] = {
platform_statistic_ids[statistic_id]["unit_of_measurement"] = unit "has_mean": meta["has_mean"],
"has_sum": meta["has_sum"],
for key, value in platform_statistic_ids.items(): "name": meta["name"],
result.setdefault(key, value) "source": meta["source"],
"display_unit_of_measurement": _display_unit(
hass, meta["unit_of_measurement"]
),
"unit_of_measurement": meta["unit_of_measurement"],
}
# Return a list of statistic_id + metadata # Return a list of statistic_id + metadata
return [ return [
@ -877,7 +883,8 @@ def list_statistic_ids(
"has_sum": info["has_sum"], "has_sum": info["has_sum"],
"name": info.get("name"), "name": info.get("name"),
"source": info["source"], "source": info["source"],
"unit_of_measurement": info["unit_of_measurement"], "display_unit_of_measurement": info["display_unit_of_measurement"],
"statistics_unit_of_measurement": info["unit_of_measurement"],
} }
for _id, info in result.items() for _id, info in result.items()
] ]

View file

@ -622,7 +622,7 @@ def list_statistic_ids(
"""Return all or filtered statistic_ids and meta data.""" """Return all or filtered statistic_ids and meta data."""
entities = _get_sensor_states(hass) entities = _get_sensor_states(hass)
result = {} result: dict[str, StatisticMetaData] = {}
for state in entities: for state in entities:
state_class = state.attributes[ATTR_STATE_CLASS] state_class = state.attributes[ATTR_STATE_CLASS]
@ -647,7 +647,9 @@ def list_statistic_ids(
result[state.entity_id] = { result[state.entity_id] = {
"has_mean": "mean" in provided_statistics, "has_mean": "mean" in provided_statistics,
"has_sum": "sum" in provided_statistics, "has_sum": "sum" in provided_statistics,
"name": None,
"source": RECORDER_DOMAIN, "source": RECORDER_DOMAIN,
"statistic_id": state.entity_id,
"unit_of_measurement": native_unit, "unit_of_measurement": native_unit,
} }
continue continue
@ -659,7 +661,9 @@ def list_statistic_ids(
result[state.entity_id] = { result[state.entity_id] = {
"has_mean": "mean" in provided_statistics, "has_mean": "mean" in provided_statistics,
"has_sum": "sum" in provided_statistics, "has_sum": "sum" in provided_statistics,
"name": None,
"source": RECORDER_DOMAIN, "source": RECORDER_DOMAIN,
"statistic_id": state.entity_id,
"unit_of_measurement": statistics_unit, "unit_of_measurement": statistics_unit,
} }

View file

@ -62,20 +62,22 @@ async def test_demo_statistics(hass, recorder_mock):
list_statistic_ids, hass list_statistic_ids, hass
) )
assert { assert {
"display_unit_of_measurement": "°C",
"has_mean": True, "has_mean": True,
"has_sum": False, "has_sum": False,
"name": "Outdoor temperature", "name": "Outdoor temperature",
"source": "demo", "source": "demo",
"statistic_id": "demo:temperature_outdoor", "statistic_id": "demo:temperature_outdoor",
"unit_of_measurement": "°C", "statistics_unit_of_measurement": "°C",
} in statistic_ids } in statistic_ids
assert { assert {
"display_unit_of_measurement": "kWh",
"has_mean": False, "has_mean": False,
"has_sum": True, "has_sum": True,
"name": "Energy consumption 1", "name": "Energy consumption 1",
"source": "demo", "source": "demo",
"statistic_id": "demo:energy_consumption_kwh", "statistic_id": "demo:energy_consumption_kwh",
"unit_of_measurement": "kWh", "statistics_unit_of_measurement": "kWh",
} in statistic_ids } in statistic_ids

View file

@ -1118,18 +1118,24 @@ async def test_statistics_during_period_bad_end_time(
@pytest.mark.parametrize( @pytest.mark.parametrize(
"units, attributes, unit", "units, attributes, display_unit, statistics_unit",
[ [
(IMPERIAL_SYSTEM, POWER_SENSOR_ATTRIBUTES, "W"), (IMPERIAL_SYSTEM, POWER_SENSOR_ATTRIBUTES, "W", "W"),
(METRIC_SYSTEM, POWER_SENSOR_ATTRIBUTES, "W"), (METRIC_SYSTEM, POWER_SENSOR_ATTRIBUTES, "W", "W"),
(IMPERIAL_SYSTEM, TEMPERATURE_SENSOR_ATTRIBUTES, "°F"), (IMPERIAL_SYSTEM, TEMPERATURE_SENSOR_ATTRIBUTES, "°F", "°C"),
(METRIC_SYSTEM, TEMPERATURE_SENSOR_ATTRIBUTES, "°C"), (METRIC_SYSTEM, TEMPERATURE_SENSOR_ATTRIBUTES, "°C", "°C"),
(IMPERIAL_SYSTEM, PRESSURE_SENSOR_ATTRIBUTES, "psi"), (IMPERIAL_SYSTEM, PRESSURE_SENSOR_ATTRIBUTES, "psi", "Pa"),
(METRIC_SYSTEM, PRESSURE_SENSOR_ATTRIBUTES, "Pa"), (METRIC_SYSTEM, PRESSURE_SENSOR_ATTRIBUTES, "Pa", "Pa"),
], ],
) )
async def test_list_statistic_ids( async def test_list_statistic_ids(
hass, hass_ws_client, recorder_mock, units, attributes, unit hass,
hass_ws_client,
recorder_mock,
units,
attributes,
display_unit,
statistics_unit,
): ):
"""Test list_statistic_ids.""" """Test list_statistic_ids."""
now = dt_util.utcnow() now = dt_util.utcnow()
@ -1158,7 +1164,8 @@ async def test_list_statistic_ids(
"has_sum": False, "has_sum": False,
"name": None, "name": None,
"source": "recorder", "source": "recorder",
"unit_of_measurement": unit, "display_unit_of_measurement": display_unit,
"statistics_unit_of_measurement": statistics_unit,
} }
] ]
@ -1178,7 +1185,8 @@ async def test_list_statistic_ids(
"has_sum": False, "has_sum": False,
"name": None, "name": None,
"source": "recorder", "source": "recorder",
"unit_of_measurement": unit, "display_unit_of_measurement": display_unit,
"statistics_unit_of_measurement": statistics_unit,
} }
] ]
@ -1200,7 +1208,8 @@ async def test_list_statistic_ids(
"has_sum": False, "has_sum": False,
"name": None, "name": None,
"source": "recorder", "source": "recorder",
"unit_of_measurement": unit, "display_unit_of_measurement": display_unit,
"statistics_unit_of_measurement": statistics_unit,
} }
] ]

View file

@ -524,12 +524,13 @@ async def test_import_statistics(
statistic_ids = list_statistic_ids(hass) statistic_ids = list_statistic_ids(hass)
assert statistic_ids == [ assert statistic_ids == [
{ {
"display_unit_of_measurement": "kWh",
"has_mean": False, "has_mean": False,
"has_sum": True, "has_sum": True,
"statistic_id": statistic_id, "statistic_id": statistic_id,
"name": "Total imported energy", "name": "Total imported energy",
"source": source, "source": source,
"unit_of_measurement": "kWh", "statistics_unit_of_measurement": "kWh",
} }
] ]
metadata = get_metadata(hass, statistic_ids=(statistic_id,)) metadata = get_metadata(hass, statistic_ids=(statistic_id,))
@ -616,12 +617,13 @@ async def test_import_statistics(
statistic_ids = list_statistic_ids(hass) statistic_ids = list_statistic_ids(hass)
assert statistic_ids == [ assert statistic_ids == [
{ {
"display_unit_of_measurement": "kWh",
"has_mean": False, "has_mean": False,
"has_sum": True, "has_sum": True,
"statistic_id": statistic_id, "statistic_id": statistic_id,
"name": "Total imported energy renamed", "name": "Total imported energy renamed",
"source": source, "source": source,
"unit_of_measurement": "kWh", "statistics_unit_of_measurement": "kWh",
} }
] ]
metadata = get_metadata(hass, statistic_ids=(statistic_id,)) metadata = get_metadata(hass, statistic_ids=(statistic_id,))

View file

@ -226,11 +226,12 @@ async def test_update_statistics_metadata(
assert response["result"] == [ assert response["result"] == [
{ {
"statistic_id": "sensor.test", "statistic_id": "sensor.test",
"display_unit_of_measurement": "W",
"has_mean": True, "has_mean": True,
"has_sum": False, "has_sum": False,
"name": None, "name": None,
"source": "recorder", "source": "recorder",
"unit_of_measurement": "W", "statistics_unit_of_measurement": "W",
} }
] ]
@ -252,11 +253,12 @@ async def test_update_statistics_metadata(
assert response["result"] == [ assert response["result"] == [
{ {
"statistic_id": "sensor.test", "statistic_id": "sensor.test",
"display_unit_of_measurement": new_unit,
"has_mean": True, "has_mean": True,
"has_sum": False, "has_sum": False,
"name": None, "name": None,
"source": "recorder", "source": "recorder",
"unit_of_measurement": new_unit, "statistics_unit_of_measurement": new_unit,
} }
] ]
@ -526,11 +528,12 @@ async def test_get_statistics_metadata(
assert response["result"] == [ assert response["result"] == [
{ {
"statistic_id": "sensor.test", "statistic_id": "sensor.test",
"display_unit_of_measurement": unit,
"has_mean": False, "has_mean": False,
"has_sum": True, "has_sum": True,
"name": None, "name": None,
"source": "recorder", "source": "recorder",
"unit_of_measurement": unit, "statistics_unit_of_measurement": unit,
} }
] ]
@ -552,11 +555,12 @@ async def test_get_statistics_metadata(
assert response["result"] == [ assert response["result"] == [
{ {
"statistic_id": "sensor.test", "statistic_id": "sensor.test",
"display_unit_of_measurement": unit,
"has_mean": False, "has_mean": False,
"has_sum": True, "has_sum": True,
"name": None, "name": None,
"source": "recorder", "source": "recorder",
"unit_of_measurement": unit, "statistics_unit_of_measurement": unit,
} }
] ]
@ -646,12 +650,13 @@ async def test_import_statistics(
statistic_ids = list_statistic_ids(hass) # TODO statistic_ids = list_statistic_ids(hass) # TODO
assert statistic_ids == [ assert statistic_ids == [
{ {
"display_unit_of_measurement": "kWh",
"has_mean": False, "has_mean": False,
"has_sum": True, "has_sum": True,
"statistic_id": statistic_id, "statistic_id": statistic_id,
"name": "Total imported energy", "name": "Total imported energy",
"source": source, "source": source,
"unit_of_measurement": "kWh", "statistics_unit_of_measurement": "kWh",
} }
] ]
metadata = get_metadata(hass, statistic_ids=(statistic_id,)) metadata = get_metadata(hass, statistic_ids=(statistic_id,))

File diff suppressed because it is too large Load diff