Fix update coordinator ContextVar log for custom integrations (#150100)

This commit is contained in:
Martin Hjelmare
2025-08-06 14:14:42 +02:00
committed by GitHub
parent e9444a2e4d
commit f26e6ad211
2 changed files with 39 additions and 17 deletions

View File

@@ -92,7 +92,7 @@ class DataUpdateCoordinator(BaseDataUpdateCoordinatorProtocol, Generic[_DataT]):
frame.report_usage( frame.report_usage(
"relies on ContextVar, but should pass the config entry explicitly.", "relies on ContextVar, but should pass the config entry explicitly.",
core_behavior=frame.ReportBehavior.ERROR, core_behavior=frame.ReportBehavior.ERROR,
custom_integration_behavior=frame.ReportBehavior.LOG, custom_integration_behavior=frame.ReportBehavior.IGNORE,
breaks_in_ha_version="2026.8", breaks_in_ha_version="2026.8",
) )

View File

@@ -942,17 +942,24 @@ async def test_config_entry_custom_integration(
# Default without context should be None # Default without context should be None
crd = update_coordinator.DataUpdateCoordinator[int](hass, _LOGGER, name="test") crd = update_coordinator.DataUpdateCoordinator[int](hass, _LOGGER, name="test")
assert crd.config_entry is None assert crd.config_entry is None
assert ( # Should not log any warnings about ContextVar usage for custom integrations
"Detected that integration 'my_integration' relies on ContextVar" frame_records = [
not in caplog.text record
) for record in caplog.records
if record.name == "homeassistant.helpers.frame"
and record.levelno >= logging.WARNING
]
assert len(frame_records) == 0
# Explicit None is OK # Explicit None is OK
caplog.clear() caplog.clear()
crd = update_coordinator.DataUpdateCoordinator[int]( crd = update_coordinator.DataUpdateCoordinator[int](
hass, _LOGGER, name="test", config_entry=None hass, _LOGGER, name="test", config_entry=None
) )
assert crd.config_entry is None assert crd.config_entry is None
assert ( assert (
"Detected that integration 'my_integration' relies on ContextVar" "Detected that integration 'my_integration' relies on ContextVar"
@@ -961,38 +968,53 @@ async def test_config_entry_custom_integration(
# Explicit entry is OK # Explicit entry is OK
caplog.clear() caplog.clear()
crd = update_coordinator.DataUpdateCoordinator[int]( crd = update_coordinator.DataUpdateCoordinator[int](
hass, _LOGGER, name="test", config_entry=entry hass, _LOGGER, name="test", config_entry=entry
) )
assert crd.config_entry is entry assert crd.config_entry is entry
assert ( frame_records = [
"Detected that integration 'my_integration' relies on ContextVar" record
not in caplog.text for record in caplog.records
) if record.name == "homeassistant.helpers.frame"
and record.levelno >= logging.WARNING
]
assert len(frame_records) == 0
# 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 match the ContextVar
caplog.clear() caplog.clear()
crd = update_coordinator.DataUpdateCoordinator[int](hass, _LOGGER, name="test") crd = update_coordinator.DataUpdateCoordinator[int](hass, _LOGGER, name="test")
assert crd.config_entry is entry assert crd.config_entry is entry
assert ( frame_records = [
"Detected that integration 'my_integration' relies on ContextVar" record
not in caplog.text for record in caplog.records
) if record.name == "homeassistant.helpers.frame"
and record.levelno >= logging.WARNING
]
assert len(frame_records) == 0
# 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()
caplog.clear() caplog.clear()
crd = update_coordinator.DataUpdateCoordinator[int]( crd = update_coordinator.DataUpdateCoordinator[int](
hass, _LOGGER, name="test", config_entry=another_entry hass, _LOGGER, name="test", config_entry=another_entry
) )
assert crd.config_entry is another_entry assert crd.config_entry is another_entry
assert ( frame_records = [
"Detected that integration 'my_integration' relies on ContextVar" record
not in caplog.text for record in caplog.records
) if record.name == "homeassistant.helpers.frame"
and record.levelno >= logging.WARNING
]
assert len(frame_records) == 0
async def test_listener_unsubscribe_releases_coordinator(hass: HomeAssistant) -> None: async def test_listener_unsubscribe_releases_coordinator(hass: HomeAssistant) -> None: