homebridge-alexa 0.5.55 → 0.5.57

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.
@@ -66,6 +66,12 @@
66
66
  "minimum": 1,
67
67
  "maximum": 59
68
68
  },
69
+ "LegacyCloudTransport": {
70
+ "title": "Cloud Server Connection Transport",
71
+ "type": "boolean",
72
+ "default": false,
73
+ "description": "Enable Legacy Transport to Cloud Servers."
74
+ },
69
75
  "filter": {
70
76
  "title": "Instance Filter",
71
77
  "type": "string",
@@ -73,14 +79,12 @@
73
79
  "description": "Limits accessories shared with Alexa to a single homebridge instance.",
74
80
  "pattern": "^[^{}/ :\\\\]+(?::\\d+)?$"
75
81
  },
76
-
77
82
  "deviceListHandling": {
78
83
  "title": "How to handle devices in the deviceList",
79
84
  "type": "string",
80
85
  "required": true,
81
86
  "default": "allow",
82
- "oneOf": [
83
- {
87
+ "oneOf": [{
84
88
  "title": "Allow devices",
85
89
  "enum": [
86
90
  "allow"
@@ -183,7 +187,8 @@
183
187
  "expandable": true,
184
188
  "expanded": false,
185
189
  "items": [
186
- "keepalive"
190
+ "keepalive",
191
+ "LegacyCloudTransport"
187
192
  ]
188
193
  },
189
194
  {
@@ -192,34 +197,29 @@
192
197
  "description": "Devices to configure as speakers as HomeKit currently does not have a Speaker service.",
193
198
  "expandable": true,
194
199
  "expanded": false,
195
- "items": [
196
- {
197
- "notitle": true,
198
- "key": "speakers",
199
- "type": "array",
200
- "items": [
200
+ "items": [{
201
+ "notitle": true,
202
+ "key": "speakers",
203
+ "type": "array",
204
+ "items": [{
205
+ "type": "div",
206
+ "displayFlex": true,
207
+ "flex-direction": "row",
208
+ "items": [{
209
+ "key": "speakers[].manufacturer",
210
+ "flex": "1 1 50px",
211
+ "notitle": true,
212
+ "placeholder": "Manufacturer"
213
+ },
201
214
  {
202
- "type": "div",
203
- "displayFlex": true,
204
- "flex-direction": "row",
205
- "items": [
206
- {
207
- "key": "speakers[].manufacturer",
208
- "flex": "1 1 50px",
209
- "notitle": true,
210
- "placeholder": "Manufacturer"
211
- },
212
- {
213
- "key": "speakers[].name",
214
- "flex": "4 4 200px",
215
- "notitle": true,
216
- "placeholder": "Name"
217
- }
218
- ]
215
+ "key": "speakers[].name",
216
+ "flex": "4 4 200px",
217
+ "notitle": true,
218
+ "placeholder": "Name"
219
219
  }
220
220
  ]
221
- }
222
- ]
221
+ }]
222
+ }]
223
223
  },
224
224
  {
225
225
  "type": "fieldset",
@@ -227,37 +227,30 @@
227
227
  "description": "Combine disparate accessories into one common device.",
228
228
  "expandable": true,
229
229
  "expanded": false,
230
- "items": [
231
- {
232
- "notitle": true,
233
- "key": "combine",
234
- "type": "array",
235
- "items": [
230
+ "items": [{
231
+ "notitle": true,
232
+ "key": "combine",
233
+ "type": "array",
234
+ "items": [{
235
+ "type": "div",
236
+ "items": [{
237
+ "key": "combine[].into",
238
+ "title": "Into",
239
+ "placeholder": "Target Accessory Name"
240
+ },
236
241
  {
237
- "type": "div",
238
- "items": [
239
- {
240
- "key": "combine[].into",
241
- "title": "Into",
242
- "placeholder": "Target Accessory Name"
243
- },
244
- {
245
- "key": "combine[].from",
246
- "notitle": true,
247
- "items": [
248
- {
249
- "title": "From",
250
- "key": "combine[].from[]",
242
+ "key": "combine[].from",
243
+ "notitle": true,
244
+ "items": [{
245
+ "title": "From",
246
+ "key": "combine[].from[]",
251
247
 
252
- "placeholder": "Source Accessory Name"
253
- }
254
- ]
255
- }
256
- ]
248
+ "placeholder": "Source Accessory Name"
249
+ }]
257
250
  }
258
251
  ]
259
- }
260
- ]
252
+ }]
253
+ }]
261
254
  }
262
255
  ]
263
256
  }
@@ -36,7 +36,7 @@ function hapDiscovery(options) {
36
36
  alexaDiscovery.call(options, null, function() {
37
37
  // debug("Events", options);
38
38
  if (options.events) {
39
- registerEvents(messages.checkEventDeviceList.call(options, hbDevices.toEvents()));
39
+ registerEvents.call(options, messages.checkEventDeviceList.call(options, hbDevices.toEvents()));
40
40
  }
41
41
  });
42
42
  });
@@ -49,7 +49,7 @@ function hapDiscovery(options) {
49
49
  }
50
50
 
51
51
  function registerEvents(message) {
52
- debug("registerEvents", message);
52
+ // debug("registerEvents", message);
53
53
 
54
54
  var HBMessage = [];
55
55
 
@@ -73,10 +73,12 @@ function registerEvents(message) {
73
73
  };
74
74
  }
75
75
  }
76
+
77
+ this.log("registerEvents - registered %s event sources", Object.keys(HBMessage).length);
76
78
  for (var register in HBMessage) {
77
79
  // console.log("send", instance, HBMessage[instance]);
78
80
  var hbInstance = JSON.parse(register);
79
- debug("Event Register %s ->", hbInstance.deviceID, HBMessage[register]);
81
+ // debug("Event Register %s ->", hbInstance.deviceID, HBMessage[register]);
80
82
  homebridge.HAPeventByDeviceID(hbInstance.deviceID, JSON.stringify(HBMessage[register]), function(err, status) {
81
83
  if (!err) {
82
84
  // debug("Registered Event %s:%s ->", hbInstance.host, hbInstance.port, status);
@@ -108,7 +110,7 @@ function alexaDiscovery(message, callback) {
108
110
  for (var i = 0; i < response.event.payload.endpoints.length; i++) {
109
111
  var endpoint = response.event.payload.endpoints[i];
110
112
  if (deleteSeen[endpoint.friendlyName]) {
111
- this.log("WARNING: Duplicate device name", endpoint.friendlyName);
113
+ this.log.warn("WARNING: Duplicate device name", endpoint.friendlyName);
112
114
  // response.event.payload.endpoints.splice(i, 1);
113
115
  } else {
114
116
  deleteSeen[endpoint.friendlyName] = true;
@@ -116,7 +118,7 @@ function alexaDiscovery(message, callback) {
116
118
  }
117
119
 
118
120
  if (response && response.event.payload.endpoints.length < 1) {
119
- this.log("ERROR: HAP Discovery failed, please review config");
121
+ this.log.error("ERROR: HAP Discovery failed, please review config");
120
122
  } else {
121
123
  if (process.uptime() > 600) { // Only use console during startup
122
124
  debug("alexaDiscovery - returned %s devices", response.event.payload.endpoints.length);
@@ -124,7 +126,7 @@ function alexaDiscovery(message, callback) {
124
126
  this.log("alexaDiscovery - returned %s devices", response.event.payload.endpoints.length);
125
127
  }
126
128
  if (response.event.payload.endpoints.length > 300) {
127
- this.log("ERROR: Maximum devices/accessories of 300 exceeded.");
129
+ this.log.error("ERROR: Maximum devices/accessories of 300 exceeded.");
128
130
  }
129
131
  }
130
132
  // debug("Discovery Response", JSON.stringify(response, null, 2));
@@ -141,7 +143,7 @@ function alexaDiscovery(message, callback) {
141
143
  function removeLargeCookieEndpoints(endpoints) {
142
144
  var response = [];
143
145
  endpoints.forEach((endpoint) => {
144
- debug("Cookie Object: ", JSON.stringify(endpoint.cookie).length);
146
+ // debug("Cookie Object: ", JSON.stringify(endpoint.cookie).length);
145
147
  if (JSON.stringify(endpoint.cookie).length < 5000) {
146
148
  response.push(endpoint);
147
149
  } else {
@@ -159,7 +161,7 @@ function removeDuplicateEndpoints(endpoints) {
159
161
  var response = [];
160
162
  endpoints.forEach((endpoint) => {
161
163
  if (deleteSeen[endpoint.endpointId]) {
162
- this.log("ERROR: Parsing failed, removing duplicate endpointID =>", endpoint.friendlyName);
164
+ this.log.error("ERROR: Parsing failed, removing duplicate endpointID =>", endpoint.friendlyName);
163
165
  } else {
164
166
  response.push(endpoint);
165
167
  }
package/lib/alexaLocal.js CHANGED
@@ -16,7 +16,6 @@ var checkAlexaMessage = Validator(alexaSchema, {
16
16
  verbose: true
17
17
  });
18
18
 
19
- var connection = {};
20
19
  var count = 0;
21
20
  var username;
22
21
  var limiter;
@@ -45,24 +44,25 @@ function alexaLocal(options) {
45
44
  }
46
45
 
47
46
  limiter.on("dropped", function(dropped) {
48
- options.log("WARNING: ( homebridge-alexa) Dropped event message, message rate too high.");
47
+ options.log.warn("WARNING: ( homebridge-alexa) Dropped event message, message rate too high.");
49
48
  });
50
49
 
51
- username = options.username;
52
- connection.client = mqtt.connect(options);
53
- // connection.client.setMaxListeners(0);
54
- connection.client.on('connect', function() {
55
- debug('connect', "command/" + options.username + "/#");
50
+ username = options.mqttOptions.username;
51
+ var connection = mqtt.connect(options.mqttURL, options.mqttOptions);
52
+ connection.on('connect', function() {
53
+ debug('connect', "command/" + username + "/#");
56
54
  if (options.alexaService) {
57
55
  options.alexaService.setCharacteristic(Characteristic.ContactSensorState, Characteristic.ContactSensorState.CONTACT_DETECTED);
58
56
  }
59
- connection.client.removeAllListeners('message'); // Clean up event handlers
60
- connection.client.subscribe("command/" + options.username + "/#");
61
- connection.client.publish("presence/" + options.username + "/1", JSON.stringify({
57
+ connection.subscribe("command/" + username + "/#");
58
+ connection.publish("presence/" + username + "/1", JSON.stringify({
62
59
  Connected: options.username,
63
60
  version: packageConfig.version
64
61
  }));
65
- connection.client.on('message', function(topic, message) {
62
+ });
63
+
64
+ connection.on('message', function(topic, message) {
65
+ try {
66
66
  var msg = JSON.parse(message.toString());
67
67
 
68
68
  // debug("Count", this.listenerCount(msg.directive.header.namespace.toLowerCase()));
@@ -74,55 +74,63 @@ function alexaLocal(options) {
74
74
  response = _alexaErrorResponse(msg);
75
75
  }
76
76
  // debug("Response", JSON.stringify(response));
77
- connection.client.publish("response/" + options.username + "/1", JSON.stringify(response));
77
+ connection.publish("response/" + username + "/1", JSON.stringify(response));
78
78
  });
79
79
  } else {
80
- debug('No listener for', msg.directive.header.namespace.toLowerCase());
80
+ debug('ERROR: No alexa action listener for', msg);
81
81
  var response = _alexaErrorResponse(msg);
82
- connection.client.publish("response/" + options.username + "/1", JSON.stringify(response));
83
- debug("Response", JSON.stringify(response, null, 2));
82
+ connection.publish("response/" + username + "/1", JSON.stringify(response));
83
+ // debug("Response", JSON.stringify(response, null, 2));
84
84
  }
85
- });
85
+ } catch (err) {
86
+ options.log.error("ERROR: MQTT Message on topic \"%s\" triggered an internal error\n", topic, err);
87
+ }
86
88
  });
87
89
 
88
- connection.client.on('offline', function() {
90
+ connection.on('offline', function() {
89
91
  debug('offline');
90
92
  if (options.alexaService) {
91
93
  options.alexaService.setCharacteristic(Characteristic.ContactSensorState, Characteristic.ContactSensorState.CONTACT_NOT_DETECTED);
92
94
  }
93
95
  });
94
96
 
95
- connection.client.on('reconnect', function() {
97
+ connection.on('reconnect', function() {
96
98
  count++;
97
99
  debug('reconnect');
98
100
  if (options.alexaService) {
99
101
  options.alexaService.setCharacteristic(Characteristic.ContactSensorState, Characteristic.ContactSensorState.CONTACT_NOT_DETECTED);
100
102
  }
101
- if (count % 5 === 0) options.log("ERROR: ( homebridge-alexa) You have an issue with your installation, please review the README.");
103
+ if (count % 5 === 0) options.log.error("ERROR: ( homebridge-alexa) You have an issue with your installation, please review the README.");
102
104
  if (count > 1000) {
103
- connection.client.end({
105
+ connection.end({
104
106
  force: true
105
107
  });
106
108
  if (options.alexaService) {
107
109
  options.alexaService.setCharacteristic(Characteristic.ContactSensorState, Characteristic.ContactSensorState.CONTACT_NOT_DETECTED);
108
110
  }
109
- options.log("ERROR: ( homebridge-alexa) Stopping Home Skill connection due to excessive reconnects, please review the README.");
111
+ options.log.error("ERROR: ( homebridge-alexa) Stopping Home Skill connection due to excessive reconnects, please review the README.");
110
112
  }
111
113
  });
112
114
 
113
- connection.client.on('error', function(err) {
115
+ connection.on('error', function(err) {
114
116
  if (options.alexaService) {
115
117
  options.alexaService.setCharacteristic(Characteristic.ContactSensorState, Characteristic.ContactSensorState.CONTACT_NOT_DETECTED);
116
118
  }
119
+ debug('ERROR: %s -> %s\n', err.message, err.code, err);
117
120
  switch (err.code) {
118
121
  case 5:
119
- options.log("ERROR: ( homebridge-alexa) Login to homebridge.ca failed, please validate your credentials in config.json and restart homebridge.");
120
- connection.client.end({
121
- force: true
122
- });
122
+ if (process.uptime() < 20) { // If uptime is less than 20 seconds then error is a bad password and not a backend issue
123
+ options.log.error("ERROR: ( homebridge-alexa) Login to homebridge.ca failed, please validate your credentials in config.json and restart homebridge.");
124
+ connection.end({
125
+ force: true
126
+ });
127
+ }
128
+ break;
129
+ case 'ECONNREFUSED':
130
+ options.log.error("ERROR: Cloud service DDOS Protection tripped, please stop homebridge for 5 to 10 minutes and try again.");
123
131
  break;
124
132
  default:
125
- debug('ERROR: %s -> ', err.message, err.code);
133
+ debug('ERROR: %s -> %d\n', err.message, err.code, err);
126
134
  }
127
135
  });
128
136
  }
@@ -139,7 +147,7 @@ function alexaEvent(message) {
139
147
  var topic = "event/" + username + "/1";
140
148
  var publish = function(callback) {
141
149
  debug("Sending message", JSON.stringify(message));
142
- connection.client.publish(topic, JSON.stringify(message), {
150
+ connection.publish(topic, JSON.stringify(message), {
143
151
  retain: false
144
152
  }, callback);
145
153
  };
@@ -151,16 +159,18 @@ function alexaPriorityEvent(message) {
151
159
  var topic = "event/" + username + "/1";
152
160
  var publish = function(callback) {
153
161
  debug("Sending priority message", JSON.stringify(message));
154
- connection.client.publish(topic, JSON.stringify(message), {
162
+ connection.publish(topic, JSON.stringify(message), {
155
163
  retain: false
156
164
  }, callback);
157
165
  };
158
- limiter.submit({ priority: 4 }, publish);
166
+ limiter.submit({
167
+ priority: 4
168
+ }, publish);
159
169
 
160
170
  /*
161
171
  var topic = "event/" + username + "/1";
162
172
  debug("Sending priority message", JSON.stringify(message));
163
- connection.client.publish(topic, JSON.stringify(message));
173
+ connection.publish(topic, JSON.stringify(message));
164
174
  */
165
175
  }
166
176
 
@@ -116,6 +116,8 @@ Accessory.prototype.toAlexa = function(context) {
116
116
  var service = this.services[index];
117
117
  if ((this.television && service.service === "Speaker") || service.service === "Television" || service.service === "Input Source") {
118
118
  // Skip television accessories
119
+ } else if (service.name === "Alexa" && service.service === "Contact Sensor" && service.info.Manufacturer === "homebridge-alexa") {
120
+ // Skip the Alexa contact sensors made by the plugin
119
121
  } else {
120
122
  var alexa = service.toAlexa(context);
121
123
  if (alexa) {
@@ -91,12 +91,16 @@ Homebridges.prototype.toEvents = function(endpoint) {
91
91
  for (var index in accessory.services) {
92
92
  var service = accessory.services[index];
93
93
  // debug("characteristics", service.characteristics.length);
94
- for (var index in service.characteristics) {
95
- var hapEvents = service.characteristics[index].hapEvents;
96
- if (Object.keys(hapEvents).length > 0) {
97
- list = Object.assign(list, hapEvents);
98
- // debug("hapEvents", hapEvents, Object.keys(hapEvents).length);
99
- // debug("List", list);
94
+ if (service.name === "Alexa" && service.service === "Contact Sensor" && service.info.Manufacturer === "homebridge-alexa") {
95
+ // Skip the Alexa contact sensors made by the plugin
96
+ } else {
97
+ for (var index in service.characteristics) {
98
+ var hapEvents = service.characteristics[index].hapEvents;
99
+ if (Object.keys(hapEvents).length > 0) {
100
+ list = Object.assign(list, hapEvents);
101
+ // debug("hapEvents", hapEvents, Object.keys(hapEvents).length);
102
+ // debug("List", list);
103
+ }
100
104
  }
101
105
  }
102
106
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "homebridge-alexa",
3
- "version": "0.5.55",
3
+ "version": "0.5.57",
4
4
  "description": "Control your Homebridge devices with Amazon Alexa.",
5
5
  "main": "plugin.js",
6
6
  "scripts": {
@@ -41,7 +41,7 @@
41
41
  "debug": ">=4.3.3",
42
42
  "hap-node-client": ">=0.1.16",
43
43
  "is-my-json-valid": ">=2.20.6",
44
- "mqtt": "4.2.8"
44
+ "mqtt": "^4.3.4"
45
45
  },
46
46
  "author": "NorthernMan54",
47
47
  "license": "Apache-2.0",
package/plugin.js CHANGED
@@ -40,14 +40,14 @@ function alexaHome(log, config, api) {
40
40
  this.deviceList = config['deviceList'] || []; // Use ea
41
41
  this.door = config['door'] || false; // Use mode controller for Garage Doors
42
42
  this.name = config['name'] || "homebridgeAlexa";
43
+ this.LegacyCloudTransport = config['LegacyCloudTransport'] || false; // Default to new Transport
43
44
  var mqttKeepalive = config['keepalive'] || 20; // MQTT Connection Keepalive
44
45
 
45
- if( mqttKeepalive < 60 )
46
- {
47
- this.keepalive = mqttKeepalive * 60;
48
- } else {
49
- this.keepalive = mqttKeepalive;
50
- }
46
+ if (mqttKeepalive < 60) {
47
+ this.keepalive = mqttKeepalive * 60;
48
+ } else {
49
+ this.keepalive = mqttKeepalive;
50
+ }
51
51
 
52
52
  // Enable config based DEBUG logging enable
53
53
  this.debug = config['debug'] || false;
@@ -94,24 +94,25 @@ alexaHome.prototype = {
94
94
  };
95
95
 
96
96
  alexaHome.prototype.didFinishLaunching = function() {
97
- var host = 'alexa.homebridge.ca';
97
+ var host = (this.LegacyCloudTransport ? 'alexa.homebridge.ca' : 'www.homebridge.ca');
98
+ var reconnectPeriod = 65000; // Increased reconnect period to allow DDOS protection to reset
98
99
  if (this.beta) {
99
- host = 'alexabeta.homebridge.ca';
100
+ host = 'clone.homebridge.ca';
101
+ // reconnectPeriod = 10000;
100
102
  }
103
+
101
104
  options = {
102
105
  // Shared Options
103
106
  log: this.log,
104
107
  debug: this.debug,
105
- // MQTT Options
106
- username: this.username,
107
- password: this.password,
108
- servers: [{
109
- protocol: 'mqtt',
110
- host: host,
111
- port: 1883
112
- }],
113
- reconnectPeriod: 65000,
114
- keepalive: this.keepalive, // Reduce client timeout to 10 minutes
108
+ mqttURL: (this.LegacyCloudTransport ? "mqtt://" + host + ":1883/" : "wss://" + host + "/ws"),
109
+ mqttOptions: {
110
+ username: this.username,
111
+ password: this.password,
112
+ reconnectPeriod: reconnectPeriod, // Increased reconnect period to allow DDOS protection to reset
113
+ keepalive: (this.LegacyCloudTransport ? this.keepalive : 55), // Keep alive not required when using WSS Transport
114
+ rejectUnauthorized: false
115
+ },
115
116
  // HAP Node Client options
116
117
  pin: this.pin,
117
118
  refresh: this.refresh,
@@ -145,7 +146,13 @@ alexaHome.prototype.didFinishLaunching = function() {
145
146
  // Alexa mesages
146
147
 
147
148
  this.eventBus.on('System', function(message) {
148
- this.log.error("ERROR: ", message.directive.header.message);
149
+ this.log.error("ERROR:", message.directive.header.message);
150
+ }.bind(this));
151
+ this.eventBus.on('Warning', function(message) {
152
+ this.log.warn("Warning:", message.directive.header.message);
153
+ }.bind(this));
154
+ this.eventBus.on('Information', function(message) {
155
+ this.log("Info:", message.directive.header.message);
149
156
  }.bind(this));
150
157
  this.eventBus.on('Alexa', alexaActions.alexaMessage.bind(this));
151
158
  this.eventBus.on('Alexa.Discovery', alexaActions.alexaDiscovery.bind(this));