Skip to content

Commit d18f627

Browse files
MartinHjelmarefrenck
authored andcommitted
Fix update coordinator ContextVar log for custom integrations (home-assistant#150100)
1 parent 94bade0 commit d18f627

File tree

2 files changed

+39
-17
lines changed

2 files changed

+39
-17
lines changed

homeassistant/helpers/update_coordinator.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ def __init__(
9292
frame.report_usage(
9393
"relies on ContextVar, but should pass the config entry explicitly.",
9494
core_behavior=frame.ReportBehavior.ERROR,
95-
custom_integration_behavior=frame.ReportBehavior.LOG,
95+
custom_integration_behavior=frame.ReportBehavior.IGNORE,
9696
breaks_in_ha_version="2026.8",
9797
)
9898

tests/helpers/test_update_coordinator.py

Lines changed: 38 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -942,17 +942,24 @@ async def test_config_entry_custom_integration(
942942

943943
# Default without context should be None
944944
crd = update_coordinator.DataUpdateCoordinator[int](hass, _LOGGER, name="test")
945+
945946
assert crd.config_entry is None
946-
assert (
947-
"Detected that integration 'my_integration' relies on ContextVar"
948-
not in caplog.text
949-
)
947+
# Should not log any warnings about ContextVar usage for custom integrations
948+
frame_records = [
949+
record
950+
for record in caplog.records
951+
if record.name == "homeassistant.helpers.frame"
952+
and record.levelno >= logging.WARNING
953+
]
954+
assert len(frame_records) == 0
950955

951956
# Explicit None is OK
952957
caplog.clear()
958+
953959
crd = update_coordinator.DataUpdateCoordinator[int](
954960
hass, _LOGGER, name="test", config_entry=None
955961
)
962+
956963
assert crd.config_entry is None
957964
assert (
958965
"Detected that integration 'my_integration' relies on ContextVar"
@@ -961,38 +968,53 @@ async def test_config_entry_custom_integration(
961968

962969
# Explicit entry is OK
963970
caplog.clear()
971+
964972
crd = update_coordinator.DataUpdateCoordinator[int](
965973
hass, _LOGGER, name="test", config_entry=entry
966974
)
975+
967976
assert crd.config_entry is entry
968-
assert (
969-
"Detected that integration 'my_integration' relies on ContextVar"
970-
not in caplog.text
971-
)
977+
frame_records = [
978+
record
979+
for record in caplog.records
980+
if record.name == "homeassistant.helpers.frame"
981+
and record.levelno >= logging.WARNING
982+
]
983+
assert len(frame_records) == 0
972984

973985
# set ContextVar
974986
config_entries.current_entry.set(entry)
975987

976988
# Default with ContextVar should match the ContextVar
977989
caplog.clear()
990+
978991
crd = update_coordinator.DataUpdateCoordinator[int](hass, _LOGGER, name="test")
992+
979993
assert crd.config_entry is entry
980-
assert (
981-
"Detected that integration 'my_integration' relies on ContextVar"
982-
not in caplog.text
983-
)
994+
frame_records = [
995+
record
996+
for record in caplog.records
997+
if record.name == "homeassistant.helpers.frame"
998+
and record.levelno >= logging.WARNING
999+
]
1000+
assert len(frame_records) == 0
9841001

9851002
# Explicit entry different from ContextVar not recommended, but should work
9861003
another_entry = MockConfigEntry()
9871004
caplog.clear()
1005+
9881006
crd = update_coordinator.DataUpdateCoordinator[int](
9891007
hass, _LOGGER, name="test", config_entry=another_entry
9901008
)
1009+
9911010
assert crd.config_entry is another_entry
992-
assert (
993-
"Detected that integration 'my_integration' relies on ContextVar"
994-
not in caplog.text
995-
)
1011+
frame_records = [
1012+
record
1013+
for record in caplog.records
1014+
if record.name == "homeassistant.helpers.frame"
1015+
and record.levelno >= logging.WARNING
1016+
]
1017+
assert len(frame_records) == 0
9961018

9971019

9981020
async def test_listener_unsubscribe_releases_coordinator(hass: HomeAssistant) -> None:

0 commit comments

Comments
 (0)