mirror of
https://github.com/home-assistant/core.git
synced 2025-07-30 16:57:19 +00:00
fix test_update_coordinator
This commit is contained in:
parent
cd03c9262f
commit
6e761f6184
@ -165,8 +165,6 @@ async def test_shutdown_on_entry_unload(
|
|||||||
) -> None:
|
) -> None:
|
||||||
"""Test shutdown is requested on entry unload."""
|
"""Test shutdown is requested on entry unload."""
|
||||||
entry = MockConfigEntry()
|
entry = MockConfigEntry()
|
||||||
config_entries.current_entry.set(entry)
|
|
||||||
|
|
||||||
calls = 0
|
calls = 0
|
||||||
|
|
||||||
async def _refresh() -> int:
|
async def _refresh() -> int:
|
||||||
@ -177,6 +175,7 @@ async def test_shutdown_on_entry_unload(
|
|||||||
crd = update_coordinator.DataUpdateCoordinator[int](
|
crd = update_coordinator.DataUpdateCoordinator[int](
|
||||||
hass,
|
hass,
|
||||||
_LOGGER,
|
_LOGGER,
|
||||||
|
config_entry=entry,
|
||||||
name="test",
|
name="test",
|
||||||
update_method=_refresh,
|
update_method=_refresh,
|
||||||
update_interval=DEFAULT_UPDATE_INTERVAL,
|
update_interval=DEFAULT_UPDATE_INTERVAL,
|
||||||
@ -206,6 +205,7 @@ async def test_shutdown_on_hass_stop(
|
|||||||
crd = update_coordinator.DataUpdateCoordinator[int](
|
crd = update_coordinator.DataUpdateCoordinator[int](
|
||||||
hass,
|
hass,
|
||||||
_LOGGER,
|
_LOGGER,
|
||||||
|
config_entry=None,
|
||||||
name="test",
|
name="test",
|
||||||
update_method=_refresh,
|
update_method=_refresh,
|
||||||
update_interval=DEFAULT_UPDATE_INTERVAL,
|
update_interval=DEFAULT_UPDATE_INTERVAL,
|
||||||
@ -844,6 +844,7 @@ async def test_timestamp_date_update_coordinator(hass: HomeAssistant) -> None:
|
|||||||
crd = update_coordinator.TimestampDataUpdateCoordinator[int](
|
crd = update_coordinator.TimestampDataUpdateCoordinator[int](
|
||||||
hass,
|
hass,
|
||||||
_LOGGER,
|
_LOGGER,
|
||||||
|
config_entry=None,
|
||||||
name="test",
|
name="test",
|
||||||
update_method=refresh,
|
update_method=refresh,
|
||||||
update_interval=timedelta(seconds=10),
|
update_interval=timedelta(seconds=10),
|
||||||
@ -870,10 +871,6 @@ async def test_config_entry(hass: HomeAssistant) -> None:
|
|||||||
"""Test behavior of coordinator.entry."""
|
"""Test behavior of coordinator.entry."""
|
||||||
entry = MockConfigEntry()
|
entry = MockConfigEntry()
|
||||||
|
|
||||||
# Default without context should be None
|
|
||||||
crd = update_coordinator.DataUpdateCoordinator[int](hass, _LOGGER, name="test")
|
|
||||||
assert crd.config_entry is None
|
|
||||||
|
|
||||||
# Explicit None is OK
|
# Explicit None is OK
|
||||||
crd = update_coordinator.DataUpdateCoordinator[int](
|
crd = update_coordinator.DataUpdateCoordinator[int](
|
||||||
hass, _LOGGER, name="test", config_entry=None
|
hass, _LOGGER, name="test", config_entry=None
|
||||||
@ -886,12 +883,28 @@ async def test_config_entry(hass: HomeAssistant) -> None:
|
|||||||
)
|
)
|
||||||
assert crd.config_entry is entry
|
assert crd.config_entry is entry
|
||||||
|
|
||||||
|
# Default without context should raise
|
||||||
|
with pytest.raises(
|
||||||
|
RuntimeError,
|
||||||
|
match="Detected code that rely on the ContextVar, "
|
||||||
|
"but should passing the config entry explicitly - "
|
||||||
|
"see https://developers.home-assistant.io/blog/xxxxxxx. "
|
||||||
|
"Please report this issue",
|
||||||
|
):
|
||||||
|
crd = update_coordinator.DataUpdateCoordinator[int](hass, _LOGGER, name="test")
|
||||||
|
|
||||||
# set ContextVar
|
# set ContextVar
|
||||||
config_entries.current_entry.set(entry)
|
config_entries.current_entry.set(entry)
|
||||||
|
|
||||||
# Default with ContextVar should match the ContextVar
|
# Default with ContextVar should raise
|
||||||
crd = update_coordinator.DataUpdateCoordinator[int](hass, _LOGGER, name="test")
|
with pytest.raises(
|
||||||
assert crd.config_entry is entry
|
RuntimeError,
|
||||||
|
match="Detected code that rely on the ContextVar, "
|
||||||
|
"but should passing the config entry explicitly - "
|
||||||
|
"see https://developers.home-assistant.io/blog/xxxxxxx. "
|
||||||
|
"Please report this issue",
|
||||||
|
):
|
||||||
|
crd = update_coordinator.DataUpdateCoordinator[int](hass, _LOGGER, name="test")
|
||||||
|
|
||||||
# Explicit entry different from ContextVar not recommended, but should work
|
# Explicit entry different from ContextVar not recommended, but should work
|
||||||
another_entry = MockConfigEntry()
|
another_entry = MockConfigEntry()
|
||||||
@ -921,7 +934,7 @@ async def test_listener_unsubscribe_releases_coordinator(hass: HomeAssistant) ->
|
|||||||
self._unsub = None
|
self._unsub = None
|
||||||
|
|
||||||
coordinator = update_coordinator.DataUpdateCoordinator[int](
|
coordinator = update_coordinator.DataUpdateCoordinator[int](
|
||||||
hass, _LOGGER, name="test"
|
hass, _LOGGER, config_entry=None, name="test"
|
||||||
)
|
)
|
||||||
subscriber = Subscriber()
|
subscriber = Subscriber()
|
||||||
subscriber.start_listen(coordinator)
|
subscriber.start_listen(coordinator)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user