Validate empty sentence triggers (#103579)

* Validate empty sentence triggers

* Add extra test for no sentences

* Remove extra line

---------

Co-authored-by: Michael Hansen <mike@rhasspy.org>
pull/103465/head^2
Tudor Sandu 2023-11-07 09:56:24 -08:00 committed by GitHub
parent 05deae09fc
commit d3ed8a6b8b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 49 additions and 1 deletions

View File

@ -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
),
}
)

View File

@ -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(