From d483ad820ce6722444354c016e76ca5317a50540 Mon Sep 17 00:00:00 2001 From: epenet <6771947+epenet@users.noreply.github.com> Date: Wed, 12 Apr 2023 23:55:40 +0200 Subject: [PATCH] Make hass test fixture async (#91264) --- tests/conftest.py | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/tests/conftest.py b/tests/conftest.py index 49cd73f504a..96f219fd466 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -484,14 +484,24 @@ def hass_fixture_setup() -> list[bool]: @pytest.fixture -def hass( +def hass(_hass: HomeAssistant) -> HomeAssistant: + """Fixture to provide a test instance of Home Assistant.""" + # This wraps the async _hass fixture inside a sync fixture, to ensure + # the `hass` context variable is set in the execution context in which + # the test itself is executed + ha._cv_hass.set(_hass) + return _hass + + +@pytest.fixture +async def _hass( hass_fixture_setup: list[bool], event_loop: asyncio.AbstractEventLoop, load_registries: bool, hass_storage: dict[str, Any], request: pytest.FixtureRequest, -) -> Generator[HomeAssistant, None, None]: - """Fixture to provide a test instance of Home Assistant.""" +) -> AsyncGenerator[HomeAssistant, None]: + """Create a test instance of Home Assistant.""" loop = event_loop hass_fixture_setup.append(True) @@ -515,15 +525,14 @@ def hass( orig_exception_handler(loop, context) exceptions: list[Exception] = [] - hass = loop.run_until_complete(async_test_home_assistant(loop, load_registries)) - ha._cv_hass.set(hass) + hass = await async_test_home_assistant(loop, load_registries) orig_exception_handler = loop.get_exception_handler() loop.set_exception_handler(exc_handle) yield hass - loop.run_until_complete(hass.async_stop(force=True)) + await hass.async_stop(force=True) # Restore timezone, it is set when creating the hass object dt_util.DEFAULT_TIME_ZONE = orig_tz