2016-03-09 09:25:50 +00:00
|
|
|
"""The tests for the Conversation component."""
|
2021-10-22 17:43:40 +00:00
|
|
|
from http import HTTPStatus
|
2022-12-09 01:30:08 +00:00
|
|
|
from unittest.mock import patch
|
2021-10-22 17:43:40 +00:00
|
|
|
|
2017-11-21 04:26:36 +00:00
|
|
|
import pytest
|
|
|
|
|
2015-09-01 07:18:26 +00:00
|
|
|
from homeassistant.components import conversation
|
2019-12-09 17:56:21 +00:00
|
|
|
from homeassistant.core import DOMAIN as HASS_DOMAIN, Context
|
2017-07-22 04:38:53 +00:00
|
|
|
from homeassistant.helpers import intent
|
2019-12-09 17:56:21 +00:00
|
|
|
from homeassistant.setup import async_setup_component
|
2015-08-30 08:24:24 +00:00
|
|
|
|
2017-11-21 04:26:36 +00:00
|
|
|
from tests.common import async_mock_intent, async_mock_service
|
2017-06-13 06:34:20 +00:00
|
|
|
|
|
|
|
|
2022-12-09 01:30:08 +00:00
|
|
|
@pytest.fixture
|
|
|
|
async def init_components(hass):
|
|
|
|
"""Initialize relevant components with empty configs."""
|
|
|
|
assert await async_setup_component(hass, "homeassistant", {})
|
|
|
|
assert await async_setup_component(hass, "conversation", {})
|
|
|
|
assert await async_setup_component(hass, "intent", {})
|
|
|
|
|
|
|
|
|
2018-03-31 00:22:48 +00:00
|
|
|
async def test_calling_intent(hass):
|
2017-07-22 04:38:53 +00:00
|
|
|
"""Test calling an intent from a conversation."""
|
2019-07-31 19:25:30 +00:00
|
|
|
intents = async_mock_intent(hass, "OrderBeer")
|
2017-06-13 06:34:20 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
result = await async_setup_component(hass, "homeassistant", {})
|
2018-02-11 17:33:19 +00:00
|
|
|
assert result
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
result = await async_setup_component(
|
|
|
|
hass,
|
|
|
|
"conversation",
|
|
|
|
{"conversation": {"intents": {"OrderBeer": ["I would like the {type} beer"]}}},
|
|
|
|
)
|
2017-07-22 04:38:53 +00:00
|
|
|
assert result
|
|
|
|
|
2019-11-26 10:30:21 +00:00
|
|
|
context = Context()
|
|
|
|
|
2018-03-31 00:22:48 +00:00
|
|
|
await hass.services.async_call(
|
2019-07-31 19:25:30 +00:00
|
|
|
"conversation",
|
|
|
|
"process",
|
|
|
|
{conversation.ATTR_TEXT: "I would like the Grolsch beer"},
|
2019-11-26 10:30:21 +00:00
|
|
|
context=context,
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2018-03-31 00:22:48 +00:00
|
|
|
await hass.async_block_till_done()
|
2017-07-22 04:38:53 +00:00
|
|
|
|
|
|
|
assert len(intents) == 1
|
|
|
|
intent = intents[0]
|
2019-07-31 19:25:30 +00:00
|
|
|
assert intent.platform == "conversation"
|
|
|
|
assert intent.intent_type == "OrderBeer"
|
|
|
|
assert intent.slots == {"type": {"value": "Grolsch"}}
|
|
|
|
assert intent.text_input == "I would like the Grolsch beer"
|
2019-11-26 10:30:21 +00:00
|
|
|
assert intent.context is context
|
2017-07-22 04:38:53 +00:00
|
|
|
|
|
|
|
|
2018-03-31 00:22:48 +00:00
|
|
|
async def test_register_before_setup(hass):
|
2017-07-22 04:38:53 +00:00
|
|
|
"""Test calling an intent from a conversation."""
|
2019-07-31 19:25:30 +00:00
|
|
|
intents = async_mock_intent(hass, "OrderBeer")
|
|
|
|
|
|
|
|
hass.components.conversation.async_register("OrderBeer", ["A {type} beer, please"])
|
|
|
|
|
|
|
|
result = await async_setup_component(
|
|
|
|
hass,
|
|
|
|
"conversation",
|
|
|
|
{"conversation": {"intents": {"OrderBeer": ["I would like the {type} beer"]}}},
|
|
|
|
)
|
2017-07-22 04:38:53 +00:00
|
|
|
assert result
|
|
|
|
|
2018-03-31 00:22:48 +00:00
|
|
|
await hass.services.async_call(
|
2019-07-31 19:25:30 +00:00
|
|
|
"conversation", "process", {conversation.ATTR_TEXT: "A Grolsch beer, please"}
|
|
|
|
)
|
2018-03-31 00:22:48 +00:00
|
|
|
await hass.async_block_till_done()
|
2017-07-22 04:38:53 +00:00
|
|
|
|
|
|
|
assert len(intents) == 1
|
|
|
|
intent = intents[0]
|
2019-07-31 19:25:30 +00:00
|
|
|
assert intent.platform == "conversation"
|
|
|
|
assert intent.intent_type == "OrderBeer"
|
|
|
|
assert intent.slots == {"type": {"value": "Grolsch"}}
|
|
|
|
assert intent.text_input == "A Grolsch beer, please"
|
2017-07-22 04:38:53 +00:00
|
|
|
|
2018-03-31 00:22:48 +00:00
|
|
|
await hass.services.async_call(
|
2019-07-31 19:25:30 +00:00
|
|
|
"conversation",
|
|
|
|
"process",
|
|
|
|
{conversation.ATTR_TEXT: "I would like the Grolsch beer"},
|
|
|
|
)
|
2018-03-31 00:22:48 +00:00
|
|
|
await hass.async_block_till_done()
|
2017-07-22 04:38:53 +00:00
|
|
|
|
|
|
|
assert len(intents) == 2
|
|
|
|
intent = intents[1]
|
2019-07-31 19:25:30 +00:00
|
|
|
assert intent.platform == "conversation"
|
|
|
|
assert intent.intent_type == "OrderBeer"
|
|
|
|
assert intent.slots == {"type": {"value": "Grolsch"}}
|
|
|
|
assert intent.text_input == "I would like the Grolsch beer"
|
2017-07-22 04:38:53 +00:00
|
|
|
|
|
|
|
|
2019-11-26 10:30:21 +00:00
|
|
|
async def test_http_processing_intent(hass, hass_client, hass_admin_user):
|
2017-07-22 04:38:53 +00:00
|
|
|
"""Test processing intent via HTTP API."""
|
2019-07-31 19:25:30 +00:00
|
|
|
|
2017-07-22 04:38:53 +00:00
|
|
|
class TestIntentHandler(intent.IntentHandler):
|
2018-03-31 00:22:48 +00:00
|
|
|
"""Test Intent Handler."""
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
intent_type = "OrderBeer"
|
2017-07-22 04:38:53 +00:00
|
|
|
|
2018-03-31 00:22:48 +00:00
|
|
|
async def async_handle(self, intent):
|
2017-07-22 04:38:53 +00:00
|
|
|
"""Handle the intent."""
|
2019-11-26 10:30:21 +00:00
|
|
|
assert intent.context.user_id == hass_admin_user.id
|
2017-07-22 04:38:53 +00:00
|
|
|
response = intent.create_response()
|
|
|
|
response.async_set_speech(
|
2019-07-31 19:25:30 +00:00
|
|
|
"I've ordered a {}!".format(intent.slots["type"]["value"])
|
|
|
|
)
|
2017-07-22 04:38:53 +00:00
|
|
|
response.async_set_card(
|
2019-07-31 19:25:30 +00:00
|
|
|
"Beer ordered", "You chose a {}.".format(intent.slots["type"]["value"])
|
|
|
|
)
|
2017-07-22 04:38:53 +00:00
|
|
|
return response
|
|
|
|
|
|
|
|
intent.async_register(hass, TestIntentHandler())
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
result = await async_setup_component(
|
|
|
|
hass,
|
|
|
|
"conversation",
|
|
|
|
{"conversation": {"intents": {"OrderBeer": ["I would like the {type} beer"]}}},
|
|
|
|
)
|
2017-07-22 04:38:53 +00:00
|
|
|
assert result
|
|
|
|
|
2018-11-27 09:41:44 +00:00
|
|
|
client = await hass_client()
|
2019-07-31 19:25:30 +00:00
|
|
|
resp = await client.post(
|
|
|
|
"/api/conversation/process", json={"text": "I would like the Grolsch beer"}
|
|
|
|
)
|
2017-07-22 04:38:53 +00:00
|
|
|
|
2021-10-22 17:43:40 +00:00
|
|
|
assert resp.status == HTTPStatus.OK
|
2018-03-31 00:22:48 +00:00
|
|
|
data = await resp.json()
|
2017-07-22 04:38:53 +00:00
|
|
|
|
|
|
|
assert data == {
|
2022-12-13 22:46:40 +00:00
|
|
|
"response": {
|
|
|
|
"response_type": "action_done",
|
|
|
|
"card": {
|
|
|
|
"simple": {"content": "You chose a Grolsch.", "title": "Beer ordered"}
|
|
|
|
},
|
|
|
|
"speech": {
|
|
|
|
"plain": {
|
|
|
|
"extra_data": None,
|
|
|
|
"speech": "I've ordered a Grolsch!",
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"language": hass.config.language,
|
|
|
|
"data": {"targets": []},
|
2019-07-31 19:25:30 +00:00
|
|
|
},
|
2022-12-13 22:46:40 +00:00
|
|
|
"conversation_id": None,
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
async def test_http_partial_action(hass, hass_client, hass_admin_user):
|
|
|
|
"""Test processing intent via HTTP API with a partial completion."""
|
|
|
|
|
|
|
|
class TestIntentHandler(intent.IntentHandler):
|
|
|
|
"""Test Intent Handler."""
|
|
|
|
|
|
|
|
intent_type = "TurnOffLights"
|
|
|
|
|
|
|
|
async def async_handle(self, handle_intent: intent.Intent):
|
|
|
|
"""Handle the intent."""
|
|
|
|
response = handle_intent.create_response()
|
|
|
|
area = handle_intent.slots["area"]["value"]
|
|
|
|
response.async_set_targets(
|
|
|
|
[
|
|
|
|
intent.IntentResponseTarget(
|
|
|
|
type=intent.IntentResponseTargetType.AREA, name=area, id=area
|
|
|
|
)
|
|
|
|
]
|
|
|
|
)
|
|
|
|
|
|
|
|
# Mark some targets as successful, others as failed
|
|
|
|
response.async_set_partial_action_done(
|
|
|
|
success_targets=[
|
|
|
|
intent.IntentResponseTarget(
|
|
|
|
type=intent.IntentResponseTargetType.ENTITY,
|
|
|
|
name="light1",
|
|
|
|
id="light.light1",
|
|
|
|
)
|
|
|
|
],
|
|
|
|
failed_targets=[
|
|
|
|
intent.IntentResponseTarget(
|
|
|
|
type=intent.IntentResponseTargetType.ENTITY,
|
|
|
|
name="light2",
|
|
|
|
id="light.light2",
|
|
|
|
)
|
|
|
|
],
|
|
|
|
)
|
|
|
|
return response
|
|
|
|
|
|
|
|
intent.async_register(hass, TestIntentHandler())
|
|
|
|
|
|
|
|
result = await async_setup_component(
|
|
|
|
hass,
|
|
|
|
"conversation",
|
|
|
|
{
|
|
|
|
"conversation": {
|
|
|
|
"intents": {"TurnOffLights": ["turn off the lights in the {area}"]}
|
2022-12-08 16:39:28 +00:00
|
|
|
}
|
|
|
|
},
|
2022-12-13 22:46:40 +00:00
|
|
|
)
|
|
|
|
assert result
|
|
|
|
|
|
|
|
client = await hass_client()
|
|
|
|
resp = await client.post(
|
|
|
|
"/api/conversation/process", json={"text": "Turn off the lights in the kitchen"}
|
|
|
|
)
|
|
|
|
|
|
|
|
assert resp.status == HTTPStatus.OK
|
|
|
|
data = await resp.json()
|
|
|
|
|
|
|
|
assert data == {
|
|
|
|
"response": {
|
|
|
|
"response_type": "partial_action_done",
|
|
|
|
"card": {},
|
|
|
|
"speech": {},
|
|
|
|
"language": hass.config.language,
|
|
|
|
"data": {
|
|
|
|
"targets": [{"type": "area", "id": "kitchen", "name": "kitchen"}],
|
|
|
|
"success": [{"type": "entity", "id": "light.light1", "name": "light1"}],
|
|
|
|
"failed": [{"type": "entity", "id": "light.light2", "name": "light2"}],
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"conversation_id": None,
|
2017-07-22 04:38:53 +00:00
|
|
|
}
|
2017-11-21 04:26:36 +00:00
|
|
|
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
@pytest.mark.parametrize("sentence", ("turn on kitchen", "turn kitchen on"))
|
2022-12-09 01:30:08 +00:00
|
|
|
async def test_turn_on_intent(hass, init_components, sentence):
|
2017-11-21 04:26:36 +00:00
|
|
|
"""Test calling the turn on intent."""
|
2019-07-31 19:25:30 +00:00
|
|
|
hass.states.async_set("light.kitchen", "off")
|
|
|
|
calls = async_mock_service(hass, HASS_DOMAIN, "turn_on")
|
2017-11-21 04:26:36 +00:00
|
|
|
|
2018-03-31 00:22:48 +00:00
|
|
|
await hass.services.async_call(
|
2019-07-31 19:25:30 +00:00
|
|
|
"conversation", "process", {conversation.ATTR_TEXT: sentence}
|
|
|
|
)
|
2018-03-31 00:22:48 +00:00
|
|
|
await hass.async_block_till_done()
|
2017-11-21 04:26:36 +00:00
|
|
|
|
|
|
|
assert len(calls) == 1
|
|
|
|
call = calls[0]
|
2018-10-30 15:38:09 +00:00
|
|
|
assert call.domain == HASS_DOMAIN
|
2019-07-31 19:25:30 +00:00
|
|
|
assert call.service == "turn_on"
|
|
|
|
assert call.data == {"entity_id": "light.kitchen"}
|
2017-11-21 04:26:36 +00:00
|
|
|
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
@pytest.mark.parametrize("sentence", ("turn off kitchen", "turn kitchen off"))
|
2022-12-09 01:30:08 +00:00
|
|
|
async def test_turn_off_intent(hass, init_components, sentence):
|
2017-11-21 04:26:36 +00:00
|
|
|
"""Test calling the turn on intent."""
|
2019-07-31 19:25:30 +00:00
|
|
|
hass.states.async_set("light.kitchen", "on")
|
|
|
|
calls = async_mock_service(hass, HASS_DOMAIN, "turn_off")
|
2017-11-21 04:26:36 +00:00
|
|
|
|
2018-03-31 00:22:48 +00:00
|
|
|
await hass.services.async_call(
|
2019-07-31 19:25:30 +00:00
|
|
|
"conversation", "process", {conversation.ATTR_TEXT: sentence}
|
|
|
|
)
|
2018-03-31 00:22:48 +00:00
|
|
|
await hass.async_block_till_done()
|
2017-11-21 04:26:36 +00:00
|
|
|
|
|
|
|
assert len(calls) == 1
|
|
|
|
call = calls[0]
|
2018-10-30 15:38:09 +00:00
|
|
|
assert call.domain == HASS_DOMAIN
|
2019-07-31 19:25:30 +00:00
|
|
|
assert call.service == "turn_off"
|
|
|
|
assert call.data == {"entity_id": "light.kitchen"}
|
2017-11-21 04:26:36 +00:00
|
|
|
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
@pytest.mark.parametrize("sentence", ("toggle kitchen", "kitchen toggle"))
|
2022-12-09 01:30:08 +00:00
|
|
|
async def test_toggle_intent(hass, init_components, sentence):
|
2018-02-11 17:33:19 +00:00
|
|
|
"""Test calling the turn on intent."""
|
2019-07-31 19:25:30 +00:00
|
|
|
hass.states.async_set("light.kitchen", "on")
|
|
|
|
calls = async_mock_service(hass, HASS_DOMAIN, "toggle")
|
2018-02-11 17:33:19 +00:00
|
|
|
|
2018-03-31 00:22:48 +00:00
|
|
|
await hass.services.async_call(
|
2019-07-31 19:25:30 +00:00
|
|
|
"conversation", "process", {conversation.ATTR_TEXT: sentence}
|
|
|
|
)
|
2018-03-31 00:22:48 +00:00
|
|
|
await hass.async_block_till_done()
|
2018-02-11 17:33:19 +00:00
|
|
|
|
|
|
|
assert len(calls) == 1
|
|
|
|
call = calls[0]
|
2018-10-30 15:38:09 +00:00
|
|
|
assert call.domain == HASS_DOMAIN
|
2019-07-31 19:25:30 +00:00
|
|
|
assert call.service == "toggle"
|
|
|
|
assert call.data == {"entity_id": "light.kitchen"}
|
2018-02-11 17:33:19 +00:00
|
|
|
|
|
|
|
|
2022-12-09 01:30:08 +00:00
|
|
|
async def test_http_api(hass, init_components, hass_client):
|
2017-11-21 04:26:36 +00:00
|
|
|
"""Test the HTTP conversation API."""
|
2018-11-27 09:41:44 +00:00
|
|
|
client = await hass_client()
|
2019-07-31 19:25:30 +00:00
|
|
|
hass.states.async_set("light.kitchen", "off")
|
|
|
|
calls = async_mock_service(hass, HASS_DOMAIN, "turn_on")
|
2017-11-21 04:26:36 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
resp = await client.post(
|
|
|
|
"/api/conversation/process", json={"text": "Turn the kitchen on"}
|
|
|
|
)
|
2021-10-22 17:43:40 +00:00
|
|
|
assert resp.status == HTTPStatus.OK
|
2022-12-09 01:30:08 +00:00
|
|
|
data = await resp.json()
|
|
|
|
|
|
|
|
assert data == {
|
2022-12-13 22:46:40 +00:00
|
|
|
"response": {
|
|
|
|
"card": {},
|
|
|
|
"speech": {"plain": {"extra_data": None, "speech": "Turned kitchen on"}},
|
|
|
|
"language": hass.config.language,
|
|
|
|
"response_type": "action_done",
|
|
|
|
"data": {
|
|
|
|
"targets": [
|
|
|
|
{
|
|
|
|
"type": "entity",
|
|
|
|
"name": "kitchen",
|
|
|
|
"id": "light.kitchen",
|
|
|
|
},
|
|
|
|
]
|
|
|
|
},
|
2022-12-09 01:30:08 +00:00
|
|
|
},
|
2022-12-13 22:46:40 +00:00
|
|
|
"conversation_id": None,
|
2022-12-09 01:30:08 +00:00
|
|
|
}
|
2017-11-21 04:26:36 +00:00
|
|
|
|
|
|
|
assert len(calls) == 1
|
|
|
|
call = calls[0]
|
2018-10-30 15:38:09 +00:00
|
|
|
assert call.domain == HASS_DOMAIN
|
2019-07-31 19:25:30 +00:00
|
|
|
assert call.service == "turn_on"
|
|
|
|
assert call.data == {"entity_id": "light.kitchen"}
|
2017-11-21 04:26:36 +00:00
|
|
|
|
|
|
|
|
2022-12-09 01:30:08 +00:00
|
|
|
async def test_http_api_no_match(hass, init_components, hass_client):
|
|
|
|
"""Test the HTTP conversation API with an intent match failure."""
|
|
|
|
client = await hass_client()
|
|
|
|
|
|
|
|
# Sentence should not match any intents
|
|
|
|
resp = await client.post("/api/conversation/process", json={"text": "do something"})
|
|
|
|
assert resp.status == HTTPStatus.OK
|
|
|
|
data = await resp.json()
|
|
|
|
|
|
|
|
assert data == {
|
2022-12-13 22:46:40 +00:00
|
|
|
"response": {
|
|
|
|
"card": {},
|
|
|
|
"speech": {
|
|
|
|
"plain": {
|
|
|
|
"extra_data": None,
|
|
|
|
"speech": "Sorry, I didn't understand that",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"language": hass.config.language,
|
|
|
|
"response_type": "error",
|
|
|
|
"data": {
|
|
|
|
"code": "no_intent_match",
|
2022-12-09 01:30:08 +00:00
|
|
|
},
|
|
|
|
},
|
2022-12-13 22:46:40 +00:00
|
|
|
"conversation_id": None,
|
2022-12-09 01:30:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
async def test_http_api_no_valid_targets(hass, init_components, hass_client):
|
|
|
|
"""Test the HTTP conversation API with no valid targets."""
|
|
|
|
client = await hass_client()
|
|
|
|
|
|
|
|
# No kitchen light
|
|
|
|
resp = await client.post(
|
|
|
|
"/api/conversation/process", json={"text": "turn on the kitchen"}
|
|
|
|
)
|
|
|
|
assert resp.status == HTTPStatus.OK
|
|
|
|
data = await resp.json()
|
|
|
|
|
|
|
|
assert data == {
|
2022-12-13 22:46:40 +00:00
|
|
|
"response": {
|
|
|
|
"response_type": "error",
|
|
|
|
"card": {},
|
|
|
|
"speech": {
|
|
|
|
"plain": {
|
|
|
|
"extra_data": None,
|
|
|
|
"speech": "Unable to find an entity called kitchen",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"language": hass.config.language,
|
|
|
|
"data": {
|
|
|
|
"code": "no_valid_targets",
|
2022-12-09 01:30:08 +00:00
|
|
|
},
|
|
|
|
},
|
2022-12-13 22:46:40 +00:00
|
|
|
"conversation_id": None,
|
2022-12-09 01:30:08 +00:00
|
|
|
}
|
2018-02-11 17:33:19 +00:00
|
|
|
|
2017-11-21 04:26:36 +00:00
|
|
|
|
2022-12-09 01:30:08 +00:00
|
|
|
async def test_http_api_handle_failure(hass, init_components, hass_client):
|
|
|
|
"""Test the HTTP conversation API with an error during handling."""
|
|
|
|
client = await hass_client()
|
|
|
|
|
|
|
|
hass.states.async_set("light.kitchen", "off")
|
|
|
|
|
|
|
|
# Raise an "unexpected" error during intent handling
|
|
|
|
def async_handle_error(*args, **kwargs):
|
|
|
|
raise intent.IntentUnexpectedError(
|
|
|
|
"Unexpected error turning on the kitchen light"
|
|
|
|
)
|
|
|
|
|
|
|
|
with patch("homeassistant.helpers.intent.async_handle", new=async_handle_error):
|
|
|
|
resp = await client.post(
|
|
|
|
"/api/conversation/process", json={"text": "turn on the kitchen"}
|
|
|
|
)
|
|
|
|
|
|
|
|
assert resp.status == HTTPStatus.OK
|
|
|
|
data = await resp.json()
|
|
|
|
|
|
|
|
assert data == {
|
2022-12-13 22:46:40 +00:00
|
|
|
"response": {
|
|
|
|
"response_type": "error",
|
|
|
|
"card": {},
|
|
|
|
"speech": {
|
|
|
|
"plain": {
|
|
|
|
"extra_data": None,
|
|
|
|
"speech": "Unexpected error turning on the kitchen light",
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"language": hass.config.language,
|
|
|
|
"data": {
|
|
|
|
"code": "failed_to_handle",
|
|
|
|
},
|
2022-12-09 01:30:08 +00:00
|
|
|
},
|
2022-12-13 22:46:40 +00:00
|
|
|
"conversation_id": None,
|
2022-12-09 01:30:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
async def test_http_api_wrong_data(hass, init_components, hass_client):
|
|
|
|
"""Test the HTTP conversation API."""
|
2018-11-27 09:41:44 +00:00
|
|
|
client = await hass_client()
|
2017-11-21 04:26:36 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
resp = await client.post("/api/conversation/process", json={"text": 123})
|
2021-10-22 17:43:40 +00:00
|
|
|
assert resp.status == HTTPStatus.BAD_REQUEST
|
2017-11-21 04:26:36 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
resp = await client.post("/api/conversation/process", json={})
|
2021-10-22 17:43:40 +00:00
|
|
|
assert resp.status == HTTPStatus.BAD_REQUEST
|
2018-03-01 15:35:12 +00:00
|
|
|
|
|
|
|
|
2019-11-26 10:30:21 +00:00
|
|
|
async def test_custom_agent(hass, hass_client, hass_admin_user):
|
2019-10-18 18:46:45 +00:00
|
|
|
"""Test a custom conversation agent."""
|
|
|
|
|
2019-11-07 20:21:12 +00:00
|
|
|
calls = []
|
|
|
|
|
2019-10-18 18:46:45 +00:00
|
|
|
class MyAgent(conversation.AbstractConversationAgent):
|
|
|
|
"""Test Agent."""
|
|
|
|
|
2022-12-08 16:39:28 +00:00
|
|
|
async def async_process(self, text, context, conversation_id, language):
|
2019-10-18 18:46:45 +00:00
|
|
|
"""Process some text."""
|
2022-12-08 16:39:28 +00:00
|
|
|
calls.append((text, context, conversation_id, language))
|
|
|
|
response = intent.IntentResponse(language=language)
|
2019-10-18 18:46:45 +00:00
|
|
|
response.async_set_speech("Test response")
|
2022-12-13 22:46:40 +00:00
|
|
|
return conversation.ConversationResult(
|
|
|
|
response=response, conversation_id=conversation_id
|
|
|
|
)
|
2019-10-18 18:46:45 +00:00
|
|
|
|
|
|
|
conversation.async_set_agent(hass, MyAgent())
|
|
|
|
|
|
|
|
assert await async_setup_component(hass, "conversation", {})
|
|
|
|
|
|
|
|
client = await hass_client()
|
|
|
|
|
2019-11-07 20:21:12 +00:00
|
|
|
resp = await client.post(
|
|
|
|
"/api/conversation/process",
|
2022-12-08 16:39:28 +00:00
|
|
|
json={
|
|
|
|
"text": "Test Text",
|
|
|
|
"conversation_id": "test-conv-id",
|
|
|
|
"language": "test-language",
|
|
|
|
},
|
2019-11-07 20:21:12 +00:00
|
|
|
)
|
2021-10-22 17:43:40 +00:00
|
|
|
assert resp.status == HTTPStatus.OK
|
2019-10-18 18:46:45 +00:00
|
|
|
assert await resp.json() == {
|
2022-12-13 22:46:40 +00:00
|
|
|
"response": {
|
|
|
|
"response_type": "action_done",
|
|
|
|
"card": {},
|
|
|
|
"speech": {
|
|
|
|
"plain": {
|
|
|
|
"extra_data": None,
|
|
|
|
"speech": "Test response",
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"language": "test-language",
|
|
|
|
"data": {"targets": []},
|
2022-12-08 16:39:28 +00:00
|
|
|
},
|
2022-12-13 22:46:40 +00:00
|
|
|
"conversation_id": "test-conv-id",
|
2019-10-18 18:46:45 +00:00
|
|
|
}
|
2019-11-07 20:21:12 +00:00
|
|
|
|
|
|
|
assert len(calls) == 1
|
|
|
|
assert calls[0][0] == "Test Text"
|
2019-11-26 10:30:21 +00:00
|
|
|
assert calls[0][1].user_id == hass_admin_user.id
|
|
|
|
assert calls[0][2] == "test-conv-id"
|
2022-12-08 16:39:28 +00:00
|
|
|
assert calls[0][3] == "test-language"
|