From 7a8600708335dd83b51a36dd579c0cb922494fca Mon Sep 17 00:00:00 2001 From: abmantis Date: Mon, 6 Oct 2025 22:08:46 +0100 Subject: [PATCH] Add tests for different action type --- tests/helpers/test_trigger.py | 53 +++++++++++++++++++++++++---------- 1 file changed, 38 insertions(+), 15 deletions(-) diff --git a/tests/helpers/test_trigger.py b/tests/helpers/test_trigger.py index aff22bd49fa..d19dd957b1c 100644 --- a/tests/helpers/test_trigger.py +++ b/tests/helpers/test_trigger.py @@ -447,7 +447,31 @@ async def test_pluggable_action( assert not plug_2 -async def test_platform_multiple_triggers(hass: HomeAssistant) -> None: +class TriggerActionFunctionTypeHelper: + """Helper for testing different trigger action function types.""" + + def __init__(self) -> None: + """Init helper.""" + self.action_calls = [] + + @callback + def cb_action(self, *args): + """Callback action.""" + self.action_calls.append([*args]) + + def sync_action(self, *args): + """Sync action.""" + self.action_calls.append([*args]) + + async def async_action(self, *args): + """Async action.""" + self.action_calls.append([*args]) + + +@pytest.mark.parametrize("action_method", ["cb_action", "sync_action", "async_action"]) +async def test_platform_multiple_triggers( + hass: HomeAssistant, action_method: str +) -> None: """Test a trigger platform with multiple trigger.""" class MockTrigger(Trigger): @@ -499,15 +523,12 @@ async def test_platform_multiple_triggers(hass: HomeAssistant) -> None: log_cb = MagicMock() - action_calls = [] + action_helper = TriggerActionFunctionTypeHelper() + action_method = getattr(action_helper, action_method) - @callback - def cb_action(*args): - action_calls.append([*args]) - - await async_initialize_triggers(hass, config_1, cb_action, "test", "", log_cb) - assert len(action_calls) == 1 - assert action_calls[0][0] == { + await async_initialize_triggers(hass, config_1, action_method, "test", "", log_cb) + assert len(action_helper.action_calls) == 1 + assert action_helper.action_calls[0][0] == { "trigger": { "alias": None, "description": "trigger 1 desc", @@ -517,11 +538,11 @@ async def test_platform_multiple_triggers(hass: HomeAssistant) -> None: "platform": "test", } } - action_calls.clear() + action_helper.action_calls.clear() - await async_initialize_triggers(hass, config_2, cb_action, "test", "", log_cb) - assert len(action_calls) == 1 - assert action_calls[0][0] == { + await async_initialize_triggers(hass, config_2, action_method, "test", "", log_cb) + assert len(action_helper.action_calls) == 1 + assert action_helper.action_calls[0][0] == { "trigger": { "alias": None, "description": "trigger 2 desc", @@ -531,10 +552,12 @@ async def test_platform_multiple_triggers(hass: HomeAssistant) -> None: "platform": "test.trig_2", } } - action_calls.clear() + action_helper.action_calls.clear() with pytest.raises(KeyError): - await async_initialize_triggers(hass, config_3, cb_action, "test", "", log_cb) + await async_initialize_triggers( + hass, config_3, action_method, "test", "", log_cb + ) async def test_platform_migrate_trigger(hass: HomeAssistant) -> None: