From 1f466e737e53e105595aa288d780d073b5359d54 Mon Sep 17 00:00:00 2001 From: Erik Montnemery Date: Fri, 2 Feb 2024 09:34:43 +0100 Subject: [PATCH] Use send_json_auto_id in recorder tests (#109355) --- tests/components/recorder/test_statistics.py | 3 +- .../components/recorder/test_websocket_api.py | 291 ++++++------------ 2 files changed, 100 insertions(+), 194 deletions(-) diff --git a/tests/components/recorder/test_statistics.py b/tests/components/recorder/test_statistics.py index 69b7f9316f7..00ffdc21b81 100644 --- a/tests/components/recorder/test_statistics.py +++ b/tests/components/recorder/test_statistics.py @@ -785,9 +785,8 @@ async def test_import_statistics( } # Adjust the statistics in a different unit - await client.send_json( + await client.send_json_auto_id( { - "id": 1, "type": "recorder/adjust_sum_statistics", "statistic_id": statistic_id, "start_time": period2.isoformat(), diff --git a/tests/components/recorder/test_websocket_api.py b/tests/components/recorder/test_websocket_api.py index 323b81211d7..e902dd49020 100644 --- a/tests/components/recorder/test_websocket_api.py +++ b/tests/components/recorder/test_websocket_api.py @@ -156,9 +156,8 @@ async def test_statistics_during_period( await async_wait_recording_done(hass) client = await hass_ws_client() - await client.send_json( + await client.send_json_auto_id( { - "id": 1, "type": "recorder/statistics_during_period", "start_time": now.isoformat(), "end_time": now.isoformat(), @@ -170,9 +169,8 @@ async def test_statistics_during_period( assert response["success"] assert response["result"] == {} - await client.send_json( + await client.send_json_auto_id( { - "id": 2, "type": "recorder/statistics_during_period", "start_time": now.isoformat(), "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", "start_time": now.isoformat(), "statistic_ids": ["sensor.test"], @@ -226,13 +223,6 @@ async def test_statistic_during_period( offset, ) -> None: """Test statistic_during_period.""" - id = 1 - - def next_id(): - nonlocal id - id += 1 - return id - now = dt_util.utcnow() 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 # No data for this period yet - await client.send_json( + await client.send_json_auto_id( { - "id": next_id(), "type": "recorder/statistic_during_period", "fixed_period": { "start_time": now.isoformat(), @@ -334,9 +323,8 @@ async def test_statistic_during_period( } # This should include imported_statistics_5min[:] - await client.send_json( + await client.send_json_auto_id( { - "id": next_id(), "type": "recorder/statistic_during_period", "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") + timedelta(minutes=5 * offset) ).isoformat() - await client.send_json( + await client.send_json_auto_id( { - "id": next_id(), "type": "recorder/statistic_during_period", "statistic_id": "sensor.test", "fixed_period": { @@ -388,9 +375,8 @@ async def test_statistic_during_period( dt_util.parse_datetime("2022-10-21T08:20:00+00:00") + timedelta(minutes=5 * offset) ).isoformat() - await client.send_json( + await client.send_json_auto_id( { - "id": next_id(), "type": "recorder/statistic_during_period", "statistic_id": "sensor.test", "fixed_period": { @@ -414,9 +400,8 @@ async def test_statistic_during_period( + timedelta(minutes=5 * offset) ).isoformat() 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", "fixed_period": { "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") + timedelta(minutes=5 * offset) ).isoformat() - await client.send_json( + await client.send_json_auto_id( { - "id": next_id(), "type": "recorder/statistic_during_period", "fixed_period": { "start_time": start_time, @@ -463,9 +447,8 @@ async def test_statistic_during_period( + timedelta(minutes=5 * offset) ).isoformat() 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", "fixed_period": { "end_time": end_time, @@ -493,9 +476,8 @@ async def test_statistic_during_period( + timedelta(minutes=5 * offset) ).isoformat() 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", "fixed_period": { "start_time": start_time, @@ -517,9 +499,8 @@ async def test_statistic_during_period( start_time = "2022-10-21T06:00:00+00:00" assert imported_stats_5min[24 - offset]["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", "fixed_period": { "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:] - await client.send_json( + await client.send_json_auto_id( { - "id": next_id(), "type": "recorder/statistic_during_period", "rolling_window": { "duration": {"hours": 1, "minutes": 25}, @@ -559,9 +539,8 @@ async def test_statistic_during_period( } # 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", "rolling_window": { "duration": {"hours": 1}, @@ -585,9 +564,8 @@ async def test_statistic_during_period( } # 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", "statistic_id": "sensor.test", "types": ["max", "change"], @@ -601,9 +579,8 @@ async def test_statistic_during_period( } # Test we can convert units - await client.send_json( + await client.send_json_auto_id( { - "id": next_id(), "type": "recorder/statistic_during_period", "statistic_id": "sensor.test", "units": {"energy": "MWh"}, @@ -621,9 +598,8 @@ async def test_statistic_during_period( # Test we can automatically convert units 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", "statistic_id": "sensor.test", } @@ -707,9 +683,8 @@ async def test_statistic_during_period_hole( await async_wait_recording_done(hass) # This should include imported_stats[:] - await client.send_json( + await client.send_json_auto_id( { - "id": next_id(), "type": "recorder/statistic_during_period", "statistic_id": "sensor.test", } @@ -728,9 +703,8 @@ async def test_statistic_during_period_hole( end_time = "2022-10-21T05:00:00+00:00" assert imported_stats[0]["start"].isoformat() == start_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", "statistic_id": "sensor.test", "fixed_period": { @@ -751,9 +725,8 @@ async def test_statistic_during_period_hole( # This should also include imported_stats[:] start_time = "2022-10-20T13:00: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", "statistic_id": "sensor.test", "fixed_period": { @@ -776,9 +749,8 @@ async def test_statistic_during_period_hole( end_time = "2022-10-20T23:00:00+00:00" assert imported_stats[1]["start"].isoformat() == start_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", "statistic_id": "sensor.test", "fixed_period": { @@ -801,9 +773,8 @@ async def test_statistic_during_period_hole( end_time = "2022-10-21T00:00:00+00:00" assert imported_stats[1]["start"].isoformat() > start_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", "statistic_id": "sensor.test", "fixed_period": { @@ -894,9 +865,8 @@ async def test_statistic_during_period_calendar( "homeassistant.components.recorder.websocket_api.statistic_during_period", return_value={}, ) as statistic_during_period: - await client.send_json( + await client.send_json_auto_id( { - "id": 1, "type": "recorder/statistic_during_period", "calendar": calendar_period, "statistic_id": "sensor.test", @@ -956,9 +926,8 @@ async def test_statistics_during_period_unit_conversion( client = await hass_ws_client() # Query in state unit - await client.send_json( + await client.send_json_auto_id( { - "id": 1, "type": "recorder/statistics_during_period", "start_time": now.isoformat(), "statistic_ids": ["sensor.test"], @@ -981,9 +950,8 @@ async def test_statistics_during_period_unit_conversion( } # Query in custom unit - await client.send_json( + await client.send_json_auto_id( { - "id": 2, "type": "recorder/statistics_during_period", "start_time": now.isoformat(), "statistic_ids": ["sensor.test"], @@ -1044,9 +1012,8 @@ async def test_sum_statistics_during_period_unit_conversion( client = await hass_ws_client() # Query in state unit - await client.send_json( + await client.send_json_auto_id( { - "id": 1, "type": "recorder/statistics_during_period", "start_time": now.isoformat(), "statistic_ids": ["sensor.test"], @@ -1069,9 +1036,8 @@ async def test_sum_statistics_during_period_unit_conversion( } # Query in custom unit - await client.send_json( + await client.send_json_auto_id( { - "id": 2, "type": "recorder/statistics_during_period", "start_time": now.isoformat(), "statistic_ids": ["sensor.test"], @@ -1121,9 +1087,8 @@ async def test_statistics_during_period_invalid_unit_conversion( client = await hass_ws_client() # Query in state unit - await client.send_json( + await client.send_json_auto_id( { - "id": 1, "type": "recorder/statistics_during_period", "start_time": now.isoformat(), "statistic_ids": ["sensor.test"], @@ -1135,9 +1100,8 @@ async def test_statistics_during_period_invalid_unit_conversion( assert response["result"] == {} # Query in custom unit - await client.send_json( + await client.send_json_auto_id( { - "id": 2, "type": "recorder/statistics_during_period", "start_time": now.isoformat(), "statistic_ids": ["sensor.test"], @@ -1176,9 +1140,8 @@ async def test_statistics_during_period_in_the_past( await async_wait_recording_done(hass) client = await hass_ws_client() - await client.send_json( + await client.send_json_auto_id( { - "id": 1, "type": "recorder/statistics_during_period", "start_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["result"] == {} - await client.send_json( + await client.send_json_auto_id( { - "id": 2, "type": "recorder/statistics_during_period", "start_time": now.isoformat(), "statistic_ids": ["sensor.test"], @@ -1204,9 +1166,8 @@ async def test_statistics_during_period_in_the_past( assert response["result"] == {} past = now - timedelta(days=3, hours=1) - await client.send_json( + await client.send_json_auto_id( { - "id": 3, "type": "recorder/statistics_during_period", "start_time": past.isoformat(), "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) - await client.send_json( + await client.send_json_auto_id( { - "id": 4, "type": "recorder/statistics_during_period", "start_time": stats_top_of_hour.isoformat(), "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", "start_time": now.isoformat(), "statistic_ids": ["sensor.test"], @@ -1272,9 +1231,8 @@ async def test_statistics_during_period_bad_start_time( ) -> None: """Test statistics_during_period.""" client = await hass_ws_client() - await client.send_json( + await client.send_json_auto_id( { - "id": 1, "type": "recorder/statistics_during_period", "start_time": "cats", "statistic_ids": ["sensor.test"], @@ -1293,9 +1251,8 @@ async def test_statistics_during_period_bad_end_time( now = dt_util.utcnow() client = await hass_ws_client() - await client.send_json( + await client.send_json_auto_id( { - "id": 1, "type": "recorder/statistics_during_period", "start_time": now.isoformat(), "end_time": "dogs", @@ -1315,9 +1272,8 @@ async def test_statistics_during_period_no_statistic_ids( now = dt_util.utcnow() client = await hass_ws_client() - await client.send_json( + await client.send_json_auto_id( { - "id": 1, "type": "recorder/statistics_during_period", "start_time": now.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() client = await hass_ws_client() - await client.send_json( + await client.send_json_auto_id( { - "id": 1, "type": "recorder/statistics_during_period", "start_time": now.isoformat(), "statistic_ids": [], @@ -1428,7 +1383,7 @@ async def test_list_statistic_ids( await async_recorder_block_till_done(hass) 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() assert response["success"] assert response["result"] == [] @@ -1436,7 +1391,7 @@ async def test_list_statistic_ids( hass.states.async_set("sensor.test", 10, attributes=attributes) 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() assert response["success"] assert response["result"] == [ @@ -1458,7 +1413,7 @@ async def test_list_statistic_ids( hass.states.async_remove("sensor.test") 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() assert response["success"] assert response["result"] == [ @@ -1474,14 +1429,14 @@ async def test_list_statistic_ids( } ] - await client.send_json( - {"id": 4, "type": "recorder/list_statistic_ids", "statistic_type": "dogs"} + await client.send_json_auto_id( + {"type": "recorder/list_statistic_ids", "statistic_type": "dogs"} ) response = await client.receive_json() assert not response["success"] - await client.send_json( - {"id": 5, "type": "recorder/list_statistic_ids", "statistic_type": "mean"} + await client.send_json_auto_id( + {"type": "recorder/list_statistic_ids", "statistic_type": "mean"} ) response = await client.receive_json() assert response["success"] @@ -1501,8 +1456,8 @@ async def test_list_statistic_ids( else: assert response["result"] == [] - await client.send_json( - {"id": 6, "type": "recorder/list_statistic_ids", "statistic_type": "sum"} + await client.send_json_auto_id( + {"type": "recorder/list_statistic_ids", "statistic_type": "sum"} ) response = await client.receive_json() assert response["success"] @@ -1591,7 +1546,7 @@ async def test_list_statistic_ids_unit_change( await async_recorder_block_till_done(hass) 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() assert response["success"] assert response["result"] == [] @@ -1602,7 +1557,7 @@ async def test_list_statistic_ids_unit_change( do_adhoc_statistics(hass, start=now) 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() assert response["success"] assert response["result"] == [ @@ -1621,7 +1576,7 @@ async def test_list_statistic_ids_unit_change( # Change the state unit 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() assert response["success"] assert response["result"] == [ @@ -1642,17 +1597,9 @@ async def test_validate_statistics( recorder_mock: Recorder, hass: HomeAssistant, hass_ws_client: WebSocketGenerator ) -> None: """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): - await client.send_json( - {"id": next_id(), "type": "recorder/validate_statistics"} - ) + await client.send_json_auto_id({"type": "recorder/validate_statistics"}) response = await client.receive_json() assert response["success"] assert response["result"] == expected_result @@ -1685,9 +1632,8 @@ async def test_clear_statistics( await async_recorder_block_till_done(hass) client = await hass_ws_client() - await client.send_json( + await client.send_json_auto_id( { - "id": 1, "type": "recorder/statistics_during_period", "start_time": now.isoformat(), "statistic_ids": ["sensor.test1", "sensor.test2", "sensor.test3"], @@ -1730,9 +1676,8 @@ async def test_clear_statistics( } assert response["result"] == expected_response - await client.send_json( + await client.send_json_auto_id( { - "id": 2, "type": "recorder/clear_statistics", "statistic_ids": ["sensor.test"], } @@ -1742,9 +1687,8 @@ async def test_clear_statistics( await async_recorder_block_till_done(hass) client = await hass_ws_client() - await client.send_json( + await client.send_json_auto_id( { - "id": 3, "type": "recorder/statistics_during_period", "statistic_ids": ["sensor.test1", "sensor.test2", "sensor.test3"], "start_time": now.isoformat(), @@ -1755,9 +1699,8 @@ async def test_clear_statistics( assert response["success"] assert response["result"] == expected_response - await client.send_json( + await client.send_json_auto_id( { - "id": 4, "type": "recorder/clear_statistics", "statistic_ids": ["sensor.test1", "sensor.test3"], } @@ -1767,9 +1710,8 @@ async def test_clear_statistics( await async_recorder_block_till_done(hass) client = await hass_ws_client() - await client.send_json( + await client.send_json_auto_id( { - "id": 5, "type": "recorder/statistics_during_period", "statistic_ids": ["sensor.test1", "sensor.test2", "sensor.test3"], "start_time": now.isoformat(), @@ -1811,7 +1753,7 @@ async def test_update_statistics_metadata( 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() assert response["success"] 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", "statistic_id": "sensor.test", "unit_of_measurement": new_unit, @@ -1839,7 +1780,7 @@ async def test_update_statistics_metadata( assert response["success"] 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() assert response["success"] 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", "start_time": now.isoformat(), "statistic_ids": ["sensor.test"], @@ -1902,7 +1842,7 @@ async def test_change_statistics_unit( 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() assert response["success"] 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", "start_time": now.isoformat(), "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", "statistic_id": "sensor.test", "new_unit_of_measurement": "W", @@ -1955,7 +1893,7 @@ async def test_change_statistics_unit( assert response["success"] 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() assert response["success"] 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", "start_time": now.isoformat(), "statistic_ids": ["sensor.test"], @@ -1997,9 +1934,8 @@ async def test_change_statistics_unit( } # 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", "statistic_id": "sensor.test", "new_unit_of_measurement": "W", @@ -2010,7 +1946,7 @@ async def test_change_statistics_unit( assert response["success"] 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() assert response["success"] assert response["result"] == [ @@ -2035,7 +1971,6 @@ async def test_change_statistics_unit_errors( ) -> None: """Test change unit of recorded statistics.""" now = dt_util.utcnow() - ws_id = 0 units = METRIC_SYSTEM 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): - nonlocal ws_id - ws_id += 1 - await client.send_json({"id": ws_id, "type": "recorder/list_statistic_ids"}) + await client.send_json_auto_id({"type": "recorder/list_statistic_ids"}) response = await client.receive_json() assert response["success"] assert response["result"] == expected async def assert_statistics(expected): - nonlocal ws_id - ws_id += 1 - await client.send_json( + await client.send_json_auto_id( { - "id": ws_id, "type": "recorder/statistics_during_period", "start_time": now.isoformat(), "statistic_ids": ["sensor.test"], @@ -2106,10 +2036,8 @@ async def test_change_statistics_unit_errors( await assert_statistics(expected_statistics) # Try changing to an invalid unit - ws_id += 1 - await client.send_json( + await client.send_json_auto_id( { - "id": ws_id, "type": "recorder/change_statistics_unit", "statistic_id": "sensor.test", "old_unit_of_measurement": "kW", @@ -2126,10 +2054,8 @@ async def test_change_statistics_unit_errors( await assert_statistics(expected_statistics) # Try changing from the wrong unit - ws_id += 1 - await client.send_json( + await client.send_json_auto_id( { - "id": ws_id, "type": "recorder/change_statistics_unit", "statistic_id": "sensor.test", "old_unit_of_measurement": "W", @@ -2155,7 +2081,7 @@ async def test_recorder_info( # Ensure there are no queued events 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() assert response["success"] assert response["result"] == { @@ -2174,7 +2100,7 @@ async def test_recorder_info_no_recorder( """Test getting recorder status when recorder is not present.""" 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() assert not response["success"] assert response["error"]["code"] == "unknown_command" @@ -2199,7 +2125,7 @@ async def test_recorder_info_bad_recorder_config( # Wait for recorder to shut down 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() assert response["success"] assert response["result"]["recording"] is False @@ -2250,7 +2176,7 @@ async def test_recorder_info_migration_queue_exhausted( client = await hass_ws_client() # 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() assert response["success"] 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) # 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() assert response["success"] 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.""" 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() assert not response["success"] 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): try: - await client.send_json({"id": 1, "type": "backup/start"}) + await client.send_json_auto_id({"type": "backup/start"}) response = await client.receive_json() assert not response["success"] assert response["error"]["code"] == "timeout_error" finally: - await client.send_json({"id": 2, "type": "backup/end"}) + await client.send_json_auto_id({"type": "backup/end"}) async def test_backup_end( @@ -2323,11 +2249,11 @@ async def test_backup_end( # Ensure there are no queued events 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() 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() assert response["success"] @@ -2349,7 +2275,7 @@ async def test_backup_end_without_start( # Ensure there are no queued events 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() assert not response["success"] assert response["error"]["code"] == "database_unlock_failed" @@ -2393,7 +2319,7 @@ async def test_get_statistics_metadata( await async_recorder_block_till_done(hass) 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() assert response["success"] assert response["result"] == [] @@ -2442,9 +2368,8 @@ async def test_get_statistics_metadata( ) await async_wait_recording_done(hass) - await client.send_json( + await client.send_json_auto_id( { - "id": 2, "type": "recorder/get_statistics_metadata", "statistic_ids": ["test:total_gas"], } @@ -2470,9 +2395,8 @@ async def test_get_statistics_metadata( hass.states.async_set("sensor.test2", 10, attributes=attributes) await async_wait_recording_done(hass) - await client.send_json( + await client.send_json_auto_id( { - "id": 3, "type": "recorder/get_statistics_metadata", "statistic_ids": ["sensor.test"], } @@ -2498,9 +2422,8 @@ async def test_get_statistics_metadata( hass.states.async_remove("sensor.test") await hass.async_block_till_done() - await client.send_json( + await client.send_json_auto_id( { - "id": 4, "type": "recorder/get_statistics_metadata", "statistic_ids": ["sensor.test"], } @@ -2568,9 +2491,8 @@ async def test_import_statistics( "unit_of_measurement": "kWh", } - await client.send_json( + await client.send_json_auto_id( { - "id": 1, "type": "recorder/import_statistics", "metadata": imported_metadata, "stats": [imported_statistics1, imported_statistics2], @@ -2656,9 +2578,8 @@ async def test_import_statistics( "sum": 6, } - await client.send_json( + await client.send_json_auto_id( { - "id": 2, "type": "recorder/import_statistics", "metadata": imported_metadata, "stats": [external_statistics], @@ -2702,9 +2623,8 @@ async def test_import_statistics( "sum": 5, } - await client.send_json( + await client.send_json_auto_id( { - "id": 3, "type": "recorder/import_statistics", "metadata": imported_metadata, "stats": [external_statistics], @@ -2785,9 +2705,8 @@ async def test_adjust_sum_statistics_energy( "unit_of_measurement": "kWh", } - await client.send_json( + await client.send_json_auto_id( { - "id": 1, "type": "recorder/import_statistics", "metadata": imported_metadata, "stats": [imported_statistics1, imported_statistics2], @@ -2852,9 +2771,8 @@ async def test_adjust_sum_statistics_energy( } # Adjust previously inserted statistics in kWh - await client.send_json( + await client.send_json_auto_id( { - "id": 4, "type": "recorder/adjust_sum_statistics", "statistic_id": statistic_id, "start_time": period2.isoformat(), @@ -2893,9 +2811,8 @@ async def test_adjust_sum_statistics_energy( } # Adjust previously inserted statistics in MWh - await client.send_json( + await client.send_json_auto_id( { - "id": 5, "type": "recorder/adjust_sum_statistics", "statistic_id": statistic_id, "start_time": period2.isoformat(), @@ -2981,9 +2898,8 @@ async def test_adjust_sum_statistics_gas( "unit_of_measurement": "m³", } - await client.send_json( + await client.send_json_auto_id( { - "id": 1, "type": "recorder/import_statistics", "metadata": imported_metadata, "stats": [imported_statistics1, imported_statistics2], @@ -3048,9 +2964,8 @@ async def test_adjust_sum_statistics_gas( } # Adjust previously inserted statistics in m³ - await client.send_json( + await client.send_json_auto_id( { - "id": 4, "type": "recorder/adjust_sum_statistics", "statistic_id": statistic_id, "start_time": period2.isoformat(), @@ -3089,9 +3004,8 @@ async def test_adjust_sum_statistics_gas( } # Adjust previously inserted statistics in ft³ - await client.send_json( + await client.send_json_auto_id( { - "id": 5, "type": "recorder/adjust_sum_statistics", "statistic_id": statistic_id, "start_time": period2.isoformat(), @@ -3194,9 +3108,8 @@ async def test_adjust_sum_statistics_errors( "unit_of_measurement": statistic_unit, } - await client.send_json( + await client.send_json_auto_id( { - "id": 1, "type": "recorder/import_statistics", "metadata": imported_metadata, "stats": [imported_statistics1, imported_statistics2], @@ -3262,10 +3175,8 @@ async def test_adjust_sum_statistics_errors( } # Try to adjust statistics - msg_id = 2 - await client.send_json( + await client.send_json_auto_id( { - "id": msg_id, "type": "recorder/adjust_sum_statistics", "statistic_id": "sensor.does_not_exist", "start_time": period2.isoformat(), @@ -3282,10 +3193,8 @@ async def test_adjust_sum_statistics_errors( assert stats == previous_stats for unit in invalid_units: - msg_id += 1 - await client.send_json( + await client.send_json_auto_id( { - "id": msg_id, "type": "recorder/adjust_sum_statistics", "statistic_id": statistic_id, "start_time": period2.isoformat(), @@ -3302,10 +3211,8 @@ async def test_adjust_sum_statistics_errors( assert stats == previous_stats for unit in valid_units: - msg_id += 1 - await client.send_json( + await client.send_json_auto_id( { - "id": msg_id, "type": "recorder/adjust_sum_statistics", "statistic_id": statistic_id, "start_time": period2.isoformat(),