Skip to content

Commit

Permalink
Pass device_id to intent handlers (#117442)
Browse files Browse the repository at this point in the history
  • Loading branch information
synesthesiam committed May 14, 2024
1 parent d88851a commit 641754e
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 0 deletions.
1 change: 1 addition & 0 deletions homeassistant/components/conversation/default_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,7 @@ async def async_process(self, user_input: ConversationInput) -> ConversationResu
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.
Expand Down
5 changes: 5 additions & 0 deletions homeassistant/helpers/intent.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -119,6 +120,7 @@ async def async_handle(
context=context,
language=language,
assistant=assistant,
device_id=device_id,
)

try:
Expand Down Expand Up @@ -1116,6 +1118,7 @@ class Intent:
"language",
"category",
"assistant",
"device_id",
]

def __init__(
Expand All @@ -1129,6 +1132,7 @@ def __init__(
language: str,
category: IntentCategory | None = None,
assistant: str | None = None,
device_id: str | None = None,
) -> None:
"""Initialize an intent."""
self.hass = hass
Expand All @@ -1140,6 +1144,7 @@ def __init__(
self.language = language
self.category = category
self.assistant = assistant
self.device_id = device_id

@callback
def create_response(self) -> IntentResponse:
Expand Down
32 changes: 32 additions & 0 deletions tests/components/conversation/test_default_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 641754e

Please sign in to comment.