Add Intent component (#8434)

* Add intent component

* Add intent script component

* Add shopping list component

* Convert Snips to use intent component

* Convert Alexa to use intent component

* Lint

* Fix Alexa tests

* Update snips test

* Add intent support to conversation

* Add API to view shopping list contents

* Lint

* Fix demo test

* Lint

* lint

* Remove type from slot schema

* Add dependency to conversation

* Move intent to be a helper

* Fix conversation

* Clean up intent helper

* Fix Alexa

* Snips to use new hass.components

* Allow registering intents with conversation at any point in time

* Shopping list to register sentences

* Add HTTP endpoint to Conversation

* Add async action option to intent_script

* Update API.ai to use intents

* Cleanup Alexa

* Shopping list component to register built-in panel

* Rename shopping list intent to inlude Hass name
This commit is contained in:
Paulus Schoutsen authored and GitHub committed 2017-07-21 21:38:53 -07:00
1 parent 7bea69ce83
commit 7edf14e55f
16 files changed
+970 -396

No files matched your search

+127 -39
View File
@@ -1,16 +1,18 @@
"""The tests for the Conversation component."""
# pylint: disable=protected-access
import asyncio
import unittest
from unittest.mock import patch
from homeassistant.core import callback
from homeassistant.setup import setup_component
from homeassistant.setup import setup_component, async_setup_component
import homeassistant.components as core_components
from homeassistant.components import conversation
from homeassistant.const import ATTR_ENTITY_ID
from homeassistant.util.async import run_coroutine_threadsafe
from homeassistant.helpers import intent
from tests.common import get_test_home_assistant, assert_setup_component
from tests.common import get_test_home_assistant, async_mock_intent
class TestConversation(unittest.TestCase):
@@ -25,10 +27,9 @@ class TestConversation(unittest.TestCase):
self.assertTrue(run_coroutine_threadsafe(
core_components.async_setup(self.hass, {}), self.hass.loop
).result())
with assert_setup_component(0):
self.assertTrue(setup_component(self.hass, conversation.DOMAIN, {
conversation.DOMAIN: {}
}))
self.assertTrue(setup_component(self.hass, conversation.DOMAIN, {
conversation.DOMAIN: {}
}))
# pylint: disable=invalid-name
def tearDown(self):
@@ -119,44 +120,131 @@ class TestConversation(unittest.TestCase):
self.assertFalse(mock_call.called)
class TestConfiguration(unittest.TestCase):
"""Test the conversation configuration component."""
@asyncio.coroutine
def test_calling_intent(hass):
"""Test calling an intent from a conversation."""
intents = async_mock_intent(hass, 'OrderBeer')
# pylint: disable=invalid-name
def setUp(self):
"""Setup things to be run when tests are started."""
self.hass = get_test_home_assistant()
self.assertTrue(setup_component(self.hass, conversation.DOMAIN, {
conversation.DOMAIN: {
'test_2': {
'sentence': 'switch boolean',
'action': {
'service': 'input_boolean.toggle'
}
}
result = yield from async_setup_component(hass, 'conversation', {
'conversation': {
'intents': {
'OrderBeer': [
'I would like the {type} beer'
]
}
}))
}
})
assert result
# pylint: disable=invalid-name
def tearDown(self):
"""Stop everything that was started."""
self.hass.stop()
yield from hass.services.async_call(
'conversation', 'process', {
conversation.ATTR_TEXT: 'I would like the Grolsch beer'
})
yield from hass.async_block_till_done()
def test_custom(self):
"""Setup and perform good turn on requests."""
calls = []
assert len(intents) == 1
intent = intents[0]
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'
@callback
def record_call(service):
"""Recorder for a call."""
calls.append(service)
self.hass.services.register('input_boolean', 'toggle', record_call)
@asyncio.coroutine
def test_register_before_setup(hass):
"""Test calling an intent from a conversation."""
intents = async_mock_intent(hass, 'OrderBeer')
event_data = {conversation.ATTR_TEXT: 'switch boolean'}
self.assertTrue(self.hass.services.call(
conversation.DOMAIN, 'process', event_data, True))
hass.components.conversation.async_register('OrderBeer', [
'A {type} beer, please'
])
call = calls[-1]
self.assertEqual('input_boolean', call.domain)
self.assertEqual('toggle', call.service)
result = yield from async_setup_component(hass, 'conversation', {
'conversation': {
'intents': {
'OrderBeer': [
'I would like the {type} beer'
]
}
}
})
assert result
yield from hass.services.async_call(
'conversation', 'process', {
conversation.ATTR_TEXT: 'A Grolsch beer, please'
})
yield from hass.async_block_till_done()
assert len(intents) == 1
intent = intents[0]
assert intent.platform == 'conversation'
assert intent.intent_type == 'OrderBeer'
assert intent.slots == {'type': {'value': 'Grolsch'}}
assert intent.text_input == 'A Grolsch beer, please'
yield from hass.services.async_call(
'conversation', 'process', {
conversation.ATTR_TEXT: 'I would like the Grolsch beer'
})
yield from hass.async_block_till_done()
assert len(intents) == 2
intent = intents[1]
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'
@asyncio.coroutine
def test_http_processing_intent(hass, test_client):
"""Test processing intent via HTTP API."""
class TestIntentHandler(intent.IntentHandler):
intent_type = 'OrderBeer'
@asyncio.coroutine
def async_handle(self, intent):
"""Handle the intent."""
response = intent.create_response()
response.async_set_speech(
"I've ordered a {}!".format(intent.slots['type']['value']))
response.async_set_card(
"Beer ordered",
"You chose a {}.".format(intent.slots['type']['value']))
return response
intent.async_register(hass, TestIntentHandler())
result = yield from async_setup_component(hass, 'conversation', {
'conversation': {
'intents': {
'OrderBeer': [
'I would like the {type} beer'
]
}
}
})
assert result
client = yield from test_client(hass.http.app)
resp = yield from client.post('/api/conversation/process', json={
'text': 'I would like the Grolsch beer'
})
assert resp.status == 200
data = yield from resp.json()
assert data == {
'card': {
'simple': {
'content': 'You chose a Grolsch.',
'title': 'Beer ordered'
}},
'speech': {
'plain': {
'extra_data': None,
'speech': "I've ordered a Grolsch!"
}
}
}