From 28ebab9c5ad87c604927fc946630e77f7db28579 Mon Sep 17 00:00:00 2001 From: Erik Montnemery Date: Mon, 25 Apr 2022 12:04:47 +0200 Subject: [PATCH] Simplify waiting for recorder in tests (#70647) --- tests/components/automation/test_recorder.py | 4 +- tests/components/camera/test_recorder.py | 4 +- tests/components/climate/test_recorder.py | 4 +- tests/components/demo/test_init.py | 4 +- tests/components/energy/test_sensor.py | 14 +- tests/components/energy/test_websocket_api.py | 10 +- tests/components/fan/test_recorder.py | 4 +- tests/components/history/test_init.py | 8 +- tests/components/humidifier/test_recorder.py | 4 +- .../components/input_boolean/test_recorder.py | 4 +- .../components/input_button/test_recorder.py | 4 +- .../input_datetime/test_recorder.py | 4 +- .../components/input_number/test_recorder.py | 4 +- .../components/input_select/test_recorder.py | 4 +- tests/components/input_text/test_recorder.py | 4 +- tests/components/light/test_recorder.py | 4 +- tests/components/logbook/test_init.py | 6 +- .../components/media_player/test_recorder.py | 4 +- tests/components/number/test_recorder.py | 4 +- tests/components/plant/test_init.py | 4 +- tests/components/recorder/common.py | 24 +-- tests/components/recorder/test_init.py | 43 ++--- tests/components/recorder/test_migrate.py | 16 +- tests/components/recorder/test_purge.py | 165 +++++++++--------- tests/components/recorder/test_statistics.py | 10 +- .../components/recorder/test_websocket_api.py | 16 +- tests/components/script/test_recorder.py | 4 +- tests/components/select/test_recorder.py | 4 +- tests/components/sensor/test_recorder.py | 14 +- tests/components/siren/test_recorder.py | 4 +- tests/components/statistics/test_sensor.py | 10 +- tests/components/sun/test_recorder.py | 4 +- tests/components/tibber/test_statistics.py | 4 +- tests/components/update/test_recorder.py | 4 +- tests/components/vacuum/test_recorder.py | 4 +- .../components/water_heater/test_recorder.py | 4 +- tests/components/weather/test_recorder.py | 4 +- tests/conftest.py | 2 +- 38 files changed, 207 insertions(+), 231 deletions(-) diff --git a/tests/components/automation/test_recorder.py b/tests/components/automation/test_recorder.py index ac80aabca8c..bfb02c0daba 100644 --- a/tests/components/automation/test_recorder.py +++ b/tests/components/automation/test_recorder.py @@ -18,7 +18,7 @@ from homeassistant.core import State from homeassistant.setup import async_setup_component from tests.common import async_mock_service -from tests.components.recorder.common import async_wait_recording_done_without_instance +from tests.components.recorder.common import async_wait_recording_done @pytest.fixture @@ -44,7 +44,7 @@ async def test_exclude_attributes(hass, recorder_mock, calls): await hass.async_block_till_done() assert len(calls) == 1 assert ["hello.world"] == calls[0].data.get(ATTR_ENTITY_ID) - await async_wait_recording_done_without_instance(hass) + await async_wait_recording_done(hass) def _fetch_states() -> list[State]: with session_scope(hass=hass) as session: diff --git a/tests/components/camera/test_recorder.py b/tests/components/camera/test_recorder.py index 86f20280fc0..0dc161fb0c0 100644 --- a/tests/components/camera/test_recorder.py +++ b/tests/components/camera/test_recorder.py @@ -17,7 +17,7 @@ from homeassistant.setup import async_setup_component from homeassistant.util import dt as dt_util from tests.common import async_fire_time_changed -from tests.components.recorder.common import async_wait_recording_done_without_instance +from tests.components.recorder.common import async_wait_recording_done async def test_exclude_attributes(hass, recorder_mock): @@ -28,7 +28,7 @@ async def test_exclude_attributes(hass, recorder_mock): await hass.async_block_till_done() async_fire_time_changed(hass, dt_util.utcnow() + timedelta(minutes=5)) await hass.async_block_till_done() - await async_wait_recording_done_without_instance(hass) + await async_wait_recording_done(hass) def _fetch_camera_states() -> list[State]: with session_scope(hass=hass) as session: diff --git a/tests/components/climate/test_recorder.py b/tests/components/climate/test_recorder.py index 42fcd9498a8..427645fb871 100644 --- a/tests/components/climate/test_recorder.py +++ b/tests/components/climate/test_recorder.py @@ -23,7 +23,7 @@ from homeassistant.setup import async_setup_component from homeassistant.util import dt as dt_util from tests.common import async_fire_time_changed -from tests.components.recorder.common import async_wait_recording_done_without_instance +from tests.components.recorder.common import async_wait_recording_done async def test_exclude_attributes(hass, recorder_mock): @@ -34,7 +34,7 @@ async def test_exclude_attributes(hass, recorder_mock): await hass.async_block_till_done() async_fire_time_changed(hass, dt_util.utcnow() + timedelta(minutes=5)) await hass.async_block_till_done() - await async_wait_recording_done_without_instance(hass) + await async_wait_recording_done(hass) def _fetch_states() -> list[State]: with session_scope(hass=hass) as session: diff --git a/tests/components/demo/test_init.py b/tests/components/demo/test_init.py index beffa3ba332..b00028e34d9 100644 --- a/tests/components/demo/test_init.py +++ b/tests/components/demo/test_init.py @@ -12,7 +12,7 @@ from homeassistant.components.recorder.statistics import list_statistic_ids from homeassistant.helpers.json import JSONEncoder from homeassistant.setup import async_setup_component -from tests.components.recorder.common import async_wait_recording_done_without_instance +from tests.components.recorder.common import async_wait_recording_done @pytest.fixture(autouse=True) @@ -51,7 +51,7 @@ async def test_demo_statistics(hass, recorder_mock): assert await async_setup_component(hass, DOMAIN, {DOMAIN: {}}) await hass.async_block_till_done() await hass.async_start() - await async_wait_recording_done_without_instance(hass) + await async_wait_recording_done(hass) statistic_ids = await get_instance(hass).async_add_executor_job( list_statistic_ids, hass diff --git a/tests/components/energy/test_sensor.py b/tests/components/energy/test_sensor.py index 1ba87455a83..5193eb186d2 100644 --- a/tests/components/energy/test_sensor.py +++ b/tests/components/energy/test_sensor.py @@ -25,7 +25,7 @@ from homeassistant.const import ( from homeassistant.setup import async_setup_component import homeassistant.util.dt as dt_util -from tests.components.recorder.common import async_wait_recording_done_without_instance +from tests.components.recorder.common import async_wait_recording_done @pytest.fixture @@ -230,7 +230,7 @@ async def test_cost_sensor_price_entity_total_increasing( assert state.attributes[ATTR_LAST_RESET] == last_reset_cost_sensor # Check generated statistics - await async_wait_recording_done_without_instance(hass) + await async_wait_recording_done(hass) all_statistics = await hass.loop.run_in_executor(None, _compile_statistics, hass) statistics = get_statistics_for_entity(all_statistics, cost_sensor_entity_id) assert statistics["stat"]["sum"] == 19.0 @@ -270,7 +270,7 @@ async def test_cost_sensor_price_entity_total_increasing( assert state.attributes[ATTR_LAST_RESET] == last_reset_cost_sensor # Check generated statistics - await async_wait_recording_done_without_instance(hass) + await async_wait_recording_done(hass) all_statistics = await hass.loop.run_in_executor(None, _compile_statistics, hass) statistics = get_statistics_for_entity(all_statistics, cost_sensor_entity_id) assert statistics["stat"]["sum"] == 38.0 @@ -435,7 +435,7 @@ async def test_cost_sensor_price_entity_total( assert state.attributes[ATTR_LAST_RESET] == last_reset_cost_sensor # Check generated statistics - await async_wait_recording_done_without_instance(hass) + await async_wait_recording_done(hass) all_statistics = await hass.loop.run_in_executor(None, _compile_statistics, hass) statistics = get_statistics_for_entity(all_statistics, cost_sensor_entity_id) assert statistics["stat"]["sum"] == 19.0 @@ -476,7 +476,7 @@ async def test_cost_sensor_price_entity_total( assert state.attributes[ATTR_LAST_RESET] == last_reset_cost_sensor # Check generated statistics - await async_wait_recording_done_without_instance(hass) + await async_wait_recording_done(hass) all_statistics = await hass.loop.run_in_executor(None, _compile_statistics, hass) statistics = get_statistics_for_entity(all_statistics, cost_sensor_entity_id) assert statistics["stat"]["sum"] == 38.0 @@ -640,7 +640,7 @@ async def test_cost_sensor_price_entity_total_no_reset( assert state.attributes[ATTR_LAST_RESET] == last_reset_cost_sensor # Check generated statistics - await async_wait_recording_done_without_instance(hass) + await async_wait_recording_done(hass) all_statistics = await hass.loop.run_in_executor(None, _compile_statistics, hass) statistics = get_statistics_for_entity(all_statistics, cost_sensor_entity_id) assert statistics["stat"]["sum"] == 19.0 @@ -657,7 +657,7 @@ async def test_cost_sensor_price_entity_total_no_reset( assert state.attributes[ATTR_LAST_RESET] == last_reset_cost_sensor # Check generated statistics - await async_wait_recording_done_without_instance(hass) + await async_wait_recording_done(hass) all_statistics = await hass.loop.run_in_executor(None, _compile_statistics, hass) statistics = get_statistics_for_entity(all_statistics, cost_sensor_entity_id) assert statistics["stat"]["sum"] == 18.0 diff --git a/tests/components/energy/test_websocket_api.py b/tests/components/energy/test_websocket_api.py index 523bb7b102f..5e2b124d64d 100644 --- a/tests/components/energy/test_websocket_api.py +++ b/tests/components/energy/test_websocket_api.py @@ -14,7 +14,7 @@ from tests.common import ( init_recorder_component, mock_platform, ) -from tests.components.recorder.common import async_wait_recording_done_without_instance +from tests.components.recorder.common import async_wait_recording_done @pytest.fixture(autouse=True) @@ -389,7 +389,7 @@ async def test_fossil_energy_consumption_no_co2(hass, hass_ws_client): async_add_external_statistics( hass, external_energy_metadata_2, external_energy_statistics_2 ) - await async_wait_recording_done_without_instance(hass) + await async_wait_recording_done(hass) client = await hass_ws_client() await client.send_json( @@ -550,7 +550,7 @@ async def test_fossil_energy_consumption_hole(hass, hass_ws_client): async_add_external_statistics( hass, external_energy_metadata_2, external_energy_statistics_2 ) - await async_wait_recording_done_without_instance(hass) + await async_wait_recording_done(hass) client = await hass_ws_client() await client.send_json( @@ -709,7 +709,7 @@ async def test_fossil_energy_consumption_no_data(hass, hass_ws_client): async_add_external_statistics( hass, external_energy_metadata_2, external_energy_statistics_2 ) - await async_wait_recording_done_without_instance(hass) + await async_wait_recording_done(hass) client = await hass_ws_client() await client.send_json( @@ -890,7 +890,7 @@ async def test_fossil_energy_consumption(hass, hass_ws_client): hass, external_energy_metadata_2, external_energy_statistics_2 ) async_add_external_statistics(hass, external_co2_metadata, external_co2_statistics) - await async_wait_recording_done_without_instance(hass) + await async_wait_recording_done(hass) client = await hass_ws_client() await client.send_json( diff --git a/tests/components/fan/test_recorder.py b/tests/components/fan/test_recorder.py index 078072350af..aa5bae45f4c 100644 --- a/tests/components/fan/test_recorder.py +++ b/tests/components/fan/test_recorder.py @@ -13,7 +13,7 @@ from homeassistant.setup import async_setup_component from homeassistant.util import dt as dt_util from tests.common import async_fire_time_changed -from tests.components.recorder.common import async_wait_recording_done_without_instance +from tests.components.recorder.common import async_wait_recording_done async def test_exclude_attributes(hass, recorder_mock): @@ -22,7 +22,7 @@ async def test_exclude_attributes(hass, recorder_mock): await hass.async_block_till_done() async_fire_time_changed(hass, dt_util.utcnow() + timedelta(minutes=5)) await hass.async_block_till_done() - await async_wait_recording_done_without_instance(hass) + await async_wait_recording_done(hass) def _fetch_states() -> list[State]: with session_scope(hass=hass) as session: diff --git a/tests/components/history/test_init.py b/tests/components/history/test_init.py index e75afedcfe6..63121fc516e 100644 --- a/tests/components/history/test_init.py +++ b/tests/components/history/test_init.py @@ -19,7 +19,7 @@ from homeassistant.util.unit_system import IMPERIAL_SYSTEM, METRIC_SYSTEM from tests.common import init_recorder_component from tests.components.recorder.common import ( - async_wait_recording_done_without_instance, + async_wait_recording_done, trigger_db_commit, wait_recording_done, ) @@ -612,11 +612,11 @@ async def test_fetch_period_api_with_minimal_response(hass, recorder_mock, hass_ await async_setup_component(hass, "history", {}) hass.states.async_set("sensor.power", 0, {"attr": "any"}) - await async_wait_recording_done_without_instance(hass) + await async_wait_recording_done(hass) hass.states.async_set("sensor.power", 50, {"attr": "any"}) - await async_wait_recording_done_without_instance(hass) + await async_wait_recording_done(hass) hass.states.async_set("sensor.power", 23, {"attr": "any"}) - await async_wait_recording_done_without_instance(hass) + await async_wait_recording_done(hass) client = await hass_client() response = await client.get( f"/api/history/period/{now.isoformat()}?filter_entity_id=sensor.power&minimal_response&no_attributes" diff --git a/tests/components/humidifier/test_recorder.py b/tests/components/humidifier/test_recorder.py index 70b61e21b70..ce694fc221b 100644 --- a/tests/components/humidifier/test_recorder.py +++ b/tests/components/humidifier/test_recorder.py @@ -17,7 +17,7 @@ from homeassistant.setup import async_setup_component from homeassistant.util import dt as dt_util from tests.common import async_fire_time_changed -from tests.components.recorder.common import async_wait_recording_done_without_instance +from tests.components.recorder.common import async_wait_recording_done async def test_exclude_attributes(hass, recorder_mock): @@ -28,7 +28,7 @@ async def test_exclude_attributes(hass, recorder_mock): await hass.async_block_till_done() async_fire_time_changed(hass, dt_util.utcnow() + timedelta(minutes=5)) await hass.async_block_till_done() - await async_wait_recording_done_without_instance(hass) + await async_wait_recording_done(hass) def _fetch_states() -> list[State]: with session_scope(hass=hass) as session: diff --git a/tests/components/input_boolean/test_recorder.py b/tests/components/input_boolean/test_recorder.py index 6fc8c80dcb3..c01c2532953 100644 --- a/tests/components/input_boolean/test_recorder.py +++ b/tests/components/input_boolean/test_recorder.py @@ -12,7 +12,7 @@ from homeassistant.setup import async_setup_component from homeassistant.util import dt as dt_util from tests.common import async_fire_time_changed -from tests.components.recorder.common import async_wait_recording_done_without_instance +from tests.components.recorder.common import async_wait_recording_done async def test_exclude_attributes( @@ -28,7 +28,7 @@ async def test_exclude_attributes( await hass.async_block_till_done() async_fire_time_changed(hass, dt_util.utcnow() + timedelta(minutes=5)) await hass.async_block_till_done() - await async_wait_recording_done_without_instance(hass) + await async_wait_recording_done(hass) def _fetch_states() -> list[State]: with session_scope(hass=hass) as session: diff --git a/tests/components/input_button/test_recorder.py b/tests/components/input_button/test_recorder.py index 9d7ea175122..eb5bcc05cf3 100644 --- a/tests/components/input_button/test_recorder.py +++ b/tests/components/input_button/test_recorder.py @@ -12,7 +12,7 @@ from homeassistant.setup import async_setup_component from homeassistant.util import dt as dt_util from tests.common import async_fire_time_changed -from tests.components.recorder.common import async_wait_recording_done_without_instance +from tests.components.recorder.common import async_wait_recording_done async def test_exclude_attributes( @@ -28,7 +28,7 @@ async def test_exclude_attributes( await hass.async_block_till_done() async_fire_time_changed(hass, dt_util.utcnow() + timedelta(minutes=5)) await hass.async_block_till_done() - await async_wait_recording_done_without_instance(hass) + await async_wait_recording_done(hass) def _fetch_states() -> list[State]: with session_scope(hass=hass) as session: diff --git a/tests/components/input_datetime/test_recorder.py b/tests/components/input_datetime/test_recorder.py index bfa6cdf6d1a..e8da8939ea9 100644 --- a/tests/components/input_datetime/test_recorder.py +++ b/tests/components/input_datetime/test_recorder.py @@ -12,7 +12,7 @@ from homeassistant.setup import async_setup_component from homeassistant.util import dt as dt_util from tests.common import async_fire_time_changed -from tests.components.recorder.common import async_wait_recording_done_without_instance +from tests.components.recorder.common import async_wait_recording_done async def test_exclude_attributes( @@ -32,7 +32,7 @@ async def test_exclude_attributes( await hass.async_block_till_done() async_fire_time_changed(hass, dt_util.utcnow() + timedelta(minutes=5)) await hass.async_block_till_done() - await async_wait_recording_done_without_instance(hass) + await async_wait_recording_done(hass) def _fetch_states() -> list[State]: with session_scope(hass=hass) as session: diff --git a/tests/components/input_number/test_recorder.py b/tests/components/input_number/test_recorder.py index 44024771fbb..9db2e2cd9c8 100644 --- a/tests/components/input_number/test_recorder.py +++ b/tests/components/input_number/test_recorder.py @@ -18,7 +18,7 @@ from homeassistant.setup import async_setup_component from homeassistant.util import dt as dt_util from tests.common import async_fire_time_changed -from tests.components.recorder.common import async_wait_recording_done_without_instance +from tests.components.recorder.common import async_wait_recording_done async def test_exclude_attributes( @@ -40,7 +40,7 @@ async def test_exclude_attributes( await hass.async_block_till_done() async_fire_time_changed(hass, dt_util.utcnow() + timedelta(minutes=5)) await hass.async_block_till_done() - await async_wait_recording_done_without_instance(hass) + await async_wait_recording_done(hass) def _fetch_states() -> list[State]: with session_scope(hass=hass) as session: diff --git a/tests/components/input_select/test_recorder.py b/tests/components/input_select/test_recorder.py index 351a0a739cd..3a5ae4e385f 100644 --- a/tests/components/input_select/test_recorder.py +++ b/tests/components/input_select/test_recorder.py @@ -12,7 +12,7 @@ from homeassistant.setup import async_setup_component from homeassistant.util import dt as dt_util from tests.common import async_fire_time_changed -from tests.components.recorder.common import async_wait_recording_done_without_instance +from tests.components.recorder.common import async_wait_recording_done async def test_exclude_attributes( @@ -39,7 +39,7 @@ async def test_exclude_attributes( await hass.async_block_till_done() async_fire_time_changed(hass, dt_util.utcnow() + timedelta(minutes=5)) await hass.async_block_till_done() - await async_wait_recording_done_without_instance(hass) + await async_wait_recording_done(hass) def _fetch_states() -> list[State]: with session_scope(hass=hass) as session: diff --git a/tests/components/input_text/test_recorder.py b/tests/components/input_text/test_recorder.py index 7ec8463bb6b..f613bbcebe1 100644 --- a/tests/components/input_text/test_recorder.py +++ b/tests/components/input_text/test_recorder.py @@ -19,7 +19,7 @@ from homeassistant.setup import async_setup_component from homeassistant.util import dt as dt_util from tests.common import async_fire_time_changed -from tests.components.recorder.common import async_wait_recording_done_without_instance +from tests.components.recorder.common import async_wait_recording_done async def test_exclude_attributes( @@ -39,7 +39,7 @@ async def test_exclude_attributes( await hass.async_block_till_done() async_fire_time_changed(hass, dt_util.utcnow() + timedelta(minutes=5)) await hass.async_block_till_done() - await async_wait_recording_done_without_instance(hass) + await async_wait_recording_done(hass) def _fetch_states() -> list[State]: with session_scope(hass=hass) as session: diff --git a/tests/components/light/test_recorder.py b/tests/components/light/test_recorder.py index 6133f61abf7..7e004891bb8 100644 --- a/tests/components/light/test_recorder.py +++ b/tests/components/light/test_recorder.py @@ -18,7 +18,7 @@ from homeassistant.setup import async_setup_component from homeassistant.util import dt as dt_util from tests.common import async_fire_time_changed -from tests.components.recorder.common import async_wait_recording_done_without_instance +from tests.components.recorder.common import async_wait_recording_done async def test_exclude_attributes(hass, recorder_mock): @@ -29,7 +29,7 @@ async def test_exclude_attributes(hass, recorder_mock): await hass.async_block_till_done() async_fire_time_changed(hass, dt_util.utcnow() + timedelta(minutes=5)) await hass.async_block_till_done() - await async_wait_recording_done_without_instance(hass) + await async_wait_recording_done(hass) def _fetch_states() -> list[State]: with session_scope(hass=hass) as session: diff --git a/tests/components/logbook/test_init.py b/tests/components/logbook/test_init.py index 1cfc90bbb6c..fc21369e2d0 100644 --- a/tests/components/logbook/test_init.py +++ b/tests/components/logbook/test_init.py @@ -41,7 +41,7 @@ import homeassistant.util.dt as dt_util from tests.common import async_capture_events, mock_platform from tests.components.recorder.common import ( async_trigger_db_commit, - async_wait_recording_done_without_instance, + async_wait_recording_done, trigger_db_commit, ) @@ -645,7 +645,7 @@ async def test_logbook_entity_no_longer_in_state_machine( await async_setup_component(hass, "automation", {}) await async_setup_component(hass, "script", {}) - await async_wait_recording_done_without_instance(hass) + await async_wait_recording_done(hass) entity_id_test = "alarm_control_panel.area_001" hass.states.async_set( @@ -656,7 +656,7 @@ async def test_logbook_entity_no_longer_in_state_machine( ) async_trigger_db_commit(hass) - await async_wait_recording_done_without_instance(hass) + await async_wait_recording_done(hass) hass.states.async_remove(entity_id_test) diff --git a/tests/components/media_player/test_recorder.py b/tests/components/media_player/test_recorder.py index dd1119efd52..7f6b15768f2 100644 --- a/tests/components/media_player/test_recorder.py +++ b/tests/components/media_player/test_recorder.py @@ -19,7 +19,7 @@ from homeassistant.setup import async_setup_component from homeassistant.util import dt as dt_util from tests.common import async_fire_time_changed -from tests.components.recorder.common import async_wait_recording_done_without_instance +from tests.components.recorder.common import async_wait_recording_done async def test_exclude_attributes(hass, recorder_mock): @@ -30,7 +30,7 @@ async def test_exclude_attributes(hass, recorder_mock): await hass.async_block_till_done() async_fire_time_changed(hass, dt_util.utcnow() + timedelta(minutes=5)) await hass.async_block_till_done() - await async_wait_recording_done_without_instance(hass) + await async_wait_recording_done(hass) def _fetch_states() -> list[State]: with session_scope(hass=hass) as session: diff --git a/tests/components/number/test_recorder.py b/tests/components/number/test_recorder.py index 3123614fe14..1f5d39ed5e9 100644 --- a/tests/components/number/test_recorder.py +++ b/tests/components/number/test_recorder.py @@ -13,7 +13,7 @@ from homeassistant.setup import async_setup_component from homeassistant.util import dt as dt_util from tests.common import async_fire_time_changed -from tests.components.recorder.common import async_wait_recording_done_without_instance +from tests.components.recorder.common import async_wait_recording_done async def test_exclude_attributes(hass, recorder_mock): @@ -24,7 +24,7 @@ async def test_exclude_attributes(hass, recorder_mock): await hass.async_block_till_done() async_fire_time_changed(hass, dt_util.utcnow() + timedelta(minutes=5)) await hass.async_block_till_done() - await async_wait_recording_done_without_instance(hass) + await async_wait_recording_done(hass) def _fetch_states() -> list[State]: with session_scope(hass=hass) as session: diff --git a/tests/components/plant/test_init.py b/tests/components/plant/test_init.py index 3c278532811..40d2930ce93 100644 --- a/tests/components/plant/test_init.py +++ b/tests/components/plant/test_init.py @@ -13,7 +13,7 @@ from homeassistant.const import ( from homeassistant.core import State from homeassistant.setup import async_setup_component -from tests.components.recorder.common import async_wait_recording_done_without_instance +from tests.components.recorder.common import async_wait_recording_done GOOD_DATA = { "moisture": 50, @@ -158,7 +158,7 @@ async def test_load_from_db(hass, recorder_mock): ) await hass.async_block_till_done() # wait for the recorder to really store the data - await async_wait_recording_done_without_instance(hass) + await async_wait_recording_done(hass) assert await async_setup_component( hass, plant.DOMAIN, {plant.DOMAIN: {plant_name: GOOD_CONFIG}} diff --git a/tests/components/recorder/common.py b/tests/components/recorder/common.py index d5c9c4f8762..ec428b7ff85 100644 --- a/tests/components/recorder/common.py +++ b/tests/components/recorder/common.py @@ -23,11 +23,6 @@ def wait_recording_done(hass: HomeAssistant) -> None: hass.block_till_done() -async def async_wait_recording_done_without_instance(hass: HomeAssistant) -> None: - """Block till recording is done.""" - await hass.loop.run_in_executor(None, wait_recording_done, hass) - - def trigger_db_commit(hass: HomeAssistant) -> None: """Force the recorder to commit.""" for _ in range(recorder.DEFAULT_COMMIT_INTERVAL): @@ -35,21 +30,16 @@ def trigger_db_commit(hass: HomeAssistant) -> None: fire_time_changed(hass, dt_util.utcnow() + timedelta(seconds=1)) -async def async_wait_recording_done( - hass: HomeAssistant, - instance: recorder.Recorder, -) -> None: +async def async_wait_recording_done(hass: HomeAssistant) -> None: """Async wait until recording is done.""" await hass.async_block_till_done() async_trigger_db_commit(hass) await hass.async_block_till_done() - await async_recorder_block_till_done(hass, instance) + await async_recorder_block_till_done(hass) await hass.async_block_till_done() -async def async_wait_purge_done( - hass: HomeAssistant, instance: recorder.Recorder, max: int = None -) -> None: +async def async_wait_purge_done(hass: HomeAssistant, max: int = None) -> None: """Wait for max number of purge events. Because a purge may insert another PurgeTask into @@ -60,7 +50,7 @@ async def async_wait_purge_done( if not max: max = DEFAULT_PURGE_TASKS for _ in range(max + 1): - await async_wait_recording_done(hass, instance) + await async_wait_recording_done(hass) @ha.callback @@ -70,11 +60,9 @@ def async_trigger_db_commit(hass: HomeAssistant) -> None: async_fire_time_changed(hass, dt_util.utcnow() + timedelta(seconds=1)) -async def async_recorder_block_till_done( - hass: HomeAssistant, - instance: recorder.Recorder, -) -> None: +async def async_recorder_block_till_done(hass: HomeAssistant) -> None: """Non blocking version of recorder.block_till_done().""" + instance: recorder.Recorder = hass.data[recorder.DATA_INSTANCE] await hass.async_add_executor_job(instance.block_till_done) diff --git a/tests/components/recorder/test_init.py b/tests/components/recorder/test_init.py index 09099f93f69..b0b2ed10580 100644 --- a/tests/components/recorder/test_init.py +++ b/tests/components/recorder/test_init.py @@ -51,12 +51,7 @@ from homeassistant.core import Context, CoreState, HomeAssistant, callback from homeassistant.setup import async_setup_component, setup_component from homeassistant.util import dt as dt_util -from .common import ( - async_wait_recording_done, - async_wait_recording_done_without_instance, - corrupt_db_file, - wait_recording_done, -) +from .common import async_wait_recording_done, corrupt_db_file, wait_recording_done from tests.common import ( SetupRecorderInstanceT, @@ -153,7 +148,7 @@ async def test_state_gets_saved_when_set_before_start_event( hass.bus.async_fire(EVENT_HOMEASSISTANT_STARTED) - await async_wait_recording_done_without_instance(hass) + await async_wait_recording_done(hass) with session_scope(hass=hass) as session: db_states = list(session.query(States)) @@ -165,7 +160,7 @@ async def test_saving_state( hass: HomeAssistant, async_setup_recorder_instance: SetupRecorderInstanceT ): """Test saving and restoring a state.""" - instance = await async_setup_recorder_instance(hass) + await async_setup_recorder_instance(hass) entity_id = "test.recorder" state = "restoring_from_db" @@ -173,7 +168,7 @@ async def test_saving_state( hass.states.async_set(entity_id, state, attributes) - await async_wait_recording_done(hass, instance) + await async_wait_recording_done(hass) with session_scope(hass=hass) as session: db_states = [] @@ -191,9 +186,7 @@ async def test_saving_many_states( hass: HomeAssistant, async_setup_recorder_instance: SetupRecorderInstanceT ): """Test we expire after many commits.""" - instance = await async_setup_recorder_instance( - hass, {recorder.CONF_COMMIT_INTERVAL: 0} - ) + await async_setup_recorder_instance(hass, {recorder.CONF_COMMIT_INTERVAL: 0}) entity_id = "test.recorder" attributes = {"test_attr": 5, "test_attr_10": "nice"} @@ -203,9 +196,9 @@ async def test_saving_many_states( ) as expire_all, patch.object(recorder, "EXPIRE_AFTER_COMMITS", 2): for _ in range(3): hass.states.async_set(entity_id, "on", attributes) - await async_wait_recording_done(hass, instance) + await async_wait_recording_done(hass) hass.states.async_set(entity_id, "off", attributes) - await async_wait_recording_done(hass, instance) + await async_wait_recording_done(hass) assert expire_all.called @@ -219,7 +212,7 @@ async def test_saving_state_with_intermixed_time_changes( hass: HomeAssistant, async_setup_recorder_instance: SetupRecorderInstanceT ): """Test saving states with intermixed time changes.""" - instance = await async_setup_recorder_instance(hass) + await async_setup_recorder_instance(hass) entity_id = "test.recorder" state = "restoring_from_db" @@ -233,7 +226,7 @@ async def test_saving_state_with_intermixed_time_changes( async_fire_time_changed(hass, dt_util.utcnow()) hass.states.async_set(entity_id, state, attributes2) - await async_wait_recording_done(hass, instance) + await async_wait_recording_done(hass) with session_scope(hass=hass) as session: db_states = list(session.query(States)) @@ -326,7 +319,7 @@ async def test_force_shutdown_with_queue_of_writes_that_generate_exceptions( entity_id = "test.recorder" attributes = {"test_attr": 5, "test_attr_10": "nice"} - await async_wait_recording_done(hass, instance) + await async_wait_recording_done(hass) with patch.object(instance, "db_retry_wait", 0.05), patch.object( instance.event_session, @@ -1230,9 +1223,9 @@ async def test_database_corruption_while_running(hass, tmpdir, caplog): "close", side_effect=OperationalError("statement", {}, []), ): - await async_wait_recording_done_without_instance(hass) + await async_wait_recording_done(hass) await hass.async_add_executor_job(corrupt_db_file, test_db_file) - await async_wait_recording_done_without_instance(hass) + await async_wait_recording_done(hass) with patch.object( hass.data[DATA_INSTANCE].event_session, @@ -1243,7 +1236,7 @@ async def test_database_corruption_while_running(hass, tmpdir, caplog): # the database corruption will be discovered # and we will have to rollback to recover hass.states.async_set("test.one", "off", {}) - await async_wait_recording_done_without_instance(hass) + await async_wait_recording_done(hass) assert "Unrecoverable sqlite3 database corruption detected" in caplog.text assert "The system will rename the corrupt database file" in caplog.text @@ -1251,7 +1244,7 @@ async def test_database_corruption_while_running(hass, tmpdir, caplog): # This state should go into the new database hass.states.async_set("test.two", "on", {}) - await async_wait_recording_done_without_instance(hass) + await async_wait_recording_done(hass) def _get_last_state(): with session_scope(hass=hass) as session: @@ -1323,7 +1316,7 @@ async def test_database_lock_and_unlock(hass: HomeAssistant, tmp_path): event_type = "EVENT_TEST" event_data = {"test_attr": 5, "test_attr_10": "nice"} hass.bus.async_fire(event_type, event_data) - task = asyncio.create_task(async_wait_recording_done(hass, instance)) + task = asyncio.create_task(async_wait_recording_done(hass)) # Recording can't be finished while lock is held with pytest.raises(asyncio.TimeoutError): @@ -1364,7 +1357,7 @@ async def test_database_lock_and_overflow(hass: HomeAssistant, tmp_path): # Check that this causes the queue to overflow and write succeeds # even before unlocking. - await async_wait_recording_done(hass, instance) + await async_wait_recording_done(hass) with session_scope(hass=hass) as session: db_events = list(session.query(Events).filter_by(event_type=event_type)) @@ -1436,7 +1429,7 @@ async def test_database_connection_keep_alive( async_fire_time_changed( hass, dt_util.utcnow() + timedelta(seconds=recorder.KEEPALIVE_TIME) ) - await async_wait_recording_done(hass, instance) + await async_wait_recording_done(hass) assert "Sending keepalive" in caplog.text @@ -1453,5 +1446,5 @@ async def test_database_connection_keep_alive_disabled_on_sqlite( async_fire_time_changed( hass, dt_util.utcnow() + timedelta(seconds=recorder.KEEPALIVE_TIME) ) - await async_wait_recording_done(hass, instance) + await async_wait_recording_done(hass) assert "Sending keepalive" not in caplog.text diff --git a/tests/components/recorder/test_migrate.py b/tests/components/recorder/test_migrate.py index ee4c16ff8e0..b32ee399bd8 100644 --- a/tests/components/recorder/test_migrate.py +++ b/tests/components/recorder/test_migrate.py @@ -26,7 +26,7 @@ from homeassistant.components.recorder.models import States from homeassistant.components.recorder.util import session_scope import homeassistant.util.dt as dt_util -from .common import async_wait_recording_done_without_instance, create_engine_test +from .common import async_wait_recording_done, create_engine_test from tests.common import async_fire_time_changed @@ -52,7 +52,7 @@ async def test_schema_update_calls(hass): await async_setup_component( hass, "recorder", {"recorder": {"db_url": "sqlite://"}} ) - await async_wait_recording_done_without_instance(hass) + await async_wait_recording_done(hass) assert recorder.util.async_migration_in_progress(hass) is False update.assert_has_calls( @@ -76,7 +76,7 @@ async def test_migration_in_progress(hass): ) await hass.data[DATA_INSTANCE].async_migration_event.wait() assert recorder.util.async_migration_in_progress(hass) is True - await async_wait_recording_done_without_instance(hass) + await async_wait_recording_done(hass) assert recorder.util.async_migration_in_progress(hass) is False @@ -132,7 +132,7 @@ async def test_database_migration_encounters_corruption(hass): ) hass.states.async_set("my.entity", "on", {}) hass.states.async_set("my.entity", "off", {}) - await async_wait_recording_done_without_instance(hass) + await async_wait_recording_done(hass) assert recorder.util.async_migration_in_progress(hass) is False assert move_away.called @@ -190,7 +190,7 @@ async def test_events_during_migration_are_queued(hass): await hass.async_block_till_done() async_fire_time_changed(hass, dt_util.utcnow() + datetime.timedelta(hours=4)) await hass.data[DATA_INSTANCE].async_recorder_ready.wait() - await async_wait_recording_done_without_instance(hass) + await async_wait_recording_done(hass) assert recorder.util.async_migration_in_progress(hass) is False db_states = await hass.async_add_executor_job(_get_native_states, hass, "my.entity") @@ -218,13 +218,13 @@ async def test_events_during_migration_queue_exhausted(hass): await hass.async_block_till_done() hass.states.async_set("my.entity", "off", {}) await hass.data[DATA_INSTANCE].async_recorder_ready.wait() - await async_wait_recording_done_without_instance(hass) + await async_wait_recording_done(hass) assert recorder.util.async_migration_in_progress(hass) is False db_states = await hass.async_add_executor_job(_get_native_states, hass, "my.entity") assert len(db_states) == 1 hass.states.async_set("my.entity", "on", {}) - await async_wait_recording_done_without_instance(hass) + await async_wait_recording_done(hass) db_states = await hass.async_add_executor_job(_get_native_states, hass, "my.entity") assert len(db_states) == 2 @@ -304,7 +304,7 @@ async def test_schema_migrate(hass, start_version): migration_stall.set() await hass.async_block_till_done() await hass.async_add_executor_job(migration_done.wait) - await async_wait_recording_done_without_instance(hass) + await async_wait_recording_done(hass) assert migration_version == models.SCHEMA_VERSION assert setup_run.called assert recorder.util.async_migration_in_progress(hass) is not True diff --git a/tests/components/recorder/test_purge.py b/tests/components/recorder/test_purge.py index ae49e3905d2..b2c6201b162 100644 --- a/tests/components/recorder/test_purge.py +++ b/tests/components/recorder/test_purge.py @@ -29,7 +29,6 @@ from .common import ( async_recorder_block_till_done, async_wait_purge_done, async_wait_recording_done, - async_wait_recording_done_without_instance, ) from tests.common import SetupRecorderInstanceT @@ -41,7 +40,7 @@ async def test_purge_old_states( """Test deleting old states.""" instance = await async_setup_recorder_instance(hass) - await _add_test_states(hass, instance) + await _add_test_states(hass) # make sure we start with 6 states with session_scope(hass=hass) as session: @@ -88,7 +87,7 @@ async def test_purge_old_states( assert "test.recorder2" not in instance._old_states # Add some more states - await _add_test_states(hass, instance) + await _add_test_states(hass) # make sure we start with 6 states with session_scope(hass=hass) as session: @@ -109,10 +108,10 @@ async def test_purge_old_states_encouters_database_corruption( hass: HomeAssistant, async_setup_recorder_instance: SetupRecorderInstanceT ): """Test database image image is malformed while deleting old states.""" - instance = await async_setup_recorder_instance(hass) + await async_setup_recorder_instance(hass) - await _add_test_states(hass, instance) - await async_wait_recording_done_without_instance(hass) + await _add_test_states(hass) + await async_wait_recording_done(hass) sqlite3_exception = DatabaseError("statement", {}, []) sqlite3_exception.__cause__ = sqlite3.DatabaseError() @@ -127,7 +126,7 @@ async def test_purge_old_states_encouters_database_corruption( recorder.DOMAIN, recorder.SERVICE_PURGE, {"keep_days": 0} ) await hass.async_block_till_done() - await async_wait_recording_done_without_instance(hass) + await async_wait_recording_done(hass) assert move_away.called @@ -145,8 +144,8 @@ async def test_purge_old_states_encounters_temporary_mysql_error( """Test retry on specific mysql operational errors.""" instance = await async_setup_recorder_instance(hass) - await _add_test_states(hass, instance) - await async_wait_recording_done_without_instance(hass) + await _add_test_states(hass) + await async_wait_recording_done(hass) mysql_exception = OperationalError("statement", {}, []) mysql_exception.orig = MagicMock(args=(1205, "retryable")) @@ -163,8 +162,8 @@ async def test_purge_old_states_encounters_temporary_mysql_error( recorder.DOMAIN, recorder.SERVICE_PURGE, {"keep_days": 0} ) await hass.async_block_till_done() - await async_wait_recording_done_without_instance(hass) - await async_wait_recording_done_without_instance(hass) + await async_wait_recording_done(hass) + await async_wait_recording_done(hass) assert "retrying" in caplog.text assert sleep_mock.called @@ -176,10 +175,10 @@ async def test_purge_old_states_encounters_operational_error( caplog, ): """Test error on operational errors that are not mysql does not retry.""" - instance = await async_setup_recorder_instance(hass) + await async_setup_recorder_instance(hass) - await _add_test_states(hass, instance) - await async_wait_recording_done_without_instance(hass) + await _add_test_states(hass) + await async_wait_recording_done(hass) exception = OperationalError("statement", {}, []) @@ -191,8 +190,8 @@ async def test_purge_old_states_encounters_operational_error( recorder.DOMAIN, recorder.SERVICE_PURGE, {"keep_days": 0} ) await hass.async_block_till_done() - await async_wait_recording_done_without_instance(hass) - await async_wait_recording_done_without_instance(hass) + await async_wait_recording_done(hass) + await async_wait_recording_done(hass) assert "retrying" not in caplog.text assert "Error executing purge" in caplog.text @@ -204,7 +203,7 @@ async def test_purge_old_events( """Test deleting old events.""" instance = await async_setup_recorder_instance(hass) - await _add_test_events(hass, instance) + await _add_test_events(hass) with session_scope(hass=hass) as session: events = session.query(Events).filter(Events.event_type.like("EVENT_TEST%")) @@ -229,7 +228,7 @@ async def test_purge_old_recorder_runs( """Test deleting old recorder runs keeps current run.""" instance = await async_setup_recorder_instance(hass) - await _add_test_recorder_runs(hass, instance) + await _add_test_recorder_runs(hass) # make sure we start with 7 recorder runs with session_scope(hass=hass) as session: @@ -253,7 +252,7 @@ async def test_purge_old_statistics_runs( """Test deleting old statistics runs keeps the latest run.""" instance = await async_setup_recorder_instance(hass) - await _add_test_statistics_runs(hass, instance) + await _add_test_statistics_runs(hass) # make sure we start with 7 statistics runs with session_scope(hass=hass) as session: @@ -289,16 +288,16 @@ async def test_purge_method( assert run1.run_id == run2.run_id assert run1.start == run2.start - instance = await async_setup_recorder_instance(hass) + await async_setup_recorder_instance(hass) service_data = {"keep_days": 4} - await _add_test_events(hass, instance) - await _add_test_states(hass, instance) - await _add_test_statistics(hass, instance) - await _add_test_recorder_runs(hass, instance) - await _add_test_statistics_runs(hass, instance) + await _add_test_events(hass) + await _add_test_states(hass) + await _add_test_statistics(hass) + await _add_test_recorder_runs(hass) + await _add_test_statistics_runs(hass) await hass.async_block_till_done() - await async_wait_recording_done(hass, instance) + await async_wait_recording_done(hass) # make sure we start with 6 states with session_scope(hass=hass) as session: @@ -325,14 +324,14 @@ async def test_purge_method( session.expunge(itm) await hass.async_block_till_done() - await async_wait_purge_done(hass, instance) + await async_wait_purge_done(hass) # run purge method - no service data, use defaults await hass.services.async_call("recorder", "purge") await hass.async_block_till_done() # Small wait for recorder thread - await async_wait_purge_done(hass, instance) + await async_wait_purge_done(hass) with session_scope(hass=hass) as session: states = session.query(States) @@ -349,7 +348,7 @@ async def test_purge_method( await hass.async_block_till_done() # Small wait for recorder thread - await async_wait_purge_done(hass, instance) + await async_wait_purge_done(hass) with session_scope(hass=hass) as session: states = session.query(States) @@ -381,7 +380,7 @@ async def test_purge_method( service_data["repack"] = True await hass.services.async_call("recorder", "purge", service_data=service_data) await hass.async_block_till_done() - await async_wait_purge_done(hass, instance) + await async_wait_purge_done(hass) assert "Vacuuming SQL DB to free space" in caplog.text @@ -421,8 +420,8 @@ async def test_purge_edge_case( ) ) - instance = await async_setup_recorder_instance(hass, None) - await async_wait_purge_done(hass, instance) + await async_setup_recorder_instance(hass, None) + await async_wait_purge_done(hass) service_data = {"keep_days": 2} timestamp = dt_util.utcnow() - timedelta(days=2, minutes=1) @@ -443,8 +442,8 @@ async def test_purge_edge_case( ) await hass.async_block_till_done() - await async_recorder_block_till_done(hass, instance) - await async_wait_purge_done(hass, instance) + await async_recorder_block_till_done(hass) + await async_wait_purge_done(hass) with session_scope(hass=hass) as session: states = session.query(States) @@ -521,7 +520,7 @@ async def test_purge_cutoff_date( ) instance = await async_setup_recorder_instance(hass, None) - await async_wait_purge_done(hass, instance) + await async_wait_purge_done(hass) service_data = {"keep_days": 2} @@ -549,8 +548,8 @@ async def test_purge_cutoff_date( instance.queue.put(PurgeTask(cutoff, repack=False, apply_filter=False)) await hass.async_block_till_done() - await async_recorder_block_till_done(hass, instance) - await async_wait_purge_done(hass, instance) + await async_recorder_block_till_done(hass) + await async_wait_purge_done(hass) with session_scope(hass=hass) as session: states = session.query(States) @@ -579,8 +578,8 @@ async def test_purge_cutoff_date( # Make sure we can purge everything instance.queue.put(PurgeTask(dt_util.utcnow(), repack=False, apply_filter=False)) - await async_recorder_block_till_done(hass, instance) - await async_wait_purge_done(hass, instance) + await async_recorder_block_till_done(hass) + await async_wait_purge_done(hass) with session_scope(hass=hass) as session: states = session.query(States) @@ -590,8 +589,8 @@ async def test_purge_cutoff_date( # Make sure we can purge everything when the db is already empty instance.queue.put(PurgeTask(dt_util.utcnow(), repack=False, apply_filter=False)) - await async_recorder_block_till_done(hass, instance) - await async_wait_purge_done(hass, instance) + await async_recorder_block_till_done(hass) + await async_wait_purge_done(hass) with session_scope(hass=hass) as session: states = session.query(States) @@ -711,8 +710,8 @@ async def test_purge_filtered_states( ) await hass.async_block_till_done() - await async_recorder_block_till_done(hass, instance) - await async_wait_purge_done(hass, instance) + await async_recorder_block_till_done(hass) + await async_wait_purge_done(hass) with session_scope(hass=hass) as session: states = session.query(States) @@ -731,11 +730,11 @@ async def test_purge_filtered_states( ) await hass.async_block_till_done() - await async_recorder_block_till_done(hass, instance) - await async_wait_purge_done(hass, instance) + await async_recorder_block_till_done(hass) + await async_wait_purge_done(hass) - await async_recorder_block_till_done(hass, instance) - await async_wait_purge_done(hass, instance) + await async_recorder_block_till_done(hass) + await async_wait_purge_done(hass) with session_scope(hass=hass) as session: states = session.query(States) @@ -767,8 +766,8 @@ async def test_purge_filtered_states( await hass.services.async_call( recorder.DOMAIN, recorder.SERVICE_PURGE, service_data ) - await async_recorder_block_till_done(hass, instance) - await async_wait_purge_done(hass, instance) + await async_recorder_block_till_done(hass) + await async_wait_purge_done(hass) with session_scope(hass=hass) as session: final_keep_state = session.query(States).get(74) @@ -782,8 +781,8 @@ async def test_purge_filtered_states( await hass.services.async_call( recorder.DOMAIN, recorder.SERVICE_PURGE, service_data ) - await async_recorder_block_till_done(hass, instance) - await async_wait_purge_done(hass, instance) + await async_recorder_block_till_done(hass) + await async_wait_purge_done(hass) with session_scope(hass=hass) as session: remaining = list(session.query(States)) @@ -830,8 +829,8 @@ async def test_purge_filtered_states_to_empty( await hass.services.async_call( recorder.DOMAIN, recorder.SERVICE_PURGE, service_data ) - await async_recorder_block_till_done(hass, instance) - await async_wait_purge_done(hass, instance) + await async_recorder_block_till_done(hass) + await async_wait_purge_done(hass) with session_scope(hass=hass) as session: states = session.query(States) @@ -844,8 +843,8 @@ async def test_purge_filtered_states_to_empty( await hass.services.async_call( recorder.DOMAIN, recorder.SERVICE_PURGE, service_data ) - await async_recorder_block_till_done(hass, instance) - await async_wait_purge_done(hass, instance) + await async_recorder_block_till_done(hass) + await async_wait_purge_done(hass) async def test_purge_without_state_attributes_filtered_states_to_empty( @@ -898,8 +897,8 @@ async def test_purge_without_state_attributes_filtered_states_to_empty( await hass.services.async_call( recorder.DOMAIN, recorder.SERVICE_PURGE, service_data ) - await async_recorder_block_till_done(hass, instance) - await async_wait_purge_done(hass, instance) + await async_recorder_block_till_done(hass) + await async_wait_purge_done(hass) with session_scope(hass=hass) as session: states = session.query(States) @@ -912,8 +911,8 @@ async def test_purge_without_state_attributes_filtered_states_to_empty( await hass.services.async_call( recorder.DOMAIN, recorder.SERVICE_PURGE, service_data ) - await async_recorder_block_till_done(hass, instance) - await async_wait_purge_done(hass, instance) + await async_recorder_block_till_done(hass) + await async_wait_purge_done(hass) async def test_purge_filtered_events( @@ -922,7 +921,7 @@ async def test_purge_filtered_events( ): """Test filtered events are purged.""" config: ConfigType = {"exclude": {"event_types": ["EVENT_PURGE"]}} - instance = await async_setup_recorder_instance(hass, config) + await async_setup_recorder_instance(hass, config) def _add_db_entries(hass: HomeAssistant) -> None: with recorder.session_scope(hass=hass) as session: @@ -971,8 +970,8 @@ async def test_purge_filtered_events( ) await hass.async_block_till_done() - await async_recorder_block_till_done(hass, instance) - await async_wait_purge_done(hass, instance) + await async_recorder_block_till_done(hass) + await async_wait_purge_done(hass) with session_scope(hass=hass) as session: events_purge = session.query(Events).filter(Events.event_type == "EVENT_PURGE") @@ -991,11 +990,11 @@ async def test_purge_filtered_events( ) await hass.async_block_till_done() - await async_recorder_block_till_done(hass, instance) - await async_wait_purge_done(hass, instance) + await async_recorder_block_till_done(hass) + await async_wait_purge_done(hass) - await async_recorder_block_till_done(hass, instance) - await async_wait_purge_done(hass, instance) + await async_recorder_block_till_done(hass) + await async_wait_purge_done(hass) with session_scope(hass=hass) as session: events_purge = session.query(Events).filter(Events.event_type == "EVENT_PURGE") @@ -1091,11 +1090,11 @@ async def test_purge_filtered_events_state_changed( ) await hass.async_block_till_done() - await async_recorder_block_till_done(hass, instance) - await async_wait_purge_done(hass, instance) + await async_recorder_block_till_done(hass) + await async_wait_purge_done(hass) - await async_recorder_block_till_done(hass, instance) - await async_wait_purge_done(hass, instance) + await async_recorder_block_till_done(hass) + await async_wait_purge_done(hass) with session_scope(hass=hass) as session: events_keep = session.query(Events).filter(Events.event_type == "EVENT_KEEP") @@ -1117,7 +1116,7 @@ async def test_purge_entities( hass: HomeAssistant, async_setup_recorder_instance: SetupRecorderInstanceT ): """Test purging of specific entities.""" - instance = await async_setup_recorder_instance(hass) + await async_setup_recorder_instance(hass) async def _purge_entities(hass, entity_ids, domains, entity_globs): service_data = { @@ -1131,8 +1130,8 @@ async def test_purge_entities( ) await hass.async_block_till_done() - await async_recorder_block_till_done(hass, instance) - await async_wait_purge_done(hass, instance) + await async_recorder_block_till_done(hass) + await async_wait_purge_done(hass) def _add_purge_records(hass: HomeAssistant) -> None: with recorder.session_scope(hass=hass) as session: @@ -1242,7 +1241,7 @@ async def test_purge_entities( assert states.count() == 0 -async def _add_test_states(hass: HomeAssistant, instance: recorder.Recorder): +async def _add_test_states(hass: HomeAssistant): """Add multiple states to the db for testing.""" utcnow = dt_util.utcnow() five_days_ago = utcnow - timedelta(days=5) @@ -1253,7 +1252,7 @@ async def _add_test_states(hass: HomeAssistant, instance: recorder.Recorder): """Set the state.""" hass.states.async_set(entity_id, state, **kwargs) await hass.async_block_till_done() - await async_wait_recording_done(hass, instance) + await async_wait_recording_done(hass) for event_id in range(6): if event_id < 2: @@ -1275,7 +1274,7 @@ async def _add_test_states(hass: HomeAssistant, instance: recorder.Recorder): await set_state("test.recorder2", state, attributes=attributes) -async def _add_test_events(hass: HomeAssistant, instance: recorder.Recorder): +async def _add_test_events(hass: HomeAssistant): """Add a few events for testing.""" utcnow = dt_util.utcnow() five_days_ago = utcnow - timedelta(days=5) @@ -1283,7 +1282,7 @@ async def _add_test_events(hass: HomeAssistant, instance: recorder.Recorder): event_data = {"test_attr": 5, "test_attr_10": "nice"} await hass.async_block_till_done() - await async_wait_recording_done(hass, instance) + await async_wait_recording_done(hass) with recorder.session_scope(hass=hass) as session: for event_id in range(6): @@ -1307,14 +1306,14 @@ async def _add_test_events(hass: HomeAssistant, instance: recorder.Recorder): ) -async def _add_test_statistics(hass: HomeAssistant, instance: recorder.Recorder): +async def _add_test_statistics(hass: HomeAssistant): """Add multiple statistics to the db for testing.""" utcnow = dt_util.utcnow() five_days_ago = utcnow - timedelta(days=5) eleven_days_ago = utcnow - timedelta(days=11) await hass.async_block_till_done() - await async_wait_recording_done(hass, instance) + await async_wait_recording_done(hass) with recorder.session_scope(hass=hass) as session: for event_id in range(6): @@ -1336,14 +1335,14 @@ async def _add_test_statistics(hass: HomeAssistant, instance: recorder.Recorder) ) -async def _add_test_recorder_runs(hass: HomeAssistant, instance: recorder.Recorder): +async def _add_test_recorder_runs(hass: HomeAssistant): """Add a few recorder_runs for testing.""" utcnow = dt_util.utcnow() five_days_ago = utcnow - timedelta(days=5) eleven_days_ago = utcnow - timedelta(days=11) await hass.async_block_till_done() - await async_wait_recording_done(hass, instance) + await async_wait_recording_done(hass) with recorder.session_scope(hass=hass) as session: for rec_id in range(6): @@ -1363,14 +1362,14 @@ async def _add_test_recorder_runs(hass: HomeAssistant, instance: recorder.Record ) -async def _add_test_statistics_runs(hass: HomeAssistant, instance: recorder.Recorder): +async def _add_test_statistics_runs(hass: HomeAssistant): """Add a few recorder_runs for testing.""" utcnow = dt_util.utcnow() five_days_ago = utcnow - timedelta(days=5) eleven_days_ago = utcnow - timedelta(days=11) await hass.async_block_till_done() - await async_wait_recording_done(hass, instance) + await async_wait_recording_done(hass) with recorder.session_scope(hass=hass) as session: for rec_id in range(6): diff --git a/tests/components/recorder/test_statistics.py b/tests/components/recorder/test_statistics.py index 818db57b382..8a73716e482 100644 --- a/tests/components/recorder/test_statistics.py +++ b/tests/components/recorder/test_statistics.py @@ -35,7 +35,7 @@ from homeassistant.exceptions import HomeAssistantError from homeassistant.setup import setup_component import homeassistant.util.dt as dt_util -from .common import async_wait_recording_done_without_instance +from .common import async_wait_recording_done from tests.common import get_test_home_assistant, mock_registry from tests.components.recorder.common import wait_recording_done @@ -389,7 +389,7 @@ async def test_external_statistics(hass, hass_ws_client, recorder_mock, caplog): async_add_external_statistics( hass, external_metadata, (external_statistics1, external_statistics2) ) - await async_wait_recording_done_without_instance(hass) + await async_wait_recording_done(hass) stats = statistics_during_period(hass, zero, period="hour") assert stats == { "test:total_energy_import": [ @@ -467,7 +467,7 @@ async def test_external_statistics(hass, hass_ws_client, recorder_mock, caplog): "sum": 6, } async_add_external_statistics(hass, external_metadata, (external_statistics,)) - await async_wait_recording_done_without_instance(hass) + await async_wait_recording_done(hass) stats = statistics_during_period(hass, zero, period="hour") assert stats == { "test:total_energy_import": [ @@ -507,7 +507,7 @@ async def test_external_statistics(hass, hass_ws_client, recorder_mock, caplog): "sum": 5, } async_add_external_statistics(hass, external_metadata, (external_statistics,)) - await async_wait_recording_done_without_instance(hass) + await async_wait_recording_done(hass) stats = statistics_during_period(hass, zero, period="hour") assert stats == { "test:total_energy_import": [ @@ -548,7 +548,7 @@ async def test_external_statistics(hass, hass_ws_client, recorder_mock, caplog): response = await client.receive_json() assert response["success"] - await async_wait_recording_done_without_instance(hass) + await async_wait_recording_done(hass) stats = statistics_during_period(hass, zero, period="hour") assert stats == { "test:total_energy_import": [ diff --git a/tests/components/recorder/test_websocket_api.py b/tests/components/recorder/test_websocket_api.py index 2193045387d..40bde7f051a 100644 --- a/tests/components/recorder/test_websocket_api.py +++ b/tests/components/recorder/test_websocket_api.py @@ -14,11 +14,7 @@ from homeassistant.setup import async_setup_component import homeassistant.util.dt as dt_util from homeassistant.util.unit_system import METRIC_SYSTEM -from .common import ( - async_wait_recording_done_without_instance, - create_engine_test, - trigger_db_commit, -) +from .common import async_wait_recording_done, create_engine_test, trigger_db_commit from tests.common import async_fire_time_changed, init_recorder_component @@ -265,7 +261,7 @@ async def test_recorder_info(hass, hass_ws_client, recorder_mock): client = await hass_ws_client() # Ensure there are no queued events - await async_wait_recording_done_without_instance(hass) + await async_wait_recording_done(hass) await client.send_json({"id": 1, "type": "recorder/info"}) response = await client.receive_json() @@ -358,7 +354,7 @@ async def test_recorder_info_migration_queue_exhausted(hass, hass_ws_client): # Let migration finish migration_done.set() - await async_wait_recording_done_without_instance(hass) + await async_wait_recording_done(hass) # Check the status after migration finished await client.send_json({"id": 2, "type": "recorder/info"}) @@ -388,7 +384,7 @@ async def test_backup_start_timeout( client = await hass_ws_client(hass, hass_supervisor_access_token) # Ensure there are no queued events - await async_wait_recording_done_without_instance(hass) + await async_wait_recording_done(hass) with patch.object(recorder, "DB_LOCK_TIMEOUT", 0): try: @@ -407,7 +403,7 @@ async def test_backup_end( client = await hass_ws_client(hass, hass_supervisor_access_token) # Ensure there are no queued events - await async_wait_recording_done_without_instance(hass) + await async_wait_recording_done(hass) await client.send_json({"id": 1, "type": "backup/start"}) response = await client.receive_json() @@ -425,7 +421,7 @@ async def test_backup_end_without_start( client = await hass_ws_client(hass, hass_supervisor_access_token) # Ensure there are no queued events - await async_wait_recording_done_without_instance(hass) + await async_wait_recording_done(hass) await client.send_json({"id": 1, "type": "backup/end"}) response = await client.receive_json() diff --git a/tests/components/script/test_recorder.py b/tests/components/script/test_recorder.py index 79f83b6c482..0dc7bd54746 100644 --- a/tests/components/script/test_recorder.py +++ b/tests/components/script/test_recorder.py @@ -18,7 +18,7 @@ from homeassistant.core import Context, State, callback from homeassistant.setup import async_setup_component from tests.common import async_mock_service -from tests.components.recorder.common import async_wait_recording_done_without_instance +from tests.components.recorder.common import async_wait_recording_done @pytest.fixture @@ -59,7 +59,7 @@ async def test_exclude_attributes(hass, recorder_mock, calls): script.DOMAIN, "test", {"greeting": "world"}, context=context ) await hass.async_block_till_done() - await async_wait_recording_done_without_instance(hass) + await async_wait_recording_done(hass) assert len(calls) == 1 def _fetch_states() -> list[State]: diff --git a/tests/components/select/test_recorder.py b/tests/components/select/test_recorder.py index 76a32db3f9f..f48679a43f1 100644 --- a/tests/components/select/test_recorder.py +++ b/tests/components/select/test_recorder.py @@ -13,7 +13,7 @@ from homeassistant.setup import async_setup_component from homeassistant.util import dt as dt_util from tests.common import async_fire_time_changed -from tests.components.recorder.common import async_wait_recording_done_without_instance +from tests.components.recorder.common import async_wait_recording_done async def test_exclude_attributes(hass, recorder_mock): @@ -24,7 +24,7 @@ async def test_exclude_attributes(hass, recorder_mock): await hass.async_block_till_done() async_fire_time_changed(hass, dt_util.utcnow() + timedelta(minutes=5)) await hass.async_block_till_done() - await async_wait_recording_done_without_instance(hass) + await async_wait_recording_done(hass) def _fetch_states() -> list[State]: with session_scope(hass=hass) as session: diff --git a/tests/components/sensor/test_recorder.py b/tests/components/sensor/test_recorder.py index a56f440acb3..b21b1d0976a 100644 --- a/tests/components/sensor/test_recorder.py +++ b/tests/components/sensor/test_recorder.py @@ -29,7 +29,7 @@ from homeassistant.util.unit_system import IMPERIAL_SYSTEM, METRIC_SYSTEM from tests.common import async_setup_component, init_recorder_component from tests.components.recorder.common import ( - async_wait_recording_done_without_instance, + async_wait_recording_done, wait_recording_done, ) @@ -374,7 +374,7 @@ async def test_compile_hourly_sum_statistics_amount( four, eight, states = await hass.async_add_executor_job( record_meter_states, hass, period0, "sensor.test1", attributes, seq ) - await async_wait_recording_done_without_instance(hass) + await async_wait_recording_done(hass) hist = history.get_significant_states( hass, period0 - timedelta.resolution, eight + timedelta.resolution ) @@ -383,15 +383,15 @@ async def test_compile_hourly_sum_statistics_amount( await hass.async_add_executor_job( partial(recorder.do_adhoc_statistics, start=period0) ) - await async_wait_recording_done_without_instance(hass) + await async_wait_recording_done(hass) await hass.async_add_executor_job( partial(recorder.do_adhoc_statistics, start=period1) ) - await async_wait_recording_done_without_instance(hass) + await async_wait_recording_done(hass) await hass.async_add_executor_job( partial(recorder.do_adhoc_statistics, start=period2) ) - await async_wait_recording_done_without_instance(hass) + await async_wait_recording_done(hass) statistic_ids = await hass.async_add_executor_job(list_statistic_ids, hass) assert statistic_ids == [ { @@ -478,7 +478,7 @@ async def test_compile_hourly_sum_statistics_amount( ) response = await client.receive_json() assert response["success"] - await async_wait_recording_done_without_instance(hass) + await async_wait_recording_done(hass) expected_stats["sensor.test1"][1]["sum"] = approx(factor * 40.0 + 100) expected_stats["sensor.test1"][2]["sum"] = approx(factor * 70.0 + 100) @@ -497,7 +497,7 @@ async def test_compile_hourly_sum_statistics_amount( ) response = await client.receive_json() assert response["success"] - await async_wait_recording_done_without_instance(hass) + await async_wait_recording_done(hass) expected_stats["sensor.test1"][1]["sum"] = approx(factor * 40.0 + 100) expected_stats["sensor.test1"][2]["sum"] = approx(factor * 70.0 - 300) diff --git a/tests/components/siren/test_recorder.py b/tests/components/siren/test_recorder.py index db3ab5f1d3d..46e066e4873 100644 --- a/tests/components/siren/test_recorder.py +++ b/tests/components/siren/test_recorder.py @@ -13,7 +13,7 @@ from homeassistant.setup import async_setup_component from homeassistant.util import dt as dt_util from tests.common import async_fire_time_changed -from tests.components.recorder.common import async_wait_recording_done_without_instance +from tests.components.recorder.common import async_wait_recording_done async def test_exclude_attributes(hass, recorder_mock): @@ -24,7 +24,7 @@ async def test_exclude_attributes(hass, recorder_mock): await hass.async_block_till_done() async_fire_time_changed(hass, dt_util.utcnow() + timedelta(minutes=5)) await hass.async_block_till_done() - await async_wait_recording_done_without_instance(hass) + await async_wait_recording_done(hass) def _fetch_states() -> list[State]: with session_scope(hass=hass) as session: diff --git a/tests/components/statistics/test_sensor.py b/tests/components/statistics/test_sensor.py index 1f714446fd5..30ea0925993 100644 --- a/tests/components/statistics/test_sensor.py +++ b/tests/components/statistics/test_sensor.py @@ -24,7 +24,7 @@ from homeassistant.setup import async_setup_component from homeassistant.util import dt as dt_util from tests.common import async_fire_time_changed, get_fixture_path -from tests.components.recorder.common import async_wait_recording_done_without_instance +from tests.components.recorder.common import async_wait_recording_done VALUES_BINARY = ["on", "off", "on", "off", "on", "off", "on", "off", "on"] VALUES_NUMERIC = [17, 20, 15.2, 5, 3.8, 9.2, 6.7, 14, 6] @@ -906,7 +906,7 @@ async def test_initialize_from_database(hass: HomeAssistant, recorder_mock): """Test initializing the statistics from the recorder database.""" # enable and pre-fill the recorder await hass.async_block_till_done() - await async_wait_recording_done_without_instance(hass) + await async_wait_recording_done(hass) for value in VALUES_NUMERIC: hass.states.async_set( @@ -915,7 +915,7 @@ async def test_initialize_from_database(hass: HomeAssistant, recorder_mock): {ATTR_UNIT_OF_MEASUREMENT: TEMP_CELSIUS}, ) await hass.async_block_till_done() - await async_wait_recording_done_without_instance(hass) + await async_wait_recording_done(hass) # create the statistics component, get filled from database assert await async_setup_component( @@ -958,7 +958,7 @@ async def test_initialize_from_database_with_maxage(hass: HomeAssistant, recorde # enable and pre-fill the recorder await hass.async_block_till_done() - await async_wait_recording_done_without_instance(hass) + await async_wait_recording_done(hass) with patch( "homeassistant.components.statistics.sensor.dt_util.utcnow", new=mock_now @@ -971,7 +971,7 @@ async def test_initialize_from_database_with_maxage(hass: HomeAssistant, recorde ) await hass.async_block_till_done() mock_data["return_time"] += timedelta(hours=1) - await async_wait_recording_done_without_instance(hass) + await async_wait_recording_done(hass) # create the statistics component, get filled from database assert await async_setup_component( hass, diff --git a/tests/components/sun/test_recorder.py b/tests/components/sun/test_recorder.py index fc1664d340b..0b0d0ad48cf 100644 --- a/tests/components/sun/test_recorder.py +++ b/tests/components/sun/test_recorder.py @@ -23,7 +23,7 @@ from homeassistant.setup import async_setup_component from homeassistant.util import dt as dt_util from tests.common import async_fire_time_changed -from tests.components.recorder.common import async_wait_recording_done_without_instance +from tests.components.recorder.common import async_wait_recording_done async def test_exclude_attributes(hass, recorder_mock): @@ -32,7 +32,7 @@ async def test_exclude_attributes(hass, recorder_mock): await hass.async_block_till_done() async_fire_time_changed(hass, dt_util.utcnow() + timedelta(minutes=5)) await hass.async_block_till_done() - await async_wait_recording_done_without_instance(hass) + await async_wait_recording_done(hass) def _fetch_sun_states() -> list[State]: with session_scope(hass=hass) as session: diff --git a/tests/components/tibber/test_statistics.py b/tests/components/tibber/test_statistics.py index 8b8ad24d6b7..17297ffda3a 100644 --- a/tests/components/tibber/test_statistics.py +++ b/tests/components/tibber/test_statistics.py @@ -7,7 +7,7 @@ from homeassistant.util import dt as dt_util from .test_common import CONSUMPTION_DATA_1, mock_get_homes -from tests.components.recorder.common import async_wait_recording_done_without_instance +from tests.components.recorder.common import async_wait_recording_done async def test_async_setup_entry(hass, recorder_mock): @@ -19,7 +19,7 @@ async def test_async_setup_entry(hass, recorder_mock): coordinator = TibberDataCoordinator(hass, tibber_connection) await coordinator._async_update_data() - await async_wait_recording_done_without_instance(hass) + await async_wait_recording_done(hass) # Validate consumption statistic_id = "tibber:energy_consumption_home_id" diff --git a/tests/components/update/test_recorder.py b/tests/components/update/test_recorder.py index db31a5d5926..d340a8dfa3f 100644 --- a/tests/components/update/test_recorder.py +++ b/tests/components/update/test_recorder.py @@ -17,7 +17,7 @@ from homeassistant.setup import async_setup_component from homeassistant.util import dt as dt_util from tests.common import async_fire_time_changed -from tests.components.recorder.common import async_wait_recording_done_without_instance +from tests.components.recorder.common import async_wait_recording_done async def test_exclude_attributes( @@ -39,7 +39,7 @@ async def test_exclude_attributes( await hass.async_block_till_done() async_fire_time_changed(hass, dt_util.utcnow() + timedelta(minutes=5)) await hass.async_block_till_done() - await async_wait_recording_done_without_instance(hass) + await async_wait_recording_done(hass) def _fetch_states() -> list[State]: with session_scope(hass=hass) as session: diff --git a/tests/components/vacuum/test_recorder.py b/tests/components/vacuum/test_recorder.py index dc8744fedc1..6267091b984 100644 --- a/tests/components/vacuum/test_recorder.py +++ b/tests/components/vacuum/test_recorder.py @@ -13,7 +13,7 @@ from homeassistant.setup import async_setup_component from homeassistant.util import dt as dt_util from tests.common import async_fire_time_changed -from tests.components.recorder.common import async_wait_recording_done_without_instance +from tests.components.recorder.common import async_wait_recording_done async def test_exclude_attributes(hass, recorder_mock): @@ -24,7 +24,7 @@ async def test_exclude_attributes(hass, recorder_mock): await hass.async_block_till_done() async_fire_time_changed(hass, dt_util.utcnow() + timedelta(minutes=5)) await hass.async_block_till_done() - await async_wait_recording_done_without_instance(hass) + await async_wait_recording_done(hass) def _fetch_states() -> list[State]: with session_scope(hass=hass) as session: diff --git a/tests/components/water_heater/test_recorder.py b/tests/components/water_heater/test_recorder.py index 199a7785638..4a70fc12c8f 100644 --- a/tests/components/water_heater/test_recorder.py +++ b/tests/components/water_heater/test_recorder.py @@ -17,7 +17,7 @@ from homeassistant.setup import async_setup_component from homeassistant.util import dt as dt_util from tests.common import async_fire_time_changed -from tests.components.recorder.common import async_wait_recording_done_without_instance +from tests.components.recorder.common import async_wait_recording_done async def test_exclude_attributes(hass, recorder_mock): @@ -28,7 +28,7 @@ async def test_exclude_attributes(hass, recorder_mock): await hass.async_block_till_done() async_fire_time_changed(hass, dt_util.utcnow() + timedelta(minutes=5)) await hass.async_block_till_done() - await async_wait_recording_done_without_instance(hass) + await async_wait_recording_done(hass) def _fetch_states() -> list[State]: with session_scope(hass=hass) as session: diff --git a/tests/components/weather/test_recorder.py b/tests/components/weather/test_recorder.py index 19cd88bb0f2..9f2e5289013 100644 --- a/tests/components/weather/test_recorder.py +++ b/tests/components/weather/test_recorder.py @@ -12,7 +12,7 @@ from homeassistant.util import dt as dt_util from homeassistant.util.unit_system import METRIC_SYSTEM from tests.common import async_fire_time_changed -from tests.components.recorder.common import async_wait_recording_done_without_instance +from tests.components.recorder.common import async_wait_recording_done async def test_exclude_attributes(hass: HomeAssistant, recorder_mock) -> None: @@ -27,7 +27,7 @@ async def test_exclude_attributes(hass: HomeAssistant, recorder_mock) -> None: await hass.async_block_till_done() async_fire_time_changed(hass, dt_util.utcnow() + timedelta(minutes=5)) await hass.async_block_till_done() - await async_wait_recording_done_without_instance(hass) + await async_wait_recording_done(hass) def _fetch_states() -> list[State]: with session_scope(hass=hass) as session: diff --git a/tests/conftest.py b/tests/conftest.py index 53b6b877eda..f0cfb65f9c0 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -767,7 +767,7 @@ async def async_setup_recorder_instance( instance = hass.data[recorder.DATA_INSTANCE] # The recorder's worker is not started until Home Assistant is running if hass.state == CoreState.running: - await async_recorder_block_till_done(hass, instance) + await async_recorder_block_till_done(hass) return instance return async_setup_recorder