forked from coldfire84/node-red-contrib-alexa-home-skill-v3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
alexa.html
384 lines (356 loc) · 14.2 KB
/
alexa.html
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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
<!--
Copyright 2016 IBM Corp.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<script type="text/x-red" data-template-name="alexa-smart-home-v3-conf">
<div class="form-row">
<label for="node-config-input-username"><i class="icon-tag"></i> Username</label>
<input type="text" id="node-config-input-username">
</div>
<div class="form-row">
<label for="node-config-input-password"><i class="icon-tag"></i> Password</label>
<input type="password" id="node-config-input-password">
</div>
<div class="form-row">
<label for="node-config-input-mqttserver"><i class="icon-tag"></i> MQTT Hostname</label>
<input type="text" id="node-config-input-mqttserver">
</div>
<div class="form-row">
<label for="node-config-input-webapiurl"><i class="icon-tag"></i> Web API Hostname</label>
<input type="text" id="node-config-input-webapiurl">
</div>
</script>
<script type="text/x-red" data-help-name="alexa-smart-home-v3-conf">
<p>If you don't have an account on the bridge create one
<a target="_blank" href="https://red.cb-net.co.uk">here</a>.</p>
</script>
<script type="text/javascript">
// ## Modified to include mqtt / webapi services ??
RED.nodes.registerType('alexa-smart-home-v3-conf',{
category: 'config',
defaults: {
username: {},
mqttserver: {value:"mq-red.cb-net.co.uk"},
webapiurl: {value:"red.cb-net.co.uk"}
},
credentials: {
username: {type:"text"},
password: {type:"password"}
},
color: '#D3D3D3',
label: function() {
return this.username;
},
oneditsave: function(){
var node = this;
var user = $('#node-config-input-username').val();
var pass = $('#node-config-input-password').val();
var mqttserver = $('#node-config-input-mqttserver').val();
// ## Modified, removed https:// + API path
var webapiurl = $('#node-config-input-webapiurl').val();
console.log("pass: ", pass);
if (pass != '_PWD_') {
var account = {
id: node.id,
user: user,
pass: pass,
mqtt: mqttserver,
webapi: webapiurl
}
$.ajax({
data: JSON.stringify(account),
url: 'alexa-smart-home-v3/new-account',
contentType: 'application/json',
type: 'POST',
processData: false
});
}
}
});
</script>
<script type="text/x-red" data-template-name="alexa-smart-home-v3">
<div class="form-row">
<label for="node-input-conf"><i class="icon-tag"></i> Account</label>
<input type="text" id="node-input-conf">
</div>
<div class="form-row">
<label for="node-input-device"><i class="icon-tag"></i> Device</label>
<select id="node-input-device">
</select>
<a id="node-input-device-refresh" class="red-ui-button"><i class="fa fa-refresh"></i></a>
</div>
<div class="form-row">
<label> </label>
<input type="checkbox" id="node-input-acknowledge" style="display: inline-block; width: auto; vertical-align: top;">
<label for="node-input-acknowledge" style="width: 70%"> Auto Acknowledge</span>
</div>
<div class="form-row">
<label for="node-input-topic"><i class="icon-tag"></i> Topic</label>
<input type="text" id="node-input-topic">
</div>
<input type="hidden" id="node-input-name">
</div>
</script>
<script type="text/x-red" data-help-name="alexa-smart-home-v3">
<p>This node provides a Device endpoint for the Alexa/ Google Assistant device bridge.
To use this node you will need to create an account and declare some devices
<a target="_blank" href="https://red.cb-net.co.uk">here</a>.
You will also need to link the skill with your Amazon Echo/ Google account</p>
<p>Instructions can be found
<a target="_blank" href="https://red.cb-net.co.uk/docs">here</a></p>
<p>The <i>Topic</i> field is optional and will be added to any messages sent from
the node.</p>
<p>Auto Acknowledge tells Alexa the command suceeded no matter what. If you untick
this option you will need to add a <i>Alexa Home Response</i> node to the flow, injecting
msg.acknowledge into your flow.</p>
</script>
<script type="text/javascript">
RED.nodes.registerType('alexa-smart-home-v3',{
category: 'alexa',
defaults: {
conf: {value: "", type:"alexa-smart-home-v3-conf"},
device: {value: "" },
acknowledge: {value: true},
name: {},
topic: {}
},
outputs: 1,
inputs: 0,
color: '#D3D3D3',
icon: 'alexa.png',
label: function() {
return this.name || "Alexa Smart Home v3";
},
oneditsave: function() {
var n = $('#node-input-device option:selected').text();
//console.log("selected name " + n);
this.name = n;
$('#node-input-name').val(n);
},
oneditprepare: function(){
var node = this;
//console.log("foo " + node.device);
if (typeof node.acknowledge === 'undefined') {
$('#node-input-acknowledge').prop('checked', true);
}
$('#node-input-device').change(function(){
$('#node-input-name').val($('#node-input-device option:selected').text());
});
var getDevs = function (account){
$.getJSON('alexa-smart-home-v3/devices/' + account, function(data){
$('#node-input-device').find('option').remove().end();
for (d in data) {
console.log("device info:", data[d].endpointId, data[d].friendlyName )
$('<option/>',{
value: parseInt(data[d].endpointId),
text: data[d].friendlyName
}).appendTo('#node-input-device');
}
if (node.device) {
$('#node-input-device').val(node.device);
}
}).fail(function(jqXHR, textStatus, errorThrown){
console.log("problem getting devices");
console.log(textStatus);
});
}
if (node.conf) {
var account = $('#node-input-conf').val();
console.log("account: ", account);
if (account != '_ADD_') {
getDevs(account);
}
}
$('#node-input-conf').change(function(){
var account = $('#node-input-conf').val();
console.log("account changed: ", account);
if (account != '_ADD_') {
getDevs(account);
} else {
console.log("new account");
$('#node-input-device').find('option').remove().end();
$('#node-input-device').val("");
}
});
$('#node-input-device-refresh').click(function(){
$('#node-input-device-refresh').addClass('disabled');
var account = $('#node-input-conf').val();
$.ajax({
url: 'alexa-smart-home-v3/refresh/' + account,
type: 'POST'
}).done(function(data){
setTimeout(function(){
getDevs(account);
$('#node-input-device-refresh').removeClass('disabled');
},3000);
});
});
}
});
</script>
<!-- ############################################################################################## -->
<script type="text/x-red" data-template-name="alexa-smart-home-v3-state">
<div class="form-row">
<label for="node-input-conf"><i class="icon-tag"></i> Account</label>
<input type="text" id="node-input-conf">
</div>
<div class="form-row">
<label for="node-input-device"><i class="icon-tag"></i> Device</label>
<select id="node-input-device">
</select>
<a id="node-input-device-refresh" class="red-ui-button"><i class="fa fa-refresh"></i></a>
</div>
<input type="hidden" id="node-input-name">
</div>
</script>
<script type="text/x-red" data-help-name="alexa-smart-home-v3-state">
<p>This node provides state reporting to the Web App.</p>
<p>Place this node after Alexa Smart Home v3, MQTT out or similar nodes.</p>
<p>Expects specific inbound message format, namely msg.payload.state and msg.acknowledge elements:</p>
<pre>
msg {
acknowledge: true,
payload {
state {
"brightness": 0-100,
"colorBrightness": 0-1,
"colorHue": 0-360,
"colorSaturation": 0-1,
"colorTemperature": 0-10000,
"contact" : "DETECTED" || "NOT_DETECTED",
"input": string,
"lock": "LOCKED" || "UNLOCKED",
"mode" : string,
"motion" : "DETECTED" || "NOT_DETECTED",
"mute" : "ON" || "OFF",
"percentage": number,
"percentageDelta": number,
"playback": playback,
"power": "ON" || "OFF",
"rangeValue" : number,
"rangeValueDelta" : number,
"temperature": number,
"thermostatMode": "HEAT" || "COOL",
"thermostatSetPoint" : number,
"targetSetpointDelta": number,
"volume": number,
"volumeDelta": number
}
}
}
</pre>
<p>Failure to include msg.acknowledge (set to true, not "true") will result in the state update being discarded.</p>
</script>
<script type="text/javascript">
RED.nodes.registerType('alexa-smart-home-v3-state',{
category: 'alexa',
defaults: {
conf: {value: "", type:"alexa-smart-home-v3-conf"},
device: {value: "" },
name: {},
topic: {}
},
outputs: 0,
inputs: 1,
color: '#D3D3D3',
icon: 'alexa.png',
label: function() {
return this.name || "Alexa Smart Home v3 State";
},
oneditsave: function() {
var n = $('#node-input-device option:selected').text();
//console.log("selected name " + n);
this.name = n;
$('#node-input-name').val(n);
},
oneditprepare: function(){
var node = this;
$('#node-input-device').change(function(){
$('#node-input-name').val($('#node-input-device option:selected').text());
});
var getDevs = function (account){
$.getJSON('alexa-smart-home-v3/devices/' + account, function(data){
$('#node-input-device').find('option').remove().end();
for (d in data) {
console.log("device info:", data[d].endpointId, data[d].friendlyName )
$('<option/>',{
value: parseInt(data[d].endpointId),
text: data[d].friendlyName
}).appendTo('#node-input-device');
}
if (node.device) {
$('#node-input-device').val(node.device);
}
}).fail(function(jqXHR, textStatus, errorThrown){
console.log("problem getting devices");
console.log(textStatus);
});
}
if (node.conf) {
var account = $('#node-input-conf').val();
console.log("account: ", account);
if (account != '_ADD_') {
getDevs(account);
}
}
$('#node-input-conf').change(function(){
var account = $('#node-input-conf').val();
console.log("account changed: ", account);
if (account != '_ADD_') {
getDevs(account);
} else {
console.log("new account");
$('#node-input-device').find('option').remove().end();
$('#node-input-device').val("");
}
});
$('#node-input-device-refresh').click(function(){
$('#node-input-device-refresh').addClass('disabled');
var account = $('#node-input-conf').val();
$.ajax({
url: 'alexa-smart-home-v3/refresh/' + account,
type: 'POST'
}).done(function(data){
setTimeout(function(){
getDevs(account);
$('#node-input-device-refresh').removeClass('disabled');
},3000);
});
});
}
});
</script>
<!-- ############################################################################################## -->
<script type="text/x-red" data-template-name="alexa-smart-home-v3-resp">
</script>
<script type="text/x-red" data-help-name="alexa-smart-home-v3-resp">
<p>This node sends a response to Alexa to acknowledge if the command
succeeded or not. To use this node you will need to un check the
<i>Auto Acknowledge</i> check box in the Alexa Home node that starts
the flow. Responses must be sent with in 2 seconds of
receiving the request otherwise a timeout message will be returned
to the Alexa sevice.</p>
<p>The message must come from the Alexa Home input node and expects msg.acknowledge to be either true or false.</p>
</script>
<script type="text/javascript">
RED.nodes.registerType('alexa-smart-home-v3-resp',{
category: 'alexa',
defaults: {
},
inputs: 1,
outputs: 0,
color: '#D3D3D3',
icon: 'alexa.png',
label: function() {
return this.name || "Alexa Smart Home v3 Response";
}
});
</script>