mirror of
https://github.com/home-assistant/core.git
synced 2025-07-23 13:17:32 +00:00
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>
This commit is contained in:
parent
05deae09fc
commit
d3ed8a6b8b
@ -26,11 +26,23 @@ def has_no_punctuation(value: list[str]) -> list[str]:
|
|||||||
return value
|
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(
|
TRIGGER_SCHEMA = cv.TRIGGER_BASE_SCHEMA.extend(
|
||||||
{
|
{
|
||||||
vol.Required(CONF_PLATFORM): DOMAIN,
|
vol.Required(CONF_PLATFORM): DOMAIN,
|
||||||
vol.Required(CONF_COMMAND): vol.All(
|
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:
|
async def test_wildcards(hass: HomeAssistant, calls, setup_comp) -> None:
|
||||||
"""Test wildcards in trigger sentences."""
|
"""Test wildcards in trigger sentences."""
|
||||||
assert await async_setup_component(
|
assert await async_setup_component(
|
||||||
|
Loading…
x
Reference in New Issue
Block a user