2017-07-01 22:58:12 +02:00
|
|
|
"""Test the Snips component."""
|
|
|
|
import asyncio
|
2018-01-07 16:42:08 -05:00
|
|
|
import json
|
2017-07-01 22:58:12 +02:00
|
|
|
|
2018-01-07 16:42:08 -05:00
|
|
|
from homeassistant.core import callback
|
2017-07-01 22:58:12 +02:00
|
|
|
from homeassistant.bootstrap import async_setup_component
|
2017-07-21 21:38:53 -07:00
|
|
|
from tests.common import async_fire_mqtt_message, async_mock_intent
|
2017-07-01 22:58:12 +02:00
|
|
|
|
|
|
|
|
|
|
|
@asyncio.coroutine
|
2018-01-07 16:42:08 -05:00
|
|
|
def test_snips_intent(hass, mqtt_mock):
|
|
|
|
"""Test intent via Snips."""
|
2017-07-01 22:58:12 +02:00
|
|
|
result = yield from async_setup_component(hass, "snips", {
|
2017-07-21 21:38:53 -07:00
|
|
|
"snips": {},
|
2017-07-01 22:58:12 +02:00
|
|
|
})
|
|
|
|
assert result
|
2018-01-07 16:42:08 -05:00
|
|
|
payload = """
|
|
|
|
{
|
|
|
|
"input": "turn the lights green",
|
|
|
|
"intent": {
|
|
|
|
"intentName": "Lights",
|
|
|
|
"probability": 1
|
|
|
|
},
|
|
|
|
"slots": [
|
|
|
|
{
|
|
|
|
"slotName": "light_color",
|
|
|
|
"value": {
|
|
|
|
"kind": "Custom",
|
|
|
|
"value": "green"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
"""
|
2017-07-01 22:58:12 +02:00
|
|
|
|
2017-07-21 21:38:53 -07:00
|
|
|
intents = async_mock_intent(hass, 'Lights')
|
|
|
|
|
2018-01-07 16:42:08 -05:00
|
|
|
async_fire_mqtt_message(hass, 'hermes/intent/Lights',
|
|
|
|
payload)
|
2017-07-01 22:58:12 +02:00
|
|
|
yield from hass.async_block_till_done()
|
2017-07-21 21:38:53 -07:00
|
|
|
assert len(intents) == 1
|
|
|
|
intent = intents[0]
|
|
|
|
assert intent.platform == 'snips'
|
|
|
|
assert intent.intent_type == 'Lights'
|
|
|
|
assert intent.slots == {'light_color': {'value': 'green'}}
|
|
|
|
assert intent.text_input == 'turn the lights green'
|
2018-01-07 16:42:08 -05:00
|
|
|
|
|
|
|
|
|
|
|
@asyncio.coroutine
|
|
|
|
def test_snips_intent_with_snips_duration(hass, mqtt_mock):
|
|
|
|
"""Test intent with Snips duration."""
|
|
|
|
result = yield from async_setup_component(hass, "snips", {
|
|
|
|
"snips": {},
|
|
|
|
})
|
|
|
|
assert result
|
|
|
|
payload = """
|
|
|
|
{
|
|
|
|
"input": "set a timer of five minutes",
|
|
|
|
"intent": {
|
|
|
|
"intentName": "SetTimer"
|
|
|
|
},
|
|
|
|
"slots": [
|
|
|
|
{
|
|
|
|
"rawValue": "five minutes",
|
|
|
|
"value": {
|
|
|
|
"kind": "Duration",
|
|
|
|
"years": 0,
|
|
|
|
"quarters": 0,
|
|
|
|
"months": 0,
|
|
|
|
"weeks": 0,
|
|
|
|
"days": 0,
|
|
|
|
"hours": 0,
|
|
|
|
"minutes": 5,
|
|
|
|
"seconds": 0,
|
|
|
|
"precision": "Exact"
|
|
|
|
},
|
|
|
|
"range": {
|
|
|
|
"start": 15,
|
|
|
|
"end": 27
|
|
|
|
},
|
|
|
|
"entity": "snips/duration",
|
|
|
|
"slotName": "timer_duration"
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
"""
|
|
|
|
intents = async_mock_intent(hass, 'SetTimer')
|
|
|
|
|
|
|
|
async_fire_mqtt_message(hass, 'hermes/intent/SetTimer',
|
|
|
|
payload)
|
|
|
|
yield from hass.async_block_till_done()
|
|
|
|
assert len(intents) == 1
|
|
|
|
intent = intents[0]
|
|
|
|
assert intent.platform == 'snips'
|
|
|
|
assert intent.intent_type == 'SetTimer'
|
|
|
|
assert intent.slots == {'timer_duration': {'value': 300}}
|
|
|
|
|
|
|
|
|
|
|
|
@asyncio.coroutine
|
|
|
|
def test_intent_speech_response(hass, mqtt_mock):
|
|
|
|
"""Test intent speech response via Snips."""
|
|
|
|
event = 'call_service'
|
|
|
|
events = []
|
|
|
|
|
|
|
|
@callback
|
|
|
|
def record_event(event):
|
|
|
|
"""Add recorded event to set."""
|
|
|
|
events.append(event)
|
|
|
|
|
|
|
|
result = yield from async_setup_component(hass, "snips", {
|
|
|
|
"snips": {},
|
|
|
|
})
|
|
|
|
assert result
|
|
|
|
result = yield from async_setup_component(hass, "intent_script", {
|
|
|
|
"intent_script": {
|
|
|
|
"spokenIntent": {
|
|
|
|
"speech": {
|
|
|
|
"type": "plain",
|
|
|
|
"text": "I am speaking to you"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
assert result
|
|
|
|
payload = """
|
|
|
|
{
|
|
|
|
"input": "speak to me",
|
|
|
|
"sessionId": "abcdef0123456789",
|
|
|
|
"intent": {
|
|
|
|
"intentName": "spokenIntent"
|
|
|
|
},
|
|
|
|
"slots": []
|
|
|
|
}
|
|
|
|
"""
|
|
|
|
hass.bus.async_listen(event, record_event)
|
|
|
|
async_fire_mqtt_message(hass, 'hermes/intent/spokenIntent',
|
|
|
|
payload)
|
|
|
|
yield from hass.async_block_till_done()
|
|
|
|
|
|
|
|
assert len(events) == 1
|
|
|
|
assert events[0].data['domain'] == 'mqtt'
|
|
|
|
assert events[0].data['service'] == 'publish'
|
|
|
|
payload = json.loads(events[0].data['service_data']['payload'])
|
|
|
|
topic = events[0].data['service_data']['topic']
|
|
|
|
assert payload['sessionId'] == 'abcdef0123456789'
|
|
|
|
assert payload['text'] == 'I am speaking to you'
|
|
|
|
assert topic == 'hermes/dialogueManager/endSession'
|
|
|
|
|
|
|
|
|
|
|
|
@asyncio.coroutine
|
|
|
|
def test_snips_unknown_intent(hass, mqtt_mock):
|
|
|
|
"""Test calling unknown Intent via Snips."""
|
|
|
|
event = 'call_service'
|
|
|
|
events = []
|
|
|
|
|
|
|
|
@callback
|
|
|
|
def record_event(event):
|
|
|
|
"""Add recorded event to set."""
|
|
|
|
events.append(event)
|
|
|
|
result = yield from async_setup_component(hass, "snips", {
|
|
|
|
"snips": {},
|
|
|
|
})
|
|
|
|
assert result
|
|
|
|
payload = """
|
|
|
|
{
|
|
|
|
"input": "what to do",
|
|
|
|
"intent": {
|
|
|
|
"intentName": "unknownIntent"
|
|
|
|
},
|
|
|
|
"slots": []
|
|
|
|
}
|
|
|
|
"""
|
|
|
|
intents = async_mock_intent(hass, 'knownIntent')
|
|
|
|
hass.bus.async_listen(event, record_event)
|
|
|
|
async_fire_mqtt_message(hass, 'hermes/intent/unknownIntent',
|
|
|
|
payload)
|
|
|
|
yield from hass.async_block_till_done()
|
|
|
|
|
|
|
|
assert len(intents) == 0
|
|
|
|
assert len(events) == 1
|
|
|
|
assert events[0].data['domain'] == 'mqtt'
|
|
|
|
assert events[0].data['service'] == 'publish'
|
|
|
|
payload = json.loads(events[0].data['service_data']['payload'])
|
|
|
|
topic = events[0].data['service_data']['topic']
|
|
|
|
assert payload['text'] == 'Unknown Intent'
|
|
|
|
assert topic == 'hermes/dialogueManager/endSession'
|