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:
1 parent
7bea69ce83
commit
7edf14e55f
16 files changed
+970
-396
No files matched your search
@@ -5,12 +5,10 @@ For more details about this component, please refer to the documentation at
|
||||
https://home-assistant.io/components/snips/
|
||||
"""
|
||||
import asyncio
|
||||
import copy
|
||||
import json
|
||||
import logging
|
||||
import voluptuous as vol
|
||||
from homeassistant.helpers import template, script, config_validation as cv
|
||||
import homeassistant.loader as loader
|
||||
from homeassistant.helpers import intent, config_validation as cv
|
||||
|
||||
DOMAIN = 'snips'
|
||||
DEPENDENCIES = ['mqtt']
|
||||
@@ -19,16 +17,10 @@ CONF_ACTION = 'action'
|
||||
|
||||
INTENT_TOPIC = 'hermes/nlu/intentParsed'
|
||||
|
||||
LOGGER = logging.getLogger(__name__)
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
CONFIG_SCHEMA = vol.Schema({
|
||||
DOMAIN: {
|
||||
CONF_INTENTS: {
|
||||
cv.string: {
|
||||
vol.Optional(CONF_ACTION): cv.SCRIPT_SCHEMA,
|
||||
}
|
||||
}
|
||||
}
|
||||
DOMAIN: {}
|
||||
}, extra=vol.ALLOW_EXTRA)
|
||||
|
||||
INTENT_SCHEMA = vol.Schema({
|
||||
@@ -49,74 +41,34 @@ INTENT_SCHEMA = vol.Schema({
|
||||
@asyncio.coroutine
|
||||
def async_setup(hass, config):
|
||||
"""Activate Snips component."""
|
||||
mqtt = loader.get_component('mqtt')
|
||||
intents = config[DOMAIN].get(CONF_INTENTS, {})
|
||||
handler = IntentHandler(hass, intents)
|
||||
|
||||
@asyncio.coroutine
|
||||
def message_received(topic, payload, qos):
|
||||
"""Handle new messages on MQTT."""
|
||||
LOGGER.debug("New intent: %s", payload)
|
||||
yield from handler.handle_intent(payload)
|
||||
_LOGGER.debug("New intent: %s", payload)
|
||||
|
||||
yield from mqtt.async_subscribe(hass, INTENT_TOPIC, message_received)
|
||||
try:
|
||||
request = json.loads(payload)
|
||||
except TypeError:
|
||||
_LOGGER.error('Received invalid JSON: %s', payload)
|
||||
return
|
||||
|
||||
try:
|
||||
request = INTENT_SCHEMA(request)
|
||||
except vol.Invalid as err:
|
||||
_LOGGER.error('Intent has invalid schema: %s. %s', err, request)
|
||||
return
|
||||
|
||||
intent_type = request['intent']['intentName'].split('__')[-1]
|
||||
slots = {slot['slotName']: {'value': slot['value']['value']}
|
||||
for slot in request.get('slots', [])}
|
||||
|
||||
try:
|
||||
yield from intent.async_handle(
|
||||
hass, DOMAIN, intent_type, slots, request['input'])
|
||||
except intent.IntentError:
|
||||
_LOGGER.exception("Error while handling intent.")
|
||||
|
||||
yield from hass.components.mqtt.async_subscribe(
|
||||
INTENT_TOPIC, message_received)
|
||||
|
||||
return True
|
||||
|
||||
|
||||
class IntentHandler(object):
|
||||
"""Help handling intents."""
|
||||
|
||||
def __init__(self, hass, intents):
|
||||
"""Initialize the intent handler."""
|
||||
self.hass = hass
|
||||
intents = copy.deepcopy(intents)
|
||||
template.attach(hass, intents)
|
||||
|
||||
for name, intent in intents.items():
|
||||
if CONF_ACTION in intent:
|
||||
intent[CONF_ACTION] = script.Script(
|
||||
hass, intent[CONF_ACTION], "Snips intent {}".format(name))
|
||||
|
||||
self.intents = intents
|
||||
|
||||
@asyncio.coroutine
|
||||
def handle_intent(self, payload):
|
||||
"""Handle an intent."""
|
||||
try:
|
||||
response = json.loads(payload)
|
||||
except TypeError:
|
||||
LOGGER.error('Received invalid JSON: %s', payload)
|
||||
return
|
||||
|
||||
try:
|
||||
response = INTENT_SCHEMA(response)
|
||||
except vol.Invalid as err:
|
||||
LOGGER.error('Intent has invalid schema: %s. %s', err, response)
|
||||
return
|
||||
|
||||
intent = response['intent']['intentName'].split('__')[-1]
|
||||
config = self.intents.get(intent)
|
||||
|
||||
if config is None:
|
||||
LOGGER.warning("Received unknown intent %s. %s", intent, response)
|
||||
return
|
||||
|
||||
action = config.get(CONF_ACTION)
|
||||
|
||||
if action is not None:
|
||||
slots = self.parse_slots(response)
|
||||
yield from action.async_run(slots)
|
||||
|
||||
# pylint: disable=no-self-use
|
||||
def parse_slots(self, response):
|
||||
"""Parse the intent slots."""
|
||||
parameters = {}
|
||||
|
||||
for slot in response.get('slots', []):
|
||||
key = slot['slotName']
|
||||
value = slot['value']['value']
|
||||
if value is not None:
|
||||
parameters[key] = value
|
||||
|
||||
return parameters
|
||||
Reference in new issue
Block a user