Make hass test fixture async (#91264)

This commit is contained in:
epenet 2023-04-12 23:55:40 +02:00 committed by GitHub
parent ff1fd86b91
commit d483ad820c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -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