mirror of
https://github.com/home-assistant/core.git
synced 2025-07-25 06:07:17 +00:00
Make hass test fixture async (#91264)
This commit is contained in:
parent
ff1fd86b91
commit
d483ad820c
@ -484,14 +484,24 @@ def hass_fixture_setup() -> list[bool]:
|
|||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
@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],
|
hass_fixture_setup: list[bool],
|
||||||
event_loop: asyncio.AbstractEventLoop,
|
event_loop: asyncio.AbstractEventLoop,
|
||||||
load_registries: bool,
|
load_registries: bool,
|
||||||
hass_storage: dict[str, Any],
|
hass_storage: dict[str, Any],
|
||||||
request: pytest.FixtureRequest,
|
request: pytest.FixtureRequest,
|
||||||
) -> Generator[HomeAssistant, None, None]:
|
) -> AsyncGenerator[HomeAssistant, None]:
|
||||||
"""Fixture to provide a test instance of Home Assistant."""
|
"""Create a test instance of Home Assistant."""
|
||||||
|
|
||||||
loop = event_loop
|
loop = event_loop
|
||||||
hass_fixture_setup.append(True)
|
hass_fixture_setup.append(True)
|
||||||
@ -515,15 +525,14 @@ def hass(
|
|||||||
orig_exception_handler(loop, context)
|
orig_exception_handler(loop, context)
|
||||||
|
|
||||||
exceptions: list[Exception] = []
|
exceptions: list[Exception] = []
|
||||||
hass = loop.run_until_complete(async_test_home_assistant(loop, load_registries))
|
hass = await async_test_home_assistant(loop, load_registries)
|
||||||
ha._cv_hass.set(hass)
|
|
||||||
|
|
||||||
orig_exception_handler = loop.get_exception_handler()
|
orig_exception_handler = loop.get_exception_handler()
|
||||||
loop.set_exception_handler(exc_handle)
|
loop.set_exception_handler(exc_handle)
|
||||||
|
|
||||||
yield hass
|
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
|
# Restore timezone, it is set when creating the hass object
|
||||||
dt_util.DEFAULT_TIME_ZONE = orig_tz
|
dt_util.DEFAULT_TIME_ZONE = orig_tz
|
||||||
|
Loading…
x
Reference in New Issue
Block a user