Sentence trigger (#94613)
* Add async_register_trigger_sentences for default agent * Add trigger response and trigger handler * Check callback in test * Clean up and move response to callback * Add trigger test * Drop TriggerAction * Test we pass sentence to callback * Match triggers once, allow multiple sentences * Don't use trigger id * Use async callback * No response for now * Use asyncio.gather for callback responses * Fix after rebase * Use a list for trigger sentences --------- Co-authored-by: Paulus Schoutsen <balloob@gmail.com>
This commit is contained in:
parent
29ef925d73
commit
d811fa0e74
4 changed files with 382 additions and 2 deletions
59
homeassistant/components/conversation/trigger.py
Normal file
59
homeassistant/components/conversation/trigger.py
Normal file
|
@ -0,0 +1,59 @@
|
|||
"""Offer sentence based automation rules."""
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import Any
|
||||
|
||||
import voluptuous as vol
|
||||
|
||||
from homeassistant.const import CONF_COMMAND, CONF_PLATFORM
|
||||
from homeassistant.core import CALLBACK_TYPE, HassJob, HomeAssistant, callback
|
||||
import homeassistant.helpers.config_validation as cv
|
||||
from homeassistant.helpers.trigger import TriggerActionType, TriggerInfo
|
||||
from homeassistant.helpers.typing import ConfigType
|
||||
|
||||
from . import HOME_ASSISTANT_AGENT, _get_agent_manager
|
||||
from .const import DOMAIN
|
||||
from .default_agent import DefaultAgent
|
||||
|
||||
TRIGGER_SCHEMA = cv.TRIGGER_BASE_SCHEMA.extend(
|
||||
{
|
||||
vol.Required(CONF_PLATFORM): DOMAIN,
|
||||
vol.Required(CONF_COMMAND): vol.All(cv.ensure_list, [cv.string]),
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
async def async_attach_trigger(
|
||||
hass: HomeAssistant,
|
||||
config: ConfigType,
|
||||
action: TriggerActionType,
|
||||
trigger_info: TriggerInfo,
|
||||
) -> CALLBACK_TYPE:
|
||||
"""Listen for events based on configuration."""
|
||||
trigger_data = trigger_info["trigger_data"]
|
||||
sentences = config.get(CONF_COMMAND, [])
|
||||
|
||||
job = HassJob(action)
|
||||
|
||||
@callback
|
||||
async def call_action(sentence: str) -> str | None:
|
||||
"""Call action with right context."""
|
||||
trigger_input: dict[str, Any] = { # Satisfy type checker
|
||||
**trigger_data,
|
||||
"platform": DOMAIN,
|
||||
"sentence": sentence,
|
||||
}
|
||||
|
||||
# Wait for the automation to complete
|
||||
if future := hass.async_run_hass_job(
|
||||
job,
|
||||
{"trigger": trigger_input},
|
||||
):
|
||||
await future
|
||||
|
||||
return None
|
||||
|
||||
default_agent = await _get_agent_manager(hass).async_get_agent(HOME_ASSISTANT_AGENT)
|
||||
assert isinstance(default_agent, DefaultAgent)
|
||||
|
||||
return default_agent.register_trigger(sentences, call_action)
|
Loading…
Add table
Add a link
Reference in a new issue