@switchbot/homebridge-switchbot 1.6.1 → 1.6.2

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.
Files changed (40) hide show
  1. package/CHANGELOG.md +10 -0
  2. package/dist/devices/bots.d.ts +1 -0
  3. package/dist/devices/bots.d.ts.map +1 -1
  4. package/dist/devices/bots.js +130 -114
  5. package/dist/devices/bots.js.map +1 -1
  6. package/dist/devices/contact.d.ts +1 -0
  7. package/dist/devices/contact.d.ts.map +1 -1
  8. package/dist/devices/contact.js +65 -55
  9. package/dist/devices/contact.js.map +1 -1
  10. package/dist/devices/curtains.d.ts +2 -1
  11. package/dist/devices/curtains.d.ts.map +1 -1
  12. package/dist/devices/curtains.js +95 -75
  13. package/dist/devices/curtains.js.map +1 -1
  14. package/dist/devices/humidifiers.d.ts +2 -1
  15. package/dist/devices/humidifiers.d.ts.map +1 -1
  16. package/dist/devices/humidifiers.js +90 -74
  17. package/dist/devices/humidifiers.js.map +1 -1
  18. package/dist/devices/meters.d.ts +1 -0
  19. package/dist/devices/meters.d.ts.map +1 -1
  20. package/dist/devices/meters.js +68 -62
  21. package/dist/devices/meters.js.map +1 -1
  22. package/dist/devices/motion.d.ts +1 -0
  23. package/dist/devices/motion.d.ts.map +1 -1
  24. package/dist/devices/motion.js +60 -54
  25. package/dist/devices/motion.js.map +1 -1
  26. package/dist/devices/plugs.js +2 -2
  27. package/dist/devices/plugs.js.map +1 -1
  28. package/dist/platform.d.ts +2 -0
  29. package/dist/platform.d.ts.map +1 -1
  30. package/dist/platform.js +12 -0
  31. package/dist/platform.js.map +1 -1
  32. package/package.json +4 -4
  33. package/src/devices/bots.ts +109 -92
  34. package/src/devices/contact.ts +65 -52
  35. package/src/devices/curtains.ts +94 -72
  36. package/src/devices/humidifiers.ts +89 -71
  37. package/src/devices/meters.ts +69 -59
  38. package/src/devices/motion.ts +60 -51
  39. package/src/devices/plugs.ts +2 -2
  40. package/src/platform.ts +13 -0
@@ -1,4 +1,3 @@
1
- import Switchbot from 'node-switchbot';
2
1
  import { interval, Subject } from 'rxjs';
3
2
  import { skipWhile } from 'rxjs/operators';
4
3
  import { SwitchBotPlatform } from '../platform';
@@ -200,12 +199,12 @@ export class Meter {
200
199
  if (this.device.scanDuration) {
201
200
  this.scanDuration = this.accessory.context.scanDuration = this.device.scanDuration;
202
201
  if (this.platform.debugMode || (this.deviceLogging === 'debug')) {
203
- this.warnLog(`Bot: ${this.accessory.displayName} Using Device Config scanDuration: ${this.scanDuration}`);
202
+ this.warnLog(`Meter: ${this.accessory.displayName} Using Device Config scanDuration: ${this.scanDuration}`);
204
203
  }
205
204
  } else {
206
205
  this.scanDuration = this.accessory.context.scanDuration = 1;
207
206
  if (this.platform.debugMode || (this.deviceLogging === 'debug')) {
208
- this.warnLog(`Bot: ${this.accessory.displayName} Using Default scanDuration: ${this.scanDuration}`);
207
+ this.warnLog(`Meter: ${this.accessory.displayName} Using Default scanDuration: ${this.scanDuration}`);
209
208
  }
210
209
  }
211
210
  }
@@ -305,77 +304,88 @@ export class Meter {
305
304
  }
306
305
 
307
306
  private connectBLE() {
308
- const switchbot = new Switchbot();
307
+ // Convert to BLE Address
309
308
  this.device.bleMac = ((this.device.deviceId!.match(/.{1,2}/g))!.join(':')).toLowerCase();
310
309
  this.debugLog(`Meter: ${this.accessory.displayName} BLE Address: ${this.device.bleMac}`);
310
+ const switchbot = this.platform.connectBLE();
311
311
  return switchbot;
312
312
  }
313
313
 
