Skip to content

Commit

Permalink
Pass through context (#27)
Browse files Browse the repository at this point in the history
  • Loading branch information
JosephAbbey authored Jul 15, 2024
1 parent 9704ac2 commit 660ddf6
Showing 1 changed file with 22 additions and 13 deletions.
35 changes: 22 additions & 13 deletions custom_components/custom_sentences/intent.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__)

Expand Down Expand Up @@ -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


Expand Down

0 comments on commit 660ddf6

Please sign in to comment.