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.
@@ -0,0 +1,189 @@
1
+ var debug = require('debug')('alexa:Accessory');
2
+ var Service = require('./Service.js').Service;
3
+ var messages = require('./messages.js');
4
+
5
+ module.exports = {
6
+ Accessory: Accessory
7
+ };
8
+
9
+ /*
10
+ * Homebridges -> Homebridge -> Accessory -> Service -> Characteristic
11
+ */
12
+
13
+ function Accessory(devices, context) {
14
+ // debug("Accessory", devices);
15
+ this.aid = devices.aid;
16
+ this.deviceID = context.deviceID;
17
+ this.homebridge = context.homebridge;
18
+ this.id = context.id;
19
+ this.options = context.options;
20
+ this.services = {};
21
+ this.playback = false;
22
+ this.television = false;
23
+ this.link = [];
24
+ devices.services.forEach(function(element) {
25
+ // debug("Service", element);
26
+ switch (element.type.substring(0, 8)) {
27
+ case "0000003E": // Accessory Information
28
+ this.info = information(element.characteristics);
29
+ this.name = this.info.Name;
30
+ break;
31
+ default:
32
+ if (!this.info) {
33
+ this.name = "Unknown";
34
+ this.info = {};
35
+ this.info.Manufacturer = "Unknown";
36
+ this.info.Name = "Unknown";
37
+ }
38
+ var service = new Service(element, this);
39
+ // debug("New", service);
40
+ // if (service.service) {
41
+ this.services[service.iid] = service;
42
+ // }
43
+ if (service.playback) {
44
+ this.playback = true;
45
+ }
46
+ // debug("New", service.service);
47
+ if (service.service === "Television") {
48
+ // debug("Found TV", service.iid);
49
+ this.television = true;
50
+ }
51
+ if (service.linked) {
52
+ this.link[service.iid] = service.linked;
53
+ }
54
+ }
55
+ }.bind(this));
56
+ // debug("Services.setup", this.name, this.services.length);
57
+ }
58
+
59
+ Accessory.prototype.toList = function(context) {
60
+ var list = [];
61
+ context.aid = this.aid;
62
+ context.name = this.info.Name;
63
+ context.manufacturer = this.info.Manufacturer;
64
+ for (var index in this.services) {
65
+ var service = this.services[index].toList(context);
66
+ if (service) {
67
+ list = list.concat(service);
68
+ }
69
+ }
70
+
71
+ // debug("opt",context.opt,list.length);
72
+ return (list);
73
+ };
74
+
75
+ Accessory.prototype.toAlexa = function(context) {
76
+ var list = [];
77
+ context.aid = this.aid;
78
+ context.name = this.info.Name;
79
+ context.manufacturer = this.info.Manufacturer;
80
+ // debug("toAlexa", context.name, this.services.length);
81
+ if (this.television) {
82
+ var television, speaker;
83
+ var inputs = [];
84
+ for (var index in this.services) {
85
+ var service = this.services[index];
86
+ if (service.service === "Television") {
87
+ // this.tvService = service.toAlexa(context);
88
+ television = service.toAlexa(context);
89
+ } else if (service.service === "Speaker") {
90
+ speaker = service.toAlexa(context);
91
+ } else if (service.service === "Input Source") {
92
+ // debug("toAlexa", service.toAlexa(context));
93
+ inputs = inputs.concat(service.toAlexa(context).cookie);
94
+ }
95
+ }
96
+ if (speaker) {
97
+ messages.mergeCookies(television.cookie, speaker.cookie);
98
+ messages.mergeCapabilities(television.capabilities, speaker.capabilities);
99
+ }
100
+ if (inputs.length > 0 && television) {
101
+ // debug("Television", messages.lookupCapabilities("ChannelController", this.options, inputs));
102
+ // debug("inputs && television", context.name, inputs.length, television);
103
+ inputs = flatten(inputs);
104
+ // debug("inputs && television", context.name);
105
+ messages.mergeInputCookies(television.cookie, inputs);
106
+ messages.mergeCapabilities(television.capabilities, messages.lookupCapabilities("Input Source", this.options, inputs));
107
+ messages.mergeCapabilities(television.capabilities, messages.lookupCapabilities("ChannelController", this.options, inputs));
108
+ }
109
+ if (television) {
110
+ // debug("Adding", service.name, alexa);
111
+ list = list.concat(television);
112
+ }
113
+ }
114
+
115
+ for (var index in this.services) {
116
+ var service = this.services[index];
117
+ if ((this.television && service.service === "Speaker") || service.service === "Television" || service.service === "Input Source") {
118
+ // Skip television accessories
119
+ } else {
120
+ var alexa = service.toAlexa(context);
121
+ if (alexa) {
122
+ list = list.concat(alexa);
123
+ }
124
+ }
125
+ }
126
+
127
+ // debug("opt",context.opt,list.length);
128
+ return (list);
129
+ };
130
+
131
+ Accessory.prototype.toCookie = function(characteristic, context) {
132
+ var list;
133
+ context.aid = this.aid;
134
+ context.name = this.info.Name;
135
+ context.manufacturer = this.info.Manufacturer;
136
+ for (var index in this.services) {
137
+ var service = this.services[index].toCookie(characteristic, context);
138
+ if (service) {
139
+ list = service;
140
+ }
141
+ }
142
+
143
+ // debug("opt",context.opt,list.length);
144
+ return (list);
145
+ };
146
+
147
+ function information(characteristics) {
148
+ var result = {};
149
+ characteristics.forEach(function(characteristic) {
150
+ if (characteristic.description) {
151
+ var key = characteristic.description.replace(/ /g, '').replace(/\./g, '_');
152
+ result[key] = characteristic.value;
153
+ }
154
+ });
155
+
156
+ // Issue #325 missing manufacturer breaks alexa
157
+ if (!result.Manufacturer) {
158
+ result.Manufacturer = "Missing Manufacturer";
159
+ }
160
+ // debug("information", result);
161
+ return result;
162
+ }
163
+
164
+ function flatten(data) {
165
+ var result = {};
166
+
167
+ function recurse(cur, prop) {
168
+ if (Object(cur) !== cur) {
169
+ result[prop] = cur;
170
+ } else if (Array.isArray(cur)) {
171
+ for (var i = 0, l = cur.length; i < l; i++)
172
+ recurse(cur[i], prop);
173
+ if (l == 0)
174
+ result[prop] = [];
175
+ } else {
176
+ var isEmpty = true;
177
+ for (var p in cur) {
178
+ isEmpty = false;
179
+ recurse(cur[p], prop ? prop + "." + p : p);
180
+ }
181
+ if (isEmpty && prop)
182
+ result[prop] = {};
183
+ }
184
+ }
185
+ recurse(data, "");
186
+ delete result["Channel"];
187
+ delete result["ReportState"];
188
+ return result;
189
+ }
@@ -0,0 +1,324 @@
1
+ var debug = require('debug')('alexa:Characteristic');
2
+ var messages = require('./messages.js');
3
+
4
+ module.exports = {
5
+ Characteristic: Characteristic
6
+ };
7
+
8
+ /*
9
+ * Homebridges -> Homebridge -> Accessory -> Service -> Characteristic
10
+ */
11
+
12
+ function Characteristic(devices, context) {
13
+ // debug("Characteristic", devices, context);
14
+ this.aid = context.aid;
15
+ this.iid = devices.iid;
16
+ context.liid = devices.iid;
17
+ this.type = devices.type.substring(0, 8);
18
+ this.perms = devices.perms;
19
+ this.value = devices.value;
20
+
21
+ // Overide Brightness/Rotation Speed with Volume for Fake speakers
22
+ // debug("Speaker", devices);
23
+ if (context.options.speakers && context.options.speakers.some(function(element) {
24
+ // debug("SPEAKER", element.manufacturer === context.info.Manufacturer && element.name === context.info.Name, devices.description);
25
+ return (element.manufacturer === context.info.Manufacturer && element.name === context.info.Name);
26
+ }) && (devices.description === "Brightness" || devices.description === "Rotation Speed")) {
27
+ this.type = "00000119"; // Volume Characteristic
28
+ this.description = "Volume";
29
+ } else {
30
+ this.description = devices.description;
31
+ }
32
+
33
+ var actions = _getActions(this.description, context, devices);
34
+ this.cookie = actions.cookie;
35
+ this.reportState = actions.reportState;
36
+ // debug("lookup", this.description, context.service); // Programmable Switch Event Doorbell
37
+ // Don't lookup capabilities for PSE unless it is a Doorbell
38
+ if (this.description === "Programmable Switch Event" && context.service === "Doorbell") {
39
+ this.capabilities = messages.lookupCapabilities(this.description, context.options);
40
+ } else if (this.description !== "Programmable Switch Event") {
41
+ this.capabilities = messages.lookupCapabilities(this.description, context.options, undefined, devices);
42
+ }
43
+ this.hapEvents = actions.hapEvents;
44
+ // debug("%s %s Cookie %s Capability %s", context.name, this.iid, JSON.stringify(this.cookie), JSON.stringify(this.capabilities));
45
+ this.getCharacteristic = {
46
+ aid: context.aid,
47
+ iid: this.iid
48
+ };
49
+ }
50
+
51
+ function _getActions(description, context, devices) {
52
+ var cookie = {};
53
+ var reportState = [];
54
+ var hapEvents = [];
55
+ debug("_getActions", description, devices);
56
+ switch (description) {
57
+ case "Active": // Active on a Fan 2 aka Dyson or Valve
58
+ case "On": // Accessory On/Off
59
+ // debug("On/Active", context.name);
60
+ reportState.push(messages.reportState("Alexa.PowerController", context));
61
+ cookie["TurnOn"] = messages.cookieV(1, context);
62
+ cookie["TurnOff"] = messages.cookieV(0, context);
63
+ break;
64
+ case "Rotation Speed": // RotationSpeed
65
+ reportState.push(messages.reportState("Alexa.PowerLevelController", context));
66
+ cookie["AdjustPowerLevel"] = messages.cookie(context);
67
+ cookie["SetPowerLevel"] = messages.cookie(context);
68
+ break;
69
+ /*
70
+ case "Configured Name": // Channel Change Hack
71
+ cookie["Channel"] = messages.cookie(context);
72
+ debug("Channel", context);
73
+ break;
74
+ */
75
+ case "Active Identifier": // Channel Change Hack
76
+ cookie["Active Identifier"] = messages.cookie(context);
77
+ break;
78
+ case "Brightness": // Brightness
79
+ reportState.push(messages.reportState("Alexa.PowerLevelController", context));
80
+ cookie["AdjustPowerLevel"] = messages.cookie(context);
81
+ cookie["SetPowerLevel"] = messages.cookie(context);
82
+ cookie["BrightnessTurnOn"] = "true";
83
+ break;
84
+ case "Current Temperature": // Current Temperature
85
+ reportState.push(messages.reportState("Alexa.TemperatureSensor", context));
86
+ break;
87
+ case "Motion Detected": // Motion sensor state
88
+ reportState.push(messages.reportState("Alexa.MotionSensor", context));
89
+ hapEvents[JSON.stringify({
90
+ "deviceID": context.deviceID,
91
+
92
+ "aid": context.aid,
93
+ "iid": context.liid
94
+ })] = {
95
+ "endpointID": context.endpointID,
96
+ "friendlyName": context.name,
97
+ "true": "DETECTED",
98
+ "false": "NOT_DETECTED",
99
+ "1": "DETECTED",
100
+ "0": "NOT_DETECTED",
101
+ "template": "MotionSensor"
102
+ };
103
+ break;
104
+ case "Current Door State": // Current Door state
105
+ case "Current Position": // Current Door state
106
+ reportState.push(messages.reportState("Alexa.ContactSensor", context));
107
+ // The value property of CurrentDoorState must be one of the following:
108
+ // Characteristic.CurrentDoorState.OPEN = 0;
109
+ // Characteristic.CurrentDoorState.CLOSED = 1;
110
+ // Characteristic.CurrentDoorState.OPENING = 2;
111
+ // Characteristic.CurrentDoorState.CLOSING = 3;
112
+ // Characteristic.CurrentDoorState.STOPPED = 4;
113
+ //
114
+ // Validated againt door states
115
+
116
+ hapEvents[JSON.stringify({
117
+ "deviceID": context.deviceID,
118
+
119
+ "aid": context.aid,
120
+ "iid": context.liid
121
+ })] = {
122
+ "endpointID": context.endpointID,
123
+ "friendlyName": context.name,
124
+ "0": "DETECTED", // Aka open for Alexa
125
+ "1": "NOT_DETECTED",
126
+ // "2": "DETECTED",
127
+ // "3": "DETECTED",
128
+ // "4": "DETECTED",
129
+ "template": "ContactSensor"
130
+ };
131
+ break;
132
+ case "Target Door State": // TargetDoorState
133
+ if (context.options.door) {
134
+ cookie["SetMode"] = messages.cookie(context); // Open
135
+ reportState.push(messages.reportState("Alexa.ModeController", context));
136
+ // reportState.push(messages.reportState("Alexa.RangeController", context));
137
+ // cookie["SetRangeValue"] = messages.cookie(context);
138
+ // cookie["AdjustRangeValue"] = messages.cookie(context);
139
+ } else {
140
+ reportState.push(messages.reportState("Alexa.PowerController", context));
141
+ cookie["TurnOn"] = messages.cookieV(0, context); // Open
142
+ cookie["TurnOff"] = messages.cookieV(1, context); // Closed
143
+ }
144
+ break;
145
+ case "Contact Sensor State": // Contact Sensor State
146
+ case "Occupancy Detected": // Occupany Sensor
147
+ reportState.push(messages.reportState("Alexa.ContactSensor", context));
148
+ // Characteristic.ContactSensorState.CONTACT_DETECTED = 0;
149
+ // Characteristic.ContactSensorState.CONTACT_NOT_DETECTED = 1;
150
+
151
+ // DETECTED means the sensor is open and the two pieces of the sensor are not in contact with each other. For example, a window has been opened.
152
+ // NOT_DETECTED means the sensor is closed and the two pieces of the sensor are in contact with each other.
153
+
154
+ // I think that Alexa and Apple are backwards
155
+
156
+ hapEvents[JSON.stringify({
157
+ "deviceID": context.deviceID,
158
+
159
+ "aid": context.aid,
160
+ "iid": context.liid
161
+ })] = {
162
+ "endpointID": context.endpointID,
163
+ "friendlyName": context.name,
164
+ "1": "DETECTED", // Aka open for Alexa
165
+ "0": "NOT_DETECTED",
166
+ "template": "ContactSensor"
167
+ };
168
+ break;
169
+ case "Programmable Switch Event": // aka Doorbell
170
+ // debug("PSE", context.service, context);
171
+ if (context.service === 'Doorbell') { // Only for PSE's that are part of the Doorbell service
172
+ hapEvents[JSON.stringify({
173
+ "deviceID": context.deviceID,
174
+
175
+ "aid": context.aid,
176
+ "iid": context.liid
177
+ })] = {
178
+ "endpointID": context.endpointID,
179
+ "friendlyName": context.name,
180
+ "0": "DETECTED", // Aka open for Alexa
181
+ "1": "NOT_DETECTED",
182
+ "template": "DoorbellEventSource"
183
+ };
184
+ }
185
+ break;
186
+ case "Color Temperature": // Color Temperature
187
+ reportState.push(messages.reportState("Alexa.ColorTemperatureController", context));
188
+ cookie["IncreaseColorTemperature"] = messages.cookie(context);
189
+ cookie["DecreaseColorTemperature"] = messages.cookie(context);
190
+ cookie["SetColorTemperature"] = messages.cookie(context);
191
+ break;
192
+ case "Target Position": // WindowCoverings Target Position
193
+ // debug(devices);
194
+ if (context.options.blind) {
195
+ reportState.push(messages.reportState("Alexa.RangeController", context));
196
+ cookie["SetRangeValue"] = messages.cookie(context);
197
+ cookie["AdjustRangeValue"] = messages.cookie(context);
198
+ } else {
199
+ reportState.push(messages.reportState("Alexa.PowerController", context));
200
+ reportState.push(messages.reportState("Alexa.PowerLevelController", context));
201
+ if (devices.maxValue) {
202
+ cookie["TurnOn"] = messages.cookieV(devices.maxValue, context);
203
+ } else {
204
+ cookie["TurnOn"] = messages.cookieV(100, context);
205
+ }
206
+ if (devices.minValue) {
207
+ cookie["TurnOff"] = messages.cookieV(devices.minValue, context);
208
+ } else {
209
+ cookie["TurnOff"] = messages.cookieV(0, context);
210
+ }
211
+ cookie["AdjustPowerLevel"] = messages.cookie(context);
212
+ cookie["SetPowerLevel"] = messages.cookie(context);
213
+ }
214
+ break;
215
+ case "Volume": // Speaker volume
216
+ // case "91288267": // This is in the homebridge-bose-soundtouch plugin
217
+ reportState.push(messages.reportState("Alexa.Speaker", context));
218
+ cookie["AdjustVolume"] = messages.cookie(context);
219
+ cookie["SetVolume"] = messages.cookie(context);
220
+ // cookie["SetMute"] = messages.cookie(context);
221
+ break;
222
+ case "Preset": // Bose Sound link presets
223
+ // case "91288267": // This is in the homebridge-bose-soundtouch plugin
224
+ reportState.push(messages.reportState("Alexa.ChannelController", context));
225
+ for (var i = devices.minValue; i <= devices.maxValue; i = i + devices.minStep) {
226
+ cookie[i] = messages.cookieV(i, context);
227
+ }
228
+ break;
229
+ case "Volume Selector": // Speaker volume
230
+ // case "91288267": // This is in the homebridge-bose-soundtouch plugin
231
+ reportState.push(messages.reportState("Alexa.StepSpeaker", context));
232
+ cookie["AdjustVolume"] = messages.cookie(context);
233
+ break;
234
+ /*
235
+ case "Mute": // Speaker volume
236
+ // case "91288267": // This is in the homebridge-bose-soundtouch plugin
237
+ reportState.push(messages.reportState("Alexa.StepSpeaker", context));
238
+ cookie["SetMute"] = messages.cookie(context);
239
+ break;
240
+ */
241
+ case "Target Temperature": // Target Temperature
242
+ reportState.push(messages.reportState("Alexa.ThermostatControllertargetSetpoint", context));
243
+ // cookie["SetTargetTemperature"] = messages.cookie(context);
244
+ cookie["targetSetpoint"] = messages.cookie(context);
245
+ break;
246
+ case "Heating Threshold Temperature": // Target Temperature
247
+ // reportState.push(messages.reportState("Alexa.ThermostatController", context));
248
+ reportState.push(messages.reportState("Alexa.ThermostatControllerlowerSetpoint", context));
249
+ cookie["lowerSetpoint"] = messages.cookie(context);
250
+ break;
251
+ case "Cooling Threshold Temperature": // Target Temperature
252
+ // reportState.push(messages.reportState("Alexa.ThermostatController", context));
253
+ reportState.push(messages.reportState("Alexa.ThermostatControllerupperSetpoint", context));
254
+ cookie["upperSetpoint"] = messages.cookie(context);
255
+ // issue #312 - target setpoint missing from heater / cooler devices
256
+ if (context.type === '000000BC') {
257
+ reportState.push(messages.reportState("Alexa.ThermostatControllertargetSetpoint", context));
258
+ cookie["targetSetpoint"] = messages.cookie(context);
259
+ }
260
+ break;
261
+ case "Target Heater-Cooler State":
262
+ case "Target Heater Cooler State":
263
+ case "Target Heating Cooling State":
264
+ reportState.push(messages.reportState("Alexa.ThermostatControllerthermostatMode", context));
265
+ // reportState.push(messages.reportState("Alexa.ThermostatController", context));
266
+ // Characteristic.TargetHeatingCoolingState.OFF = 0;
267
+ // Characteristic.TargetHeatingCoolingState.HEAT = 1;
268
+ // Characteristic.TargetHeatingCoolingState.COOL = 2;
269
+ // Characteristic.TargetHeatingCoolingState.AUTO = 3;
270
+ cookie["thermostatMode"] = messages.cookie(context);
271
+ cookie["thermostatModeOFF"] = messages.cookieV(0, context);
272
+ cookie["thermostatModeHEAT"] = messages.cookieV(1, context);
273
+ cookie["thermostatModeCOOL"] = messages.cookieV(2, context);
274
+ cookie["thermostatModeAUTO"] = messages.cookieV(3, context);
275
+ break;
276
+ case "Current Heating Cooling State":
277
+ // Alexa doesn't report Furnace state. Issue # 312
278
+ // reportState.push(messages.reportState("Alexa.ThermostatControllerthermostatMode", context));
279
+ // Characteristic.CurrentHeatingCoolingState.OFF = 0;
280
+ // Characteristic.CurrentHeatingCoolingState.HEAT = 1;
281
+ // Characteristic.CurrentHeatingCoolingState.COOL = 2;
282
+ // cookie["thermostatMode"] = messages.cookie(context);
283
+ break;
284
+ case "Lock Current State":
285
+ reportState.push(messages.reportState("Alexa.LockController", context));
286
+ break;
287
+ case "Lock Target State":
288
+ // reportState.push(messages.reportState("Alexa.LockController", context));
289
+ cookie["Lock"] = messages.cookieV(1, context);
290
+ cookie["Unlock"] = messages.cookieV(0, context);
291
+ break;
292
+ case "Remote Key":
293
+ /*
294
+ Characteristic.RemoteKey.REWIND = 0;
295
+ Characteristic.RemoteKey.FAST_FORWARD = 1;
296
+ Characteristic.RemoteKey.NEXT_TRACK = 2;
297
+ Characteristic.RemoteKey.PREVIOUS_TRACK = 3;
298
+ Characteristic.RemoteKey.ARROW_UP = 4;
299
+ Characteristic.RemoteKey.ARROW_DOWN = 5;
300
+ Characteristic.RemoteKey.ARROW_LEFT = 6;
301
+ Characteristic.RemoteKey.ARROW_RIGHT = 7;
302
+ Characteristic.RemoteKey.SELECT = 8;
303
+ Characteristic.RemoteKey.BACK = 9;
304
+ Characteristic.RemoteKey.EXIT = 10;
305
+ Characteristic.RemoteKey.PLAY_PAUSE = 11;
306
+ Characteristic.RemoteKey.INFORMATION = 15;
307
+ */
308
+ // "Play", "Pause", "Stop", "Next", "Previous"
309
+ cookie["Pause"] = messages.cookieV(11, context);
310
+ cookie["Play"] = messages.cookieV(8, context);
311
+ cookie["Stop"] = messages.cookieV(9, context);
312
+ cookie["Next"] = messages.cookieV(7, context);
313
+ cookie["Previous"] = messages.cookieV(6, context);
314
+ break;
315
+ default:
316
+ // debug("Missing Characteristic '%s' %s - %s", description, context.name, context.function);
317
+ }
318
+ // debug("HP", hapEvents, hapEvents.length);
319
+ return ({
320
+ cookie: cookie,
321
+ reportState: reportState,
322
+ hapEvents: hapEvents
323
+ });
324
+ }