Ensure trigger sentences do not contain punctuation (#95633)

* Ensure trigger sentences do not contain punctuation

* Update homeassistant/components/conversation/trigger.py

Co-authored-by: Paulus Schoutsen <balloob@gmail.com>

---------

Co-authored-by: Paulus Schoutsen <balloob@gmail.com>
This commit is contained in:
Michael Hansen 2023-06-30 13:06:26 -05:00 committed by GitHub
parent 958359260e
commit 6b8ae0ec86
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 37 additions and 1 deletions

View File

@ -3,6 +3,7 @@ from __future__ import annotations
from typing import Any
from hassil.recognize import PUNCTUATION
import voluptuous as vol
from homeassistant.const import CONF_COMMAND, CONF_PLATFORM
@ -15,10 +16,22 @@ from . import HOME_ASSISTANT_AGENT, _get_agent_manager
from .const import DOMAIN
from .default_agent import DefaultAgent
def has_no_punctuation(value: list[str]) -> list[str]:
"""Validate result does not contain punctuation."""
for sentence in value:
if PUNCTUATION.search(sentence):
raise vol.Invalid("sentence should not contain punctuation")
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]),
vol.Required(CONF_COMMAND): vol.All(
cv.ensure_list, [cv.string], has_no_punctuation
),
}
)

View File

@ -1,7 +1,9 @@
"""Test conversation triggers."""
import pytest
import voluptuous as vol
from homeassistant.core import HomeAssistant
from homeassistant.helpers import trigger
from homeassistant.setup import async_setup_component
from tests.common import async_mock_service
@ -165,3 +167,24 @@ async def test_same_sentence_multiple_triggers(
("trigger1", "conversation", "hello"),
("trigger2", "conversation", "hello"),
}
@pytest.mark.parametrize(
"command",
["hello?", "hello!", "4 a.m."],
)
async def test_fails_on_punctuation(hass: HomeAssistant, command: str) -> None:
"""Test that validation fails when sentences contain punctuation."""
with pytest.raises(vol.Invalid):
await trigger.async_validate_trigger_config(
hass,
[
{
"id": "trigger1",
"platform": "conversation",
"command": [
command,
],
},
],
)