Skip to content

Commit

Permalink
Sync async_test_home_assistant with core (#3971)
Browse files Browse the repository at this point in the history
  • Loading branch information
ludeeus authored Aug 14, 2024
1 parent eac2352 commit f545e86
Showing 1 changed file with 21 additions and 9 deletions.
30 changes: 21 additions & 9 deletions tests/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

import asyncio
from collections.abc import AsyncGenerator, Iterable, Mapping, Sequence
from contextlib import asynccontextmanager, contextmanager
from contextlib import asynccontextmanager, contextmanager, suppress
from contextvars import ContextVar
import functools as ft
from inspect import currentframe
Expand Down Expand Up @@ -419,7 +419,7 @@ async def async_test_home_assistant_dev(
"""Return a Home Assistant object pointing at test config dir.
This should be copied from latest Home Assistant version,
currently Home Assistant Core 2024.6.0dev0 (2024-05-21).
currently Home Assistant Core 2024.9.0dev0 (2024-08-14).
https://github.com/home-assistant/core/blob/dev/tests/common.py
"""
hass = HomeAssistant(config_dir or get_test_config_dir())
Expand Down Expand Up @@ -486,7 +486,12 @@ def async_create_task_internal(coroutine, name=None, eager_start=True):

hass.config_entries = config_entries.ConfigEntries(
hass,
{"_": ("Not empty or else some bad checks for hass config in discovery.py breaks")},
{
"_": (
"Not empty or else some bad checks for hass config in discovery.py"
" breaks"
)
},
)
hass.bus.async_listen_once(
EVENT_HOMEASSISTANT_STOP,
Expand All @@ -498,10 +503,13 @@ def async_create_task_internal(coroutine, name=None, eager_start=True):
loader.async_setup(hass)

# setup translation cache instead of calling translation.async_setup(hass)
hass.data[translation.TRANSLATION_FLATTEN_CACHE] = translation._TranslationCache(hass)
hass.data[translation.TRANSLATION_FLATTEN_CACHE] = translation._TranslationCache(
hass
)
if load_registries:
with (
patch.object(StoreWithoutWriteLoad, "async_load", return_value=None),
patch.object(StoreWithoutWriteLoad,
"async_load", return_value=None),
patch(
"homeassistant.helpers.area_registry.AreaRegistryStore",
StoreWithoutWriteLoad,
Expand Down Expand Up @@ -550,10 +558,14 @@ def clear_instance(event):

hass.bus.async_listen_once(EVENT_HOMEASSISTANT_CLOSE, clear_instance)

yield hass

# Restore timezone, it is set when creating the hass object
dt_util.set_default_time_zone(orig_tz)
try:
yield hass
finally:
# Restore timezone, it is set when creating the hass object
dt_util.set_default_time_zone(orig_tz)
# Remove loop shutdown indicator to not interfere with additional hass objects
with suppress(AttributeError):
delattr(hass.loop, "_shutdown_run_callback_threadsafe")


@ha.callback
Expand Down

0 comments on commit f545e86

Please sign in to comment.