diff --git a/pylint/plugins/hass_enforce_type_hints.py b/pylint/plugins/hass_enforce_type_hints.py index f5d5b86635a..ec129d97c16 100644 --- a/pylint/plugins/hass_enforce_type_hints.py +++ b/pylint/plugins/hass_enforce_type_hints.py @@ -100,7 +100,7 @@ _TEST_FIXTURES: dict[str, list[str] | str] = { "aiohttp_client": "ClientSessionGenerator", "aiohttp_server": "Callable[[], TestServer]", "area_registry": "AreaRegistry", - "async_setup_recorder_instance": "RecorderInstanceGenerator", + "async_test_recorder": "RecorderInstanceGenerator", "caplog": "pytest.LogCaptureFixture", "capsys": "pytest.CaptureFixture[str]", "current_request_with_host": "None", diff --git a/tests/components/history/conftest.py b/tests/components/history/conftest.py index 075909dfd63..dd10fccccdc 100644 --- a/tests/components/history/conftest.py +++ b/tests/components/history/conftest.py @@ -13,7 +13,7 @@ from tests.typing import RecorderInstanceGenerator @pytest.fixture async def mock_recorder_before_hass( - async_setup_recorder_instance: RecorderInstanceGenerator, + async_test_recorder: RecorderInstanceGenerator, ) -> None: """Set up recorder.""" diff --git a/tests/components/recorder/auto_repairs/statistics/test_duplicates.py b/tests/components/recorder/auto_repairs/statistics/test_duplicates.py index 5ed86698c58..457f180bb91 100644 --- a/tests/components/recorder/auto_repairs/statistics/test_duplicates.py +++ b/tests/components/recorder/auto_repairs/statistics/test_duplicates.py @@ -29,7 +29,7 @@ from tests.typing import RecorderInstanceGenerator @pytest.fixture async def mock_recorder_before_hass( - async_setup_recorder_instance: RecorderInstanceGenerator, + async_test_recorder: RecorderInstanceGenerator, ) -> None: """Set up recorder.""" diff --git a/tests/components/recorder/test_entity_registry.py b/tests/components/recorder/test_entity_registry.py index a74992525b1..ad438dcc525 100644 --- a/tests/components/recorder/test_entity_registry.py +++ b/tests/components/recorder/test_entity_registry.py @@ -40,7 +40,7 @@ def _count_entity_id_in_states_meta( @pytest.fixture async def mock_recorder_before_hass( - async_setup_recorder_instance: RecorderInstanceGenerator, + async_test_recorder: RecorderInstanceGenerator, ) -> None: """Set up recorder.""" diff --git a/tests/components/recorder/test_history.py b/tests/components/recorder/test_history.py index af846353467..e031909edd6 100644 --- a/tests/components/recorder/test_history.py +++ b/tests/components/recorder/test_history.py @@ -47,7 +47,7 @@ from tests.typing import RecorderInstanceGenerator @pytest.fixture async def mock_recorder_before_hass( - async_setup_recorder_instance: RecorderInstanceGenerator, + async_test_recorder: RecorderInstanceGenerator, ) -> None: """Set up recorder.""" diff --git a/tests/components/recorder/test_history_db_schema_30.py b/tests/components/recorder/test_history_db_schema_30.py index e5e80b0cdb9..0e5f6cf7f79 100644 --- a/tests/components/recorder/test_history_db_schema_30.py +++ b/tests/components/recorder/test_history_db_schema_30.py @@ -33,7 +33,7 @@ from tests.typing import RecorderInstanceGenerator @pytest.fixture async def mock_recorder_before_hass( - async_setup_recorder_instance: RecorderInstanceGenerator, + async_test_recorder: RecorderInstanceGenerator, ) -> None: """Set up recorder.""" diff --git a/tests/components/recorder/test_history_db_schema_32.py b/tests/components/recorder/test_history_db_schema_32.py index 8a3e6a58ab3..3ee6edd8e1e 100644 --- a/tests/components/recorder/test_history_db_schema_32.py +++ b/tests/components/recorder/test_history_db_schema_32.py @@ -33,7 +33,7 @@ from tests.typing import RecorderInstanceGenerator @pytest.fixture async def mock_recorder_before_hass( - async_setup_recorder_instance: RecorderInstanceGenerator, + async_test_recorder: RecorderInstanceGenerator, ) -> None: """Set up recorder.""" diff --git a/tests/components/recorder/test_history_db_schema_42.py b/tests/components/recorder/test_history_db_schema_42.py index 083d4c0930e..974a642fc31 100644 --- a/tests/components/recorder/test_history_db_schema_42.py +++ b/tests/components/recorder/test_history_db_schema_42.py @@ -42,7 +42,7 @@ from tests.typing import RecorderInstanceGenerator @pytest.fixture async def mock_recorder_before_hass( - async_setup_recorder_instance: RecorderInstanceGenerator, + async_test_recorder: RecorderInstanceGenerator, ) -> None: """Set up recorder.""" diff --git a/tests/components/recorder/test_init.py b/tests/components/recorder/test_init.py index 52a220662ae..48138bbc952 100644 --- a/tests/components/recorder/test_init.py +++ b/tests/components/recorder/test_init.py @@ -104,7 +104,7 @@ from tests.typing import RecorderInstanceGenerator @pytest.fixture async def mock_recorder_before_hass( - async_setup_recorder_instance: RecorderInstanceGenerator, + async_test_recorder: RecorderInstanceGenerator, ) -> None: """Set up recorder.""" diff --git a/tests/components/recorder/test_migrate.py b/tests/components/recorder/test_migrate.py index e75aa5588c4..f31d3530500 100644 --- a/tests/components/recorder/test_migrate.py +++ b/tests/components/recorder/test_migrate.py @@ -40,7 +40,7 @@ from tests.typing import RecorderInstanceGenerator @pytest.fixture async def mock_recorder_before_hass( - async_setup_recorder_instance: RecorderInstanceGenerator, + async_test_recorder: RecorderInstanceGenerator, ) -> None: """Set up recorder.""" diff --git a/tests/components/recorder/test_statistics.py b/tests/components/recorder/test_statistics.py index 7d8bc6e3415..cd8cd1a51df 100644 --- a/tests/components/recorder/test_statistics.py +++ b/tests/components/recorder/test_statistics.py @@ -51,7 +51,7 @@ from tests.typing import RecorderInstanceGenerator, WebSocketGenerator @pytest.fixture async def mock_recorder_before_hass( - async_setup_recorder_instance: RecorderInstanceGenerator, + async_test_recorder: RecorderInstanceGenerator, ) -> None: """Set up recorder.""" diff --git a/tests/components/recorder/test_util.py b/tests/components/recorder/test_util.py index d72978c57bb..089d1938227 100644 --- a/tests/components/recorder/test_util.py +++ b/tests/components/recorder/test_util.py @@ -48,7 +48,7 @@ from tests.typing import RecorderInstanceGenerator @pytest.fixture async def mock_recorder_before_hass( - async_setup_recorder_instance: RecorderInstanceGenerator, + async_test_recorder: RecorderInstanceGenerator, ) -> None: """Set up recorder.""" diff --git a/tests/components/sensor/test_recorder.py b/tests/components/sensor/test_recorder.py index 62cb66d2053..afa543ac12d 100644 --- a/tests/components/sensor/test_recorder.py +++ b/tests/components/sensor/test_recorder.py @@ -95,7 +95,7 @@ KW_SENSOR_ATTRIBUTES = { @pytest.fixture async def mock_recorder_before_hass( - async_setup_recorder_instance: RecorderInstanceGenerator, + async_test_recorder: RecorderInstanceGenerator, ) -> None: """Set up recorder patches.""" diff --git a/tests/conftest.py b/tests/conftest.py index 50258db8f3b..565e0e42534 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -4,7 +4,7 @@ from __future__ import annotations import asyncio from collections.abc import AsyncGenerator, Callable, Coroutine, Generator -from contextlib import asynccontextmanager, contextmanager +from contextlib import AsyncExitStack, asynccontextmanager, contextmanager import datetime import functools import gc @@ -1385,7 +1385,7 @@ async def _async_init_recorder_component( @pytest.fixture -async def async_setup_recorder_instance( +async def async_test_recorder( recorder_db_url: str, enable_nightly_purge: bool, enable_statistics: bool, @@ -1394,7 +1394,7 @@ async def async_setup_recorder_instance( enable_migrate_event_type_ids: bool, enable_migrate_entity_ids: bool, ) -> AsyncGenerator[RecorderInstanceGenerator]: - """Yield callable to setup recorder instance.""" + """Yield context manager to setup recorder instance.""" # pylint: disable-next=import-outside-toplevel from homeassistant.components import recorder @@ -1477,12 +1477,13 @@ async def async_setup_recorder_instance( ), ): - async def async_setup_recorder( + @asynccontextmanager + async def async_test_recorder( hass: HomeAssistant, config: ConfigType | None = None, *, wait_recorder: bool = True, - ) -> recorder.Recorder: + ) -> AsyncGenerator[recorder.Recorder]: """Setup and return recorder instance.""" # noqa: D401 await _async_init_recorder_component(hass, config, recorder_db_url) await hass.async_block_till_done() @@ -1490,7 +1491,34 @@ async def async_setup_recorder_instance( # The recorder's worker is not started until Home Assistant is running if hass.state is CoreState.running and wait_recorder: await async_recorder_block_till_done(hass) - return instance + try: + yield instance + finally: + if instance.is_alive(): + await instance._async_shutdown(None) + + yield async_test_recorder + + +@pytest.fixture +async def async_setup_recorder_instance( + async_test_recorder: RecorderInstanceGenerator, +) -> AsyncGenerator[RecorderInstanceGenerator]: + """Yield callable to setup recorder instance.""" + + async with AsyncExitStack() as stack: + + async def async_setup_recorder( + hass: HomeAssistant, + config: ConfigType | None = None, + *, + wait_recorder: bool = True, + ) -> AsyncGenerator[recorder.Recorder]: + """Set up and return recorder instance.""" + + return await stack.enter_async_context( + async_test_recorder(hass, config, wait_recorder=wait_recorder) + ) yield async_setup_recorder @@ -1498,11 +1526,12 @@ async def async_setup_recorder_instance( @pytest.fixture async def recorder_mock( recorder_config: dict[str, Any] | None, - async_setup_recorder_instance: RecorderInstanceGenerator, + async_test_recorder: RecorderInstanceGenerator, hass: HomeAssistant, -) -> recorder.Recorder: +) -> AsyncGenerator[recorder.Recorder]: """Fixture with in-memory recorder.""" - return await async_setup_recorder_instance(hass, recorder_config) + async with async_test_recorder(hass, recorder_config) as instance: + yield instance @pytest.fixture