homebridge-alexa 0.5.48
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.github/dependabot.yml +12 -0
- package/CHANGELOG.md +99 -0
- package/README.md +883 -0
- package/Roadmap.md +42 -0
- package/Troubleshooting.md +216 -0
- package/V1_README.md +63 -0
- package/alexaDiscovery.json +3938 -0
- package/config.schema.json +261 -0
- package/lib/alexaActions.js +1004 -0
- package/lib/alexaLocal.js +183 -0
- package/lib/alexaMessages.js +705 -0
- package/lib/alexa_smart_home_message_schema.json +5129 -0
- package/lib/parse/Accessory.js +189 -0
- package/lib/parse/Characteristic.js +324 -0
- package/lib/parse/Homebridge.js +225 -0
- package/lib/parse/Homebridges.js +130 -0
- package/lib/parse/Service.js +290 -0
- package/lib/parse/messages.js +1452 -0
- package/package.json +52 -0
- package/plugin.js +189 -0
|
@@ -0,0 +1,1004 @@
|
|
|
1
|
+
var HAPNodeJSClient = require('hap-node-client').HAPNodeJSClient;
|
|
2
|
+
var Homebridges = require('./parse/Homebridges.js').Homebridges;
|
|
3
|
+
var alexaMessages = require('./alexaMessages.js');
|
|
4
|
+
var messages = require('./parse/messages');
|
|
5
|
+
var debug = require('debug')('alexaActions');
|
|
6
|
+
var alexaLocal = require('./alexaLocal.js');
|
|
7
|
+
|
|
8
|
+
var homebridge;
|
|
9
|
+
var hbDevices;
|
|
10
|
+
|
|
11
|
+
module.exports = {
|
|
12
|
+
alexaDiscovery: alexaDiscovery,
|
|
13
|
+
alexaColorTemperatureController: alexaColorTemperatureController,
|
|
14
|
+
alexaPlaybackController: alexaPlaybackController,
|
|
15
|
+
alexaPowerController: alexaPowerController,
|
|
16
|
+
alexaThermostatController: alexaThermostatController,
|
|
17
|
+
alexaColorController: alexaColorController,
|
|
18
|
+
alexaPowerLevelController: alexaPowerLevelController,
|
|
19
|
+
alexaSpeaker: alexaSpeaker,
|
|
20
|
+
alexaStepSpeaker: alexaStepSpeaker,
|
|
21
|
+
alexaMessage: alexaMessage,
|
|
22
|
+
alexaEvent: alexaEvent,
|
|
23
|
+
registerEvents: registerEvents,
|
|
24
|
+
hapDiscovery: hapDiscovery,
|
|
25
|
+
alexaLockController: alexaLockController,
|
|
26
|
+
alexaChannelController: alexaChannelController,
|
|
27
|
+
alexaInputController: alexaInputController,
|
|
28
|
+
alexaRangeController: alexaRangeController,
|
|
29
|
+
alexaModeController: alexaModeController
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
function hapDiscovery(options) {
|
|
33
|
+
homebridge = new HAPNodeJSClient(options);
|
|
34
|
+
|
|
35
|
+
homebridge.on('Ready', function() {
|
|
36
|
+
alexaDiscovery.call(options, null, function() {
|
|
37
|
+
// debug("Events", options);
|
|
38
|
+
if (options.events) {
|
|
39
|
+
|
|
40
|
+
registerEvents(messages.checkEventDeviceList.call(this, hbDevices.toEvents()));
|
|
41
|
+
|
|
42
|
+
}
|
|
43
|
+
});
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
homebridge.on('hapEvent', function(event) {
|
|
47
|
+
// debug("Event Relay - 2", event);
|
|
48
|
+
options.eventBus.emit('hapEvent', event);
|
|
49
|
+
});
|
|
50
|
+
// debug("Event Relay - 1", homebridge);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
function registerEvents(message) {
|
|
54
|
+
debug("registerEvents", message);
|
|
55
|
+
|
|
56
|
+
var HBMessage = [];
|
|
57
|
+
|
|
58
|
+
for (var key in message) {
|
|
59
|
+
// console.log("Key", key, JSON.parse(key));
|
|
60
|
+
var endpoint = JSON.parse(key);
|
|
61
|
+
var device = {
|
|
62
|
+
"aid": endpoint.aid,
|
|
63
|
+
"iid": endpoint.iid,
|
|
64
|
+
"ev": true
|
|
65
|
+
};
|
|
66
|
+
|
|
67
|
+
var x = {
|
|
68
|
+
"deviceID": endpoint.deviceID
|
|
69
|
+
};
|
|
70
|
+
|
|
71
|
+
if (HBMessage[JSON.stringify(x)]) {
|
|
72
|
+
HBMessage[JSON.stringify(x)].characteristics.push(device);
|
|
73
|
+
} else {
|
|
74
|
+
HBMessage[JSON.stringify(x)] = {
|
|
75
|
+
"characteristics": [device]
|
|
76
|
+
};
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
for (var register in HBMessage) {
|
|
80
|
+
// console.log("send", instance, HBMessage[instance]);
|
|
81
|
+
var hbInstance = JSON.parse(register);
|
|
82
|
+
debug("Event Register %s ->", hbInstance.deviceID, HBMessage[register]);
|
|
83
|
+
homebridge.HAPeventByDeviceID(hbInstance.deviceID, JSON.stringify(HBMessage[register]), function(err, status) {
|
|
84
|
+
if (!err) {
|
|
85
|
+
// debug("Registered Event %s:%s ->", hbInstance.host, hbInstance.port, status);
|
|
86
|
+
} else {
|
|
87
|
+
debug("Error: Event Register %s:%s ->", hbInstance.deviceID, err, status);
|
|
88
|
+
}
|
|
89
|
+
});
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
function alexaDiscovery(message, callback) {
|
|
94
|
+
// debug('alexaDiscovery', this);
|
|
95
|
+
homebridge.HAPaccessories(function(endPoints) {
|
|
96
|
+
debug("alexaDiscovery");
|
|
97
|
+
var response;
|
|
98
|
+
|
|
99
|
+
hbDevices = new Homebridges(endPoints, this);
|
|
100
|
+
// debug("RESPONSE", JSON.stringify(hbDevices, null, 2));
|
|
101
|
+
response = hbDevices.toAlexa(this, message);
|
|
102
|
+
|
|
103
|
+
// debug("RESPONSE", JSON.stringify(response, null, 2));
|
|
104
|
+
|
|
105
|
+
response.event.payload.endpoints = checkDeviceList.call(this, response.event.payload.endpoints);
|
|
106
|
+
response.event.payload.endpoints = removeLargeCookieEndpoints.call(this, response.event.payload.endpoints);
|
|
107
|
+
response.event.payload.endpoints = removeDuplicateEndpoints.call(this, response.event.payload.endpoints);
|
|
108
|
+
|
|
109
|
+
var deleteSeen = [];
|
|
110
|
+
|
|
111
|
+
for (var i = 0; i < response.event.payload.endpoints.length; i++) {
|
|
112
|
+
var endpoint = response.event.payload.endpoints[i];
|
|
113
|
+
if (deleteSeen[endpoint.friendlyName]) {
|
|
114
|
+
this.log("WARNING: Duplicate device name", endpoint.friendlyName);
|
|
115
|
+
// response.event.payload.endpoints.splice(i, 1);
|
|
116
|
+
} else {
|
|
117
|
+
deleteSeen[endpoint.friendlyName] = true;
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
if (response && response.event.payload.endpoints.length < 1) {
|
|
122
|
+
this.log("ERROR: HAP Discovery failed, please review config");
|
|
123
|
+
} else {
|
|
124
|
+
if (process.uptime() > 600) { // Only use console during startup
|
|
125
|
+
debug("alexaDiscovery - returned %s devices", response.event.payload.endpoints.length);
|
|
126
|
+
} else {
|
|
127
|
+
this.log("alexaDiscovery - returned %s devices", response.event.payload.endpoints.length);
|
|
128
|
+
}
|
|
129
|
+
if (response.event.payload.endpoints.length > 300) {
|
|
130
|
+
this.log("ERROR: Maximum devices/accessories of 300 exceeded.");
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
// debug("Discovery Response", JSON.stringify(response, null, 2));
|
|
134
|
+
if (this.debug) {
|
|
135
|
+
// const fs = require('fs');
|
|
136
|
+
// fs.writeFileSync('alexaDiscovery.json', JSON.stringify(response, null, 2));
|
|
137
|
+
}
|
|
138
|
+
callback(null, response);
|
|
139
|
+
}.bind(this));
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
// Maximum cookie size is 5K
|
|
143
|
+
|
|
144
|
+
function removeLargeCookieEndpoints(endpoints) {
|
|
145
|
+
var response = [];
|
|
146
|
+
endpoints.forEach((endpoint) => {
|
|
147
|
+
debug("Cookie Object: ", JSON.stringify(endpoint.cookie).length);
|
|
148
|
+
if (JSON.stringify(endpoint.cookie).length < 5000) {
|
|
149
|
+
response.push(endpoint);
|
|
150
|
+
} else {
|
|
151
|
+
console.log("ERROR: Large endpoint Cookie, removing endpointID =>", endpoint.friendlyName);
|
|
152
|
+
}
|
|
153
|
+
});
|
|
154
|
+
|
|
155
|
+
// console.log(response.length);
|
|
156
|
+
// console.log(response);
|
|
157
|
+
return (response);
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
function removeDuplicateEndpoints(endpoints) {
|
|
161
|
+
var deleteSeen = [];
|
|
162
|
+
var response = [];
|
|
163
|
+
endpoints.forEach((endpoint) => {
|
|
164
|
+
if (deleteSeen[endpoint.endpointId]) {
|
|
165
|
+
this.log("ERROR: Parsing failed, removing duplicate endpointID =>", endpoint.friendlyName);
|
|
166
|
+
} else {
|
|
167
|
+
response.push(endpoint);
|
|
168
|
+
}
|
|
169
|
+
deleteSeen[endpoint.endpointId] = true;
|
|
170
|
+
});
|
|
171
|
+
return (response);
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
function checkDeviceList(endpoints) {
|
|
175
|
+
if (this.deviceList && this.deviceList.length > 0 && ['allow', 'deny'].includes(this.deviceListHandling)) {
|
|
176
|
+
this.log(`INFO: DeviceList - The following devices are ${this.deviceListHandling} =>`, this.deviceList);
|
|
177
|
+
var response = [];
|
|
178
|
+
endpoints.forEach((endpoint) => {
|
|
179
|
+
if (this.deviceListHandling === "allow") {
|
|
180
|
+
if (verifyDeviceInList(this.deviceList, endpoint.friendlyName)) {
|
|
181
|
+
response.push(endpoint);
|
|
182
|
+
this.log("INFO: DeviceList - allow =>", endpoint.friendlyName);
|
|
183
|
+
}
|
|
184
|
+
} else if (this.deviceListHandling === "deny") {
|
|
185
|
+
if (verifyDeviceInList(this.deviceList, endpoint.friendlyName)) {
|
|
186
|
+
this.log("INFO: DeviceList - deny =>", endpoint.friendlyName);
|
|
187
|
+
} else {
|
|
188
|
+
response.push(endpoint);
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
});
|
|
192
|
+
return (response);
|
|
193
|
+
} else {
|
|
194
|
+
// this.log("INFO: DeviceList empty feature not enabled or config error in deviceListHandling");
|
|
195
|
+
return endpoints;
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
function verifyDeviceInList(deviceList, deviceName) {
|
|
200
|
+
for (var i = 0, len = deviceList.length; i < len; i++) {
|
|
201
|
+
if (deviceName === deviceList[i] || deviceName.match(new RegExp(deviceList[i]))) return true;
|
|
202
|
+
}
|
|
203
|
+
return false;
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
function alexaColorTemperatureController(message, callback) {
|
|
207
|
+
var action = message.directive.header.name;
|
|
208
|
+
var colorTemperature;
|
|
209
|
+
|
|
210
|
+
try {
|
|
211
|
+
var haAction = JSON.parse(message.directive.endpoint.cookie[action]);
|
|
212
|
+
} catch (e) {
|
|
213
|
+
this.log("alexaColorTemperatureController missing action", action, e.message, message.directive.endpoint.cookie);
|
|
214
|
+
var response = alexaMessages.alexaResponse(message, "", e);
|
|
215
|
+
response.event.payload.type = "INVALID_VALUE"; // The directive contains a value that is not valid for the target endpoint. For example, an invalid heating mode, channel, or program value.
|
|
216
|
+
callback(e, response);
|
|
217
|
+
return;
|
|
218
|
+
}
|
|
219
|
+
switch (action.toLowerCase()) {
|
|
220
|
+
case "decreasecolortemperature":
|
|
221
|
+
case "increasecolortemperature":
|
|
222
|
+
// This characteristic describes color temperature which is represented in the reciprocal megakelvin (MK-1) or mirek scale. MK = 1,000,000 / K where MK is the desired mirek value and K is temperature in Kelvins.
|
|
223
|
+
|
|
224
|
+
homebridge.HAPstatusByDeviceID(haAction.deviceID, "?id=" + haAction.aid + "." + haAction.iid, function(err, status) {
|
|
225
|
+
this.log("ColorTemperatureController-get", action, haAction.deviceID, status, err);
|
|
226
|
+
|
|
227
|
+
var colorTemperatureDelta = 40;
|
|
228
|
+
if (action.toLowerCase() === "decreasecolortemperature") {
|
|
229
|
+
colorTemperatureDelta = -40;
|
|
230
|
+
}
|
|
231
|
+
colorTemperature = status.characteristics[0].value + colorTemperatureDelta > 500 ? 500 : status.characteristics[0].value + colorTemperatureDelta;
|
|
232
|
+
colorTemperature = colorTemperature < 140 ? 140 : colorTemperature;
|
|
233
|
+
var body = {
|
|
234
|
+
"characteristics": [{
|
|
235
|
+
"aid": haAction.aid,
|
|
236
|
+
"iid": haAction.iid,
|
|
237
|
+
"value": colorTemperature
|
|
238
|
+
}]
|
|
239
|
+
};
|
|
240
|
+
homebridge.HAPcontrolByDeviceID(haAction.deviceID, JSON.stringify(body), function(err, status) {
|
|
241
|
+
this.log("ColorTemperatureController-change", action, haAction.deviceID, status, body, err);
|
|
242
|
+
var response = alexaMessages.alexaResponse(message, status, err, _round(1000000 / colorTemperature));
|
|
243
|
+
callback(err, response);
|
|
244
|
+
}.bind(this));
|
|
245
|
+
}.bind(this));
|
|
246
|
+
break;
|
|
247
|
+
case "setcolortemperature":
|
|
248
|
+
// No need to do anything
|
|
249
|
+
colorTemperature = _round(1000000 / message.directive.payload.colorTemperatureInKelvin);
|
|
250
|
+
var body = {
|
|
251
|
+
"characteristics": [{
|
|
252
|
+
"aid": haAction.aid,
|
|
253
|
+
"iid": haAction.iid,
|
|
254
|
+
"value": colorTemperature
|
|
255
|
+
}]
|
|
256
|
+
};
|
|
257
|
+
homebridge.HAPcontrolByDeviceID(haAction.deviceID, JSON.stringify(body), function(err, status) {
|
|
258
|
+
this.log("ColorTemperatureController-set", action, haAction.deviceID, status, body, err);
|
|
259
|
+
var response = alexaMessages.alexaResponse(message, status, err, message.directive.payload.colorTemperatureInKelvin);
|
|
260
|
+
callback(err, response);
|
|
261
|
+
}.bind(this));
|
|
262
|
+
break;
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
function alexaPlaybackController(message, callback) {
|
|
267
|
+
var action = message.directive.header.name;
|
|
268
|
+
try {
|
|
269
|
+
var haAction = JSON.parse(message.directive.endpoint.cookie[action]);
|
|
270
|
+
} catch (e) {
|
|
271
|
+
this.log("alexaPlaybackController missing action", action, e.message, message.directive.endpoint.cookie);
|
|
272
|
+
var response = alexaMessages.alexaResponse(message, "", e);
|
|
273
|
+
response.event.payload.type = "INVALID_VALUE"; // The directive contains a value that is not valid for the target endpoint. For example, an invalid heating mode, channel, or program value.
|
|
274
|
+
callback(e, response);
|
|
275
|
+
return;
|
|
276
|
+
}
|
|
277
|
+
var body = {
|
|
278
|
+
"characteristics": [{
|
|
279
|
+
"aid": haAction.aid,
|
|
280
|
+
"iid": haAction.iid,
|
|
281
|
+
"value": haAction.value
|
|
282
|
+
}]
|
|
283
|
+
};
|
|
284
|
+
homebridge.HAPcontrolByDeviceID(haAction.deviceID, JSON.stringify(body), function(err, status) {
|
|
285
|
+
this.log("PlaybackController", action, haAction.deviceID, status, err);
|
|
286
|
+
var response = alexaMessages.alexaResponse(message, status, err);
|
|
287
|
+
callback(err, response);
|
|
288
|
+
}.bind(this));
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
function alexaInputController(message, callback) {
|
|
292
|
+
var action = message.directive.payload.input;
|
|
293
|
+
try {
|
|
294
|
+
var haAction = JSON.parse(message.directive.endpoint.cookie[action]);
|
|
295
|
+
} catch (e) {
|
|
296
|
+
this.log("alexaInputController missing action", action, e.message, message.directive.endpoint.cookie);
|
|
297
|
+
var response = alexaMessages.alexaResponse(message, "", e);
|
|
298
|
+
response.event.payload.type = "INVALID_VALUE"; // The directive contains a value that is not valid for the target endpoint. For example, an invalid heating mode, channel, or program value.
|
|
299
|
+
callback(e, response);
|
|
300
|
+
return;
|
|
301
|
+
}
|
|
302
|
+
var body = {
|
|
303
|
+
"characteristics": [{
|
|
304
|
+
"aid": haAction.aid,
|
|
305
|
+
"iid": haAction.iid,
|
|
306
|
+
"value": haAction.value
|
|
307
|
+
}]
|
|
308
|
+
};
|
|
309
|
+
homebridge.HAPcontrolByDeviceID(haAction.deviceID, JSON.stringify(body), function(err, status) {
|
|
310
|
+
this.log("InputController", action, haAction.deviceID, status, err);
|
|
311
|
+
var response = alexaMessages.alexaResponse(message, status, err);
|
|
312
|
+
callback(err, response);
|
|
313
|
+
}.bind(this));
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
function alexaPowerController(message, callback) {
|
|
317
|
+
var action = message.directive.header.name;
|
|
318
|
+
try {
|
|
319
|
+
var haAction = JSON.parse(message.directive.endpoint.cookie[action]);
|
|
320
|
+
} catch (e) {
|
|
321
|
+
this.log("alexaPowerController missing action", action, e.message, message.directive.endpoint.cookie);
|
|
322
|
+
var response = alexaMessages.alexaResponse(message, "", e);
|
|
323
|
+
response.event.payload.type = "INVALID_VALUE"; // The directive contains a value that is not valid for the target endpoint. For example, an invalid heating mode, channel, or program value.
|
|
324
|
+
callback(e, response);
|
|
325
|
+
return;
|
|
326
|
+
}
|
|
327
|
+
var body = {
|
|
328
|
+
"characteristics": [{
|
|
329
|
+
"aid": haAction.aid,
|
|
330
|
+
"iid": haAction.iid,
|
|
331
|
+
"value": haAction.value
|
|
332
|
+
}]
|
|
333
|
+
};
|
|
334
|
+
homebridge.HAPcontrolByDeviceID(haAction.deviceID, JSON.stringify(body), function(err, status) {
|
|
335
|
+
this.log("PowerController", action, haAction.deviceID, status, err);
|
|
336
|
+
var response = alexaMessages.alexaResponse(message, status, err);
|
|
337
|
+
callback(err, response);
|
|
338
|
+
}.bind(this));
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
function alexaModeController(message, callback) {
|
|
342
|
+
var action = message.directive.header.name;
|
|
343
|
+
try {
|
|
344
|
+
var haAction = JSON.parse(message.directive.endpoint.cookie[action]);
|
|
345
|
+
} catch (e) {
|
|
346
|
+
this.log("alexaModeController missing action", action, e.message, message.directive.endpoint.cookie);
|
|
347
|
+
var response = alexaMessages.alexaResponse(message, "", e);
|
|
348
|
+
response.event.payload.type = "INVALID_VALUE"; // The directive contains a value that is not valid for the target endpoint. For example, an invalid heating mode, channel, or program value.
|
|
349
|
+
callback(e, response);
|
|
350
|
+
return;
|
|
351
|
+
}
|
|
352
|
+
var value;
|
|
353
|
+
switch (message.directive.payload.mode) {
|
|
354
|
+
case "Position.Down":
|
|
355
|
+
value = 1;
|
|
356
|
+
break;
|
|
357
|
+
case "Position.Up":
|
|
358
|
+
value = 0;
|
|
359
|
+
break;
|
|
360
|
+
default:
|
|
361
|
+
this.log("alexaModeController missing mode", action, message.directive.payload.mode);
|
|
362
|
+
var e = new Error("alexaModeController missing mode " + action + " " + message.directive.payload);
|
|
363
|
+
var response = alexaMessages.alexaResponse(message, "", e);
|
|
364
|
+
response.event.payload.type = "INVALID_VALUE"; // The directive contains a value that is not valid for the target endpoint. For example, an invalid heating mode, channel, or program value.
|
|
365
|
+
callback(e, response);
|
|
366
|
+
return;
|
|
367
|
+
}
|
|
368
|
+
var body = {
|
|
369
|
+
"characteristics": [{
|
|
370
|
+
"aid": haAction.aid,
|
|
371
|
+
"iid": haAction.iid,
|
|
372
|
+
"value": value
|
|
373
|
+
}]
|
|
374
|
+
};
|
|
375
|
+
homebridge.HAPcontrolByDeviceID(haAction.deviceID, JSON.stringify(body), function(err, status) {
|
|
376
|
+
this.log("ModeController", action, haAction.deviceID, status, err);
|
|
377
|
+
var response = alexaMessages.alexaResponse(message, status, err);
|
|
378
|
+
callback(err, response);
|
|
379
|
+
}.bind(this));
|
|
380
|
+
}
|
|
381
|
+
|
|
382
|
+
function alexaLockController(message, callback) {
|
|
383
|
+
var action = message.directive.header.name;
|
|
384
|
+
try {
|
|
385
|
+
var haAction = JSON.parse(message.directive.endpoint.cookie[action]);
|
|
386
|
+
} catch (e) {
|
|
387
|
+
this.log("alexaLockController missing action", action, e.message, message.directive.endpoint.cookie);
|
|
388
|
+
var response = alexaMessages.alexaResponse(message, "", e);
|
|
389
|
+
response.event.payload.type = "INVALID_VALUE"; // The directive contains a value that is not valid for the target endpoint. For example, an invalid heating mode, channel, or program value.
|
|
390
|
+
callback(e, response);
|
|
391
|
+
return;
|
|
392
|
+
}
|
|
393
|
+
var body = {
|
|
394
|
+
"characteristics": [{
|
|
395
|
+
"aid": haAction.aid,
|
|
396
|
+
"iid": haAction.iid,
|
|
397
|
+
"value": haAction.value
|
|
398
|
+
}]
|
|
399
|
+
};
|
|
400
|
+
homebridge.HAPcontrolByDeviceID(haAction.deviceID, JSON.stringify(body), function(err, status) {
|
|
401
|
+
this.log("alexaLockController", action, haAction.deviceID, status, err);
|
|
402
|
+
var response = alexaMessages.alexaResponse(message, status, err);
|
|
403
|
+
callback(err, response);
|
|
404
|
+
}.bind(this));
|
|
405
|
+
}
|
|
406
|
+
|
|
407
|
+
function alexaChannelController(message, callback) {
|
|
408
|
+
// debug("Directive", JSON.stringify(message));
|
|
409
|
+
/* = Version 1.0
|
|
410
|
+
var action = message.directive.header.name;
|
|
411
|
+
try {
|
|
412
|
+
var haAction = JSON.parse(message.directive.endpoint.cookie[action]);
|
|
413
|
+
} catch (e) {
|
|
414
|
+
this.log("alexaChannelController missing action", action, e.message, message.directive.endpoint.cookie);
|
|
415
|
+
var response = alexaMessages.alexaResponse(message, "", e);
|
|
416
|
+
response.event.payload.type = "INVALID_VALUE"; // The directive contains a value that is not valid for the target endpoint. For example, an invalid heating mode, channel, or program value.
|
|
417
|
+
callback(e, response);
|
|
418
|
+
return;
|
|
419
|
+
} */
|
|
420
|
+
|
|
421
|
+
switch (message.directive.header.name) {
|
|
422
|
+
case "ChangeChannel":
|
|
423
|
+
break;
|
|
424
|
+
default:
|
|
425
|
+
this.log("alexaChannelController missing action", message.directive.header.name);
|
|
426
|
+
var response = alexaMessages.alexaResponse(message, "", new Error("alexaChannelController missing action"));
|
|
427
|
+
response.event.payload.type = "INVALID_DIRECTIVE"; // The directive contains a value that is not valid for the target endpoint. For example, an invalid heating mode, channel, or program value.
|
|
428
|
+
callback(new Error("Error:"), response);
|
|
429
|
+
return;
|
|
430
|
+
}
|
|
431
|
+
|
|
432
|
+
var channel = message.directive.header.name;
|
|
433
|
+
if (message.directive.payload.channel.number) {
|
|
434
|
+
channel = message.directive.payload.channel.number;
|
|
435
|
+
} else if (message.directive.payload.channelMetadata.name) {
|
|
436
|
+
channel = message.directive.payload.channelMetadata.name;
|
|
437
|
+
} else {
|
|
438
|
+
var e = new Error("ERROR: alexaChannelController missing channel", message.directive.endpoint.payload);
|
|
439
|
+
this.log(e.message);
|
|
440
|
+
var response = alexaMessages.alexaResponse(message, "", e);
|
|
441
|
+
response.event.payload.type = "INVALID_DIRECTIVE"; // The directive contains a value that is not valid for the target endpoint. For example, an invalid heating mode, channel, or program value.
|
|
442
|
+
callback(e, response);
|
|
443
|
+
return;
|
|
444
|
+
}
|
|
445
|
+
try {
|
|
446
|
+
var haAction = JSON.parse(message.directive.endpoint.cookie[channel.toUpperCase()]);
|
|
447
|
+
} catch (e) {
|
|
448
|
+
this.log("alexaChannelController invalid station", channel, e.message, message.directive.endpoint.cookie);
|
|
449
|
+
var response = alexaMessages.alexaResponse(message, "", e);
|
|
450
|
+
response.event.payload.type = "VALUE_OUT_OF_RANGE"; // The directive contains a value that is not valid for the target endpoint. For example, an invalid heating mode, channel, or program value.
|
|
451
|
+
callback(e, response);
|
|
452
|
+
return;
|
|
453
|
+
}
|
|
454
|
+
|
|
455
|
+
var body = {
|
|
456
|
+
characteristics: [{
|
|
457
|
+
aid: haAction.aid,
|
|
458
|
+
iid: haAction.iid,
|
|
459
|
+
value: haAction.value
|
|
460
|
+
}]
|
|
461
|
+
};
|
|
462
|
+
|
|
463
|
+
// debug("alexaChannelController", JSON.stringify(message), JSON.stringify(body));
|
|
464
|
+
|
|
465
|
+
homebridge.HAPcontrolByDeviceID(haAction.deviceID, JSON.stringify(body), function(err, status) {
|
|
466
|
+
this.log("alexaChannelController", channel, haAction.deviceID, status, err);
|
|
467
|
+
var response = alexaMessages.alexaResponse(message, status, err);
|
|
468
|
+
callback(err, response);
|
|
469
|
+
}.bind(this));
|
|
470
|
+
}
|
|
471
|
+
|
|
472
|
+
function alexaThermostatController(message, callback) {
|
|
473
|
+
// debug("alexaThermostatController", JSON.stringify(message));
|
|
474
|
+
var action = message.directive.header.name;
|
|
475
|
+
var payloads = message.directive.payload;
|
|
476
|
+
// directive.header.name = SetThermostatMode, SetTargetTemperature
|
|
477
|
+
switch (action) {
|
|
478
|
+
case "SetThermostatMode":
|
|
479
|
+
try {
|
|
480
|
+
var mode = message.directive.payload.thermostatMode.value;
|
|
481
|
+
var haAction = JSON.parse(message.directive.endpoint.cookie["thermostatMode" + mode]);
|
|
482
|
+
} catch (e) {
|
|
483
|
+
this.log("alexaThermostatController missing action", "thermostatMode" + mode, e.message, message.directive.endpoint.cookie);
|
|
484
|
+
var response = alexaMessages.alexaResponse(message, "", e);
|
|
485
|
+
response.event.payload.type = "INVALID_VALUE"; // The directive contains a value that is not valid for the target endpoint. For example, an invalid heating mode, channel, or program value.
|
|
486
|
+
callback(e, response);
|
|
487
|
+
return;
|
|
488
|
+
}
|
|
489
|
+
var body = {
|
|
490
|
+
"characteristics": [{
|
|
491
|
+
"aid": haAction.aid,
|
|
492
|
+
"iid": haAction.iid,
|
|
493
|
+
"value": haAction.value
|
|
494
|
+
}]
|
|
495
|
+
};
|
|
496
|
+
homebridge.HAPcontrolByDeviceID(haAction.deviceID, JSON.stringify(body), function(err, status) {
|
|
497
|
+
this.log("SetThermostatMode", mode, haAction.deviceID, status, err);
|
|
498
|
+
var response = alexaMessages.alexaResponse(message, status, err);
|
|
499
|
+
callback(err, response);
|
|
500
|
+
}.bind(this));
|
|
501
|
+
break;
|
|
502
|
+
case "SetTargetTemperature":
|
|
503
|
+
// targetSetpoint, lowerSetpoint, upperSetpoint
|
|
504
|
+
var characteristics = [];
|
|
505
|
+
for (var index in payloads) {
|
|
506
|
+
try {
|
|
507
|
+
var haAction = JSON.parse(message.directive.endpoint.cookie[index]);
|
|
508
|
+
} catch (e) {
|
|
509
|
+
this.log("alexaThermostatController missing action", index, e.message, message.directive.endpoint.cookie);
|
|
510
|
+
var response = alexaMessages.alexaResponse(message, "", e);
|
|
511
|
+
response.event.payload.type = "INVALID_VALUE"; // The directive contains a value that is not valid for the target endpoint. For example, an invalid heating mode, channel, or program value.
|
|
512
|
+
callback(e, response);
|
|
513
|
+
return;
|
|
514
|
+
}
|
|
515
|
+
var value = payloads[index].value;
|
|
516
|
+
if (payloads[index].scale === "FAHRENHEIT") {
|
|
517
|
+
value = (value - 32) * 5 / 9;
|
|
518
|
+
}
|
|
519
|
+
characteristics.push({
|
|
520
|
+
"aid": haAction.aid,
|
|
521
|
+
"iid": haAction.iid,
|
|
522
|
+
"value": value
|
|
523
|
+
});
|
|
524
|
+
}
|
|
525
|
+
|
|
526
|
+
var body = {
|
|
527
|
+
"characteristics": characteristics
|
|
528
|
+
};
|
|
529
|
+
homebridge.HAPcontrolByDeviceID(haAction.deviceID, JSON.stringify(body), function(err, status) {
|
|
530
|
+
this.log("ThermostatController", action, haAction.deviceID, status, err);
|
|
531
|
+
var response = alexaMessages.alexaResponse(message, status, err);
|
|
532
|
+
callback(err, response);
|
|
533
|
+
}.bind(this));
|
|
534
|
+
break;
|
|
535
|
+
// case "AdjustTargetTemperature":
|
|
536
|
+
// Future expansion
|
|
537
|
+
default:
|
|
538
|
+
var err = {
|
|
539
|
+
message: "Unknown action" + action
|
|
540
|
+
};
|
|
541
|
+
var response = alexaMessages.alexaResponse(message, "", err);
|
|
542
|
+
response.event.payload.type = "INVALID_VALUE"; // The directive contains a value that is not valid for the target endpoint. For example, an invalid heating mode, channel, or program value.
|
|
543
|
+
callback(err, response);
|
|
544
|
+
}
|
|
545
|
+
}
|
|
546
|
+
|
|
547
|
+
function alexaColorController(message, callback) {
|
|
548
|
+
var action = message.directive.header.name;
|
|
549
|
+
try {
|
|
550
|
+
var haAction = JSON.parse(message.directive.endpoint.cookie[action]);
|
|
551
|
+
} catch (e) {
|
|
552
|
+
this.log("alexaColorController missing action", action, e.message, message.directive.endpoint.cookie);
|
|
553
|
+
var response = alexaMessages.alexaResponse(message, "", e);
|
|
554
|
+
response.event.payload.type = "INVALID_VALUE"; // The directive contains a value that is not valid for the target endpoint. For example, an invalid heating mode, channel, or program value.
|
|
555
|
+
callback(e, response);
|
|
556
|
+
return;
|
|
557
|
+
}
|
|
558
|
+
debug("action", haAction, message.directive.payload);
|
|
559
|
+
var body = {
|
|
560
|
+
"characteristics": [{
|
|
561
|
+
"aid": haAction.on.aid,
|
|
562
|
+
"iid": haAction.on.iid,
|
|
563
|
+
"value": 1
|
|
564
|
+
}, {
|
|
565
|
+
"aid": haAction.hue.aid,
|
|
566
|
+
"iid": haAction.hue.iid,
|
|
567
|
+
"value": message.directive.payload.color.hue
|
|
568
|
+
}, {
|
|
569
|
+
"aid": haAction.saturation.aid,
|
|
570
|
+
"iid": haAction.saturation.iid,
|
|
571
|
+
"value": message.directive.payload.color.saturation * 100
|
|
572
|
+
}]
|
|
573
|
+
};
|
|
574
|
+
debug("color HB command", body);
|
|
575
|
+
homebridge.HAPcontrolByDeviceID(haAction.deviceID, JSON.stringify(body), function(err, status) {
|
|
576
|
+
this.log("ColorController", action, haAction.deviceID, status, err);
|
|
577
|
+
var response = alexaMessages.alexaResponse(message, status, err);
|
|
578
|
+
callback(err, response);
|
|
579
|
+
}.bind(this));
|
|
580
|
+
}
|
|
581
|
+
|
|
582
|
+
function alexaRangeController(message, callback) {
|
|
583
|
+
// debug(JSON.stringify(message, null, 2));
|
|
584
|
+
var action = message.directive.header.name;
|
|
585
|
+
var rangeLevel, haAction;
|
|
586
|
+
try {
|
|
587
|
+
haAction = JSON.parse(message.directive.endpoint.cookie[action]);
|
|
588
|
+
} catch (e) {
|
|
589
|
+
this.log("alexaRangeController missing action", action, e.message, message.directive.endpoint.cookie);
|
|
590
|
+
var response = alexaMessages.alexaResponse(message, "", e);
|
|
591
|
+
response.event.payload.type = "INVALID_VALUE"; // The directive contains a value that is not valid for the target endpoint. For example, an invalid heating mode, channel, or program value.
|
|
592
|
+
callback(e, response);
|
|
593
|
+
return;
|
|
594
|
+
}
|
|
595
|
+
|
|
596
|
+
switch (action.toLowerCase()) {
|
|
597
|
+
case "adjustrangevalue":
|
|
598
|
+
// Need to get current value prior to dimming
|
|
599
|
+
homebridge.HAPstatusByDeviceID(haAction.deviceID, "?id=" + haAction.aid + "." + haAction.iid, function(err, status) {
|
|
600
|
+
this.log("RangeController-get", action, haAction.deviceID, status, err);
|
|
601
|
+
|
|
602
|
+
var rangeValueDelta = message.directive.payload.rangeValueDelta;
|
|
603
|
+
rangeLevel = status.characteristics[0].value + rangeValueDelta > 100 ? 100 : status.characteristics[0].value + rangeValueDelta;
|
|
604
|
+
rangeLevel = rangeLevel < 0 ? 0 : rangeLevel;
|
|
605
|
+
rangeLevel = (rangeLevel >= 0 && rangeLevel <= 100) ? rangeLevel : 0;
|
|
606
|
+
var body = {
|
|
607
|
+
"characteristics": [{
|
|
608
|
+
"aid": haAction.aid,
|
|
609
|
+
"iid": haAction.iid,
|
|
610
|
+
"value": rangeLevel
|
|
611
|
+
}]
|
|
612
|
+
};
|
|
613
|
+
homebridge.HAPcontrolByDeviceID(haAction.deviceID, JSON.stringify(body), function(err, status) {
|
|
614
|
+
this.log("RangeController-set", action, haAction.deviceID, status, body, err);
|
|
615
|
+
var response = alexaMessages.alexaResponse(message, status, err, rangeLevel);
|
|
616
|
+
callback(err, response);
|
|
617
|
+
}.bind(this));
|
|
618
|
+
}.bind(this));
|
|
619
|
+
break;
|
|
620
|
+
case "setrangevalue":
|
|
621
|
+
// No need to do anything
|
|
622
|
+
var rangeValue = message.directive.payload.rangeValue;
|
|
623
|
+
var body = {
|
|
624
|
+
"characteristics": [{
|
|
625
|
+
"aid": haAction.aid,
|
|
626
|
+
"iid": haAction.iid,
|
|
627
|
+
"value": rangeValue
|
|
628
|
+
}]
|
|
629
|
+
};
|
|
630
|
+
homebridge.HAPcontrolByDeviceID(haAction.deviceID, JSON.stringify(body), function(err, status) {
|
|
631
|
+
this.log("RangeController", action, haAction.deviceID, status, body, err);
|
|
632
|
+
var response = alexaMessages.alexaResponse(message, status, err, rangeValue);
|
|
633
|
+
callback(err, response);
|
|
634
|
+
}.bind(this));
|
|
635
|
+
break;
|
|
636
|
+
default:
|
|
637
|
+
var e = new Error("ERROR: alexaRangeController missing " + action);
|
|
638
|
+
this.log(e.message, message.directive.endpoint.cookie);
|
|
639
|
+
var response = alexaMessages.alexaResponse(message, "", e);
|
|
640
|
+
response.event.payload.type = "INVALID_VALUE"; // The directive contains a value that is not valid for the target endpoint. For example, an invalid heating mode, channel, or program value.
|
|
641
|
+
callback(e, response);
|
|
642
|
+
}
|
|
643
|
+
}
|
|
644
|
+
|
|
645
|
+
function alexaPowerLevelController(message, callback) {
|
|
646
|
+
// debug(JSON.stringify(message, null, 2));
|
|
647
|
+
var action = message.directive.header.name;
|
|
648
|
+
var powerLevel, haAction, turnOn;
|
|
649
|
+
try {
|
|
650
|
+
haAction = JSON.parse(message.directive.endpoint.cookie[action]);
|
|
651
|
+
// Issue a seperate turn on for brightness only ( workaround for hue concern )
|
|
652
|
+
if (message.directive.endpoint.cookie["BrightnessTurnOn"] && message.directive.endpoint.cookie["TurnOn"]) {
|
|
653
|
+
turnOn = JSON.parse(message.directive.endpoint.cookie["TurnOn"]);
|
|
654
|
+
}
|
|
655
|
+
} catch (e) {
|
|
656
|
+
this.log("alexaPowerLevelController missing action", action, e.message, message.directive.endpoint.cookie);
|
|
657
|
+
var response = alexaMessages.alexaResponse(message, "", e);
|
|
658
|
+
response.event.payload.type = "INVALID_VALUE"; // The directive contains a value that is not valid for the target endpoint. For example, an invalid heating mode, channel, or program value.
|
|
659
|
+
callback(e, response);
|
|
660
|
+
return;
|
|
661
|
+
}
|
|
662
|
+
|
|
663
|
+
switch (action.toLowerCase()) {
|
|
664
|
+
case "adjustpowerlevel":
|
|
665
|
+
// Need to get current value prior to dimming
|
|
666
|
+
homebridge.HAPstatusByDeviceID(haAction.deviceID, "?id=" + haAction.aid + "." + haAction.iid, function(err, status) {
|
|
667
|
+
this.log("PowerLevelController-get", action, haAction.deviceID, status, err);
|
|
668
|
+
|
|
669
|
+
var powerLevelDelta = message.directive.payload.powerLevelDelta;
|
|
670
|
+
powerLevel = status.characteristics[0].value + powerLevelDelta > 100 ? 100 : status.characteristics[0].value + powerLevelDelta;
|
|
671
|
+
powerLevel = powerLevel < 0 ? 0 : powerLevel;
|
|
672
|
+
var body = {
|
|
673
|
+
"characteristics": [{
|
|
674
|
+
"aid": haAction.aid,
|
|
675
|
+
"iid": haAction.iid,
|
|
676
|
+
"value": powerLevel
|
|
677
|
+
}]
|
|
678
|
+
};
|
|
679
|
+
if (turnOn) {
|
|
680
|
+
body.characteristics.push({
|
|
681
|
+
"aid": turnOn.aid,
|
|
682
|
+
"iid": turnOn.iid,
|
|
683
|
+
"value": turnOn.value
|
|
684
|
+
});
|
|
685
|
+
}
|
|
686
|
+
homebridge.HAPcontrolByDeviceID(haAction.deviceID, JSON.stringify(body), function(err, status) {
|
|
687
|
+
this.log("PowerLevelController-set", action, haAction.deviceID, status, body, err);
|
|
688
|
+
var response = alexaMessages.alexaResponse(message, status, err, powerLevel);
|
|
689
|
+
callback(err, response);
|
|
690
|
+
}.bind(this));
|
|
691
|
+
}.bind(this));
|
|
692
|
+
break;
|
|
693
|
+
case "setpowerlevel":
|
|
694
|
+
// No need to do anything
|
|
695
|
+
powerLevel = message.directive.payload.powerLevel;
|
|
696
|
+
var body = {
|
|
697
|
+
"characteristics": [{
|
|
698
|
+
"aid": haAction.aid,
|
|
699
|
+
"iid": haAction.iid,
|
|
700
|
+
"value": powerLevel
|
|
701
|
+
}]
|
|
702
|
+
};
|
|
703
|
+
if (turnOn) {
|
|
704
|
+
body.characteristics.push({
|
|
705
|
+
"aid": turnOn.aid,
|
|
706
|
+
"iid": turnOn.iid,
|
|
707
|
+
"value": turnOn.value
|
|
708
|
+
});
|
|
709
|
+
}
|
|
710
|
+
homebridge.HAPcontrolByDeviceID(haAction.deviceID, JSON.stringify(body), function(err, status) {
|
|
711
|
+
this.log("PowerLevelController", action, haAction.deviceID, status, body, err);
|
|
712
|
+
var response = alexaMessages.alexaResponse(message, status, err, powerLevel);
|
|
713
|
+
callback(err, response);
|
|
714
|
+
}.bind(this));
|
|
715
|
+
break;
|
|
716
|
+
}
|
|
717
|
+
}
|
|
718
|
+
|
|
719
|
+
function alexaSpeaker(message, callback) {
|
|
720
|
+
// debug(JSON.stringify(message, null, 2));
|
|
721
|
+
var action = message.directive.header.name;
|
|
722
|
+
var volume, haAction;
|
|
723
|
+
try {
|
|
724
|
+
haAction = JSON.parse(message.directive.endpoint.cookie[action]);
|
|
725
|
+
} catch (e) {
|
|
726
|
+
this.log("alexaSpeaker missing action", action, e.message, message.directive.endpoint.cookie);
|
|
727
|
+
var response = alexaMessages.alexaResponse(message, "", e);
|
|
728
|
+
response.event.payload.type = "INVALID_VALUE"; // The directive contains a value that is not valid for the target endpoint. For example, an invalid heating mode, channel, or program value.
|
|
729
|
+
callback(e, response);
|
|
730
|
+
return;
|
|
731
|
+
}
|
|
732
|
+
|
|
733
|
+
switch (action.toLowerCase()) {
|
|
734
|
+
case "adjustvolume":
|
|
735
|
+
// Need to get current value prior to dimming
|
|
736
|
+
homebridge.HAPstatusByDeviceID(haAction.deviceID, "?id=" + haAction.aid + "." + haAction.iid, function(err, status) {
|
|
737
|
+
this.log("Speaker-get", action, haAction.deviceID, status, err);
|
|
738
|
+
|
|
739
|
+
if (!err) {
|
|
740
|
+
var volumeDelta = message.directive.payload.volume;
|
|
741
|
+
volume = status.characteristics[0].value + volumeDelta > 100 ? 100 : status.characteristics[0].value + volumeDelta;
|
|
742
|
+
volume = volume < 0 ? 0 : volume;
|
|
743
|
+
var body = {
|
|
744
|
+
"characteristics": [{
|
|
745
|
+
"aid": haAction.aid,
|
|
746
|
+
"iid": haAction.iid,
|
|
747
|
+
"value": volume
|
|
748
|
+
}]
|
|
749
|
+
};
|
|
750
|
+
homebridge.HAPcontrolByDeviceID(haAction.deviceID, JSON.stringify(body), function(err, status) {
|
|
751
|
+
this.log("Speaker-set", action, haAction.deviceID, status, body, err);
|
|
752
|
+
var response = alexaMessages.alexaResponse(message, status, err, volume);
|
|
753
|
+
callback(err, response);
|
|
754
|
+
return;
|
|
755
|
+
}.bind(this));
|
|
756
|
+
} else {
|
|
757
|
+
callback(err);
|
|
758
|
+
}
|
|
759
|
+
}.bind(this));
|
|
760
|
+
break;
|
|
761
|
+
case "setvolume":
|
|
762
|
+
// No need to do anything
|
|
763
|
+
volume = message.directive.payload.volume;
|
|
764
|
+
var body = {
|
|
765
|
+
"characteristics": [{
|
|
766
|
+
"aid": haAction.aid,
|
|
767
|
+
"iid": haAction.iid,
|
|
768
|
+
"value": volume
|
|
769
|
+
}]
|
|
770
|
+
};
|
|
771
|
+
homebridge.HAPcontrolByDeviceID(haAction.deviceID, JSON.stringify(body), function(err, status) {
|
|
772
|
+
this.log("Speaker", action, haAction.deviceID, status, body, err);
|
|
773
|
+
var response = alexaMessages.alexaResponse(message, status, err, volume);
|
|
774
|
+
callback(err, response);
|
|
775
|
+
}.bind(this));
|
|
776
|
+
break;
|
|
777
|
+
}
|
|
778
|
+
}
|
|
779
|
+
|
|
780
|
+
function alexaStepSpeaker(message, callback) {
|
|
781
|
+
// debug(JSON.stringify(message, null, 2));
|
|
782
|
+
var action = message.directive.header.name;
|
|
783
|
+
var volume, haAction;
|
|
784
|
+
try {
|
|
785
|
+
haAction = JSON.parse(message.directive.endpoint.cookie[action]);
|
|
786
|
+
} catch (e) {
|
|
787
|
+
this.log("alexaStepSpeaker missing action", action, e.message, message.directive.endpoint.cookie);
|
|
788
|
+
var response = alexaMessages.alexaResponse(message, "", e);
|
|
789
|
+
response.event.payload.type = "INVALID_VALUE"; // The directive contains a value that is not valid for the target endpoint. For example, an invalid heating mode, channel, or program value.
|
|
790
|
+
callback(e, response);
|
|
791
|
+
return;
|
|
792
|
+
}
|
|
793
|
+
|
|
794
|
+
switch (action.toLowerCase()) {
|
|
795
|
+
case "adjustvolume":
|
|
796
|
+
// Characteristic.VolumeSelector.INCREMENT = 0;
|
|
797
|
+
// Characteristic.VolumeSelector.DECREMENT = 1;
|
|
798
|
+
volume = message.directive.payload.volumeSteps;
|
|
799
|
+
var value = (volume > 0 ? 0 : 1);
|
|
800
|
+
var body = {
|
|
801
|
+
"characteristics": [{
|
|
802
|
+
"aid": haAction.aid,
|
|
803
|
+
"iid": haAction.iid,
|
|
804
|
+
"value": value
|
|
805
|
+
}]
|
|
806
|
+
};
|
|
807
|
+
homebridge.HAPcontrolByDeviceID(haAction.deviceID, JSON.stringify(body), function(err, status) {
|
|
808
|
+
this.log("StepSpeaker", action, haAction.deviceID, status, body, err);
|
|
809
|
+
var response = alexaMessages.alexaResponse(message, status, err, volume);
|
|
810
|
+
callback(err, response);
|
|
811
|
+
}.bind(this));
|
|
812
|
+
break;
|
|
813
|
+
}
|
|
814
|
+
}
|
|
815
|
+
|
|
816
|
+
function alexaMessage(message, callback) {
|
|
817
|
+
debug("alexaMessage:", JSON.stringify(message));
|
|
818
|
+
switch (message.directive.header.name.toLowerCase()) {
|
|
819
|
+
case "reportstate": // aka getStatus
|
|
820
|
+
var action = message.directive.header.name;
|
|
821
|
+
try {
|
|
822
|
+
var reportState = JSON.parse(message.directive.endpoint.cookie[action]);
|
|
823
|
+
} catch (e) {
|
|
824
|
+
this.log("alexaMessage missing action", action, e.message, message.directive.endpoint.cookie);
|
|
825
|
+
var response = alexaMessages.alexaResponse(message, "", e);
|
|
826
|
+
response.event.payload.type = "INVALID_VALUE"; // The directive contains a value that is not valid for the target endpoint. For example, an invalid heating mode, channel, or program value.
|
|
827
|
+
callback(e, response);
|
|
828
|
+
return;
|
|
829
|
+
}
|
|
830
|
+
|
|
831
|
+
var statusArray = [];
|
|
832
|
+
reportState.forEach(function(element) {
|
|
833
|
+
if (!statusArray[element.deviceID]) {
|
|
834
|
+
statusArray[element.deviceID] = {};
|
|
835
|
+
statusArray[element.deviceID].body = "?id=";
|
|
836
|
+
statusArray[element.deviceID].interface = "";
|
|
837
|
+
statusArray[element.deviceID].deviceID = element.deviceID;
|
|
838
|
+
statusArray[element.deviceID].spacer = "";
|
|
839
|
+
statusArray[element.deviceID].elements = [];
|
|
840
|
+
}
|
|
841
|
+
switch (element.interface) {
|
|
842
|
+
case "Alexa.ColorController":
|
|
843
|
+
statusArray[element.deviceID].body = statusArray[element.deviceID].body + statusArray[element.deviceID].spacer + element.hue.aid + "." + element.hue.iid;
|
|
844
|
+
statusArray[element.deviceID].interface = statusArray[element.deviceID].interface + statusArray[element.deviceID].spacer + element.interface;
|
|
845
|
+
statusArray[element.deviceID].spacer = ",";
|
|
846
|
+
statusArray[element.deviceID].body = statusArray[element.deviceID].body + statusArray[element.deviceID].spacer + element.saturation.aid + "." + element.saturation.iid;
|
|
847
|
+
// statusArray[element.deviceID].body = statusArray[element.deviceID].body + statusArray[element.deviceID].spacer + element.brightness.aid + "." + element.brightness.iid; Fix for #446
|
|
848
|
+
statusArray[element.deviceID].elements.push({
|
|
849
|
+
interface: element.interface,
|
|
850
|
+
hue: {
|
|
851
|
+
aid: element.hue.aid,
|
|
852
|
+
iid: element.hue.iid
|
|
853
|
+
},
|
|
854
|
+
saturation: {
|
|
855
|
+
aid: element.saturation.aid,
|
|
856
|
+
iid: element.saturation.iid
|
|
857
|
+
},
|
|
858
|
+
brightness: {
|
|
859
|
+
aid: element.brightness.aid,
|
|
860
|
+
iid: element.brightness.iid
|
|
861
|
+
}
|
|
862
|
+
});
|
|
863
|
+
break;
|
|
864
|
+
// case "Alexa.ThermostatControllerthermostatMode":
|
|
865
|
+
// case "Alexa.ThermostatControllertargetSetpoint":
|
|
866
|
+
default:
|
|
867
|
+
statusArray[element.deviceID].body = statusArray[element.deviceID].body + statusArray[element.deviceID].spacer + element.aid + "." + element.iid;
|
|
868
|
+
statusArray[element.deviceID].interface = statusArray[element.deviceID].interface + statusArray[element.deviceID].spacer + element.interface;
|
|
869
|
+
statusArray[element.deviceID].spacer = ",";
|
|
870
|
+
statusArray[element.deviceID].elements.push({
|
|
871
|
+
interface: element.interface,
|
|
872
|
+
aid: element.aid,
|
|
873
|
+
iid: element.iid
|
|
874
|
+
});
|
|
875
|
+
}
|
|
876
|
+
});
|
|
877
|
+
|
|
878
|
+
// For performance HAP GET Characteristices supports getting multiple in one call
|
|
879
|
+
// debug("alexaMessage - statusArray", statusArray);
|
|
880
|
+
|
|
881
|
+
processStatusArray(statusArray, message).then(response => {
|
|
882
|
+
debug("alexaMessage: Response", JSON.stringify(response,
|
|
883
|
+
null, 2));
|
|
884
|
+
callback(null, response);
|
|
885
|
+
}).catch(err => {
|
|
886
|
+
debug("alexaMessage:Error:", err.message);
|
|
887
|
+
callback(alexaMessages.alexaStateResponse(err, message));
|
|
888
|
+
});
|
|
889
|
+
|
|
890
|
+
/*
|
|
891
|
+
homebridge.HAPstatusByDeviceID(deviceID, body, function(err, status) {
|
|
892
|
+
debug("reportState", action, deviceID, status, err);
|
|
893
|
+
var response = alexaMessages.alexaStateResponse(message, reportState, status, err);
|
|
894
|
+
debug("reportState", JSON.stringify(response));
|
|
895
|
+
callback(err, response);
|
|
896
|
+
});
|
|
897
|
+
*/
|
|
898
|
+
break;
|
|
899
|
+
|
|
900
|
+
default:
|
|
901
|
+
this.log("Unhandled alexaMessage Directive", message.directive.header.name);
|
|
902
|
+
var response = {
|
|
903
|
+
"event": {
|
|
904
|
+
"header": {
|
|
905
|
+
"name": "ErrorResponse",
|
|
906
|
+
"namespace": "Alexa",
|
|
907
|
+
"payloadVersion": "3",
|
|
908
|
+
"messageId": message.directive.header.messageId
|
|
909
|
+
},
|
|
910
|
+
"payload": {
|
|
911
|
+
"endpoints": []
|
|
912
|
+
}
|
|
913
|
+
}
|
|
914
|
+
};
|
|
915
|
+
callback(new Error("Unhandled alexaMessage Directive"), response);
|
|
916
|
+
}
|
|
917
|
+
}
|
|
918
|
+
|
|
919
|
+
async function processStatusArray(statusArray, message) {
|
|
920
|
+
try {
|
|
921
|
+
var messageArray = [];
|
|
922
|
+
// debug("processStatusArray-1", statusArray);
|
|
923
|
+
for (let item in statusArray) {
|
|
924
|
+
// debug("processStatusArray-item", statusArray[item]);
|
|
925
|
+
messageArray.push(_HAPstatusByDeviceID(statusArray[item], message));
|
|
926
|
+
}
|
|
927
|
+
|
|
928
|
+
// debug("processStatusArray", messageArray);
|
|
929
|
+
var resultArray = await Promise.all(messageArray);
|
|
930
|
+
|
|
931
|
+
return (alexaMessages.alexaStateResponse(resultArray, message));
|
|
932
|
+
} catch (err) {
|
|
933
|
+
return (alexaMessages.alexaStateResponse(err, message));
|
|
934
|
+
}
|
|
935
|
+
}
|
|
936
|
+
|
|
937
|
+
function _HAPstatusByDeviceID(statusObject, message) {
|
|
938
|
+
debug("_HAPstatusByDeviceID-1", statusObject);
|
|
939
|
+
return new Promise((resolve, reject) => {
|
|
940
|
+
homebridge.HAPstatusByDeviceID(statusObject.deviceID, statusObject.body, function(err, status) {
|
|
941
|
+
if (err) {
|
|
942
|
+
// debug("Error: _HAPstatusByDeviceID", err);
|
|
943
|
+
reject(err);
|
|
944
|
+
} else {
|
|
945
|
+
debug("_HAPstatusByDeviceID-2", statusObject.deviceID, status);
|
|
946
|
+
resolve(messages.stateToProperties(statusObject, status.characteristics));
|
|
947
|
+
}
|
|
948
|
+
});
|
|
949
|
+
});
|
|
950
|
+
}
|
|
951
|
+
|
|
952
|
+
function alexaEvent(events) {
|
|
953
|
+
debug("Events", JSON.stringify(events));
|
|
954
|
+
// create alexa message
|
|
955
|
+
events.forEach(function(event) {
|
|
956
|
+
// debug('alexaEvent - event', event);
|
|
957
|
+
var x = {
|
|
958
|
+
'deviceID': event.deviceID,
|
|
959
|
+
'aid': event.aid,
|
|
960
|
+
'iid': event.iid
|
|
961
|
+
};
|
|
962
|
+
var device;
|
|
963
|
+
|
|
964
|
+
// Partial fix for #441
|
|
965
|
+
switch (event.status) {
|
|
966
|
+
case true:
|
|
967
|
+
case false:
|
|
968
|
+
case 1:
|
|
969
|
+
case 0:
|
|
970
|
+
if (event.value !== undefined) {
|
|
971
|
+
device = hbDevices.toEvents(JSON.stringify(x));
|
|
972
|
+
// debug('alexaEvent - device', device);
|
|
973
|
+
var message = alexaMessages.eventMessage(event, device);
|
|
974
|
+
// debug('alexaEvent - message', message);
|
|
975
|
+
|
|
976
|
+
// debug("message to be sent", JSON.stringify(message, null, 2));
|
|
977
|
+
// fix for issue #186
|
|
978
|
+
if (message && message.event.payload.change && message.event.payload.change.properties[0].value) {
|
|
979
|
+
alexaLocal.alexaEvent(message);
|
|
980
|
+
} else if (message && message.event.header.namespace === "Alexa.DoorbellEventSource") {
|
|
981
|
+
alexaLocal.alexaPriorityEvent(message);
|
|
982
|
+
} else {
|
|
983
|
+
// debug("Event message not being sent", message);
|
|
984
|
+
}
|
|
985
|
+
} else {
|
|
986
|
+
debug("Event message not being sent - no value", event);
|
|
987
|
+
}
|
|
988
|
+
break;
|
|
989
|
+
default:
|
|
990
|
+
debug("Event message not being sent - invalid state", event);
|
|
991
|
+
}
|
|
992
|
+
});
|
|
993
|
+
}
|
|
994
|
+
|
|
995
|
+
/*
|
|
996
|
+
|
|
997
|
+
Utility functions
|
|
998
|
+
|
|
999
|
+
*/
|
|
1000
|
+
|
|
1001
|
+
function _round(value, precision) {
|
|
1002
|
+
var multiplier = Math.pow(10, precision || 0);
|
|
1003
|
+
return Math.round(value * multiplier) / multiplier;
|
|
1004
|
+
}
|