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
parent
05deae09fc
commit
d3ed8a6b8b
|
@ -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
|
||||
),
|
||||
}
|
||||
)
|
||||
|
|
|
@ -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(
|
||||
|
|
Loading…
Reference in New Issue