Skip to content

Commit

Permalink
Add new api to fetch sentence triggers (#132764)
Browse files Browse the repository at this point in the history
* Add new api to fetch sentence triggers

* With latest packages
  • Loading branch information
balloob authored Dec 10, 2024
1 parent cd39e4a commit 5062a7f
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 6 deletions.
12 changes: 6 additions & 6 deletions homeassistant/components/conversation/default_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ def __init__(
self._unexposed_names_trie: Trie | None = None

# Sentences that will trigger a callback (skipping intent recognition)
self._trigger_sentences: list[TriggerData] = []
self.trigger_sentences: list[TriggerData] = []
self._trigger_intents: Intents | None = None
self._unsub_clear_slot_list: list[Callable[[], None]] | None = None
self._load_intents_lock = asyncio.Lock()
Expand Down Expand Up @@ -1188,7 +1188,7 @@ def register_trigger(
) -> core.CALLBACK_TYPE:
"""Register a list of sentences that will trigger a callback when recognized."""
trigger_data = TriggerData(sentences=sentences, callback=callback)
self._trigger_sentences.append(trigger_data)
self.trigger_sentences.append(trigger_data)

# Force rebuild on next use
self._trigger_intents = None
Expand All @@ -1205,7 +1205,7 @@ def _rebuild_trigger_intents(self) -> None:
# This works because the intents are rebuilt on every
# register/unregister.
str(trigger_id): {"data": [{"sentences": trigger_data.sentences}]}
for trigger_id, trigger_data in enumerate(self._trigger_sentences)
for trigger_id, trigger_data in enumerate(self.trigger_sentences)
},
}

Expand All @@ -1228,7 +1228,7 @@ def _rebuild_trigger_intents(self) -> None:
@core.callback
def _unregister_trigger(self, trigger_data: TriggerData) -> None:
"""Unregister a set of trigger sentences."""
self._trigger_sentences.remove(trigger_data)
self.trigger_sentences.remove(trigger_data)

# Force rebuild on next use
self._trigger_intents = None
Expand All @@ -1241,7 +1241,7 @@ async def async_recognize_sentence_trigger(
Calls the registered callbacks if there's a match and returns a sentence
trigger result.
"""
if not self._trigger_sentences:
if not self.trigger_sentences:
# No triggers registered
return None

Expand Down Expand Up @@ -1286,7 +1286,7 @@ async def _handle_trigger_result(

# Gather callback responses in parallel
trigger_callbacks = [
self._trigger_sentences[trigger_id].callback(user_input, trigger_result)
self.trigger_sentences[trigger_id].callback(user_input, trigger_result)
for trigger_id, trigger_result in result.matched_triggers.items()
]

Expand Down
22 changes: 22 additions & 0 deletions homeassistant/components/conversation/http.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ def async_setup(hass: HomeAssistant) -> None:
websocket_api.async_register_command(hass, websocket_process)
websocket_api.async_register_command(hass, websocket_prepare)
websocket_api.async_register_command(hass, websocket_list_agents)
websocket_api.async_register_command(hass, websocket_list_sentences)
websocket_api.async_register_command(hass, websocket_hass_agent_debug)


Expand Down Expand Up @@ -150,6 +151,27 @@ async def websocket_list_agents(
connection.send_message(websocket_api.result_message(msg["id"], {"agents": agents}))


@websocket_api.websocket_command(
{
vol.Required("type"): "conversation/sentences/list",
}
)
@websocket_api.require_admin
@websocket_api.async_response
async def websocket_list_sentences(
hass: HomeAssistant, connection: websocket_api.ActiveConnection, msg: dict
) -> None:
"""List custom registered sentences."""
agent = hass.data.get(DATA_DEFAULT_ENTITY)
assert isinstance(agent, DefaultAgent)

sentences = []
for trigger_data in agent.trigger_sentences:
sentences.extend(trigger_data.sentences)

connection.send_result(msg["id"], {"trigger_sentences": sentences})


@websocket_api.websocket_command(
{
vol.Required("type"): "conversation/agent/homeassistant/debug",
Expand Down
8 changes: 8 additions & 0 deletions tests/components/conversation/snapshots/test_http.ambr
Original file line number Diff line number Diff line change
Expand Up @@ -693,6 +693,14 @@
})
# ---
# name: test_ws_hass_agent_debug_sentence_trigger
dict({
'trigger_sentences': list([
'hello',
'hello[ world]',
]),
})
# ---
# name: test_ws_hass_agent_debug_sentence_trigger.1
dict({
'results': list([
dict({
Expand Down
13 changes: 13 additions & 0 deletions tests/components/conversation/test_http.py
Original file line number Diff line number Diff line change
Expand Up @@ -501,6 +501,19 @@ async def test_ws_hass_agent_debug_sentence_trigger(

client = await hass_ws_client(hass)

# List sentence
await client.send_json_auto_id(
{
"type": "conversation/sentences/list",
}
)
await hass.async_block_till_done()

msg = await client.receive_json()

assert msg["success"]
assert msg["result"] == snapshot

# Use trigger sentence
await client.send_json_auto_id(
{
Expand Down

0 comments on commit 5062a7f

Please sign in to comment.