Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ docs/_build
.vscode
.*.swp
.idea
*.egg-info
1 change: 1 addition & 0 deletions custom_components/pyscript/decorator_abc.py
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,7 @@ async def validate(self) -> None:
trig_decorators_reqd = {
"event_trigger",
"mqtt_trigger",
"sentence_trigger",
"state_trigger",
"time_trigger",
"webhook_trigger",
Expand Down
2 changes: 2 additions & 0 deletions custom_components/pyscript/decorators/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from .event import EventTriggerDecorator
from .mqtt import MQTTTriggerDecorator
from .sentence import SentenceTriggerDecorator
from .service import ServiceDecorator
from .state import StateActiveDecorator, StateTriggerDecorator
from .task import TaskUniqueDecorator
Expand All @@ -16,6 +17,7 @@
TaskUniqueDecorator,
EventTriggerDecorator,
MQTTTriggerDecorator,
SentenceTriggerDecorator,
WebhookTriggerDecorator,
WebhookHandlerDecorator,
ServiceDecorator,
Expand Down
152 changes: 152 additions & 0 deletions custom_components/pyscript/decorators/sentence.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
"""Sentence trigger decorator."""

from __future__ import annotations

from collections.abc import Callable
import logging
from typing import TYPE_CHECKING, Any

import voluptuous as vol

from homeassistant.core import CALLBACK_TYPE
from homeassistant.helpers import config_validation as cv

from ..decorator_abc import DispatchData, TriggerDecorator
from .base import AutoKwargsDecorator, RequestResponseDecorator

if TYPE_CHECKING:
from homeassistant.components.conversation import ConversationInput, RecognizeResult

_LOGGER = logging.getLogger(__name__)

Validator = Callable[[list[str]], list[str]]


def _import_validators() -> list[Validator] | None:
"""
Import the sentence validators supported by HA.

Delay imports to avoid breaking installs that haven't added the conversation component yet.
"""
try:
from homeassistant.components.conversation import trigger as conversation_trigger
except ImportError:
return None

validators = [
conversation_trigger.has_one_non_empty_item,
conversation_trigger.has_no_punctuation,
]

# is_valid_sentence is available in HA 2026.7 and later.
if is_valid_sentence := getattr(conversation_trigger, "is_valid_sentence", None):
validators.append(is_valid_sentence)

return validators


def _validate_sentences(sentences: list[str]) -> list[str]:
"""Run HA's sentence validators when the conversation component is available."""
if validators := _import_validators():
return vol.All(*validators)(sentences)
return sentences


def _flatten_sentence_args(args: list[Any]) -> list[Any]:
"""Flatten string and list positional arguments into one sentence list."""
return [sentence for arg in args for sentence in cv.ensure_list(arg)]


class SentenceTriggerDecorator(TriggerDecorator, AutoKwargsDecorator, RequestResponseDecorator):
"""
Implementation for @sentence_trigger.

Registers sentences with HA's conversation agent manager. The decorated
function's string return value becomes the spoken response.
"""

name = "sentence_trigger"
args_schema = vol.Schema(
vol.All(
_flatten_sentence_args,
[cv.string],
vol.Length(min=1, msg="at least one sentence is required"),
_validate_sentences,
)
)
kwargs_schema = vol.Schema(
{
vol.Optional("timeout", default=3.0): vol.All(
vol.Coerce(float), vol.Range(min=0, min_included=False)
),
}
)

sentences: list[str]
timeout: float
_unregister: CALLBACK_TYPE | None = None

async def validate(self):
"""Validate the sentence trigger configuration."""
await super().validate()
self.sentences = self.args

async def _trigger_callback(self, user_input: ConversationInput, result: RecognizeResult) -> str | None:
"""Handle a matched sentence from the conversation agent."""
details = {
entity_name: {
"name": entity_name,
"text": entity.text.strip() if isinstance(entity.text, str) else entity.text,
"value": (entity.value.strip() if isinstance(entity.value, str) else entity.value),
}
for entity_name, entity in result.entities.items()
}

func_args: dict[str, Any] = {
"trigger_type": "sentence",
"sentence": user_input.text,
"details": details,
"device_id": user_input.device_id,
"satellite_id": user_input.satellite_id,
}
func_args.update({n: d["value"] for n, d in details.items()})

try:
response = await self.handle_request(func_args, timeout=self.timeout)
except TimeoutError:
_LOGGER.warning(
"sentence_trigger %s timed out after %ss",
self.dm.name,
self.timeout,
)
return None

if response is None or isinstance(response, str):
return response
_LOGGER.warning(
"sentence_trigger %s returned unsupported type %s", self.dm.name, type(response).__name__
)
return None

async def dispatch_request(self, data: DispatchData) -> None:
"""Start the call through the trigger handlers."""
await self.dispatch(data)

async def start(self):
"""Register sentences with the conversation agent manager."""
await super().start()
from homeassistant.components.conversation.agent_manager import get_agent_manager

mgr = get_agent_manager(self.dm.hass)
self._unregister = mgr.register_trigger(
sentences=self.sentences,
trigger_callback=self._trigger_callback,
)
_LOGGER.debug("sentence_trigger %s registered sentences: %s", self.dm.name, self.sentences)

async def stop(self):
"""Unregister sentences from the conversation agent manager."""
await super().stop()
if self._unregister:
self._unregister()
self._unregister = None
31 changes: 31 additions & 0 deletions custom_components/pyscript/stubs/pyscript_builtins.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,37 @@ def webhook_handler(
...


def sentence_trigger(
*sentences: str | list[str],
timeout: float = 3.0,
kwargs: dict | None = None,
) -> Callable[..., Any]:
"""Trigger when a spoken sentence matches the given template(s).

Uses Home Assistant's conversation agent to match sentences with hassil
wildcard syntax (e.g. ``{slot_name}``). A string return value becomes the
spoken response. None gives the default "Done"; other types warn and use "Done".

Args:
sentences: One or more sentence templates or lists of templates using hassil ``{slot}`` wildcards.
timeout: Positive seconds to wait for the function to return (default 3.0; zero is invalid).
kwargs: Extra keyword arguments merged into each invocation.

Trigger kwargs:
- Each matched slot is passed as a named argument.
- ``trigger_type`` = ``"sentence"``
- ``sentence``: the raw spoken text
- ``details``: full hassil match info per slot (name, text, value)
- ``device_id``: HA device registry id of the voice hardware (or None)
- ``satellite_id``: entity_id of the assist_satellite entity (or None)

Unused arguments may be omitted or captured with **kwargs. Missing slots are
not passed; give optional parameters defaults, e.g. name=None, area=None.
Slots override standard arguments on collision; explicit kwargs override both.
"""
...


def pyscript_compile() -> Callable[..., Any]:
"""Compile the wrapped function into native (synchronous) Python.

Expand Down
82 changes: 82 additions & 0 deletions docs/reference.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1012,6 +1012,88 @@ For full control over the response, return an ``aiohttp.web.Response``:
def webhook_redirect():
return Response(status=302, headers={"Location": "https://example.com/"})

@sentence_trigger
^^^^^^^^^^^^^^^^^

.. code:: python

@sentence_trigger(*sentences, timeout=3.0, kwargs=None)

``@sentence_trigger`` registers one or more sentence templates with Home Assistant's conversation agent. When a spoken (or typed) sentence matches a template, the decorated function is called. If the function returns a string, it becomes the spoken response.

Sentence templates use `hassil <https://github.com/home-assistant/hassil>`__ wildcard syntax. Wrap slot names in curly braces to capture them: ``"turn on {name} in the {area}"``. The matched slot values are passed as named function arguments, such as ``name`` and ``area``. The ``details`` dictionary is also available for full match information.

Functions can omit arguments they do not need, or use ``**kwargs`` to receive all arguments. Only matched slots are passed: for optional slots or multiple triggers with different slots, give the corresponding function parameters defaults. Missing slots are not automatically set to ``None``.

Arguments:

``sentences``
One or more sentence template strings or lists of templates. Each template is registered independently with the conversation agent.

``timeout``
Positive number of seconds to wait for the function to return before giving up on a spoken response. Defaults to ``3.0``; zero is invalid. If the function does not finish in time, the spoken response will be the default "Done" (the function continues running in the background).

``kwargs``
Optional dict of additional keyword arguments merged into each call.

When the trigger fires, these keyword arguments are passed to the function:

.. code:: python

kwargs = {
"trigger_type": "sentence",
"sentence": "turn on lights in the kitchen",
"name": "lights",
"area": "kitchen",
"details": {
"name": {"name": "name", "text": "lights", "value": "lights"},
"area": {"name": "area", "text": "kitchen", "value": "kitchen"},
},
"device_id": "abc123...", # or None
"satellite_id": "assist_satellite.kitchen", # or None
}

- ``sentence`` is the raw spoken text.
- ``details`` contains the full hassil match information per slot (name, raw text, resolved value).
- ``device_id`` is the HA device registry ID of the physical voice hardware that captured the command. Use it for hardware-level identity.
- ``satellite_id`` is the ``entity_id`` of the ``assist_satellite`` entity representing the voice satellite. Use it to resolve area context (the satellite entity carries area assignment and pipeline config).

Matched slots take precedence over the standard arguments if their names collide. Explicit ``kwargs`` take precedence over both.

The function's return value becomes the spoken response:

- Return a ``str`` to have it spoken back.
- Return ``None`` (or omit a return statement) for default response of "Done".
- Other return types log a warning and use the default response of "Done".
- Exceptions and calls skipped or canceled by another decorator also use the default response.

Examples:

.. code:: python

@sentence_trigger("turn on {name} in the {area}")
def voice_on(sentence, name, area):
service.call("light", "turn_on", entity_id=f"light.{name}")
return f"Turned on {name} in the {area}"

@sentence_trigger("what time is it", "tell me the time")
def voice_time():
from datetime import datetime
return f"It is {datetime.now().strftime('%I:%M %p')}"

@sentence_trigger("set {name} to {level}")
def voice_set(name, level, device_id, satellite_id):
# Use satellite_id to determine which area the command came from
log.info(f"Command from satellite {satellite_id} (device {device_id})")
input_number.set_value(entity_id=f"input_number.{name}", value=int(level))

@sentence_trigger("hello {name} [in the {area}]")
@sentence_trigger("hello")
def voice_hello(name=None, area=None):
return f"Hello {name or 'there'}"

NOTE: The `Conversation Integration <https://www.home-assistant.io/integrations/conversation/>`__ must be set up for ``@sentence_trigger`` to work.

@state_active
^^^^^^^^^^^^^

Expand Down
Loading
Loading