diff --git a/homeassistant/components/ifttt/services.yaml b/homeassistant/components/ifttt/services.yaml index 550aecad56be..12d95f8223f5 100644 --- a/homeassistant/components/ifttt/services.yaml +++ b/homeassistant/components/ifttt/services.yaml @@ -20,6 +20,11 @@ trigger: example: "MY_HA_EVENT" selector: text: + target: + example: "my_key_name" + selector: + text: + multiple: true value1: example: "Hello World" selector: diff --git a/homeassistant/components/ifttt/strings.json b/homeassistant/components/ifttt/strings.json index c9fb89907570..82fd4fc8b860 100644 --- a/homeassistant/components/ifttt/strings.json +++ b/homeassistant/components/ifttt/strings.json @@ -45,6 +45,10 @@ "description": "The name of the event to send.", "name": "Event" }, + "target": { + "description": "The names of the configured API keys to send the event to. Sends the event to all keys if this field is omitted.", + "name": "Target" + }, "value1": { "description": "Generic field to send data via the event.", "name": "Value 1" diff --git a/tests/components/ifttt/test_init.py b/tests/components/ifttt/test_init.py index a5cf7382fe1f..2e24103124d8 100644 --- a/tests/components/ifttt/test_init.py +++ b/tests/components/ifttt/test_init.py @@ -1,6 +1,6 @@ """Test the init file of IFTTT.""" -from unittest.mock import patch +from unittest.mock import MagicMock, patch import pytest from pytest_homeassistant.common import async_setup_component @@ -82,3 +82,38 @@ async def test_trigger_service_raises_when_ifttt_unreachable( assert exc_info.value.translation_domain == DOMAIN assert exc_info.value.translation_key == "trigger_failed" + + +@pytest.mark.parametrize( + ("service_data", "expected_keys"), + [ + pytest.param({}, ["secret_a", "secret_b"], id="all_keys"), + pytest.param({"target": "a"}, ["secret_a"], id="single_target"), + pytest.param( + {"target": ["a", "b"]}, ["secret_a", "secret_b"], id="multiple_targets" + ), + pytest.param({"target": "unknown"}, [], id="unknown_target"), + ], +) +async def test_trigger_service_target( + hass: HomeAssistant, + service_data: dict[str, str | list[str]], + expected_keys: list[str], +) -> None: + """Test the trigger action sends the event to the targeted API keys.""" + await async_setup_component( + hass, DOMAIN, {DOMAIN: {CONF_KEY: {"a": "secret_a", "b": "secret_b"}}} + ) + + with patch( + "homeassistant.components.ifttt.services.pyfttt.send_event", + return_value=MagicMock(status_code=200), + ) as send_event: + await hass.services.async_call( + DOMAIN, + "trigger", + {"event": "test_event", **service_data}, + blocking=True, + ) + + assert [call.args[0] for call in send_event.call_args_list] == expected_keys