2019-02-13 20:21:14 +00:00
|
|
|
"""Support for functionality to have conversations with Home Assistant."""
|
2015-03-10 07:08:50 +00:00
|
|
|
import logging
|
|
|
|
import re
|
|
|
|
|
2016-04-13 16:48:39 +00:00
|
|
|
import voluptuous as vol
|
|
|
|
|
2015-08-17 03:44:46 +00:00
|
|
|
from homeassistant import core
|
2017-12-29 09:06:39 +00:00
|
|
|
from homeassistant.components import http
|
2018-08-27 22:20:12 +00:00
|
|
|
from homeassistant.components.conversation.util import create_matcher
|
2018-02-14 20:06:03 +00:00
|
|
|
from homeassistant.components.http.data_validator import (
|
|
|
|
RequestDataValidator)
|
2018-03-31 00:22:48 +00:00
|
|
|
from homeassistant.components.cover import (INTENT_OPEN_COVER,
|
|
|
|
INTENT_CLOSE_COVER)
|
|
|
|
from homeassistant.const import EVENT_COMPONENT_LOADED
|
|
|
|
from homeassistant.core import callback
|
2017-12-29 09:06:39 +00:00
|
|
|
from homeassistant.helpers import config_validation as cv
|
|
|
|
from homeassistant.helpers import intent
|
2018-02-11 17:33:19 +00:00
|
|
|
from homeassistant.loader import bind_hass
|
2018-03-31 00:22:48 +00:00
|
|
|
from homeassistant.setup import (ATTR_COMPONENT)
|
2015-03-10 07:08:50 +00:00
|
|
|
|
2017-12-29 09:06:39 +00:00
|
|
|
_LOGGER = logging.getLogger(__name__)
|
2016-08-22 12:19:19 +00:00
|
|
|
|
|
|
|
ATTR_TEXT = 'text'
|
2017-12-29 09:06:39 +00:00
|
|
|
|
|
|
|
DEPENDENCIES = ['http']
|
2016-08-22 12:19:19 +00:00
|
|
|
DOMAIN = 'conversation'
|
|
|
|
|
|
|
|
REGEX_TURN_COMMAND = re.compile(r'turn (?P<name>(?: |\w)+) (?P<command>\w+)')
|
2017-12-29 09:06:39 +00:00
|
|
|
REGEX_TYPE = type(re.compile(''))
|
2015-03-10 07:08:50 +00:00
|
|
|
|
2018-03-31 00:22:48 +00:00
|
|
|
UTTERANCES = {
|
|
|
|
'cover': {
|
|
|
|
INTENT_OPEN_COVER: ['Open [the] [a] [an] {name}[s]'],
|
|
|
|
INTENT_CLOSE_COVER: ['Close [the] [a] [an] {name}[s]']
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-22 12:19:19 +00:00
|
|
|
SERVICE_PROCESS = 'process'
|
2015-03-10 07:08:50 +00:00
|
|
|
|
2016-04-13 16:48:39 +00:00
|
|
|
SERVICE_PROCESS_SCHEMA = vol.Schema({
|
2017-07-22 04:38:53 +00:00
|
|
|
vol.Required(ATTR_TEXT): cv.string,
|
2016-04-13 16:48:39 +00:00
|
|
|
})
|
|
|
|
|
2017-06-13 06:34:20 +00:00
|
|
|
CONFIG_SCHEMA = vol.Schema({DOMAIN: vol.Schema({
|
2017-07-22 04:38:53 +00:00
|
|
|
vol.Optional('intents'): vol.Schema({
|
|
|
|
cv.string: vol.All(cv.ensure_list, [cv.string])
|
2017-06-13 06:34:20 +00:00
|
|
|
})
|
|
|
|
})}, extra=vol.ALLOW_EXTRA)
|
2016-09-30 02:02:22 +00:00
|
|
|
|
2017-07-22 04:38:53 +00:00
|
|
|
|
|
|
|
@core.callback
|
|
|
|
@bind_hass
|
|
|
|
def async_register(hass, intent_type, utterances):
|
2018-02-11 17:33:19 +00:00
|
|
|
"""Register utterances and any custom intents.
|
2017-07-22 04:38:53 +00:00
|
|
|
|
|
|
|
Registrations don't require conversations to be loaded. They will become
|
|
|
|
active once the conversation component is loaded.
|
|
|
|
"""
|
|
|
|
intents = hass.data.get(DOMAIN)
|
|
|
|
|
|
|
|
if intents is None:
|
|
|
|
intents = hass.data[DOMAIN] = {}
|
|
|
|
|
|
|
|
conf = intents.get(intent_type)
|
|
|
|
|
|
|
|
if conf is None:
|
|
|
|
conf = intents[intent_type] = []
|
|
|
|
|
2017-11-21 04:26:36 +00:00
|
|
|
for utterance in utterances:
|
|
|
|
if isinstance(utterance, REGEX_TYPE):
|
|
|
|
conf.append(utterance)
|
|
|
|
else:
|
2018-08-27 22:20:12 +00:00
|
|
|
conf.append(create_matcher(utterance))
|
2017-07-22 04:38:53 +00:00
|
|
|
|
|
|
|
|
2018-03-01 15:35:12 +00:00
|
|
|
async def async_setup(hass, config):
|
2016-03-08 16:55:57 +00:00
|
|
|
"""Register the process service."""
|
2017-06-13 06:34:20 +00:00
|
|
|
config = config.get(DOMAIN, {})
|
2017-07-22 04:38:53 +00:00
|
|
|
intents = hass.data.get(DOMAIN)
|
2017-06-13 06:34:20 +00:00
|
|
|
|
2017-07-22 04:38:53 +00:00
|
|
|
if intents is None:
|
|
|
|
intents = hass.data[DOMAIN] = {}
|
2015-03-10 07:08:50 +00:00
|
|
|
|
2017-07-22 04:38:53 +00:00
|
|
|
for intent_type, utterances in config.get('intents', {}).items():
|
|
|
|
conf = intents.get(intent_type)
|
|
|
|
|
|
|
|
if conf is None:
|
|
|
|
conf = intents[intent_type] = []
|
|
|
|
|
2018-08-27 22:20:12 +00:00
|
|
|
conf.extend(create_matcher(utterance) for utterance in utterances)
|
2017-07-22 04:38:53 +00:00
|
|
|
|
2018-03-01 15:35:12 +00:00
|
|
|
async def process(service):
|
2016-03-08 16:55:57 +00:00
|
|
|
"""Parse text into commands."""
|
2016-04-13 16:48:39 +00:00
|
|
|
text = service.data[ATTR_TEXT]
|
2018-05-08 17:24:27 +00:00
|
|
|
_LOGGER.debug('Processing: <%s>', text)
|
2018-03-01 15:35:12 +00:00
|
|
|
try:
|
|
|
|
await _process(hass, text)
|
|
|
|
except intent.IntentHandleError as err:
|
|
|
|
_LOGGER.error('Error processing %s: %s', text, err)
|
2017-07-22 04:38:53 +00:00
|
|
|
|
|
|
|
hass.services.async_register(
|
|
|
|
DOMAIN, SERVICE_PROCESS, process, schema=SERVICE_PROCESS_SCHEMA)
|
|
|
|
|
|
|
|
hass.http.register_view(ConversationProcessView)
|
|
|
|
|
2018-03-01 15:35:12 +00:00
|
|
|
# We strip trailing 's' from name because our state matcher will fail
|
|
|
|
# if a letter is not there. By removing 's' we can match singular and
|
|
|
|
# plural names.
|
|
|
|
|
|
|
|
async_register(hass, intent.INTENT_TURN_ON, [
|
|
|
|
'Turn [the] [a] {name}[s] on',
|
|
|
|
'Turn on [the] [a] [an] {name}[s]',
|
|
|
|
])
|
|
|
|
async_register(hass, intent.INTENT_TURN_OFF, [
|
|
|
|
'Turn [the] [a] [an] {name}[s] off',
|
|
|
|
'Turn off [the] [a] [an] {name}[s]',
|
|
|
|
])
|
|
|
|
async_register(hass, intent.INTENT_TOGGLE, [
|
|
|
|
'Toggle [the] [a] [an] {name}[s]',
|
|
|
|
'[the] [a] [an] {name}[s] toggle',
|
|
|
|
])
|
2017-11-21 04:26:36 +00:00
|
|
|
|
2018-03-31 00:22:48 +00:00
|
|
|
@callback
|
|
|
|
def register_utterances(component):
|
|
|
|
"""Register utterances for a component."""
|
|
|
|
if component not in UTTERANCES:
|
|
|
|
return
|
|
|
|
for intent_type, sentences in UTTERANCES[component].items():
|
|
|
|
async_register(hass, intent_type, sentences)
|
|
|
|
|
|
|
|
@callback
|
|
|
|
def component_loaded(event):
|
|
|
|
"""Handle a new component loaded."""
|
|
|
|
register_utterances(event.data[ATTR_COMPONENT])
|
|
|
|
|
|
|
|
hass.bus.async_listen(EVENT_COMPONENT_LOADED, component_loaded)
|
|
|
|
|
|
|
|
# Check already loaded components.
|
|
|
|
for component in hass.config.components:
|
|
|
|
register_utterances(component)
|
|
|
|
|
2017-07-22 04:38:53 +00:00
|
|
|
return True
|
2015-03-10 07:08:50 +00:00
|
|
|
|
|
|
|
|
2018-03-01 15:35:12 +00:00
|
|
|
async def _process(hass, text):
|
2017-07-22 04:38:53 +00:00
|
|
|
"""Process a line of text."""
|
|
|
|
intents = hass.data.get(DOMAIN, {})
|
|
|
|
|
|
|
|
for intent_type, matchers in intents.items():
|
|
|
|
for matcher in matchers:
|
|
|
|
match = matcher.match(text)
|
|
|
|
|
|
|
|
if not match:
|
|
|
|
continue
|
|
|
|
|
2018-03-01 15:35:12 +00:00
|
|
|
response = await hass.helpers.intent.async_handle(
|
2017-11-21 04:26:36 +00:00
|
|
|
DOMAIN, intent_type,
|
2017-07-22 04:38:53 +00:00
|
|
|
{key: {'value': value} for key, value
|
|
|
|
in match.groupdict().items()}, text)
|
|
|
|
return response
|
|
|
|
|
|
|
|
|
|
|
|
class ConversationProcessView(http.HomeAssistantView):
|
|
|
|
"""View to retrieve shopping list content."""
|
|
|
|
|
|
|
|
url = '/api/conversation/process'
|
|
|
|
name = "api:conversation:process"
|
|
|
|
|
2018-02-14 20:06:03 +00:00
|
|
|
@RequestDataValidator(vol.Schema({
|
2017-11-21 04:26:36 +00:00
|
|
|
vol.Required('text'): str,
|
|
|
|
}))
|
2018-03-01 15:35:12 +00:00
|
|
|
async def post(self, request, data):
|
2017-07-22 04:38:53 +00:00
|
|
|
"""Send a request for processing."""
|
|
|
|
hass = request.app['hass']
|
|
|
|
|
2018-03-01 15:35:12 +00:00
|
|
|
try:
|
|
|
|
intent_result = await _process(hass, data['text'])
|
|
|
|
except intent.IntentHandleError as err:
|
|
|
|
intent_result = intent.IntentResponse()
|
|
|
|
intent_result.async_set_speech(str(err))
|
2017-07-22 04:38:53 +00:00
|
|
|
|
2017-07-25 07:42:59 +00:00
|
|
|
if intent_result is None:
|
|
|
|
intent_result = intent.IntentResponse()
|
|
|
|
intent_result.async_set_speech("Sorry, I didn't understand that")
|
|
|
|
|
2017-07-22 04:38:53 +00:00
|
|
|
return self.json(intent_result)
|