Added HassOpenCover and HassCloseCover intents (#13372)

* Added intents to cover

* Added test for cover intents

* Style fixes

* Reverted reversions

* Async fixes

* Woof

* Added conditional loading

* Added conditional loading

* Added conditional loading

* Moved tests, fixed logic

* Moved tests, fixed logic

* Pylint

* Pylint

* Refactored componenet registration

* Refactored componenet registration

* Lint
This commit is contained in:
Tod Schmidt authored and Paulus Schoutsen committed 2018-03-30 17:22:48 -07:00
1 parent bf58945680
commit bf44dc422c
5 files changed
+185 -83

No files matched your search

+74 -56
View File
@@ -1,26 +1,24 @@
"""The tests for the Conversation component."""
# pylint: disable=protected-access
import asyncio
import pytest
from homeassistant.setup import async_setup_component
from homeassistant.components import conversation
import homeassistant.components as component
from homeassistant.components.cover import (SERVICE_OPEN_COVER)
from homeassistant.helpers import intent
from tests.common import async_mock_intent, async_mock_service
@asyncio.coroutine
def test_calling_intent(hass):
async def test_calling_intent(hass):
"""Test calling an intent from a conversation."""
intents = async_mock_intent(hass, 'OrderBeer')
result = yield from component.async_setup(hass, {})
result = await component.async_setup(hass, {})
assert result
result = yield from async_setup_component(hass, 'conversation', {
result = await async_setup_component(hass, 'conversation', {
'conversation': {
'intents': {
'OrderBeer': [
@@ -31,11 +29,11 @@ def test_calling_intent(hass):
})
assert result
yield from hass.services.async_call(
await hass.services.async_call(
'conversation', 'process', {
conversation.ATTR_TEXT: 'I would like the Grolsch beer'
})
yield from hass.async_block_till_done()
await hass.async_block_till_done()
assert len(intents) == 1
intent = intents[0]
@@ -45,8 +43,7 @@ def test_calling_intent(hass):
assert intent.text_input == 'I would like the Grolsch beer'
@asyncio.coroutine
def test_register_before_setup(hass):
async def test_register_before_setup(hass):
"""Test calling an intent from a conversation."""
intents = async_mock_intent(hass, 'OrderBeer')
@@ -54,7 +51,7 @@ def test_register_before_setup(hass):
'A {type} beer, please'
])
result = yield from async_setup_component(hass, 'conversation', {
result = await async_setup_component(hass, 'conversation', {
'conversation': {
'intents': {
'OrderBeer': [
@@ -65,11 +62,11 @@ def test_register_before_setup(hass):
})
assert result
yield from hass.services.async_call(
await hass.services.async_call(
'conversation', 'process', {
conversation.ATTR_TEXT: 'A Grolsch beer, please'
})
yield from hass.async_block_till_done()
await hass.async_block_till_done()
assert len(intents) == 1
intent = intents[0]
@@ -78,11 +75,11 @@ def test_register_before_setup(hass):
assert intent.slots == {'type': {'value': 'Grolsch'}}
assert intent.text_input == 'A Grolsch beer, please'
yield from hass.services.async_call(
await hass.services.async_call(
'conversation', 'process', {
conversation.ATTR_TEXT: 'I would like the Grolsch beer'
})
yield from hass.async_block_till_done()
await hass.async_block_till_done()
assert len(intents) == 2
intent = intents[1]
@@ -92,14 +89,14 @@ def test_register_before_setup(hass):
assert intent.text_input == 'I would like the Grolsch beer'
@asyncio.coroutine
def test_http_processing_intent(hass, aiohttp_client):
async def test_http_processing_intent(hass, test_client):
"""Test processing intent via HTTP API."""
class TestIntentHandler(intent.IntentHandler):
"""Test Intent Handler."""
intent_type = 'OrderBeer'
@asyncio.coroutine
def async_handle(self, intent):
async def async_handle(self, intent):
"""Handle the intent."""
response = intent.create_response()
response.async_set_speech(
@@ -111,7 +108,7 @@ def test_http_processing_intent(hass, aiohttp_client):
intent.async_register(hass, TestIntentHandler())
result = yield from async_setup_component(hass, 'conversation', {
result = await async_setup_component(hass, 'conversation', {
'conversation': {
'intents': {
'OrderBeer': [
@@ -122,13 +119,13 @@ def test_http_processing_intent(hass, aiohttp_client):
})
assert result
client = yield from aiohttp_client(hass.http.app)
resp = yield from client.post('/api/conversation/process', json={
client = await test_client(hass.http.app)
resp = await client.post('/api/conversation/process', json={
'text': 'I would like the Grolsch beer'
})
assert resp.status == 200
data = yield from resp.json()
data = await resp.json()
assert data == {
'card': {
@@ -145,24 +142,23 @@ def test_http_processing_intent(hass, aiohttp_client):
}
@asyncio.coroutine
@pytest.mark.parametrize('sentence', ('turn on kitchen', 'turn kitchen on'))
def test_turn_on_intent(hass, sentence):
async def test_turn_on_intent(hass, sentence):
"""Test calling the turn on intent."""
result = yield from component.async_setup(hass, {})
result = await component.async_setup(hass, {})
assert result
result = yield from async_setup_component(hass, 'conversation', {})
result = await async_setup_component(hass, 'conversation', {})
assert result
hass.states.async_set('light.kitchen', 'off')
calls = async_mock_service(hass, 'homeassistant', 'turn_on')
yield from hass.services.async_call(
await hass.services.async_call(
'conversation', 'process', {
conversation.ATTR_TEXT: sentence
})
yield from hass.async_block_till_done()
await hass.async_block_till_done()
assert len(calls) == 1
call = calls[0]
@@ -171,24 +167,49 @@ def test_turn_on_intent(hass, sentence):
assert call.data == {'entity_id': 'light.kitchen'}
@asyncio.coroutine
@pytest.mark.parametrize('sentence', ('turn off kitchen', 'turn kitchen off'))
def test_turn_off_intent(hass, sentence):
"""Test calling the turn on intent."""
result = yield from component.async_setup(hass, {})
async def test_cover_intents_loading(hass):
"""Test Cover Intents Loading."""
with pytest.raises(intent.UnknownIntent):
await intent.async_handle(
hass, 'test', 'HassOpenCover', {'name': {'value': 'garage door'}}
)
result = await async_setup_component(hass, 'cover', {})
assert result
result = yield from async_setup_component(hass, 'conversation', {})
hass.states.async_set('cover.garage_door', 'closed')
calls = async_mock_service(hass, 'cover', SERVICE_OPEN_COVER)
response = await intent.async_handle(
hass, 'test', 'HassOpenCover', {'name': {'value': 'garage door'}}
)
await hass.async_block_till_done()
assert response.speech['plain']['speech'] == 'Opened garage door'
assert len(calls) == 1
call = calls[0]
assert call.domain == 'cover'
assert call.service == 'open_cover'
assert call.data == {'entity_id': 'cover.garage_door'}
@pytest.mark.parametrize('sentence', ('turn off kitchen', 'turn kitchen off'))
async def test_turn_off_intent(hass, sentence):
"""Test calling the turn on intent."""
result = await component.async_setup(hass, {})
assert result
result = await async_setup_component(hass, 'conversation', {})
assert result
hass.states.async_set('light.kitchen', 'on')
calls = async_mock_service(hass, 'homeassistant', 'turn_off')
yield from hass.services.async_call(
await hass.services.async_call(
'conversation', 'process', {
conversation.ATTR_TEXT: sentence
})
yield from hass.async_block_till_done()
await hass.async_block_till_done()
assert len(calls) == 1
call = calls[0]
@@ -197,24 +218,23 @@ def test_turn_off_intent(hass, sentence):
assert call.data == {'entity_id': 'light.kitchen'}
@asyncio.coroutine
@pytest.mark.parametrize('sentence', ('toggle kitchen', 'kitchen toggle'))
def test_toggle_intent(hass, sentence):
async def test_toggle_intent(hass, sentence):
"""Test calling the turn on intent."""
result = yield from component.async_setup(hass, {})
result = await component.async_setup(hass, {})
assert result
result = yield from async_setup_component(hass, 'conversation', {})
result = await async_setup_component(hass, 'conversation', {})
assert result
hass.states.async_set('light.kitchen', 'on')
calls = async_mock_service(hass, 'homeassistant', 'toggle')
yield from hass.services.async_call(
await hass.services.async_call(
'conversation', 'process', {
conversation.ATTR_TEXT: sentence
})
yield from hass.async_block_till_done()
await hass.async_block_till_done()
assert len(calls) == 1
call = calls[0]
@@ -223,20 +243,19 @@ def test_toggle_intent(hass, sentence):
assert call.data == {'entity_id': 'light.kitchen'}
@asyncio.coroutine
def test_http_api(hass, aiohttp_client):
async def test_http_api(hass, test_client):
"""Test the HTTP conversation API."""
result = yield from component.async_setup(hass, {})
result = await component.async_setup(hass, {})
assert result
result = yield from async_setup_component(hass, 'conversation', {})
result = await async_setup_component(hass, 'conversation', {})
assert result
client = yield from aiohttp_client(hass.http.app)
client = await test_client(hass.http.app)
hass.states.async_set('light.kitchen', 'off')
calls = async_mock_service(hass, 'homeassistant', 'turn_on')
resp = yield from client.post('/api/conversation/process', json={
resp = await client.post('/api/conversation/process', json={
'text': 'Turn the kitchen on'
})
assert resp.status == 200
@@ -248,23 +267,22 @@ def test_http_api(hass, aiohttp_client):
assert call.data == {'entity_id': 'light.kitchen'}
@asyncio.coroutine
def test_http_api_wrong_data(hass, aiohttp_client):
async def test_http_api_wrong_data(hass, test_client):
"""Test the HTTP conversation API."""
result = yield from component.async_setup(hass, {})
result = await component.async_setup(hass, {})
assert result
result = yield from async_setup_component(hass, 'conversation', {})
result = await async_setup_component(hass, 'conversation', {})
assert result
client = yield from aiohttp_client(hass.http.app)
client = await test_client(hass.http.app)
resp = yield from client.post('/api/conversation/process', json={
resp = await client.post('/api/conversation/process', json={
'text': 123
})
assert resp.status == 400
resp = yield from client.post('/api/conversation/process', json={
resp = await client.post('/api/conversation/process', json={
})
assert resp.status == 400