Pass device_id to intent handlers (#117442)

pull/117460/head
Michael Hansen 2024-05-14 13:59:49 -05:00 committed by GitHub
parent d88851a85a
commit 641754e0bb
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 38 additions and 0 deletions

View File

@ -358,6 +358,7 @@ class DefaultAgent(ConversationEntity):
user_input.context,
language,
assistant=DOMAIN,
device_id=user_input.device_id,
)
except intent.MatchFailedError as match_error:
# Intent was valid, but no entities matched the constraints.

View File

@ -97,6 +97,7 @@ async def async_handle(
context: Context | None = None,
language: str | None = None,
assistant: str | None = None,
device_id: str | None = None,
) -> IntentResponse:
"""Handle an intent."""
handler = hass.data.get(DATA_KEY, {}).get(intent_type)
@ -119,6 +120,7 @@ async def async_handle(
context=context,
language=language,
assistant=assistant,
device_id=device_id,
)
try:
@ -1116,6 +1118,7 @@ class Intent:
"language",
"category",
"assistant",
"device_id",
]
def __init__(
@ -1129,6 +1132,7 @@ class Intent:
language: str,
category: IntentCategory | None = None,
assistant: str | None = None,
device_id: str | None = None,
) -> None:
"""Initialize an intent."""
self.hass = hass
@ -1140,6 +1144,7 @@ class Intent:
self.language = language
self.category = category
self.assistant = assistant
self.device_id = device_id
@callback
def create_response(self) -> IntentResponse:

View File

@ -1090,3 +1090,35 @@ async def test_same_aliased_entities_in_different_areas(
hass, "how many lights are on?", None, Context(), None
)
assert result.response.response_type == intent.IntentResponseType.QUERY_ANSWER
async def test_device_id_in_handler(hass: HomeAssistant, init_components) -> None:
"""Test that the default agent passes device_id to intent handler."""
device_id = "test_device"
# Reuse custom sentences in test config to trigger default agent.
class OrderBeerIntentHandler(intent.IntentHandler):
intent_type = "OrderBeer"
def __init__(self) -> None:
super().__init__()
self.device_id: str | None = None
async def async_handle(
self, intent_obj: intent.Intent
) -> intent.IntentResponse:
self.device_id = intent_obj.device_id
return intent_obj.create_response()
handler = OrderBeerIntentHandler()
intent.async_register(hass, handler)
result = await conversation.async_converse(
hass,
"I'd like to order a stout please",
None,
Context(),
device_id=device_id,
)
assert result.response.response_type == intent.IntentResponseType.ACTION_DONE
assert handler.device_id == device_id