1 """Offer sentence based automation rules."""
3 from __future__
import annotations
7 from hassil.recognize
import RecognizeResult
8 from hassil.util
import PUNCTUATION_ALL
9 import voluptuous
as vol
18 from .const
import DATA_DEFAULT_ENTITY, DOMAIN
22 """Validate result does not contain punctuation."""
23 for sentence
in value:
24 if PUNCTUATION_ALL.search(sentence):
25 raise vol.Invalid(
"sentence should not contain punctuation")
31 """Validate result has at least one item."""
33 raise vol.Invalid(
"at least one sentence is required")
35 for sentence
in value:
37 raise vol.Invalid(f
"sentence too short: '{sentence}'")
42 TRIGGER_SCHEMA = cv.TRIGGER_BASE_SCHEMA.extend(
44 vol.Required(CONF_PLATFORM): DOMAIN,
45 vol.Required(CONF_COMMAND): vol.All(
46 cv.ensure_list, [cv.string], has_one_non_empty_item, has_no_punctuation
55 action: TriggerActionType,
56 trigger_info: TriggerInfo,
58 """Listen for events based on configuration."""
59 trigger_data = trigger_info[
"trigger_data"]
60 sentences = config.get(CONF_COMMAND, [])
64 async
def call_action(
65 sentence: str, result: RecognizeResult, device_id: str |
None
67 """Call action with right context."""
73 "text": entity.text.strip(),
76 if isinstance(entity.value, str)
80 for entity_name, entity
in result.entities.items()
83 trigger_input: dict[str, Any] = {
89 entity_name: entity[
"value"]
for entity_name, entity
in details.items()
91 "device_id": device_id,
95 if future := hass.async_run_hass_job(
97 {
"trigger": trigger_input},
99 automation_result = await future
101 automation_result, ScriptRunResult
102 )
and automation_result.conversation_response
not in (
None, UNDEFINED):
104 return automation_result.conversation_response
113 return hass.data[DATA_DEFAULT_ENTITY].register_trigger(sentences, call_action)
list[str] has_no_punctuation(list[str] value)
CALLBACK_TYPE async_attach_trigger(HomeAssistant hass, ConfigType config, TriggerActionType action, TriggerInfo trigger_info)
list[str] has_one_non_empty_item(list[str] value)