-
Notifications
You must be signed in to change notification settings - Fork 5
/
googlehome.html
299 lines (277 loc) · 9.43 KB
/
googlehome.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
<!--
Copyright 2017-2020 Ben Hardill
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/html" data-template-name="google-home-conf">
<div class="form-row">
<label for="node-config-input-username"><i class="fa fa-user"></i> Username</label>
<input type="text" id="node-config-input-username">
</div>
<div class="form-row">
<label for="node-config-input-password"><i class="fa fa-key"></i> Password</label>
<input type="password" id="node-config-input-password">
</div>
</script>
<script type="text/x-red" data-help-name="google-home-conf">
<p>If you don't have an account on the bridge create one
<a target="_blank" href="https://googlehome.hardill.me.uk/">here</a>.</p>
</script>
<script type="text/javascript">
RED.nodes.registerType('google-home-conf',{
category: 'config',
defaults: {
username: {}
},
credentials: {
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();
//console.log("pass: ", pass);
if (pass != '_PWD_') {
var account = {
id: node.id,
user: user,
pass: pass
}
$.ajax({
data: JSON.stringify(account),
url: 'google-home/new-account',
contentType: 'application/json',
type: 'POST',
processData: false
});
}
}
});
</script>
<script type="text/html" data-template-name="google-home">
<div class="form-row">
<label for="node-input-conf"><i class="fa fa-tag"></i> Account</label>
<input type="text" id="node-input-conf">
</div>
<div class="form-row">
<label for="node-input-device"><i class="fa fa-tag"></i> Device</label>
<select id="node-input-device">
</select>
<a id="node-input-device-refresh" class="btn"><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="fa fa-tag"></i> Topic</label>
<input type="text" id="node-input-topic">
</div>
<div class="form-row">
<label for="node-input-name"><i class="fa fa-tag"></i> Name</label>
<input type="text" id="node-input-name">
</div>
</script>
<script type="text/html" data-help-name="google-home">
<p></p>
<p>When Auto Acknowledge is enabled there is no need for a Google Home Response node,
but all actions will be assumed to have succeeded</p>
</script>
<script type="text/javascript">
function getDevs(account, node) {
$.getJSON('google-home/devices/' + account, function(data){
// Split device response to rooms (in case you have e.g. multiple "Christmass lights", one in "Garden" and one in "Living room"
var rooms = { 'No roomhint assigned': [] };
for (d in data) {
var roomHint = (data[d].roomHint || '').trim();
if (roomHint == '') {
roomHint = 'No roomhint assigned';
}
if (!rooms.hasOwnProperty(roomHint)) {
rooms[roomHint] = [];
}
rooms[roomHint].push(data[d]);
}
// Add all rooms and devices to the list
$('#node-input-device').empty();
Object.keys(rooms).sort().forEach(function(key) {
let group = $('<optgroup label="' + key + '" />');
for (d in rooms[key]) {
group.append($('<option/>',{
value: parseInt(rooms[key][d].id),
text: rooms[key][d].name.name
}));
}
if (rooms[key].length) {
$('#node-input-device').append(group);
}
});
if (node.device) {
$('#node-input-device').val(node.device);
if ($('#node-input-name').text() === '') {
$('#node-input-name').val(node.name);
}
}
})
}
RED.nodes.registerType('google-home',{
category: 'Google_Assistant',
defaults: {
conf: {value: "", type: "google-home-conf"},
device: {value: "", required: true},
acknowledge: {value: true},
name: {value:""},
topic: {}
},
outputs: 1,
inputs: 0,
color: '#D3D3D3',
icon: 'assistant.png',
label: function() {
return this.name || "Google Assistants";
},
// 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;
if (typeof node.acknowledge === 'undefined') {
$('#node-input-acknowledge').prop('checked', true);
}
if (node.conf) {
var account = $('#node-input-conf').val();
//console.log("account: ", account);
if (account != '_ADD_') {
getDevs(account, node);
}
}
$('#node-input-device').change(function(){
var n = $('#node-input-device option:selected').text();
$('#node-input-name').val(n);
});
$('#node-input-conf').change(function(){
var account = $('#node-input-conf').val();
//console.log("account changed: ", account);
if (account != '_ADD_') {
$('#node-input-device-refresh').addClass('disabled');
setTimeout(function(){
getDevs(account, node);
$('#node-input-device-refresh').removeClass('disabled');
},2000);
} else {
$('#node-input-device').empty().val('');
}
});
$('#node-input-device-refresh').click(function(){
$('#node-input-device-refresh').addClass('disabled');
var account = $('#node-input-conf').val();
$.ajax({
url: 'google-home/refresh/' + account,
type: 'POST'
}).done(function(data){
setTimeout(function(){
getDevs(account, node);
$('#node-input-device-refresh').removeClass('disabled');
},1500);
});
});
}
})
</script>
<script type="text/html" data-template-name="google-home-response">
<div class="form-row">
<label for="node-input-conf"><i class="fa fa-tag"></i> Account</label>
<input type="text" id="node-input-conf">
</div>
<div class="form-row">
<label for="node-input-device"><i class="fa fa-tag"></i> Device</label>
<select id="node-input-device">
</select>
<a id="node-input-device-refresh" class="btn"><i class="fa fa-refresh"></i></a>
</div>
<div class="form-row">
<label for="node-input-name"><i class="fa fa-tag"></i> Name</label>
<input type="text" id="node-input-name">
</div>
</script>
<script type="text/html" data-help-name="google-home-response">
<p>Please make sure you pick the same Account as the input node.</p>
</script>
<script type="text/javascript">
RED.nodes.registerType('google-home-response',{
category: 'Google_Assistant',
defaults: {
conf: {required: true, type: "google-home-conf", value:""},
device: {value: ""},
name: {value:""}
},
outputs: 0,
inputs: 1,
color: '#D3D3D3',
icon: 'assistant.png',
label: function() {
return this.name || "Google Assistant Response";
},
// 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;
if (node.conf) {
var account = $('#node-input-conf').val();
//console.log("account: ", account);
if (account != '_ADD_') {
getDevs(account, node);
}
}
$('#node-input-device').change(function(){
var n = $('#node-input-device option:selected').text();
$('#node-input-name').val(n);
});
$('#node-input-conf').change(function(){
var account = $('#node-input-conf').val();
//console.log("account changed: ", account);
if (account != '_ADD_') {
$('#node-input-device-refresh').addClass('disabled');
setTimeout(function(){
getDevs(account, node);
$('#node-input-device-refresh').removeClass('disabled');
},2000);
} else {
$('#node-input-device').empty().val('');
}
});
$('#node-input-device-refresh').click(function(){
$('#node-input-device-refresh').addClass('disabled');
var account = $('#node-input-conf').val();
$.ajax({
url: 'google-home/refresh/' + account,
type: 'POST'
}).done(function(data){
setTimeout(function(){
getDevs(account, node);
$('#node-input-device-refresh').removeClass('disabled');
},1500);
});
});
}
})
</script>