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
period.
"""
units = hass.config.units
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
with session_scope(hass=hass) as session:
metadata = get_metadata_with_session(
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 = {
meta["statistic_id"]: {
"has_mean": meta["has_mean"],
"has_sum": meta["has_sum"],
"name": meta["name"],
"source": meta["source"],
"display_unit_of_measurement": _display_unit(
hass, meta["unit_of_measurement"]
),
"unit_of_measurement": meta["unit_of_measurement"],
}
for _, meta in metadata.values()
@ -860,14 +861,19 @@ def list_statistic_ids(
hass, statistic_ids=statistic_ids, statistic_type=statistic_type
)
for statistic_id, info in platform_statistic_ids.items():
if (unit := info["unit_of_measurement"]) is not None:
# Display unit according to user settings
unit = _configured_unit(unit, units)
platform_statistic_ids[statistic_id]["unit_of_measurement"] = unit
for key, value in platform_statistic_ids.items():
result.setdefault(key, value)
for key, meta in platform_statistic_ids.items():
if key in result:
continue
result[key] = {
"has_mean": meta["has_mean"],
"has_sum": meta["has_sum"],
"name": meta["name"],
"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 [
@ -877,7 +883,8 @@ def list_statistic_ids(
"has_sum": info["has_sum"],
"name": info.get("name"),
"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()
]

View File

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

View File

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

View File

@ -1118,18 +1118,24 @@ async def test_statistics_during_period_bad_end_time(
@pytest.mark.parametrize(
"units, attributes, unit",
"units, attributes, display_unit, statistics_unit",
[
(IMPERIAL_SYSTEM, POWER_SENSOR_ATTRIBUTES, "W"),
(METRIC_SYSTEM, POWER_SENSOR_ATTRIBUTES, "W"),
(IMPERIAL_SYSTEM, TEMPERATURE_SENSOR_ATTRIBUTES, "°F"),
(METRIC_SYSTEM, TEMPERATURE_SENSOR_ATTRIBUTES, "°C"),
(IMPERIAL_SYSTEM, PRESSURE_SENSOR_ATTRIBUTES, "psi"),
(METRIC_SYSTEM, PRESSURE_SENSOR_ATTRIBUTES, "Pa"),
(IMPERIAL_SYSTEM, POWER_SENSOR_ATTRIBUTES, "W", "W"),
(METRIC_SYSTEM, POWER_SENSOR_ATTRIBUTES, "W", "W"),
(IMPERIAL_SYSTEM, TEMPERATURE_SENSOR_ATTRIBUTES, "°F", "°C"),
(METRIC_SYSTEM, TEMPERATURE_SENSOR_ATTRIBUTES, "°C", "°C"),
(IMPERIAL_SYSTEM, PRESSURE_SENSOR_ATTRIBUTES, "psi", "Pa"),
(METRIC_SYSTEM, PRESSURE_SENSOR_ATTRIBUTES, "Pa", "Pa"),
],
)
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."""
now = dt_util.utcnow()
@ -1158,7 +1164,8 @@ async def test_list_statistic_ids(
"has_sum": False,
"name": None,
"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,
"name": None,
"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,
"name": None,
"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)
assert statistic_ids == [
{
"display_unit_of_measurement": "kWh",
"has_mean": False,
"has_sum": True,
"statistic_id": statistic_id,
"name": "Total imported energy",
"source": source,
"unit_of_measurement": "kWh",
"statistics_unit_of_measurement": "kWh",
}
]
metadata = get_metadata(hass, statistic_ids=(statistic_id,))
@ -616,12 +617,13 @@ async def test_import_statistics(
statistic_ids = list_statistic_ids(hass)
assert statistic_ids == [
{
"display_unit_of_measurement": "kWh",
"has_mean": False,
"has_sum": True,
"statistic_id": statistic_id,
"name": "Total imported energy renamed",
"source": source,
"unit_of_measurement": "kWh",
"statistics_unit_of_measurement": "kWh",
}
]
metadata = get_metadata(hass, statistic_ids=(statistic_id,))

View File

@ -226,11 +226,12 @@ async def test_update_statistics_metadata(
assert response["result"] == [
{
"statistic_id": "sensor.test",
"display_unit_of_measurement": "W",
"has_mean": True,
"has_sum": False,
"name": None,
"source": "recorder",
"unit_of_measurement": "W",
"statistics_unit_of_measurement": "W",
}
]
@ -252,11 +253,12 @@ async def test_update_statistics_metadata(
assert response["result"] == [
{
"statistic_id": "sensor.test",
"display_unit_of_measurement": new_unit,
"has_mean": True,
"has_sum": False,
"name": None,
"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"] == [
{
"statistic_id": "sensor.test",
"display_unit_of_measurement": unit,
"has_mean": False,
"has_sum": True,
"name": None,
"source": "recorder",
"unit_of_measurement": unit,
"statistics_unit_of_measurement": unit,
}
]
@ -552,11 +555,12 @@ async def test_get_statistics_metadata(
assert response["result"] == [
{
"statistic_id": "sensor.test",
"display_unit_of_measurement": unit,
"has_mean": False,
"has_sum": True,
"name": None,
"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
assert statistic_ids == [
{
"display_unit_of_measurement": "kWh",
"has_mean": False,
"has_sum": True,
"statistic_id": statistic_id,
"name": "Total imported energy",
"source": source,
"unit_of_measurement": "kWh",
"statistics_unit_of_measurement": "kWh",
}
]
metadata = get_metadata(hass, statistic_ids=(statistic_id,))

File diff suppressed because it is too large Load Diff