Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make hass test fixture async #91264

Merged
merged 4 commits into from
Apr 12, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 15 additions & 6 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -515,15 +525,14 @@ def exc_handle(loop, context):
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
Expand Down