From 660ddf632e424fa82070b8a13196274218cfb734 Mon Sep 17 00:00:00 2001 From: Joseph Abbey Date: Mon, 15 Jul 2024 09:33:36 +0000 Subject: [PATCH] Pass through context (#27) --- custom_components/custom_sentences/intent.py | 35 ++++++++++++-------- 1 file changed, 22 insertions(+), 13 deletions(-) diff --git a/custom_components/custom_sentences/intent.py b/custom_components/custom_sentences/intent.py index 225e70d..4e4a273 100644 --- a/custom_components/custom_sentences/intent.py +++ b/custom_components/custom_sentences/intent.py @@ -8,8 +8,10 @@ import homeassistant.helpers.config_validation as cv import pytz import voluptuous as vol +from homeassistant.components.conversation.agent_manager import async_converse from homeassistant.core import HomeAssistant from homeassistant.helpers import intent +from homeassistant.helpers.intent import IntentResponseTarget, IntentResponseTargetType _LOGGER = logging.getLogger(__name__) @@ -63,26 +65,33 @@ async def async_handle(self, intent_obj: intent.Intent) -> intent.IntentResponse response.async_set_speech(f"I am {name}!") return response - # Call the conversation.process service and wait for the result - result = await intent_obj.hass.services.async_call( - "conversation", - "process", - {"text": text, "agent_id": matched_entity_id}, - blocking=True, - return_response=True, + result = await async_converse( + hass=intent_obj.hass, + text=text, + conversation_id=None, + context=intent_obj.context, + language=intent_obj.language, + agent_id=matched_entity_id, + device_id=intent_obj.device_id, ) # Extract the response text - response_text = ( - result.get("response", {}) - .get("speech", {}) - .get("plain", {}) - .get("speech", "") - ) + response_text = result.response.speech["plain"]["speech"] # Create and return a response response = intent_obj.create_response() response.async_set_speech(f"{name} says '{response_text}'") + + response.async_set_results( + [ + IntentResponseTarget( + type=IntentResponseTargetType.ENTITY, + name=name, + id=matched_entity_id, + ) + ] + ) + return response