diff --git a/homeassistant/components/conversation/trigger.py b/homeassistant/components/conversation/trigger.py index 71ddb5c1237..30cc9a0d5d0 100644 --- a/homeassistant/components/conversation/trigger.py +++ b/homeassistant/components/conversation/trigger.py @@ -26,11 +26,23 @@ def has_no_punctuation(value: list[str]) -> list[str]: return value +def has_one_non_empty_item(value: list[str]) -> list[str]: + """Validate result has at least one item.""" + if len(value) < 1: + raise vol.Invalid("at least one sentence is required") + + for sentence in value: + if not sentence: + raise vol.Invalid(f"sentence too short: '{sentence}'") + + return value + + TRIGGER_SCHEMA = cv.TRIGGER_BASE_SCHEMA.extend( { vol.Required(CONF_PLATFORM): DOMAIN, vol.Required(CONF_COMMAND): vol.All( - cv.ensure_list, [cv.string], has_no_punctuation + cv.ensure_list, [cv.string], has_one_non_empty_item, has_no_punctuation ), } ) diff --git a/tests/components/conversation/test_trigger.py b/tests/components/conversation/test_trigger.py index 3f4dd9e3a7e..4fe9fed6bb2 100644 --- a/tests/components/conversation/test_trigger.py +++ b/tests/components/conversation/test_trigger.py @@ -194,6 +194,42 @@ async def test_fails_on_punctuation(hass: HomeAssistant, command: str) -> None: ) +@pytest.mark.parametrize( + "command", + [""], +) +async def test_fails_on_empty(hass: HomeAssistant, command: str) -> None: + """Test that validation fails when sentences are empty.""" + with pytest.raises(vol.Invalid): + await trigger.async_validate_trigger_config( + hass, + [ + { + "id": "trigger1", + "platform": "conversation", + "command": [ + command, + ], + }, + ], + ) + + +async def test_fails_on_no_sentences(hass: HomeAssistant) -> None: + """Test that validation fails when no sentences are provided.""" + with pytest.raises(vol.Invalid): + await trigger.async_validate_trigger_config( + hass, + [ + { + "id": "trigger1", + "platform": "conversation", + "command": [], + }, + ], + ) + + async def test_wildcards(hass: HomeAssistant, calls, setup_comp) -> None: """Test wildcards in trigger sentences.""" assert await async_setup_component(