This repository has been archived by the owner on Jul 4, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
home_control_google.py
330 lines (291 loc) · 12.4 KB
/
home_control_google.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
import appdaemon.plugins.hass.hassapi as hassapi
import home_control as home_control
class HomeControl(hassapi.Hass):
def initialize(self):
self.handler = self.register_endpoint(self.api_call)
def api_call(self, data):
#self.log(data)
if ("responseId" in data):
self.v2 = True
user_id = data["originalDetectIntentRequest"]["payload"]["user"]["userId"]
else:
#self.v2 = False
#user_id = data["originalRequest"]["data"]["user"]["userId"]
return "", 500
response = ""
code = 200
if not (home_control.authorizeGoogleUse(self, user_id)):
code = 404
else:
intent = self.getIntentName(data)
try:
func = getattr(self, intent)
response = func(data)
except:
response = self.tellText(data, home_control.handleUnknownIntentText(self, intent))
#self.log(response)
return response, code
#########################
######## Intents ########
#########################
def LaunchRequest(self, data):
prompt, reprompt = home_control.handleLaunchText()
response = self.askText(data, prompt, reprompt)
self.setSessionAttribute(data, response, "previous_intent", "LaunchRequest")
return response
def LocatePhoneIntent(self, data):
selectedProfile, prompt, reprompt = home_control.handleLocateProfileText(self, self.getParameterValue(data, "name").lower() , self.isNewConversation(data))
response = self.askText(data, prompt, reprompt)
self.setSessionAttribute(data, response, "previous_intent", "LocatePhoneIntent")
self.setSessionAttribute(data, response, "person_profile", selectedProfile)
return response
def CancelIntent(self, data):
prompt, reprompt = home_control.handleCancelText(self.getSessionAttributes(data))
if (reprompt is None):
response = self.tellText(data, prompt)
else:
response = response = self.askText(data, prompt, reprompt)
self.setSessionAttribute(data, response, "previous_intent", "CancelIntent")
return response
def HelpIntent(self, data):
prompt, reprompt, title, content = home_control.handleHelpCardSsml(True)
response = self.askSSML(data, prompt, reprompt)
self.setSimpleCard(response, title, content)
return response
def NoIntent(self, data):
prompt, reprompt = home_control.handleNoText(self.getSessionAttributes(data))
if (reprompt is None):
response = self.tellText(data, prompt)
else:
response = response = self.askText(data, prompt, reprompt)
self.setSessionAttribute(data, response, "previous_intent", "NoIntent")
return response
def StopIntent(self, data):
prompt, reprompt = home_control.handleStopText(self.getSessionAttributes(data))
if (reprompt is None):
response = self.tellText(data, prompt)
else:
response = response = self.askText(data, prompt, reprompt)
self.setSessionAttribute(data, response, "previous_intent", "StopIntent")
return response
def YesIntent(self, data):
prompt, reprompt, title, context = home_control.handleYesCardText(self, self.getSessionAttributes(data))
if (reprompt is None):
response = self.tellText(data, prompt)
else:
response = self.askText(data, prompt, reprompt)
if not (title is None):
self.setSimpleCard(response, title, context)
self.setSessionAttribute(data, response, "previous_intent", "YesIntent")
return response
def IdentifyIntent(self, data):
prompt, reprompt = home_control.handleIdentifySsml(self.isNewConversation(data))
response = self.askSSML(data, prompt, reprompt)
self.setSessionAttribute(data, response, "previous_intent", "IdentifyIntent")
return response
def SensorsIntent(self, data):
prompt, reprompt = home_control.handleSensorsText(self, self.isNewConversation(data))
response = self.askText(data, prompt, reprompt)
self.setSessionAttribute(data, response, "previous_intent", "SensorsIntent")
return response
def SystemDataIntent(self, data):
prompt, reprompt = home_control.handleSystemDataSsml(self, self.isNewConversation(data))
response = self.askSSML(data, prompt, reprompt)
self.setSessionAttribute(data, response, "previous_intent", "SystemDataIntent")
return response
def NetworkDataIntent(self, data):
prompt, reprompt = home_control.handleNetworkSsml(self, self.isNewConversation(data))
response = self.askSSML(data, prompt, reprompt)
self.setSessionAttribute(data, response, "previous_intent", "NetworkDataIntent")
return response
def StorageDataIntent(self, data):
prompt, reprompt = home_control.handleStorageSsml(self, self.isNewConversation(data))
response = self.askSSML(data, prompt, reprompt)
self.setSessionAttribute(data, response, "previous_intent", "StorageDataIntent")
return response
def DevicesDataIntent(self, data):
prompt, reprompt = home_control.handleDevicesSsml(self, self.isNewConversation(data))
response = self.askSSML(data, prompt, reprompt)
self.setSessionAttribute(data, response, "previous_intent", "DevicesDataIntent")
return response
def SystemOperationsIntent(self, data):
prompt, reprompt = home_control.handleSystemOprationsSsml(self.isNewConversation(data))
response = self.askSSML(data, prompt, reprompt)
self.setSessionAttribute(data, response, "previous_intent", "SystemOperationsIntent")
return response
def BoilerRequestsIntent(self, data):
attributes = self.getSessionAttributes(data)
duration = self.getParameterValue(data, "Duration")
action = self.getParameterValue(data, "Action")
prompt, reprompt, title, context = home_control.handleBoilerCardSsml(self, attributes, action, duration, self.isNewConversation(data), False)
self.log(prompt)
self.log(reprompt)
if (reprompt is None):
response = self.tellSSML(data, prompt)
else:
response = self.askSSML(data, prompt, reprompt)
if not (title is None):
self.setSimpleCard(response, title, context)
self.setSessionAttribute(data, response, "previous_intent", "BoilerRequestsIntent")
return response
#########################
######## Helpers ########
#########################
def getSessionAttributes(self, data):
if ("parameters" not in data["queryResult"]):
return {}
else:
return data["queryResult"]["parameters"]
def setSessionAttribute(self, data, response, key, value):
existing_context = None
for context in response["outputContexts"]:
if (context["name"] == data["session"] + "/contexts/custom_data_context"):
existing_context = context
break
if (existing_context is None):
response["outputContexts"].append(
{
"name": data["session"] + "/contexts/custom_data_context",
"lifespanCount": 10,
"parameters": {
key: value
}
})
else:
existing_context["parameters"][key] = value
response["outputContexts"].append(existing_context)
def setSimpleCard(self, response, title, content):
response["fulfillmentMessages"].append(
{
"card": {
"title": title,
"subtitle": content
}
})
def tellText(self, data, prompt):
response = \
{
"fulfillmentText": "Home Assitant",
"fulfillmentMessages": [
],
"source": "TomerFi Custom",
"payload": {
},
"outputContexts": [
],
"followupEventInput": {
"name": "END_CONVERSATION",
"parameters": {
"end_prompt": prompt
},
"languageCode": "en"
}
}
return response
def askText(self, data, prompt, reprompt):
response = \
{
"fulfillmentText": "Home Assitant",
"fulfillmentMessages": [
{
"platform": "ACTIONS_ON_GOOGLE",
"simple_responses": {
"simple_responses": [
{
"text_to_speech": prompt,
"display_text": prompt
}
]
}
}
],
"source": "TomerFi Custom",
"payload": {
},
"outputContexts": [
],
"followupEventInput": {
}
}
self.setSessionAttribute(data, response, "reprompt", reprompt)
return response
def tellSSML(self, data, prompt):
response = \
{
"fulfillmentText": "Home Assitant",
"fulfillmentMessages": [
],
"source": "TomerFi Custom",
"payload": {
},
"outputContexts": [
],
"followupEventInput": {
"name": "END_CONVERSATION",
"parameters": {
"end_prompt": prompt
},
"languageCode": "en"
}
}
return response
def askSSML(self, data, prompt, reprompt):
response = \
{
"fulfillmentText": "Home Assitant",
"fulfillmentMessages": [
{
"platform": "ACTIONS_ON_GOOGLE",
"simple_responses": {
"simple_responses": [
{
"ssml": prompt,
"display_text": prompt
}
]
}
}
],
"source": "TomerFi Custom",
"payload": {
},
"outputContexts": [
],
"followupEventInput": {
}
}
self.setSessionAttribute(data, response, "reprompt", reprompt)
return response
def isNewConversation(self, data):
if (self.v2):
if ("originalDetectIntentRequest" in data and "payload" in data["originalDetectIntentRequest"] and "conversation" in data["originalDetectIntentRequest"]["payload"] and "type" in data["originalDetectIntentRequest"]["payload"]["conversation"]):
return data["originalDetectIntentRequest"]["payload"]["conversation"]["type"].upper() == "NEW"
else:
return False
else:
if ("originalRequest" in data and "data" in data["originalRequest"] and "conversation" in data["originalRequest"]["data"] and "type" in data["originalRequest"]["data"]["conversation"]):
return data["originalRequest"]["data"]["conversation"]["type"].upper() == "NEW"
else:
return False
def getParameterValue(self, data, name):
if (self.v2):
if ("queryResult" in data and "parameters" in data["queryResult"] and name in data["queryResult"]["parameters"]):
return data["queryResult"]["parameters"][name]
else:
return None
else:
if ("result" in data and "parameters" in data["result"] and name in data["result"]["parameters"]):
return data["result"]["parameters"][name]
else:
return None
def getIntentName(self, data):
if (self.v2):
if ("queryResult" in data and "intent" in data["queryResult"] and "displayName" in data["queryResult"]["intent"]):
return data["queryResult"]["intent"]["displayName"]
else:
return None
else:
if ("result" in data and "metadata" in data["result"] and "intentName" in data["result"]["metadata"]):
return data["result"]["metadata"]["intentName"]
else:
return None