Compare commits

...

7 Commits

Author SHA1 Message Date
Michael cb266387aa Merge branch 'dev' into deprecate-ContextVar-for-using-config_entry-in-coordinator 2025-04-19 22:09:13 +02:00
Michael ca67726672 adjust wording 2025-02-17 12:37:30 +00:00
Michael 158f15cd57 adjust report_usage parameters 2025-02-17 12:32:30 +00:00
mib1185 1585f922cc fix test_config_entries.py 2025-02-09 21:57:50 +00:00
mib1185 6e761f6184 fix test_update_coordinator 2025-02-09 21:49:15 +00:00
mib1185 cd03c9262f improve message 2025-02-09 21:36:06 +00:00
mib1185 c7e17ffb36 deprecate usage of ContextVar for config_entry in coordinator 2025-02-09 20:38:53 +00:00
3 changed files with 39 additions and 12 deletions
+12 -2
View File
@@ -84,9 +84,19 @@ class DataUpdateCoordinator(BaseDataUpdateCoordinatorProtocol, Generic[_DataT]):
self.update_interval = update_interval
self._shutdown_requested = False
if config_entry is UNDEFINED:
# late import to avoid circular imports
from . import frame # pylint: disable=import-outside-toplevel
frame.report_usage(
"relies on ContextVar, but should pass the config entry explicitly - see "
"https://developers.home-assistant.io/blog/xxxxxxx",
core_behavior=frame.ReportBehavior.LOG,
core_integration_behavior=frame.ReportBehavior.LOG,
custom_integration_behavior=frame.ReportBehavior.IGNORE,
breaks_in_ha_version="2026.3",
)
self.config_entry = config_entries.current_entry.get()
# This should be deprecated once all core integrations are updated
# to pass in the config entry explicitly.
else:
self.config_entry = config_entry
self.always_update = always_update
+23 -10
View File
@@ -165,8 +165,6 @@ async def test_shutdown_on_entry_unload(
) -> None:
"""Test shutdown is requested on entry unload."""
entry = MockConfigEntry()
config_entries.current_entry.set(entry)
calls = 0
async def _refresh() -> int:
@@ -177,6 +175,7 @@ async def test_shutdown_on_entry_unload(
crd = update_coordinator.DataUpdateCoordinator[int](
hass,
_LOGGER,
config_entry=entry,
name="test",
update_method=_refresh,
update_interval=DEFAULT_UPDATE_INTERVAL,
@@ -206,6 +205,7 @@ async def test_shutdown_on_hass_stop(
crd = update_coordinator.DataUpdateCoordinator[int](
hass,
_LOGGER,
config_entry=None,
name="test",
update_method=_refresh,
update_interval=DEFAULT_UPDATE_INTERVAL,
@@ -843,6 +843,7 @@ async def test_timestamp_date_update_coordinator(hass: HomeAssistant) -> None:
crd = update_coordinator.TimestampDataUpdateCoordinator[int](
hass,
_LOGGER,
config_entry=None,
name="test",
update_method=refresh,
update_interval=timedelta(seconds=10),
@@ -869,10 +870,6 @@ async def test_config_entry(hass: HomeAssistant) -> None:
"""Test behavior of coordinator.entry."""
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
crd = update_coordinator.DataUpdateCoordinator[int](
hass, _LOGGER, name="test", config_entry=None
@@ -885,12 +882,28 @@ async def test_config_entry(hass: HomeAssistant) -> None:
)
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
config_entries.current_entry.set(entry)
# Default with ContextVar should match the ContextVar
crd = update_coordinator.DataUpdateCoordinator[int](hass, _LOGGER, name="test")
assert crd.config_entry is entry
# Default with ContextVar 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")
# Explicit entry different from ContextVar not recommended, but should work
another_entry = MockConfigEntry()
@@ -920,7 +933,7 @@ async def test_listener_unsubscribe_releases_coordinator(hass: HomeAssistant) ->
self._unsub = None
coordinator = update_coordinator.DataUpdateCoordinator[int](
hass, _LOGGER, name="test"
hass, _LOGGER, config_entry=None, name="test"
)
subscriber = Subscriber()
subscriber.start_listen(coordinator)
+4
View File
@@ -4937,6 +4937,7 @@ async def test_setup_raise_entry_error_from_first_coordinator_update(
hass,
logging.getLogger(__name__),
name="any",
config_entry=entry,
update_method=_async_update_data,
update_interval=timedelta(seconds=1000),
)
@@ -4977,6 +4978,7 @@ async def test_setup_not_raise_entry_error_from_future_coordinator_update(
hass,
logging.getLogger(__name__),
name="any",
config_entry=entry,
update_method=_async_update_data,
update_interval=timedelta(seconds=1000),
)
@@ -5056,6 +5058,7 @@ async def test_setup_raise_auth_failed_from_first_coordinator_update(
hass,
logging.getLogger(__name__),
name="any",
config_entry=entry,
update_method=_async_update_data,
update_interval=timedelta(seconds=1000),
)
@@ -5108,6 +5111,7 @@ async def test_setup_raise_auth_failed_from_future_coordinator_update(
hass,
logging.getLogger(__name__),
name="any",
config_entry=entry,
update_method=_async_update_data,
update_interval=timedelta(seconds=1000),
)