-
-
Notifications
You must be signed in to change notification settings - Fork 32.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* 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>
- Loading branch information
1 parent
29ef925
commit d811fa0
Showing
4 changed files
with
382 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.