Make conversation and intent context aware (#28965)

* WIP

* LINT
This commit is contained in:
Paulus Schoutsen 2019-11-26 02:30:21 -08:00 committed by Pascal Vizeli
parent 6c6a5c50a5
commit c76f768a82
9 changed files with 82 additions and 57 deletions

View file

@ -2,7 +2,7 @@
# pylint: disable=protected-access
import pytest
from homeassistant.core import DOMAIN as HASS_DOMAIN
from homeassistant.core import DOMAIN as HASS_DOMAIN, Context
from homeassistant.setup import async_setup_component
from homeassistant.components import conversation
from homeassistant.components.cover import SERVICE_OPEN_COVER
@ -25,10 +25,13 @@ async def test_calling_intent(hass):
)
assert result
context = Context()
await hass.services.async_call(
"conversation",
"process",
{conversation.ATTR_TEXT: "I would like the Grolsch beer"},
context=context,
)
await hass.async_block_till_done()
@ -38,6 +41,7 @@ async def test_calling_intent(hass):
assert intent.intent_type == "OrderBeer"
assert intent.slots == {"type": {"value": "Grolsch"}}
assert intent.text_input == "I would like the Grolsch beer"
assert intent.context is context
async def test_register_before_setup(hass):
@ -80,7 +84,7 @@ async def test_register_before_setup(hass):
assert intent.text_input == "I would like the Grolsch beer"
async def test_http_processing_intent(hass, hass_client):
async def test_http_processing_intent(hass, hass_client, hass_admin_user):
"""Test processing intent via HTTP API."""
class TestIntentHandler(intent.IntentHandler):
@ -90,6 +94,7 @@ async def test_http_processing_intent(hass, hass_client):
async def async_handle(self, intent):
"""Handle the intent."""
assert intent.context.user_id == hass_admin_user.id
response = intent.create_response()
response.async_set_speech(
"I've ordered a {}!".format(intent.slots["type"]["value"])
@ -124,7 +129,7 @@ async def test_http_processing_intent(hass, hass_client):
}
async def test_http_handle_intent(hass, hass_client):
async def test_http_handle_intent(hass, hass_client, hass_admin_user):
"""Test handle intent via HTTP API."""
class TestIntentHandler(intent.IntentHandler):
@ -134,6 +139,7 @@ async def test_http_handle_intent(hass, hass_client):
async def async_handle(self, intent):
"""Handle the intent."""
assert intent.context.user_id == hass_admin_user.id
response = intent.create_response()
response.async_set_speech(
"I've ordered a {}!".format(intent.slots["type"]["value"])
@ -308,7 +314,7 @@ async def test_http_api_wrong_data(hass, hass_client):
assert resp.status == 400
async def test_custom_agent(hass, hass_client):
async def test_custom_agent(hass, hass_client, hass_admin_user):
"""Test a custom conversation agent."""
calls = []
@ -316,9 +322,9 @@ async def test_custom_agent(hass, hass_client):
class MyAgent(conversation.AbstractConversationAgent):
"""Test Agent."""
async def async_process(self, text, conversation_id):
async def async_process(self, text, context, conversation_id):
"""Process some text."""
calls.append((text, conversation_id))
calls.append((text, context, conversation_id))
response = intent.IntentResponse()
response.async_set_speech("Test response")
return response
@ -341,4 +347,5 @@ async def test_custom_agent(hass, hass_client):
assert len(calls) == 1
assert calls[0][0] == "Test Text"
assert calls[0][1] == "test-conv-id"
assert calls[0][1].user_id == hass_admin_user.id
assert calls[0][2] == "test-conv-id"