-
Notifications
You must be signed in to change notification settings - Fork 26
/
dingdong.py
177 lines (123 loc) · 4.98 KB
/
dingdong.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
"""
Support for DingDong webhook.
Ver 0.0.1
For more details about this component, please refer to the documentation at
https://home-assistant.io/components/DingDong/
"""
import asyncio
import copy
import logging
import voluptuous as vol
from homeassistant.const import PROJECT_NAME, HTTP_BAD_REQUEST
from homeassistant.helpers import template, script, config_validation as cv
from homeassistant.components.http import HomeAssistantView
_LOGGER = logging.getLogger(__name__)
INTENTS_API_ENDPOINT = '/api/dingdong'
CONF_INTENTS = 'intents'
CONF_SPEECH = 'speech'
CONF_ACTION = 'action'
CONF_ASYNC_ACTION = 'async_action'
DEFAULT_CONF_ASYNC_ACTION = False
DOMAIN = 'dingdong'
DEPENDENCIES = ['http']
CONFIG_SCHEMA = vol.Schema({
DOMAIN: {
CONF_INTENTS: {
cv.string: {
vol.Optional(CONF_SPEECH): cv.template,
vol.Optional(CONF_ACTION): cv.SCRIPT_SCHEMA,
vol.Optional(CONF_ASYNC_ACTION,
default=DEFAULT_CONF_ASYNC_ACTION): cv.boolean
}
}
}
}, extra=vol.ALLOW_EXTRA)
def setup(hass, config):
"""Activate API.AI component."""
intents = config[DOMAIN].get(CONF_INTENTS, {})
hass.http.register_view(DingdongIntentsView(hass, intents))
return True
class DingdongIntentsView(HomeAssistantView):
"""Handle DingDong requests."""
url = INTENTS_API_ENDPOINT
name = 'api:dingdong'
def __init__(self,hass,intents):
super().__init__()
_LOGGER.debug("DingdongIntentsView init ")
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],"DingDong {}".format(name))
self.intents = intents
@asyncio.coroutine
def post(self,request):
"""Handle DingDong"""
data = yield from request.json()
_LOGGER.debug("Received DingDong request: %s", data)
serverREQ = data.get('result')
if serverREQ is None:
_LOGGER.error("Received invalid data from Server: %s", data)
return self.json_message('"Expected result value not received"',HTTP_BAD_REQUEST)
# use intent to no mix HASS actions with this parameter
intent = serverREQ.get('action')
parameters = serverREQ.get('parameters')
if parameters is None:
_LOGGER.error("Received invalid parameters from Server: %s", data)
return self.json_message('"Expected result value not received"', HTTP_BAD_REQUEST)
# contexts = req.get('contexts')
response = DingdongResponse(parameters)
if intent == "":
_LOGGER.warning("Received intent with empty action")
response.add_speech(
"You have not defined an action in your DingDong intent.")
return self.json(response)
config = self.intents.get(intent)
if config is None:
_LOGGER.warning("Received unknown intent %s", intent)
response.add_speech(
"请在家庭助理中配置%s意图" %
intent)
return self.json(response)
speech = config.get(CONF_SPEECH)
action = config.get(CONF_ACTION)
async_action = config.get(CONF_ASYNC_ACTION)
if action is not None:
# DingDong expects a response in less than 5s
_LOGGER.debug("DingDong response.parameters: %s", response.parameters)
if async_action:
# Do not wait for the action to be executed.
# Needed if the action will take longer than 5s to execute
self.hass.async_add_job(action.async_run(response.parameters))
else:
# Wait for the action to be executed so we can use results to
# render the answer
yield from action.async_run(response.parameters)
# pylint: disable=unsubscriptable-object
if speech is not None:
response.add_speech(speech)
return self.json(response)
class DingdongResponse(object):
"""Help generating the response for API.AI."""
def __init__(self, parameters):
"""Initialize the response."""
self.speech = None
self.parameters = {}
# Parameter names replace '.' and '-' for '_'
for key, value in parameters.items():
underscored_key = key.replace('.', '_').replace('-', '_')
self.parameters[underscored_key] = value
def add_speech(self, text):
"""Add speech to the response."""
assert self.speech is None
if isinstance(text, template.Template):
text = text.async_render(self.parameters)
self.speech = text
def as_dict(self):
"""Return response in an API.AI valid dict."""
return {
'speech': self.speech,
'displayText': self.speech,
'source': PROJECT_NAME,
}