314
- private BLErefreshStatus() {
314
+
315
+ private async BLErefreshStatus() {
315
316
  this.debugLog(`Meter: ${this.accessory.displayName} BLE RefreshStatus`);
316
317
  // eslint-disable-next-line @typescript-eslint/no-var-requires
317
318
  const switchbot = this.connectBLE();
318
319
  // Start to monitor advertisement packets
319
- switchbot.startScan({
320
- model: 'T',
321
- id: this.device.bleMac,
322
- }).then(() => {
323
- // Set an event hander
324
- switchbot.onadvertisement = (ad: ad) => {
325
- this.serviceData = ad.serviceData;
326
- this.temperature = ad.serviceData.temperature!.c;
327
- this.humidity = ad.serviceData.humidity;
328
- this.battery = ad.serviceData.battery;
329
- this.debugLog(`Meter: ${this.accessory.displayName} serviceData: ${JSON.stringify(ad.serviceData)}`);
330
- this.debugLog(`Meter: ${this.accessory.displayName} model: ${ad.serviceData.model}, modelName: ${ad.serviceData.modelName}, `
331
- + `temperature: ${JSON.stringify(ad.serviceData.temperature?.c)}, humidity: ${ad.serviceData.humidity}, `
332
- + `battery: ${ad.serviceData.battery}`);
333
-
334
- if (this.serviceData) {
335
- this.connected = true;
336
- this.debugLog(`Meter: ${this.accessory.displayName} connected: ${this.connected}`);
320
+ this.debugLog(`Meter: ${this.accessory.displayName} platform.Switchbot: ${JSON.stringify(switchbot)}`);
321
+ if (switchbot) {
322
+ switchbot.startScan({
323
+ model: 'T',
324
+ id: this.device.bleMac,
325
+ }).then(() => {
326
+ // Set an event hander
327
+ switchbot.onadvertisement = (ad: ad) => {
328
+ this.serviceData = ad.serviceData;
329
+ this.temperature = ad.serviceData.temperature!.c;
330
+ this.humidity = ad.serviceData.humidity;
331
+ this.battery = ad.serviceData.battery;
332
+ this.debugLog(`Meter: ${this.accessory.displayName} serviceData: ${JSON.stringify(ad.serviceData)}`);
333
+ this.debugLog(`Meter: ${this.accessory.displayName} model: ${ad.serviceData.model}, modelName: ${ad.serviceData.modelName}, `
334
+ + `temperature: ${JSON.stringify(ad.serviceData.temperature?.c)}, humidity: ${ad.serviceData.humidity}, `
335
+ + `battery: ${ad.serviceData.battery}`);
336
+
337
+ if (this.serviceData) {
338
+ this.connected = true;
339
+ this.debugLog(`Meter: ${this.accessory.displayName} connected: ${this.connected}`);
340
+ } else {
341
+ this.connected = false;
342
+ this.debugLog(`Meter: ${this.accessory.displayName} connected: ${this.connected}`);
343
+ }
344
+ };
345
+ // Wait 10 seconds
346
+ return switchbot.wait(this.scanDuration * 1000);
347
+ }).then(async () => {
348
+ // Stop to monitor
349
+ switchbot.stopScan();
350
+ if (this.connected) {
351
+ this.CurrentTemperatureCached = this.temperature;
352
+ this.accessory.context.CurrentTemperature = this.CurrentTemperatureCached;
353
+ this.CurrentRelativeHumidityCached = this.humidity!;
354
+ this.accessory.context.CurrentRelativeHumidity = this.CurrentRelativeHumidityCached;
355
+ this.BatteryLevelCached = this.battery!;
356
+ this.accessory.context.BatteryLevel = this.BatteryLevelCached;
357
+ this.parseStatus();
358
+ this.updateHomeKitCharacteristics();
337
359
  } else {
338
- this.connected = false;
339
- this.debugLog(`Meter: ${this.accessory.displayName} connected: ${this.connected}`);
360
+ await this.BLEconnection();
361
+ }
362
+ }).catch(async (e: any) => {
363
+ this.errorLog(`Meter: ${this.accessory.displayName} failed refreshStatus with BLE Connection`);
364
+ if (this.deviceLogging === 'debug') {
365
+ this.errorLog(`Meter: ${this.accessory.displayName} failed refreshStatus with BLE Connection,`
366
+ + ` Error Message: ${JSON.stringify(e.message)}`);
367
+ }
368
+ if (this.platform.debugMode) {
369
+ this.errorLog(`Meter: ${this.accessory.displayName} failed refreshStatus with BLE Connection,`
370
+ + ` Error: ${JSON.stringify(e)}`);
340
371
  }
341
- };
342
- // Wait 10 seconds
343
- return switchbot.wait(this.scanDuration * 1000);
344
- }).then(async () => {
345
- // Stop to monitor
346
- switchbot.stopScan();
347
- if (this.connected) {
348
- this.CurrentTemperatureCached = this.temperature;
349
- this.accessory.context.CurrentTemperature = this.CurrentTemperatureCached;
350
- this.CurrentRelativeHumidityCached = this.humidity!;
351
- this.accessory.context.CurrentRelativeHumidity = this.CurrentRelativeHumidityCached;
352
- this.BatteryLevelCached = this.battery!;
353
- this.accessory.context.BatteryLevel = this.BatteryLevelCached;
354
- this.parseStatus();
355
- this.updateHomeKitCharacteristics();
356
- } else {
357
- this.errorLog(`Meter: ${this.accessory.displayName} wasn't able to establish BLE Connection`);
358
372
  if (this.platform.config.credentials?.openToken) {
359
373
  this.warnLog(`Meter: ${this.accessory.displayName} Using OpenAPI Connection`);
360
374
  this.openAPIRefreshStatus();
361
375
  }
362
- }
363
- }).catch(async (e: any) => {
364
- this.errorLog(`Meter: ${this.accessory.displayName} failed refreshStatus with BLE Connection`);
365
- if (this.deviceLogging === 'debug') {
366
- this.errorLog(`Meter: ${this.accessory.displayName} failed refreshStatus with BLE Connection,`
367
- + ` Error Message: ${JSON.stringify(e.message)}`);
368
- }
369
- if (this.platform.debugMode) {
370
- this.errorLog(`Meter: ${this.accessory.displayName} failed refreshStatus with BLE Connection,`
371
- + ` Error: ${JSON.stringify(e)}`);
372
- }
373
- if (this.platform.config.credentials?.openToken) {
374
- this.warnLog(`Meter: ${this.accessory.displayName} Using OpenAPI Connection`);
375
- this.openAPIRefreshStatus();
376
- }
377
- this.apiError(e);
378
- });
376
+ this.apiError(e);
377
+ });
378
+ } else {
379
+ await this.BLEconnection();
380
+ }
381
+ }
382
+
383
+ private async BLEconnection() {
384
+ this.errorLog(`Meter: ${this.accessory.displayName} wasn't able to establish BLE Connection`);
385
+ if (this.platform.config.credentials?.openToken) {
386
+ this.warnLog(`Meter: ${this.accessory.displayName} Using OpenAPI Connection`);
387
+ await this.openAPIRefreshStatus();
388
+ }
379
389
  }
380
390
 
381
391
  private async openAPIRefreshStatus() {
@@ -1,4 +1,3 @@
1
- import Switchbot from 'node-switchbot';
2
1
  import { interval, Subject } from 'rxjs';
3
2
  import { skipWhile } from 'rxjs/operators';
4
3
  import { SwitchBotPlatform } from '../platform';
@@ -131,12 +130,12 @@ export class Motion {
131
130
  if (this.device.scanDuration) {
132
131
  this.scanDuration = this.accessory.context.scanDuration = this.device.scanDuration;
133
132
  if (this.platform.debugMode || (this.deviceLogging === 'debug')) {
134
- this.warnLog(`Bot: ${this.accessory.displayName} Using Device Config scanDuration: ${this.scanDuration}`);
133
+ this.warnLog(`Motion Sensor: ${this.accessory.displayName} Using Device Config scanDuration: ${this.scanDuration}`);
135
134
  }
136
135
  } else {
137
136
  this.scanDuration = this.accessory.context.scanDuration = 1;
138
137
  if (this.platform.debugMode || (this.deviceLogging === 'debug')) {
139
- this.warnLog(`Bot: ${this.accessory.displayName} Using Default scanDuration: ${this.scanDuration}`);
138
+ this.warnLog(`Motion Sensor: ${this.accessory.displayName} Using Default scanDuration: ${this.scanDuration}`);
140
139
  }
141
140
  }
142
141
  }
@@ -228,9 +227,10 @@ export class Motion {
228
227
  }
229
228
 
230
229
  private connectBLE() {
231
- const switchbot = new Switchbot();
230
+ // Convert to BLE Address
232
231
  this.device.bleMac = ((this.device.deviceId!.match(/.{1,2}/g))!.join(':')).toLowerCase();
233
232
  this.debugLog(`Motion Sensor: ${this.accessory.displayName} BLE Address: ${this.device.bleMac}`);
233
+ const switchbot = this.platform.connectBLE();
234
234
  return switchbot;
235
235
  }
236
236
 
@@ -239,59 +239,68 @@ export class Motion {
239
239
  // eslint-disable-next-line @typescript-eslint/no-var-requires
240
240
  const switchbot = this.connectBLE();
241
241
  // Start to monitor advertisement packets
242
- switchbot.startScan({
243
- model: 's',
244
- id: this.device.bleMac,
245
- }).then(() => {
246
- // Set an event hander
247
- switchbot.onadvertisement = (ad: any) => {
248
- this.serviceData = ad.serviceData;
249
- this.movement = ad.serviceData.movement;
250
- this.battery = ad.serviceData.battery;
251
- this.lightLevel = ad.serviceData.lightLevel;
252
- this.debugLog(`Motion Sensor: ${this.accessory.displayName} serviceData: ${JSON.stringify(ad.serviceData)}`);
253
- this.debugLog(`Motion Sensor: ${this.accessory.displayName} movement: ${ad.serviceData.movement}, lightLevel: `
254
- + `${ad.serviceData.lightLevel}, battery: ${ad.serviceData.battery}`);
255
-
256
- if (this.serviceData) {
257
- this.connected = true;
258
- this.debugLog(`Motion Sensor: ${this.accessory.displayName} connected: ${this.connected}`);
242
+ this.debugLog(`Motion Sensor: ${this.accessory.displayName} platform.Switchbot: ${JSON.stringify(switchbot)}`);
243
+ if (switchbot) {
244
+ switchbot.startScan({
245
+ model: 's',
246
+ id: this.device.bleMac,
247
+ }).then(() => {
248
+ // Set an event hander
249
+ switchbot.onadvertisement = (ad: any) => {
250
+ this.serviceData = ad.serviceData;
251
+ this.movement = ad.serviceData.movement;
252
+ this.battery = ad.serviceData.battery;
253
+ this.lightLevel = ad.serviceData.lightLevel;
254
+ this.debugLog(`Motion Sensor: ${this.accessory.displayName} serviceData: ${JSON.stringify(ad.serviceData)}`);
255
+ this.debugLog(`Motion Sensor: ${this.accessory.displayName} movement: ${ad.serviceData.movement}, lightLevel: `
256
+ + `${ad.serviceData.lightLevel}, battery: ${ad.serviceData.battery}`);
257
+
258
+ if (this.serviceData) {
259
+ this.connected = true;
260
+ this.debugLog(`Motion Sensor: ${this.accessory.displayName} connected: ${this.connected}`);
261
+ } else {
262
+ this.connected = false;
263
+ this.debugLog(`Motion Sensor: ${this.accessory.displayName} connected: ${this.connected}`);
264
+ }
265
+ };
266
+ // Wait 10 seconds
267
+ return switchbot.wait(this.scanDuration * 1000);
268
+ }).then(async () => {
269
+ // Stop to monitor
270
+ switchbot.stopScan();
271
+ if (this.connected) {
272
+ this.parseStatus();
273
+ this.updateHomeKitCharacteristics();
259
274
  } else {
260
- this.connected = false;
261
- this.debugLog(`Motion Sensor: ${this.accessory.displayName} connected: ${this.connected}`);
275
+ await this.BLEconnection();
276
+ }
277
+ }).catch(async (e: any) => {
278
+ this.errorLog(`Motion Sensor: ${this.accessory.displayName} failed refreshStatus with BLE Connection`);
279
+ if (this.deviceLogging === 'debug') {
280
+ this.errorLog(`Motion Sensor: ${this.accessory.displayName} failed refreshStatus with BLE Connection,`
281
+ + ` Error Message: ${JSON.stringify(e.message)}`);
282
+ }
283
+ if (this.platform.debugMode) {
284
+ this.errorLog(`Motion Sensor: ${this.accessory.displayName} failed refreshStatus with BLE Connection,`
285
+ + ` Error: ${JSON.stringify(e)}`);
262
286
  }
263
- };
264
- // Wait 10 seconds
265
- return switchbot.wait(this.scanDuration * 1000);
266
- }).then(async () => {
267
- // Stop to monitor
268
- switchbot.stopScan();
269
- if (this.connected) {
270
- this.parseStatus();
271
- this.updateHomeKitCharacteristics();
272
- } else {
273
- this.errorLog(`Motion Sensor: ${this.accessory.displayName} wasn't able to establish BLE Connection`);
274
287
  if (this.platform.config.credentials?.openToken) {
275
288
  this.warnLog(`Motion Sensor: ${this.accessory.displayName} Using OpenAPI Connection`);
276
289
  await this.openAPIRefreshStatus();
277
290
  }
278
- }
279
- }).catch(async (e: any) => {
280
- this.errorLog(`Motion Sensor: ${this.accessory.displayName} failed refreshStatus with BLE Connection`);
281
- if (this.deviceLogging === 'debug') {
282
- this.errorLog(`Motion Sensor: ${this.accessory.displayName} failed refreshStatus with BLE Connection,`
283
- + ` Error Message: ${JSON.stringify(e.message)}`);
284
- }
285
- if (this.platform.debugMode) {
286
- this.errorLog(`Motion Sensor: ${this.accessory.displayName} failed refreshStatus with BLE Connection,`
287
- + ` Error: ${JSON.stringify(e)}`);
288
- }
289
- if (this.platform.config.credentials?.openToken) {
290
- this.warnLog(`Motion Sensor: ${this.accessory.displayName} Using OpenAPI Connection`);
291
- await this.openAPIRefreshStatus();
292
- }
293
- this.apiError(e);
294
- });
291
+ this.apiError(e);
292
+ });
293
+ } else {
294
+ await this.BLEconnection();
295
+ }
296
+ }
297
+
298
+ private async BLEconnection() {
299
+ this.errorLog(`Motion Sensor: ${this.accessory.displayName} wasn't able to establish BLE Connection`);
300
+ if (this.platform.config.credentials?.openToken) {
301
+ this.warnLog(`Motion Sensor: ${this.accessory.displayName} Using OpenAPI Connection`);
302
+ await this.openAPIRefreshStatus();
303
+ }
295
304
  }
296
305
 
297
306
  private async openAPIRefreshStatus() {
@@ -134,12 +134,12 @@ export class Plug {
134
134
  if (this.device.scanDuration) {
135
135
  this.scanDuration = this.accessory.context.scanDuration = this.device.scanDuration;
136
136
  if (this.platform.debugMode || (this.deviceLogging === 'debug')) {
137
- this.warnLog(`Bot: ${this.accessory.displayName} Using Device Config scanDuration: ${this.scanDuration}`);
137
+ this.warnLog(`Plug: ${this.accessory.displayName} Using Device Config scanDuration: ${this.scanDuration}`);
138
138
  }
139
139
  } else {
140
140
  this.scanDuration = this.accessory.context.scanDuration = 1;
141
141
  if (this.platform.debugMode || (this.deviceLogging === 'debug')) {
142
- this.warnLog(`Bot: ${this.accessory.displayName} Using Default scanDuration: ${this.scanDuration}`);
142
+ this.warnLog(`Plug: ${this.accessory.displayName} Using Default scanDuration: ${this.scanDuration}`);
143
143
  }
144
144
  }
145
145
  }
package/src/platform.ts CHANGED
@@ -41,6 +41,7 @@ export class SwitchBotPlatform implements DynamicPlatformPlugin {
41
41
  registeringDevice!: boolean;
42
42
  debugMode!: boolean;
43
43
  platformLogging?: string;
44
+ switchbot: any;
44
45
 
45
46
  constructor(public readonly log: Logger, public readonly config: SwitchBotPlatformConfig, public readonly api: API) {
46
47
  this.logs();
@@ -192,6 +193,18 @@ export class SwitchBotPlatform implements DynamicPlatformPlugin {
192
193
  }
193
194
  }
194
195
 
196
+ connectBLE() {
197
+ try {
198
+ // eslint-disable-next-line @typescript-eslint/no-var-requires
199
+ const Switchbot = require('node-switchbot');
200
+ this.switchbot = new Switchbot();
201
+ } catch (e) {
202
+ this.switchbot = false;
203
+ this.errorLog(`Was 'node-switchbot' found: ${this.switchbot}`);
204
+ }
205
+ return this.switchbot;
206
+ }
207
+
195
208
  /**
196
209
  * this method discovers the Locations
197
210
  */