Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion homeassistant/components/recorder/statistics.py
Original file line number Diff line number Diff line change
Expand Up @@ -720,7 +720,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
Expand All @@ -744,6 +744,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"],
Expand Down Expand Up @@ -772,6 +774,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"],
Expand Down
4 changes: 4 additions & 0 deletions homeassistant/components/sensor/recorder.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
Expand All @@ -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,
}
Expand Down
4 changes: 4 additions & 0 deletions tests/components/demo/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
6 changes: 6 additions & 0 deletions tests/components/history/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand Down
2 changes: 2 additions & 0 deletions tests/components/recorder/test_statistics.py
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,8 @@ def test_external_statistics(hass_recorder, 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",
Expand Down
8 changes: 8 additions & 0 deletions tests/components/recorder/test_websocket_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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,
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand Down
Loading