Snips add say and say_actions services (new) (#11596)

* Added snips.say and snips.say_action services

* Added snips.say and snips.say_action services

* Merged services.yaml changes I missed

* added tests for new service configs

* Woof

* Woof Woof

* Changed attribute names to follow hass standards.

* updated test_snips with new attribute names
This commit is contained in:
tschmidty69 2018-01-12 13:19:43 -05:00 committed by Paulus Schoutsen
parent cc236529c4
commit b8e4c2ff69
3 changed files with 158 additions and 1 deletions

View file

@ -8,17 +8,29 @@ import asyncio
import json
import logging
from datetime import timedelta
import voluptuous as vol
from homeassistant.helpers import intent, config_validation as cv
import homeassistant.components.mqtt as mqtt
DOMAIN = 'snips'
DEPENDENCIES = ['mqtt']
CONF_INTENTS = 'intents'
CONF_ACTION = 'action'
SERVICE_SAY = 'say'
SERVICE_SAY_ACTION = 'say_action'
INTENT_TOPIC = 'hermes/intent/#'
ATTR_TEXT = 'text'
ATTR_SITE_ID = 'site_id'
ATTR_CUSTOM_DATA = 'custom_data'
ATTR_CAN_BE_ENQUEUED = 'can_be_enqueued'
ATTR_INTENT_FILTER = 'intent_filter'
_LOGGER = logging.getLogger(__name__)
CONFIG_SCHEMA = vol.Schema({
@ -40,6 +52,20 @@ INTENT_SCHEMA = vol.Schema({
}]
}, extra=vol.ALLOW_EXTRA)
SERVICE_SCHEMA_SAY = vol.Schema({
vol.Required(ATTR_TEXT): str,
vol.Optional(ATTR_SITE_ID, default='default'): str,
vol.Optional(ATTR_CUSTOM_DATA, default=''): str
})
SERVICE_SCHEMA_SAY_ACTION = vol.Schema({
vol.Required(ATTR_TEXT): str,
vol.Optional(ATTR_SITE_ID, default='default'): str,
vol.Optional(ATTR_CUSTOM_DATA, default=''): str,
vol.Optional(ATTR_CAN_BE_ENQUEUED, default=True): cv.boolean,
vol.Optional(ATTR_INTENT_FILTER): vol.All(cv.ensure_list),
})
@asyncio.coroutine
def async_setup(hass, config):
@ -93,6 +119,39 @@ def async_setup(hass, config):
yield from hass.components.mqtt.async_subscribe(
INTENT_TOPIC, message_received)
@asyncio.coroutine
def snips_say(call):
"""Send a Snips notification message."""
notification = {'siteId': call.data.get(ATTR_SITE_ID, 'default'),
'customData': call.data.get(ATTR_CUSTOM_DATA, ''),
'init': {'type': 'notification',
'text': call.data.get(ATTR_TEXT)}}
mqtt.async_publish(hass, 'hermes/dialogueManager/startSession',
json.dumps(notification))
return
@asyncio.coroutine
def snips_say_action(call):
"""Send a Snips action message."""
notification = {'siteId': call.data.get(ATTR_SITE_ID, 'default'),
'customData': call.data.get(ATTR_CUSTOM_DATA, ''),
'init': {'type': 'action',
'text': call.data.get(ATTR_TEXT),
'canBeEnqueued': call.data.get(
ATTR_CAN_BE_ENQUEUED, True),
'intentFilter':
call.data.get(ATTR_INTENT_FILTER, [])}}
mqtt.async_publish(hass, 'hermes/dialogueManager/startSession',
json.dumps(notification))
return
hass.services.async_register(
DOMAIN, SERVICE_SAY, snips_say,
schema=SERVICE_SCHEMA_SAY)
hass.services.async_register(
DOMAIN, SERVICE_SAY_ACTION, snips_say_action,
schema=SERVICE_SCHEMA_SAY_ACTION)
return True