From 9cb260ad56c68edf607ce0565cc4adf0ab665b14 Mon Sep 17 00:00:00 2001 From: epenet <6771947+epenet@users.noreply.github.com> Date: Thu, 8 Oct 2026 10:14:46 +0200 Subject: [PATCH] Fix flaky automation test_shared_context (#184701) Co-authored-by: Claude Opus 5.5 --- tests/components/automation/test_init.py | 36 +++++++++++------------- 1 file changed, 17 insertions(+), 19 deletions(-) diff --git a/tests/components/automation/test_init.py b/tests/components/automation/test_init.py index b9cfd80af45d..5883a4bbd608 100644 --- a/tests/components/automation/test_init.py +++ b/tests/components/automation/test_init.py @@ -440,30 +440,28 @@ async def test_shared_context(hass: HomeAssistant, calls: list[ServiceCall]) -> ) context = Context() - first_automation_listener = Mock() - event_mock = Mock() - - hass.bus.async_listen("test_event2", first_automation_listener) - hass.bus.async_listen(EVENT_AUTOMATION_TRIGGERED, event_mock) + first_automation_events = async_capture_events(hass, "test_event2") + triggered_events = async_capture_events(hass, EVENT_AUTOMATION_TRIGGERED) hass.bus.async_fire("test_event", context=context) await hass.async_block_till_done() + # Event triggers schedule their action with call_soon, so wait for 'bye' as well + await hass.async_block_till_done() # Ensure events was fired - assert first_automation_listener.call_count == 1 - assert event_mock.call_count == 2 + assert len(first_automation_events) == 1 + assert len(triggered_events) == 2 # Verify automation triggered evenet for 'hello' automation - args, _ = event_mock.call_args_list[0] - first_trigger_context = args[0].context + event = triggered_events[0] + first_trigger_context = event.context assert first_trigger_context.parent_id == context.id # Ensure event data has all attributes set - assert args[0].data.get(ATTR_NAME) is not None - assert args[0].data.get(ATTR_ENTITY_ID) is not None - assert args[0].data.get(ATTR_SOURCE) is not None + assert event.data.get(ATTR_NAME) is not None + assert event.data.get(ATTR_ENTITY_ID) is not None + assert event.data.get(ATTR_SOURCE) is not None # Ensure context set correctly for event fired by 'hello' automation - args, _ = first_automation_listener.call_args - assert args[0].context is first_trigger_context + assert first_automation_events[0].context is first_trigger_context # Ensure the 'hello' automation state has the right context state = hass.states.get("automation.hello") @@ -471,13 +469,13 @@ async def test_shared_context(hass: HomeAssistant, calls: list[ServiceCall]) -> assert state.context is first_trigger_context # Verify automation triggered evenet for 'bye' automation - args, _ = event_mock.call_args_list[1] - second_trigger_context = args[0].context + event = triggered_events[1] + second_trigger_context = event.context assert second_trigger_context.parent_id == first_trigger_context.id # Ensure event data has all attributes set - assert args[0].data.get(ATTR_NAME) is not None - assert args[0].data.get(ATTR_ENTITY_ID) is not None - assert args[0].data.get(ATTR_SOURCE) is not None + assert event.data.get(ATTR_NAME) is not None + assert event.data.get(ATTR_ENTITY_ID) is not None + assert event.data.get(ATTR_SOURCE) is not None # Ensure the service call from the second automation # shares the same context