Skip to content

Commit b14a739

Browse files
committed
added process_conversation and reload_conversation
1 parent 5d3d99d commit b14a739

File tree

1 file changed

+121
-3
lines changed

1 file changed

+121
-3
lines changed

appdaemon/plugins/hass/hassapi.py

Lines changed: 121 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -465,9 +465,9 @@ async def call_service(
465465
self,
466466
service: str,
467467
namespace: str | None = None,
468-
timeout: str | int | float | None = None,
468+
timeout: str | int | float | None = None, # used by the sync_decorator
469469
callback: Callable[[Any], Any] | None = None,
470-
**kwargs
470+
**kwargs,
471471
) -> Any:
472472
"""Calls a Service within AppDaemon.
473473
@@ -552,7 +552,8 @@ async def call_service(
552552
"""
553553
# We just wrap the ADAPI.call_service method here to add some additional arguments and docstrings
554554
kwargs = utils.remove_literals(kwargs, (None,))
555-
return await super().call_service(service, namespace, timeout=timeout, callback=callback, **kwargs)
555+
# We intentionally don't pass the timeout kwarg here because it's applied by the sync_decorator
556+
return await super().call_service(service, namespace, callback=callback, **kwargs)
556557

557558
def get_service_info(self, service: str) -> dict | None:
558559
"""Get some information about what kind of data the service expects to receive, which is helpful for debugging.
@@ -1668,3 +1669,120 @@ def label_entities(self, label_name_or_id: str) -> list[str]:
16681669
information.
16691670
"""
16701671
return self._template_command('label_entities', label_name_or_id)
1672+
1673+
# Conversation
1674+
# https://developers.home-assistant.io/docs/intent_conversation_api
1675+
1676+
def process_conversation(
1677+
self,
1678+
text: str,
1679+
language: str | None = None,
1680+
agent_id: str | None = None,
1681+
conversation_id: str | None = None,
1682+
*,
1683+
namespace: str | None = None,
1684+
timeout: str | int | float | None = None,
1685+
hass_timeout: str | int | float | None = None,
1686+
callback: ServiceCallback | None = None,
1687+
return_response: bool = True,
1688+
) -> dict[str, Any]:
1689+
"""Send a message to a conversation agent for processing.
1690+
1691+
This action is able to return
1692+
`response data <https://www.home-assistant.io/docs/scripts/perform-actions/#use-templates-to-handle-response-data>`_.
1693+
The response is the same response as for the `/api/conversation/process API <https://developers.home-assistant.io/docs/intent_conversation_api#conversation-response>`_.
1694+
1695+
See the docs on the `conversation integration <https://www.home-assistant.io/integrations/conversation/>`__ for
1696+
more information.
1697+
1698+
Args:
1699+
text (str): Transcribed text input to send to the conversation agent.
1700+
language (str, optional): Language of the text. Defaults to None.
1701+
agent_id (str, optional): ID of conversation agent. The conversation agent is the brains of the assistant.
1702+
It processes the incoming text commands. Defaults to None.
1703+
conversation_id (str, optional): ID of a new or previous conversation. Will continue an old conversation
1704+
or start a new one. Defaults to None.
1705+
namespace (str, optional): If provided, changes the namespace for the service call. Defaults to the current
1706+
namespace of the app, so it's safe to ignore this parameter most of the time. See the section on
1707+
`namespaces <APPGUIDE.html#namespaces>`__ for a detailed description.
1708+
timeout (str | int | float, optional): Timeout for the app thread to wait for a response from the main
1709+
thread.
1710+
hass_timeout (str | int | float, optional): Timeout for AppDaemon waiting on a response from Home Assistant
1711+
to respond to the backup request. Cannot be set lower than the timeout value.
1712+
callback (ServiceCallback, optional): Function to call with the results of the request.
1713+
return_response (bool, optional): Whether Home Assistant should return a response to the service call. Even
1714+
if it's False, Home Assistant will still respond with an acknowledgement. Defaults to True
1715+
1716+
Returns:
1717+
dict: The response from the conversation agent. See the docs on
1718+
`conversation response <https://developers.home-assistant.io/docs/intent_conversation_api/#conversation-response>`_
1719+
for more information.
1720+
1721+
Examples:
1722+
Extracting the text of the speech response, continuation flag, and conversation ID:
1723+
1724+
>>> full_response = self.process_conversation("Hello world!")
1725+
>>> match full_response:
1726+
... case {'success': True, 'result': dict(result)}:
1727+
... match result['response']:
1728+
... case {
1729+
... 'response': dict(response),
1730+
... 'continue_conversation': bool(continue_conv),
1731+
... 'conversation_id': str(conv_id),
1732+
... }:
1733+
... speech: str = response['speech']['plain']['speech']
1734+
... self.log(speech, ascii_encode=False)
1735+
... self.log(continue_conv)
1736+
... self.log(conv_id)
1737+
1738+
Extracting entity IDs from a successful action response:
1739+
1740+
>>> full_response = self.process_conversation("Turn on the living room lights")
1741+
>>> match full_response:
1742+
... case {'success': True, 'result': dict(result)}:
1743+
... match result['response']:
1744+
... case {'response': {'data': {'success': list(entities)}}}:
1745+
... eids = [e['id'] for e in entities]
1746+
... self.log(eids)
1747+
"""
1748+
return self.call_service(
1749+
service='conversation/process',
1750+
text=text,
1751+
language=language,
1752+
agent_id=agent_id,
1753+
conversation_id=conversation_id,
1754+
namespace=namespace if namespace is not None else self.namespace,
1755+
timeout=timeout,
1756+
callback=callback,
1757+
hass_timeout=hass_timeout,
1758+
return_response=return_response,
1759+
)
1760+
1761+
def reload_conversation(
1762+
self,
1763+
language: str | None = None,
1764+
agent_id: str | None = None,
1765+
*,
1766+
namespace: str | None = None,
1767+
) -> dict[str, Any]:
1768+
"""Reload the intent cache for a conversation agent.
1769+
1770+
See the docs on the `conversation integration <https://www.home-assistant.io/integrations/conversation/>`__ for
1771+
more information.
1772+
1773+
Args:
1774+
language (str, optional): Language to clear intent cache for. No value clears all languages. Defaults to None.
1775+
agent_id (str, optional): ID of conversation agent. Defaults to the built-in Home Assistant agent.
1776+
namespace (str, optional): If provided, changes the namespace for the service call. Defaults to the current
1777+
namespace of the app, so it's safe to ignore this parameter most of the time. See the section on
1778+
`namespaces <APPGUIDE.html#namespaces>`__ for a detailed description.
1779+
1780+
Returns:
1781+
dict: The acknowledgement response from Home Assistant.
1782+
"""
1783+
return self.call_service(
1784+
service='conversation/reload',
1785+
language=language,
1786+
agent_id=agent_id,
1787+
namespace=namespace if namespace is not None else self.namespace,
1788+
)

0 commit comments

Comments
 (0)