Use send_json_auto_id in recorder tests (#109355)

This commit is contained in:
Erik Montnemery 2024-02-02 09:34:43 +01:00 committed by GitHub
parent 596f61ff07
commit 1f466e737e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 100 additions and 194 deletions

View File

@ -785,9 +785,8 @@ async def test_import_statistics(
} }
# Adjust the statistics in a different unit # Adjust the statistics in a different unit
await client.send_json( await client.send_json_auto_id(
{ {
"id": 1,
"type": "recorder/adjust_sum_statistics", "type": "recorder/adjust_sum_statistics",
"statistic_id": statistic_id, "statistic_id": statistic_id,
"start_time": period2.isoformat(), "start_time": period2.isoformat(),

View File

@ -156,9 +156,8 @@ async def test_statistics_during_period(
await async_wait_recording_done(hass) await async_wait_recording_done(hass)
client = await hass_ws_client() client = await hass_ws_client()
await client.send_json( await client.send_json_auto_id(
{ {
"id": 1,
"type": "recorder/statistics_during_period", "type": "recorder/statistics_during_period",
"start_time": now.isoformat(), "start_time": now.isoformat(),
"end_time": now.isoformat(), "end_time": now.isoformat(),
@ -170,9 +169,8 @@ async def test_statistics_during_period(
assert response["success"] assert response["success"]
assert response["result"] == {} assert response["result"] == {}
await client.send_json( await client.send_json_auto_id(
{ {
"id": 2,
"type": "recorder/statistics_during_period", "type": "recorder/statistics_during_period",
"start_time": now.isoformat(), "start_time": now.isoformat(),
"statistic_ids": ["sensor.test"], "statistic_ids": ["sensor.test"],
@ -194,9 +192,8 @@ async def test_statistics_during_period(
] ]
} }
await client.send_json( await client.send_json_auto_id(
{ {
"id": 3,
"type": "recorder/statistics_during_period", "type": "recorder/statistics_during_period",
"start_time": now.isoformat(), "start_time": now.isoformat(),
"statistic_ids": ["sensor.test"], "statistic_ids": ["sensor.test"],
@ -226,13 +223,6 @@ async def test_statistic_during_period(
offset, offset,
) -> None: ) -> None:
"""Test statistic_during_period.""" """Test statistic_during_period."""
id = 1
def next_id():
nonlocal id
id += 1
return id
now = dt_util.utcnow() now = dt_util.utcnow()
await async_recorder_block_till_done(hass) await async_recorder_block_till_done(hass)
@ -313,9 +303,8 @@ async def test_statistic_during_period(
assert run_cache.get_latest_ids({metadata_id}) is not None assert run_cache.get_latest_ids({metadata_id}) is not None
# No data for this period yet # No data for this period yet
await client.send_json( await client.send_json_auto_id(
{ {
"id": next_id(),
"type": "recorder/statistic_during_period", "type": "recorder/statistic_during_period",
"fixed_period": { "fixed_period": {
"start_time": now.isoformat(), "start_time": now.isoformat(),
@ -334,9 +323,8 @@ async def test_statistic_during_period(
} }
# This should include imported_statistics_5min[:] # This should include imported_statistics_5min[:]
await client.send_json( await client.send_json_auto_id(
{ {
"id": next_id(),
"type": "recorder/statistic_during_period", "type": "recorder/statistic_during_period",
"statistic_id": "sensor.test", "statistic_id": "sensor.test",
} }
@ -359,9 +347,8 @@ async def test_statistic_during_period(
dt_util.parse_datetime("2022-10-21T07:15:00+00:00") dt_util.parse_datetime("2022-10-21T07:15:00+00:00")
+ timedelta(minutes=5 * offset) + timedelta(minutes=5 * offset)
).isoformat() ).isoformat()
await client.send_json( await client.send_json_auto_id(
{ {
"id": next_id(),
"type": "recorder/statistic_during_period", "type": "recorder/statistic_during_period",
"statistic_id": "sensor.test", "statistic_id": "sensor.test",
"fixed_period": { "fixed_period": {
@ -388,9 +375,8 @@ async def test_statistic_during_period(
dt_util.parse_datetime("2022-10-21T08:20:00+00:00") dt_util.parse_datetime("2022-10-21T08:20:00+00:00")
+ timedelta(minutes=5 * offset) + timedelta(minutes=5 * offset)
).isoformat() ).isoformat()
await client.send_json( await client.send_json_auto_id(
{ {
"id": next_id(),
"type": "recorder/statistic_during_period", "type": "recorder/statistic_during_period",
"statistic_id": "sensor.test", "statistic_id": "sensor.test",
"fixed_period": { "fixed_period": {
@ -414,9 +400,8 @@ async def test_statistic_during_period(
+ timedelta(minutes=5 * offset) + timedelta(minutes=5 * offset)
).isoformat() ).isoformat()
assert imported_stats_5min[26]["start"].isoformat() == start_time assert imported_stats_5min[26]["start"].isoformat() == start_time
await client.send_json( await client.send_json_auto_id(
{ {
"id": next_id(),
"type": "recorder/statistic_during_period", "type": "recorder/statistic_during_period",
"fixed_period": { "fixed_period": {
"start_time": start_time, "start_time": start_time,
@ -438,9 +423,8 @@ async def test_statistic_during_period(
dt_util.parse_datetime("2022-10-21T06:09:00+00:00") dt_util.parse_datetime("2022-10-21T06:09:00+00:00")
+ timedelta(minutes=5 * offset) + timedelta(minutes=5 * offset)
).isoformat() ).isoformat()
await client.send_json( await client.send_json_auto_id(
{ {
"id": next_id(),
"type": "recorder/statistic_during_period", "type": "recorder/statistic_during_period",
"fixed_period": { "fixed_period": {
"start_time": start_time, "start_time": start_time,
@ -463,9 +447,8 @@ async def test_statistic_during_period(
+ timedelta(minutes=5 * offset) + timedelta(minutes=5 * offset)
).isoformat() ).isoformat()
assert imported_stats_5min[26]["start"].isoformat() == end_time assert imported_stats_5min[26]["start"].isoformat() == end_time
await client.send_json( await client.send_json_auto_id(
{ {
"id": next_id(),
"type": "recorder/statistic_during_period", "type": "recorder/statistic_during_period",
"fixed_period": { "fixed_period": {
"end_time": end_time, "end_time": end_time,
@ -493,9 +476,8 @@ async def test_statistic_during_period(
+ timedelta(minutes=5 * offset) + timedelta(minutes=5 * offset)
).isoformat() ).isoformat()
assert imported_stats_5min[32]["start"].isoformat() == end_time assert imported_stats_5min[32]["start"].isoformat() == end_time
await client.send_json( await client.send_json_auto_id(
{ {
"id": next_id(),
"type": "recorder/statistic_during_period", "type": "recorder/statistic_during_period",
"fixed_period": { "fixed_period": {
"start_time": start_time, "start_time": start_time,
@ -517,9 +499,8 @@ async def test_statistic_during_period(
start_time = "2022-10-21T06:00:00+00:00" start_time = "2022-10-21T06:00:00+00:00"
assert imported_stats_5min[24 - offset]["start"].isoformat() == start_time assert imported_stats_5min[24 - offset]["start"].isoformat() == start_time
assert imported_stats[2]["start"].isoformat() == start_time assert imported_stats[2]["start"].isoformat() == start_time
await client.send_json( await client.send_json_auto_id(
{ {
"id": next_id(),
"type": "recorder/statistic_during_period", "type": "recorder/statistic_during_period",
"fixed_period": { "fixed_period": {
"start_time": start_time, "start_time": start_time,
@ -538,9 +519,8 @@ async def test_statistic_during_period(
} }
# This should also include imported_statistics[2:] + imported_statistics_5min[36:] # This should also include imported_statistics[2:] + imported_statistics_5min[36:]
await client.send_json( await client.send_json_auto_id(
{ {
"id": next_id(),
"type": "recorder/statistic_during_period", "type": "recorder/statistic_during_period",
"rolling_window": { "rolling_window": {
"duration": {"hours": 1, "minutes": 25}, "duration": {"hours": 1, "minutes": 25},
@ -559,9 +539,8 @@ async def test_statistic_during_period(
} }
# This should include imported_statistics[2:3] # This should include imported_statistics[2:3]
await client.send_json( await client.send_json_auto_id(
{ {
"id": next_id(),
"type": "recorder/statistic_during_period", "type": "recorder/statistic_during_period",
"rolling_window": { "rolling_window": {
"duration": {"hours": 1}, "duration": {"hours": 1},
@ -585,9 +564,8 @@ async def test_statistic_during_period(
} }
# Test we can get only selected types # Test we can get only selected types
await client.send_json( await client.send_json_auto_id(
{ {
"id": next_id(),
"type": "recorder/statistic_during_period", "type": "recorder/statistic_during_period",
"statistic_id": "sensor.test", "statistic_id": "sensor.test",
"types": ["max", "change"], "types": ["max", "change"],
@ -601,9 +579,8 @@ async def test_statistic_during_period(
} }
# Test we can convert units # Test we can convert units
await client.send_json( await client.send_json_auto_id(
{ {
"id": next_id(),
"type": "recorder/statistic_during_period", "type": "recorder/statistic_during_period",
"statistic_id": "sensor.test", "statistic_id": "sensor.test",
"units": {"energy": "MWh"}, "units": {"energy": "MWh"},
@ -621,9 +598,8 @@ async def test_statistic_during_period(
# Test we can automatically convert units # Test we can automatically convert units
hass.states.async_set("sensor.test", None, attributes=ENERGY_SENSOR_WH_ATTRIBUTES) hass.states.async_set("sensor.test", None, attributes=ENERGY_SENSOR_WH_ATTRIBUTES)
await client.send_json( await client.send_json_auto_id(
{ {
"id": next_id(),
"type": "recorder/statistic_during_period", "type": "recorder/statistic_during_period",
"statistic_id": "sensor.test", "statistic_id": "sensor.test",
} }
@ -707,9 +683,8 @@ async def test_statistic_during_period_hole(
await async_wait_recording_done(hass) await async_wait_recording_done(hass)
# This should include imported_stats[:] # This should include imported_stats[:]
await client.send_json( await client.send_json_auto_id(
{ {
"id": next_id(),
"type": "recorder/statistic_during_period", "type": "recorder/statistic_during_period",
"statistic_id": "sensor.test", "statistic_id": "sensor.test",
} }
@ -728,9 +703,8 @@ async def test_statistic_during_period_hole(
end_time = "2022-10-21T05:00:00+00:00" end_time = "2022-10-21T05:00:00+00:00"
assert imported_stats[0]["start"].isoformat() == start_time assert imported_stats[0]["start"].isoformat() == start_time
assert imported_stats[-1]["start"].isoformat() < end_time assert imported_stats[-1]["start"].isoformat() < end_time
await client.send_json( await client.send_json_auto_id(
{ {
"id": next_id(),
"type": "recorder/statistic_during_period", "type": "recorder/statistic_during_period",
"statistic_id": "sensor.test", "statistic_id": "sensor.test",
"fixed_period": { "fixed_period": {
@ -751,9 +725,8 @@ async def test_statistic_during_period_hole(
# This should also include imported_stats[:] # This should also include imported_stats[:]
start_time = "2022-10-20T13:00:00+00:00" start_time = "2022-10-20T13:00:00+00:00"
end_time = "2022-10-21T08:20:00+00:00" end_time = "2022-10-21T08:20:00+00:00"
await client.send_json( await client.send_json_auto_id(
{ {
"id": next_id(),
"type": "recorder/statistic_during_period", "type": "recorder/statistic_during_period",
"statistic_id": "sensor.test", "statistic_id": "sensor.test",
"fixed_period": { "fixed_period": {
@ -776,9 +749,8 @@ async def test_statistic_during_period_hole(
end_time = "2022-10-20T23:00:00+00:00" end_time = "2022-10-20T23:00:00+00:00"
assert imported_stats[1]["start"].isoformat() == start_time assert imported_stats[1]["start"].isoformat() == start_time
assert imported_stats[3]["start"].isoformat() < end_time assert imported_stats[3]["start"].isoformat() < end_time
await client.send_json( await client.send_json_auto_id(
{ {
"id": next_id(),
"type": "recorder/statistic_during_period", "type": "recorder/statistic_during_period",
"statistic_id": "sensor.test", "statistic_id": "sensor.test",
"fixed_period": { "fixed_period": {
@ -801,9 +773,8 @@ async def test_statistic_during_period_hole(
end_time = "2022-10-21T00:00:00+00:00" end_time = "2022-10-21T00:00:00+00:00"
assert imported_stats[1]["start"].isoformat() > start_time assert imported_stats[1]["start"].isoformat() > start_time
assert imported_stats[3]["start"].isoformat() < end_time assert imported_stats[3]["start"].isoformat() < end_time
await client.send_json( await client.send_json_auto_id(
{ {
"id": next_id(),
"type": "recorder/statistic_during_period", "type": "recorder/statistic_during_period",
"statistic_id": "sensor.test", "statistic_id": "sensor.test",
"fixed_period": { "fixed_period": {
@ -894,9 +865,8 @@ async def test_statistic_during_period_calendar(
"homeassistant.components.recorder.websocket_api.statistic_during_period", "homeassistant.components.recorder.websocket_api.statistic_during_period",
return_value={}, return_value={},
) as statistic_during_period: ) as statistic_during_period:
await client.send_json( await client.send_json_auto_id(
{ {
"id": 1,
"type": "recorder/statistic_during_period", "type": "recorder/statistic_during_period",
"calendar": calendar_period, "calendar": calendar_period,
"statistic_id": "sensor.test", "statistic_id": "sensor.test",
@ -956,9 +926,8 @@ async def test_statistics_during_period_unit_conversion(
client = await hass_ws_client() client = await hass_ws_client()
# Query in state unit # Query in state unit
await client.send_json( await client.send_json_auto_id(
{ {
"id": 1,
"type": "recorder/statistics_during_period", "type": "recorder/statistics_during_period",
"start_time": now.isoformat(), "start_time": now.isoformat(),
"statistic_ids": ["sensor.test"], "statistic_ids": ["sensor.test"],
@ -981,9 +950,8 @@ async def test_statistics_during_period_unit_conversion(
} }
# Query in custom unit # Query in custom unit
await client.send_json( await client.send_json_auto_id(
{ {
"id": 2,
"type": "recorder/statistics_during_period", "type": "recorder/statistics_during_period",
"start_time": now.isoformat(), "start_time": now.isoformat(),
"statistic_ids": ["sensor.test"], "statistic_ids": ["sensor.test"],
@ -1044,9 +1012,8 @@ async def test_sum_statistics_during_period_unit_conversion(
client = await hass_ws_client() client = await hass_ws_client()
# Query in state unit # Query in state unit
await client.send_json( await client.send_json_auto_id(
{ {
"id": 1,
"type": "recorder/statistics_during_period", "type": "recorder/statistics_during_period",
"start_time": now.isoformat(), "start_time": now.isoformat(),
"statistic_ids": ["sensor.test"], "statistic_ids": ["sensor.test"],
@ -1069,9 +1036,8 @@ async def test_sum_statistics_during_period_unit_conversion(
} }
# Query in custom unit # Query in custom unit
await client.send_json( await client.send_json_auto_id(
{ {
"id": 2,
"type": "recorder/statistics_during_period", "type": "recorder/statistics_during_period",
"start_time": now.isoformat(), "start_time": now.isoformat(),
"statistic_ids": ["sensor.test"], "statistic_ids": ["sensor.test"],
@ -1121,9 +1087,8 @@ async def test_statistics_during_period_invalid_unit_conversion(
client = await hass_ws_client() client = await hass_ws_client()
# Query in state unit # Query in state unit
await client.send_json( await client.send_json_auto_id(
{ {
"id": 1,
"type": "recorder/statistics_during_period", "type": "recorder/statistics_during_period",
"start_time": now.isoformat(), "start_time": now.isoformat(),
"statistic_ids": ["sensor.test"], "statistic_ids": ["sensor.test"],
@ -1135,9 +1100,8 @@ async def test_statistics_during_period_invalid_unit_conversion(
assert response["result"] == {} assert response["result"] == {}
# Query in custom unit # Query in custom unit
await client.send_json( await client.send_json_auto_id(
{ {
"id": 2,
"type": "recorder/statistics_during_period", "type": "recorder/statistics_during_period",
"start_time": now.isoformat(), "start_time": now.isoformat(),
"statistic_ids": ["sensor.test"], "statistic_ids": ["sensor.test"],
@ -1176,9 +1140,8 @@ async def test_statistics_during_period_in_the_past(
await async_wait_recording_done(hass) await async_wait_recording_done(hass)
client = await hass_ws_client() client = await hass_ws_client()
await client.send_json( await client.send_json_auto_id(
{ {
"id": 1,
"type": "recorder/statistics_during_period", "type": "recorder/statistics_during_period",
"start_time": now.isoformat(), "start_time": now.isoformat(),
"end_time": now.isoformat(), "end_time": now.isoformat(),
@ -1190,9 +1153,8 @@ async def test_statistics_during_period_in_the_past(
assert response["success"] assert response["success"]
assert response["result"] == {} assert response["result"] == {}
await client.send_json( await client.send_json_auto_id(
{ {
"id": 2,
"type": "recorder/statistics_during_period", "type": "recorder/statistics_during_period",
"start_time": now.isoformat(), "start_time": now.isoformat(),
"statistic_ids": ["sensor.test"], "statistic_ids": ["sensor.test"],
@ -1204,9 +1166,8 @@ async def test_statistics_during_period_in_the_past(
assert response["result"] == {} assert response["result"] == {}
past = now - timedelta(days=3, hours=1) past = now - timedelta(days=3, hours=1)
await client.send_json( await client.send_json_auto_id(
{ {
"id": 3,
"type": "recorder/statistics_during_period", "type": "recorder/statistics_during_period",
"start_time": past.isoformat(), "start_time": past.isoformat(),
"statistic_ids": ["sensor.test"], "statistic_ids": ["sensor.test"],
@ -1229,9 +1190,8 @@ async def test_statistics_during_period_in_the_past(
} }
start_of_day = stats_top_of_hour.replace(hour=0, minute=0) start_of_day = stats_top_of_hour.replace(hour=0, minute=0)
await client.send_json( await client.send_json_auto_id(
{ {
"id": 4,
"type": "recorder/statistics_during_period", "type": "recorder/statistics_during_period",
"start_time": stats_top_of_hour.isoformat(), "start_time": stats_top_of_hour.isoformat(),
"statistic_ids": ["sensor.test"], "statistic_ids": ["sensor.test"],
@ -1253,9 +1213,8 @@ async def test_statistics_during_period_in_the_past(
] ]
} }
await client.send_json( await client.send_json_auto_id(
{ {
"id": 5,
"type": "recorder/statistics_during_period", "type": "recorder/statistics_during_period",
"start_time": now.isoformat(), "start_time": now.isoformat(),
"statistic_ids": ["sensor.test"], "statistic_ids": ["sensor.test"],
@ -1272,9 +1231,8 @@ async def test_statistics_during_period_bad_start_time(
) -> None: ) -> None:
"""Test statistics_during_period.""" """Test statistics_during_period."""
client = await hass_ws_client() client = await hass_ws_client()
await client.send_json( await client.send_json_auto_id(
{ {
"id": 1,
"type": "recorder/statistics_during_period", "type": "recorder/statistics_during_period",
"start_time": "cats", "start_time": "cats",
"statistic_ids": ["sensor.test"], "statistic_ids": ["sensor.test"],
@ -1293,9 +1251,8 @@ async def test_statistics_during_period_bad_end_time(
now = dt_util.utcnow() now = dt_util.utcnow()
client = await hass_ws_client() client = await hass_ws_client()
await client.send_json( await client.send_json_auto_id(
{ {
"id": 1,
"type": "recorder/statistics_during_period", "type": "recorder/statistics_during_period",
"start_time": now.isoformat(), "start_time": now.isoformat(),
"end_time": "dogs", "end_time": "dogs",
@ -1315,9 +1272,8 @@ async def test_statistics_during_period_no_statistic_ids(
now = dt_util.utcnow() now = dt_util.utcnow()
client = await hass_ws_client() client = await hass_ws_client()
await client.send_json( await client.send_json_auto_id(
{ {
"id": 1,
"type": "recorder/statistics_during_period", "type": "recorder/statistics_during_period",
"start_time": now.isoformat(), "start_time": now.isoformat(),
"end_time": (now + timedelta(seconds=1)).isoformat(), "end_time": (now + timedelta(seconds=1)).isoformat(),
@ -1336,9 +1292,8 @@ async def test_statistics_during_period_empty_statistic_ids(
now = dt_util.utcnow() now = dt_util.utcnow()
client = await hass_ws_client() client = await hass_ws_client()
await client.send_json( await client.send_json_auto_id(
{ {
"id": 1,
"type": "recorder/statistics_during_period", "type": "recorder/statistics_during_period",
"start_time": now.isoformat(), "start_time": now.isoformat(),
"statistic_ids": [], "statistic_ids": [],
@ -1428,7 +1383,7 @@ async def test_list_statistic_ids(
await async_recorder_block_till_done(hass) await async_recorder_block_till_done(hass)
client = await hass_ws_client() client = await hass_ws_client()
await client.send_json({"id": 1, "type": "recorder/list_statistic_ids"}) await client.send_json_auto_id({"type": "recorder/list_statistic_ids"})
response = await client.receive_json() response = await client.receive_json()
assert response["success"] assert response["success"]
assert response["result"] == [] assert response["result"] == []
@ -1436,7 +1391,7 @@ async def test_list_statistic_ids(
hass.states.async_set("sensor.test", 10, attributes=attributes) hass.states.async_set("sensor.test", 10, attributes=attributes)
await async_wait_recording_done(hass) await async_wait_recording_done(hass)
await client.send_json({"id": 2, "type": "recorder/list_statistic_ids"}) await client.send_json_auto_id({"type": "recorder/list_statistic_ids"})
response = await client.receive_json() response = await client.receive_json()
assert response["success"] assert response["success"]
assert response["result"] == [ assert response["result"] == [
@ -1458,7 +1413,7 @@ async def test_list_statistic_ids(
hass.states.async_remove("sensor.test") hass.states.async_remove("sensor.test")
await hass.async_block_till_done() await hass.async_block_till_done()
await client.send_json({"id": 3, "type": "recorder/list_statistic_ids"}) await client.send_json_auto_id({"type": "recorder/list_statistic_ids"})
response = await client.receive_json() response = await client.receive_json()
assert response["success"] assert response["success"]
assert response["result"] == [ assert response["result"] == [
@ -1474,14 +1429,14 @@ async def test_list_statistic_ids(
} }
] ]
await client.send_json( await client.send_json_auto_id(
{"id": 4, "type": "recorder/list_statistic_ids", "statistic_type": "dogs"} {"type": "recorder/list_statistic_ids", "statistic_type": "dogs"}
) )
response = await client.receive_json() response = await client.receive_json()
assert not response["success"] assert not response["success"]
await client.send_json( await client.send_json_auto_id(
{"id": 5, "type": "recorder/list_statistic_ids", "statistic_type": "mean"} {"type": "recorder/list_statistic_ids", "statistic_type": "mean"}
) )
response = await client.receive_json() response = await client.receive_json()
assert response["success"] assert response["success"]
@ -1501,8 +1456,8 @@ async def test_list_statistic_ids(
else: else:
assert response["result"] == [] assert response["result"] == []
await client.send_json( await client.send_json_auto_id(
{"id": 6, "type": "recorder/list_statistic_ids", "statistic_type": "sum"} {"type": "recorder/list_statistic_ids", "statistic_type": "sum"}
) )
response = await client.receive_json() response = await client.receive_json()
assert response["success"] assert response["success"]
@ -1591,7 +1546,7 @@ async def test_list_statistic_ids_unit_change(
await async_recorder_block_till_done(hass) await async_recorder_block_till_done(hass)
client = await hass_ws_client() client = await hass_ws_client()
await client.send_json({"id": 1, "type": "recorder/list_statistic_ids"}) await client.send_json_auto_id({"type": "recorder/list_statistic_ids"})
response = await client.receive_json() response = await client.receive_json()
assert response["success"] assert response["success"]
assert response["result"] == [] assert response["result"] == []
@ -1602,7 +1557,7 @@ async def test_list_statistic_ids_unit_change(
do_adhoc_statistics(hass, start=now) do_adhoc_statistics(hass, start=now)
await async_recorder_block_till_done(hass) await async_recorder_block_till_done(hass)
await client.send_json({"id": 2, "type": "recorder/list_statistic_ids"}) await client.send_json_auto_id({"type": "recorder/list_statistic_ids"})
response = await client.receive_json() response = await client.receive_json()
assert response["success"] assert response["success"]
assert response["result"] == [ assert response["result"] == [
@ -1621,7 +1576,7 @@ async def test_list_statistic_ids_unit_change(
# Change the state unit # Change the state unit
hass.states.async_set("sensor.test", 10, attributes=attributes2) hass.states.async_set("sensor.test", 10, attributes=attributes2)
await client.send_json({"id": 3, "type": "recorder/list_statistic_ids"}) await client.send_json_auto_id({"type": "recorder/list_statistic_ids"})
response = await client.receive_json() response = await client.receive_json()
assert response["success"] assert response["success"]
assert response["result"] == [ assert response["result"] == [
@ -1642,17 +1597,9 @@ async def test_validate_statistics(
recorder_mock: Recorder, hass: HomeAssistant, hass_ws_client: WebSocketGenerator recorder_mock: Recorder, hass: HomeAssistant, hass_ws_client: WebSocketGenerator
) -> None: ) -> None:
"""Test validate_statistics can be called.""" """Test validate_statistics can be called."""
id = 1
def next_id():
nonlocal id
id += 1
return id
async def assert_validation_result(client, expected_result): async def assert_validation_result(client, expected_result):
await client.send_json( await client.send_json_auto_id({"type": "recorder/validate_statistics"})
{"id": next_id(), "type": "recorder/validate_statistics"}
)
response = await client.receive_json() response = await client.receive_json()
assert response["success"] assert response["success"]
assert response["result"] == expected_result assert response["result"] == expected_result
@ -1685,9 +1632,8 @@ async def test_clear_statistics(
await async_recorder_block_till_done(hass) await async_recorder_block_till_done(hass)
client = await hass_ws_client() client = await hass_ws_client()
await client.send_json( await client.send_json_auto_id(
{ {
"id": 1,
"type": "recorder/statistics_during_period", "type": "recorder/statistics_during_period",
"start_time": now.isoformat(), "start_time": now.isoformat(),
"statistic_ids": ["sensor.test1", "sensor.test2", "sensor.test3"], "statistic_ids": ["sensor.test1", "sensor.test2", "sensor.test3"],
@ -1730,9 +1676,8 @@ async def test_clear_statistics(
} }
assert response["result"] == expected_response assert response["result"] == expected_response
await client.send_json( await client.send_json_auto_id(
{ {
"id": 2,
"type": "recorder/clear_statistics", "type": "recorder/clear_statistics",
"statistic_ids": ["sensor.test"], "statistic_ids": ["sensor.test"],
} }
@ -1742,9 +1687,8 @@ async def test_clear_statistics(
await async_recorder_block_till_done(hass) await async_recorder_block_till_done(hass)
client = await hass_ws_client() client = await hass_ws_client()
await client.send_json( await client.send_json_auto_id(
{ {
"id": 3,
"type": "recorder/statistics_during_period", "type": "recorder/statistics_during_period",
"statistic_ids": ["sensor.test1", "sensor.test2", "sensor.test3"], "statistic_ids": ["sensor.test1", "sensor.test2", "sensor.test3"],
"start_time": now.isoformat(), "start_time": now.isoformat(),
@ -1755,9 +1699,8 @@ async def test_clear_statistics(
assert response["success"] assert response["success"]
assert response["result"] == expected_response assert response["result"] == expected_response
await client.send_json( await client.send_json_auto_id(
{ {
"id": 4,
"type": "recorder/clear_statistics", "type": "recorder/clear_statistics",
"statistic_ids": ["sensor.test1", "sensor.test3"], "statistic_ids": ["sensor.test1", "sensor.test3"],
} }
@ -1767,9 +1710,8 @@ async def test_clear_statistics(
await async_recorder_block_till_done(hass) await async_recorder_block_till_done(hass)
client = await hass_ws_client() client = await hass_ws_client()
await client.send_json( await client.send_json_auto_id(
{ {
"id": 5,
"type": "recorder/statistics_during_period", "type": "recorder/statistics_during_period",
"statistic_ids": ["sensor.test1", "sensor.test2", "sensor.test3"], "statistic_ids": ["sensor.test1", "sensor.test2", "sensor.test3"],
"start_time": now.isoformat(), "start_time": now.isoformat(),
@ -1811,7 +1753,7 @@ async def test_update_statistics_metadata(
client = await hass_ws_client() client = await hass_ws_client()
await client.send_json({"id": 1, "type": "recorder/list_statistic_ids"}) await client.send_json_auto_id({"type": "recorder/list_statistic_ids"})
response = await client.receive_json() response = await client.receive_json()
assert response["success"] assert response["success"]
assert response["result"] == [ assert response["result"] == [
@ -1827,9 +1769,8 @@ async def test_update_statistics_metadata(
} }
] ]
await client.send_json( await client.send_json_auto_id(
{ {
"id": 2,
"type": "recorder/update_statistics_metadata", "type": "recorder/update_statistics_metadata",
"statistic_id": "sensor.test", "statistic_id": "sensor.test",
"unit_of_measurement": new_unit, "unit_of_measurement": new_unit,
@ -1839,7 +1780,7 @@ async def test_update_statistics_metadata(
assert response["success"] assert response["success"]
await async_recorder_block_till_done(hass) await async_recorder_block_till_done(hass)
await client.send_json({"id": 3, "type": "recorder/list_statistic_ids"}) await client.send_json_auto_id({"type": "recorder/list_statistic_ids"})
response = await client.receive_json() response = await client.receive_json()
assert response["success"] assert response["success"]
assert response["result"] == [ assert response["result"] == [
@ -1855,9 +1796,8 @@ async def test_update_statistics_metadata(
} }
] ]
await client.send_json( await client.send_json_auto_id(
{ {
"id": 5,
"type": "recorder/statistics_during_period", "type": "recorder/statistics_during_period",
"start_time": now.isoformat(), "start_time": now.isoformat(),
"statistic_ids": ["sensor.test"], "statistic_ids": ["sensor.test"],
@ -1902,7 +1842,7 @@ async def test_change_statistics_unit(
client = await hass_ws_client() client = await hass_ws_client()
await client.send_json({"id": 1, "type": "recorder/list_statistic_ids"}) await client.send_json_auto_id({"type": "recorder/list_statistic_ids"})
response = await client.receive_json() response = await client.receive_json()
assert response["success"] assert response["success"]
assert response["result"] == [ assert response["result"] == [
@ -1918,9 +1858,8 @@ async def test_change_statistics_unit(
} }
] ]
await client.send_json( await client.send_json_auto_id(
{ {
"id": 2,
"type": "recorder/statistics_during_period", "type": "recorder/statistics_during_period",
"start_time": now.isoformat(), "start_time": now.isoformat(),
"statistic_ids": ["sensor.test"], "statistic_ids": ["sensor.test"],
@ -1942,9 +1881,8 @@ async def test_change_statistics_unit(
], ],
} }
await client.send_json( await client.send_json_auto_id(
{ {
"id": 3,
"type": "recorder/change_statistics_unit", "type": "recorder/change_statistics_unit",
"statistic_id": "sensor.test", "statistic_id": "sensor.test",
"new_unit_of_measurement": "W", "new_unit_of_measurement": "W",
@ -1955,7 +1893,7 @@ async def test_change_statistics_unit(
assert response["success"] assert response["success"]
await async_recorder_block_till_done(hass) await async_recorder_block_till_done(hass)
await client.send_json({"id": 4, "type": "recorder/list_statistic_ids"}) await client.send_json_auto_id({"type": "recorder/list_statistic_ids"})
response = await client.receive_json() response = await client.receive_json()
assert response["success"] assert response["success"]
assert response["result"] == [ assert response["result"] == [
@ -1971,9 +1909,8 @@ async def test_change_statistics_unit(
} }
] ]
await client.send_json( await client.send_json_auto_id(
{ {
"id": 5,
"type": "recorder/statistics_during_period", "type": "recorder/statistics_during_period",
"start_time": now.isoformat(), "start_time": now.isoformat(),
"statistic_ids": ["sensor.test"], "statistic_ids": ["sensor.test"],
@ -1997,9 +1934,8 @@ async def test_change_statistics_unit(
} }
# Changing to the same unit is allowed but does nothing # Changing to the same unit is allowed but does nothing
await client.send_json( await client.send_json_auto_id(
{ {
"id": 6,
"type": "recorder/change_statistics_unit", "type": "recorder/change_statistics_unit",
"statistic_id": "sensor.test", "statistic_id": "sensor.test",
"new_unit_of_measurement": "W", "new_unit_of_measurement": "W",
@ -2010,7 +1946,7 @@ async def test_change_statistics_unit(
assert response["success"] assert response["success"]
await async_recorder_block_till_done(hass) await async_recorder_block_till_done(hass)
await client.send_json({"id": 7, "type": "recorder/list_statistic_ids"}) await client.send_json_auto_id({"type": "recorder/list_statistic_ids"})
response = await client.receive_json() response = await client.receive_json()
assert response["success"] assert response["success"]
assert response["result"] == [ assert response["result"] == [
@ -2035,7 +1971,6 @@ async def test_change_statistics_unit_errors(
) -> None: ) -> None:
"""Test change unit of recorded statistics.""" """Test change unit of recorded statistics."""
now = dt_util.utcnow() now = dt_util.utcnow()
ws_id = 0
units = METRIC_SYSTEM units = METRIC_SYSTEM
attributes = POWER_SENSOR_KW_ATTRIBUTES | {"device_class": None} attributes = POWER_SENSOR_KW_ATTRIBUTES | {"device_class": None}
@ -2068,19 +2003,14 @@ async def test_change_statistics_unit_errors(
} }
async def assert_statistic_ids(expected): async def assert_statistic_ids(expected):
nonlocal ws_id await client.send_json_auto_id({"type": "recorder/list_statistic_ids"})
ws_id += 1
await client.send_json({"id": ws_id, "type": "recorder/list_statistic_ids"})
response = await client.receive_json() response = await client.receive_json()
assert response["success"] assert response["success"]
assert response["result"] == expected assert response["result"] == expected
async def assert_statistics(expected): async def assert_statistics(expected):
nonlocal ws_id await client.send_json_auto_id(
ws_id += 1
await client.send_json(
{ {
"id": ws_id,
"type": "recorder/statistics_during_period", "type": "recorder/statistics_during_period",
"start_time": now.isoformat(), "start_time": now.isoformat(),
"statistic_ids": ["sensor.test"], "statistic_ids": ["sensor.test"],
@ -2106,10 +2036,8 @@ async def test_change_statistics_unit_errors(
await assert_statistics(expected_statistics) await assert_statistics(expected_statistics)
# Try changing to an invalid unit # Try changing to an invalid unit
ws_id += 1 await client.send_json_auto_id(
await client.send_json(
{ {
"id": ws_id,
"type": "recorder/change_statistics_unit", "type": "recorder/change_statistics_unit",
"statistic_id": "sensor.test", "statistic_id": "sensor.test",
"old_unit_of_measurement": "kW", "old_unit_of_measurement": "kW",
@ -2126,10 +2054,8 @@ async def test_change_statistics_unit_errors(
await assert_statistics(expected_statistics) await assert_statistics(expected_statistics)
# Try changing from the wrong unit # Try changing from the wrong unit
ws_id += 1 await client.send_json_auto_id(
await client.send_json(
{ {
"id": ws_id,
"type": "recorder/change_statistics_unit", "type": "recorder/change_statistics_unit",
"statistic_id": "sensor.test", "statistic_id": "sensor.test",
"old_unit_of_measurement": "W", "old_unit_of_measurement": "W",
@ -2155,7 +2081,7 @@ async def test_recorder_info(
# Ensure there are no queued events # Ensure there are no queued events
await async_wait_recording_done(hass) await async_wait_recording_done(hass)
await client.send_json({"id": 1, "type": "recorder/info"}) await client.send_json_auto_id({"type": "recorder/info"})
response = await client.receive_json() response = await client.receive_json()
assert response["success"] assert response["success"]
assert response["result"] == { assert response["result"] == {
@ -2174,7 +2100,7 @@ async def test_recorder_info_no_recorder(
"""Test getting recorder status when recorder is not present.""" """Test getting recorder status when recorder is not present."""
client = await hass_ws_client() client = await hass_ws_client()
await client.send_json({"id": 1, "type": "recorder/info"}) await client.send_json_auto_id({"type": "recorder/info"})
response = await client.receive_json() response = await client.receive_json()
assert not response["success"] assert not response["success"]
assert response["error"]["code"] == "unknown_command" assert response["error"]["code"] == "unknown_command"
@ -2199,7 +2125,7 @@ async def test_recorder_info_bad_recorder_config(
# Wait for recorder to shut down # Wait for recorder to shut down
await hass.async_add_executor_job(recorder.get_instance(hass).join) await hass.async_add_executor_job(recorder.get_instance(hass).join)
await client.send_json({"id": 1, "type": "recorder/info"}) await client.send_json_auto_id({"type": "recorder/info"})
response = await client.receive_json() response = await client.receive_json()
assert response["success"] assert response["success"]
assert response["result"]["recording"] is False assert response["result"]["recording"] is False
@ -2250,7 +2176,7 @@ async def test_recorder_info_migration_queue_exhausted(
client = await hass_ws_client() client = await hass_ws_client()
# Check the status # Check the status
await client.send_json({"id": 1, "type": "recorder/info"}) await client.send_json_auto_id({"type": "recorder/info"})
response = await client.receive_json() response = await client.receive_json()
assert response["success"] assert response["success"]
assert response["result"]["migration_in_progress"] is True assert response["result"]["migration_in_progress"] is True
@ -2262,7 +2188,7 @@ async def test_recorder_info_migration_queue_exhausted(
await async_wait_recording_done(hass) await async_wait_recording_done(hass)
# Check the status after migration finished # Check the status after migration finished
await client.send_json({"id": 2, "type": "recorder/info"}) await client.send_json_auto_id({"type": "recorder/info"})
response = await client.receive_json() response = await client.receive_json()
assert response["success"] assert response["success"]
assert response["result"]["migration_in_progress"] is False assert response["result"]["migration_in_progress"] is False
@ -2278,7 +2204,7 @@ async def test_backup_start_no_recorder(
"""Test getting backup start when recorder is not present.""" """Test getting backup start when recorder is not present."""
client = await hass_ws_client(hass, hass_supervisor_access_token) client = await hass_ws_client(hass, hass_supervisor_access_token)
await client.send_json({"id": 1, "type": "backup/start"}) await client.send_json_auto_id({"type": "backup/start"})
response = await client.receive_json() response = await client.receive_json()
assert not response["success"] assert not response["success"]
assert response["error"]["code"] == "unknown_command" assert response["error"]["code"] == "unknown_command"
@ -2303,12 +2229,12 @@ async def test_backup_start_timeout(
with patch.object(recorder.core, "DB_LOCK_TIMEOUT", 0): with patch.object(recorder.core, "DB_LOCK_TIMEOUT", 0):
try: try:
await client.send_json({"id": 1, "type": "backup/start"}) await client.send_json_auto_id({"type": "backup/start"})
response = await client.receive_json() response = await client.receive_json()
assert not response["success"] assert not response["success"]
assert response["error"]["code"] == "timeout_error" assert response["error"]["code"] == "timeout_error"
finally: finally:
await client.send_json({"id": 2, "type": "backup/end"}) await client.send_json_auto_id({"type": "backup/end"})
async def test_backup_end( async def test_backup_end(
@ -2323,11 +2249,11 @@ async def test_backup_end(
# Ensure there are no queued events # Ensure there are no queued events
await async_wait_recording_done(hass) await async_wait_recording_done(hass)
await client.send_json({"id": 1, "type": "backup/start"}) await client.send_json_auto_id({"type": "backup/start"})
response = await client.receive_json() response = await client.receive_json()
assert response["success"] assert response["success"]
await client.send_json({"id": 2, "type": "backup/end"}) await client.send_json_auto_id({"type": "backup/end"})
response = await client.receive_json() response = await client.receive_json()
assert response["success"] assert response["success"]
@ -2349,7 +2275,7 @@ async def test_backup_end_without_start(
# Ensure there are no queued events # Ensure there are no queued events
await async_wait_recording_done(hass) await async_wait_recording_done(hass)
await client.send_json({"id": 1, "type": "backup/end"}) await client.send_json_auto_id({"type": "backup/end"})
response = await client.receive_json() response = await client.receive_json()
assert not response["success"] assert not response["success"]
assert response["error"]["code"] == "database_unlock_failed" assert response["error"]["code"] == "database_unlock_failed"
@ -2393,7 +2319,7 @@ async def test_get_statistics_metadata(
await async_recorder_block_till_done(hass) await async_recorder_block_till_done(hass)
client = await hass_ws_client() client = await hass_ws_client()
await client.send_json({"id": 1, "type": "recorder/get_statistics_metadata"}) await client.send_json_auto_id({"type": "recorder/get_statistics_metadata"})
response = await client.receive_json() response = await client.receive_json()
assert response["success"] assert response["success"]
assert response["result"] == [] assert response["result"] == []
@ -2442,9 +2368,8 @@ async def test_get_statistics_metadata(
) )
await async_wait_recording_done(hass) await async_wait_recording_done(hass)
await client.send_json( await client.send_json_auto_id(
{ {
"id": 2,
"type": "recorder/get_statistics_metadata", "type": "recorder/get_statistics_metadata",
"statistic_ids": ["test:total_gas"], "statistic_ids": ["test:total_gas"],
} }
@ -2470,9 +2395,8 @@ async def test_get_statistics_metadata(
hass.states.async_set("sensor.test2", 10, attributes=attributes) hass.states.async_set("sensor.test2", 10, attributes=attributes)
await async_wait_recording_done(hass) await async_wait_recording_done(hass)
await client.send_json( await client.send_json_auto_id(
{ {
"id": 3,
"type": "recorder/get_statistics_metadata", "type": "recorder/get_statistics_metadata",
"statistic_ids": ["sensor.test"], "statistic_ids": ["sensor.test"],
} }
@ -2498,9 +2422,8 @@ async def test_get_statistics_metadata(
hass.states.async_remove("sensor.test") hass.states.async_remove("sensor.test")
await hass.async_block_till_done() await hass.async_block_till_done()
await client.send_json( await client.send_json_auto_id(
{ {
"id": 4,
"type": "recorder/get_statistics_metadata", "type": "recorder/get_statistics_metadata",
"statistic_ids": ["sensor.test"], "statistic_ids": ["sensor.test"],
} }
@ -2568,9 +2491,8 @@ async def test_import_statistics(
"unit_of_measurement": "kWh", "unit_of_measurement": "kWh",
} }
await client.send_json( await client.send_json_auto_id(
{ {
"id": 1,
"type": "recorder/import_statistics", "type": "recorder/import_statistics",
"metadata": imported_metadata, "metadata": imported_metadata,
"stats": [imported_statistics1, imported_statistics2], "stats": [imported_statistics1, imported_statistics2],
@ -2656,9 +2578,8 @@ async def test_import_statistics(
"sum": 6, "sum": 6,
} }
await client.send_json( await client.send_json_auto_id(
{ {
"id": 2,
"type": "recorder/import_statistics", "type": "recorder/import_statistics",
"metadata": imported_metadata, "metadata": imported_metadata,
"stats": [external_statistics], "stats": [external_statistics],
@ -2702,9 +2623,8 @@ async def test_import_statistics(
"sum": 5, "sum": 5,
} }
await client.send_json( await client.send_json_auto_id(
{ {
"id": 3,
"type": "recorder/import_statistics", "type": "recorder/import_statistics",
"metadata": imported_metadata, "metadata": imported_metadata,
"stats": [external_statistics], "stats": [external_statistics],
@ -2785,9 +2705,8 @@ async def test_adjust_sum_statistics_energy(
"unit_of_measurement": "kWh", "unit_of_measurement": "kWh",
} }
await client.send_json( await client.send_json_auto_id(
{ {
"id": 1,
"type": "recorder/import_statistics", "type": "recorder/import_statistics",
"metadata": imported_metadata, "metadata": imported_metadata,
"stats": [imported_statistics1, imported_statistics2], "stats": [imported_statistics1, imported_statistics2],
@ -2852,9 +2771,8 @@ async def test_adjust_sum_statistics_energy(
} }
# Adjust previously inserted statistics in kWh # Adjust previously inserted statistics in kWh
await client.send_json( await client.send_json_auto_id(
{ {
"id": 4,
"type": "recorder/adjust_sum_statistics", "type": "recorder/adjust_sum_statistics",
"statistic_id": statistic_id, "statistic_id": statistic_id,
"start_time": period2.isoformat(), "start_time": period2.isoformat(),
@ -2893,9 +2811,8 @@ async def test_adjust_sum_statistics_energy(
} }
# Adjust previously inserted statistics in MWh # Adjust previously inserted statistics in MWh
await client.send_json( await client.send_json_auto_id(
{ {
"id": 5,
"type": "recorder/adjust_sum_statistics", "type": "recorder/adjust_sum_statistics",
"statistic_id": statistic_id, "statistic_id": statistic_id,
"start_time": period2.isoformat(), "start_time": period2.isoformat(),
@ -2981,9 +2898,8 @@ async def test_adjust_sum_statistics_gas(
"unit_of_measurement": "", "unit_of_measurement": "",
} }
await client.send_json( await client.send_json_auto_id(
{ {
"id": 1,
"type": "recorder/import_statistics", "type": "recorder/import_statistics",
"metadata": imported_metadata, "metadata": imported_metadata,
"stats": [imported_statistics1, imported_statistics2], "stats": [imported_statistics1, imported_statistics2],
@ -3048,9 +2964,8 @@ async def test_adjust_sum_statistics_gas(
} }
# Adjust previously inserted statistics in m³ # Adjust previously inserted statistics in m³
await client.send_json( await client.send_json_auto_id(
{ {
"id": 4,
"type": "recorder/adjust_sum_statistics", "type": "recorder/adjust_sum_statistics",
"statistic_id": statistic_id, "statistic_id": statistic_id,
"start_time": period2.isoformat(), "start_time": period2.isoformat(),
@ -3089,9 +3004,8 @@ async def test_adjust_sum_statistics_gas(
} }
# Adjust previously inserted statistics in ft³ # Adjust previously inserted statistics in ft³
await client.send_json( await client.send_json_auto_id(
{ {
"id": 5,
"type": "recorder/adjust_sum_statistics", "type": "recorder/adjust_sum_statistics",
"statistic_id": statistic_id, "statistic_id": statistic_id,
"start_time": period2.isoformat(), "start_time": period2.isoformat(),
@ -3194,9 +3108,8 @@ async def test_adjust_sum_statistics_errors(
"unit_of_measurement": statistic_unit, "unit_of_measurement": statistic_unit,
} }
await client.send_json( await client.send_json_auto_id(
{ {
"id": 1,
"type": "recorder/import_statistics", "type": "recorder/import_statistics",
"metadata": imported_metadata, "metadata": imported_metadata,
"stats": [imported_statistics1, imported_statistics2], "stats": [imported_statistics1, imported_statistics2],
@ -3262,10 +3175,8 @@ async def test_adjust_sum_statistics_errors(
} }
# Try to adjust statistics # Try to adjust statistics
msg_id = 2 await client.send_json_auto_id(
await client.send_json(
{ {
"id": msg_id,
"type": "recorder/adjust_sum_statistics", "type": "recorder/adjust_sum_statistics",
"statistic_id": "sensor.does_not_exist", "statistic_id": "sensor.does_not_exist",
"start_time": period2.isoformat(), "start_time": period2.isoformat(),
@ -3282,10 +3193,8 @@ async def test_adjust_sum_statistics_errors(
assert stats == previous_stats assert stats == previous_stats
for unit in invalid_units: for unit in invalid_units:
msg_id += 1 await client.send_json_auto_id(
await client.send_json(
{ {
"id": msg_id,
"type": "recorder/adjust_sum_statistics", "type": "recorder/adjust_sum_statistics",
"statistic_id": statistic_id, "statistic_id": statistic_id,
"start_time": period2.isoformat(), "start_time": period2.isoformat(),
@ -3302,10 +3211,8 @@ async def test_adjust_sum_statistics_errors(
assert stats == previous_stats assert stats == previous_stats
for unit in valid_units: for unit in valid_units:
msg_id += 1 await client.send_json_auto_id(
await client.send_json(
{ {
"id": msg_id,
"type": "recorder/adjust_sum_statistics", "type": "recorder/adjust_sum_statistics",
"statistic_id": statistic_id, "statistic_id": statistic_id,
"start_time": period2.isoformat(), "start_time": period2.isoformat(),