@switchbot/homebridge-switchbot 1.6.2-beta.0 → 1.6.3

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 +17 -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 +131 -120
  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 +67 -61
  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 +96 -81
  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 +91 -80
  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 +70 -68
  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 +62 -60
  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 +71 -71
  33. package/src/devices/bots.ts +110 -100
  34. package/src/devices/contact.ts +67 -60
  35. package/src/devices/curtains.ts +95 -80
  36. package/src/devices/humidifiers.ts +90 -79
  37. package/src/devices/meters.ts +70 -67
  38. package/src/devices/motion.ts +62 -59
  39. package/src/devices/plugs.ts +2 -2
  40. package/src/platform.ts +13 -0
@@ -209,12 +209,12 @@ export class Humidifier {
209
209
  if (this.device.scanDuration) {
210
210
  this.scanDuration = this.accessory.context.scanDuration = this.device.scanDuration;
211
211
  if (this.platform.debugMode || (this.deviceLogging === 'debug')) {
212
- this.warnLog(`Bot: ${this.accessory.displayName} Using Device Config scanDuration: ${this.scanDuration}`);
212
+ this.warnLog(`Humidifier: ${this.accessory.displayName} Using Device Config scanDuration: ${this.scanDuration}`);
213
213
  }
214
214
  } else {
215
215
  this.scanDuration = this.accessory.context.scanDuration = 1;
216
216
  if (this.platform.debugMode || (this.deviceLogging === 'debug')) {
217
- this.warnLog(`Bot: ${this.accessory.displayName} Using Default scanDuration: ${this.scanDuration}`);
217
+ this.warnLog(`Humidifier: ${this.accessory.displayName} Using Default scanDuration: ${this.scanDuration}`);
218
218
  }
219
219
  }
220
220
  }
@@ -340,16 +340,11 @@ export class Humidifier {
340
340
  }
341
341
 
342
342
  private connectBLE() {
343
- let Switchbot: any;
344
- try {
345
- Switchbot = require('node-switchbot');
346
- const switchbot = new Switchbot();
347
- this.device.bleMac = ((this.device.deviceId!.match(/.{1,2}/g))!.join(':')).toLowerCase();
348
- this.debugLog(`Bot: ${this.accessory.displayName} BLE Address: ${this.device.bleMac}`);
349
- return switchbot;
350
- } catch (e) {
351
- Switchbot = false;
352
- }
343
+ // Convert to BLE Address
344
+ this.device.bleMac = ((this.device.deviceId!.match(/.{1,2}/g))!.join(':')).toLowerCase();
345
+ this.debugLog(`Humidifier: ${this.accessory.displayName} BLE Address: ${this.device.bleMac}`);
346
+ const switchbot = this.platform.connectBLE();
347
+ return switchbot;
353
348
  }
354
349
 
355
350
  private async BLERefreshStatus() {
@@ -357,59 +352,67 @@ export class Humidifier {
357
352
  // eslint-disable-next-line @typescript-eslint/no-var-requires
358
353
  const switchbot = this.connectBLE();
359
354
  // Start to monitor advertisement packets
360
- switchbot.startScan({
361
- model: 'e',
362
- id: this.device.bleMac,
363
- }).then(() => {
364
- // Set an event hander
365
- switchbot.onadvertisement = (ad: ad) => {
366
- this.serviceData = ad.serviceData;
367
- this.autoMode = ad.serviceData.autoMode;
368
- this.onState = ad.serviceData.onState;
369
- this.percentage = ad.serviceData.percentage;
370
- this.debugLog(`Humidifier: ${this.accessory.displayName} serviceData: ${JSON.stringify(ad.serviceData)}`);
371
- this.debugLog(`Humidifier: ${this.accessory.displayName} model: ${ad.serviceData.model}, modelName: ${ad.serviceData.modelName},`
372
- + `autoMode: ${ad.serviceData.autoMode}, onState: ${ad.serviceData.onState}, percentage: ${ad.serviceData.percentage}`);
373
-
374
- if (this.serviceData) {
375
- this.connected = true;
376
- this.debugLog(`Humidifier: ${this.accessory.displayName} connected: ${this.connected}`);
355
+ if (switchbot !== false) {
356
+ switchbot.startScan({
357
+ model: 'e',
358
+ id: this.device.bleMac,
359
+ }).then(() => {
360
+ // Set an event hander
361
+ switchbot.onadvertisement = (ad: ad) => {
362
+ this.serviceData = ad.serviceData;
363
+ this.autoMode = ad.serviceData.autoMode;
364
+ this.onState = ad.serviceData.onState;
365
+ this.percentage = ad.serviceData.percentage;
366
+ this.debugLog(`Humidifier: ${this.accessory.displayName} serviceData: ${JSON.stringify(ad.serviceData)}`);
367
+ this.debugLog(`Humidifier: ${this.accessory.displayName} model: ${ad.serviceData.model}, modelName: ${ad.serviceData.modelName},`
368
+ + `autoMode: ${ad.serviceData.autoMode}, onState: ${ad.serviceData.onState}, percentage: ${ad.serviceData.percentage}`);
369
+
370
+ if (this.serviceData) {
371
+ this.connected = true;
372
+ this.debugLog(`Humidifier: ${this.accessory.displayName} connected: ${this.connected}`);
373
+ } else {
374
+ this.connected = false;
375
+ this.debugLog(`Humidifier: ${this.accessory.displayName} connected: ${this.connected}`);
376
+ }
377
+ };
378
+ // Wait 2 seconds
379
+ return switchbot.wait(this.scanDuration * 1000);
380
+ }).then(async () => {
381
+ // Stop to monitor
382
+ switchbot.stopScan();
383
+ if (this.connected) {
384
+ this.parseStatus();
385
+ this.updateHomeKitCharacteristics();
377
386
  } else {
378
- this.connected = false;
379
- this.debugLog(`Humidifier: ${this.accessory.displayName} connected: ${this.connected}`);
387
+ await this.BLEconnection();
388
+ }
389
+ }).catch(async (e: any) => {
390
+ this.errorLog(`Humidifier: ${this.accessory.displayName} failed refreshStatus with BLE Connection`);
391
+ if (this.deviceLogging === 'debug') {
392
+ this.errorLog(`Humidifier: ${this.accessory.displayName} failed refreshStatus with BLE Connection,`
393
+ + ` Error Message: ${JSON.stringify(e.message)}`);
394
+ }
395
+ if (this.platform.debugMode) {
396
+ this.errorLog(`Humidifier: ${this.accessory.displayName} failed refreshStatus with BLE Connection,`
397
+ + ` Error: ${JSON.stringify(e)}`);
380
398
  }
381
- };
382
- // Wait 2 seconds
383
- return switchbot.wait(this.scanDuration * 1000);
384
- }).then(async () => {
385
- // Stop to monitor
386
- switchbot.stopScan();
387
- if (this.connected) {
388
- this.parseStatus();
389
- this.updateHomeKitCharacteristics();
390
- } else {
391
- this.errorLog(`Humidifier: ${this.accessory.displayName} wasn't able to establish BLE Connection`);
392
399
  if (this.platform.config.credentials?.openToken) {
393
400
  this.warnLog(`Humidifier: ${this.accessory.displayName} Using OpenAPI Connection`);
394
401
  await this.openAPIRefreshStatus();
395
402
  }
396
- }
397
- }).catch(async (e: any) => {
398
- this.errorLog(`Humidifier: ${this.accessory.displayName} failed refreshStatus with BLE Connection`);
399
- if (this.deviceLogging === 'debug') {
400
- this.errorLog(`Humidifier: ${this.accessory.displayName} failed refreshStatus with BLE Connection,`
401
- + ` Error Message: ${JSON.stringify(e.message)}`);
402
- }
403
- if (this.platform.debugMode) {
404
- this.errorLog(`Humidifier: ${this.accessory.displayName} failed refreshStatus with BLE Connection,`
405
- + ` Error: ${JSON.stringify(e)}`);
406
- }
407
- if (this.platform.config.credentials?.openToken) {
408
- this.warnLog(`Humidifier: ${this.accessory.displayName} Using OpenAPI Connection`);
409
- await this.openAPIRefreshStatus();
410
- }
411
- this.apiError(e);
412
- });
403
+ this.apiError(e);
404
+ });
405
+ } else {
406
+ await this.BLEconnection();
407
+ }
408
+ }
409
+
410
+ private async BLEconnection() {
411
+ this.errorLog(`Humidifier: ${this.accessory.displayName} wasn't able to establish BLE Connection`);
412
+ if (this.platform.config.credentials?.openToken) {
413
+ this.warnLog(`Humidifier: ${this.accessory.displayName} Using OpenAPI Connection`);
414
+ await this.openAPIRefreshStatus();
415
+ }
413
416
  }
414
417
 
415
418
  private async openAPIRefreshStatus() {
@@ -446,7 +449,7 @@ export class Humidifier {
446
449
  //if (this.device.ble) {
447
450
  // await this.BLEpushChanges();
448
451
  //} else {
449
- await this.OpenAPIpushChanges();
452
+ await this.openAPIpushChanges();
450
453
  //}
451
454
  interval(5000)
452
455
  .pipe(skipWhile(() => this.humidifierUpdateInProgress))
@@ -459,30 +462,38 @@ export class Humidifier {
459
462
  private async BLEpushChanges() {
460
463
  this.debugLog(`Humidifier: ${this.accessory.displayName} BLE pushChanges`);
461
464
  const switchbot = this.connectBLE();
462
- switchbot.discover({ model: 'e', quick: true, id: this.device.bleMac }).then((device_list) => {
463
- this.infoLog(`${this.accessory.displayName} Target Position: ${this.Active}`);
464
- return device_list[0].percentage(this.RelativeHumidityHumidifierThreshold);
465
- }).then(() => {
466
- this.debugLog(`Humidifier: ${this.accessory.displayName} Done.`);
467
- }).catch(async (e: any) => {
468
- this.errorLog(`Humidifier: ${this.accessory.displayName} failed pushChanges with BLE Connection`);
469
- if (this.deviceLogging === 'debug') {
470
- this.errorLog(`Humidifier: ${this.accessory.displayName} failed pushChanges with BLE Connection,`
471
- + ` Error Message: ${JSON.stringify(e.message)}`);
472
- }
473
- if (this.platform.debugMode) {
474
- this.errorLog(`Humidifier: ${this.accessory.displayName} failed pushChanges with BLE Connection,`
475
- + ` Error: ${JSON.stringify(e)}`);
476
- }
465
+ if (switchbot !== false) {
466
+ switchbot.discover({ model: 'e', quick: true, id: this.device.bleMac }).then((device_list) => {
467
+ this.infoLog(`${this.accessory.displayName} Target Position: ${this.Active}`);
468
+ return device_list[0].percentage(this.RelativeHumidityHumidifierThreshold);
469
+ }).then(() => {
470
+ this.debugLog(`Humidifier: ${this.accessory.displayName} Done.`);
471
+ }).catch(async (e: any) => {
472
+ this.errorLog(`Humidifier: ${this.accessory.displayName} failed pushChanges with BLE Connection`);
473
+ if (this.deviceLogging === 'debug') {
474
+ this.errorLog(`Humidifier: ${this.accessory.displayName} failed pushChanges with BLE Connection,`
475
+ + ` Error Message: ${JSON.stringify(e.message)}`);
476
+ }
477
+ if (this.platform.debugMode) {
478
+ this.errorLog(`Humidifier: ${this.accessory.displayName} failed pushChanges with BLE Connection,`
479
+ + ` Error: ${JSON.stringify(e)}`);
480
+ }
481
+ if (this.platform.config.credentials?.openToken) {
482
+ this.warnLog(`Humidifier: ${this.accessory.displayName} Using OpenAPI Connection`);
483
+ await this.openAPIpushChanges();
484
+ }
485
+ this.apiError(e);
486
+ });
487
+ } else {
488
+ this.errorLog(`Humidifier: ${this.accessory.displayName} wasn't able to establish BLE Connection`);
477
489
  if (this.platform.config.credentials?.openToken) {
478
490
  this.warnLog(`Humidifier: ${this.accessory.displayName} Using OpenAPI Connection`);
479
- await this.OpenAPIpushChanges();
491
+ await this.openAPIpushChanges();
480
492
  }
481
- this.apiError(e);
482
- });
493
+ }
483
494
  }
484
495
 
485
- private async OpenAPIpushChanges() {
496
+ private async openAPIpushChanges() {
486
497
  if (this.platform.config.credentials?.openToken) {
487
498
  this.debugLog(`Humidifier: ${this.accessory.displayName} OpenAPI pushChanges`);
488
499
  if (
@@ -199,12 +199,12 @@ export class Meter {
199
199
  if (this.device.scanDuration) {
200
200
  this.scanDuration = this.accessory.context.scanDuration = this.device.scanDuration;
201
201
  if (this.platform.debugMode || (this.deviceLogging === 'debug')) {
202
- 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}`);
203
203
  }
204
204
  } else {
205
205
  this.scanDuration = this.accessory.context.scanDuration = 1;
206
206
  if (this.platform.debugMode || (this.deviceLogging === 'debug')) {
207
- this.warnLog(`Bot: ${this.accessory.displayName} Using Default scanDuration: ${this.scanDuration}`);
207
+ this.warnLog(`Meter: ${this.accessory.displayName} Using Default scanDuration: ${this.scanDuration}`);
208
208
  }
209
209
  }
210
210
  }
@@ -304,84 +304,87 @@ export class Meter {
304
304
  }
305
305
 
306
306
  private connectBLE() {
307
- let Switchbot: any;
308
- try {
309
- Switchbot = require('node-switchbot');
310
- const switchbot = new Switchbot();
311
- this.device.bleMac = ((this.device.deviceId!.match(/.{1,2}/g))!.join(':')).toLowerCase();
312
- this.debugLog(`Bot: ${this.accessory.displayName} BLE Address: ${this.device.bleMac}`);
313
- return switchbot;
314
- } catch (e) {
315
- Switchbot = false;
316
- }
307
+ // Convert to BLE Address
308
+ this.device.bleMac = ((this.device.deviceId!.match(/.{1,2}/g))!.join(':')).toLowerCase();
309
+ this.debugLog(`Meter: ${this.accessory.displayName} BLE Address: ${this.device.bleMac}`);
310
+ const switchbot = this.platform.connectBLE();
311
+ return switchbot;
317
312
  }
318
313
 
319
314
 
320
- private BLErefreshStatus() {
315
+ private async BLErefreshStatus() {
321
316
  this.debugLog(`Meter: ${this.accessory.displayName} BLE RefreshStatus`);
322
317
  // eslint-disable-next-line @typescript-eslint/no-var-requires
323
318
  const switchbot = this.connectBLE();
324
319
  // Start to monitor advertisement packets
325
- switchbot.startScan({
326
- model: 'T',
327
- id: this.device.bleMac,
328
- }).then(() => {
329
- // Set an event hander
330
- switchbot.onadvertisement = (ad: ad) => {
331
- this.serviceData = ad.serviceData;
332
- this.temperature = ad.serviceData.temperature!.c;
333
- this.humidity = ad.serviceData.humidity;
334
- this.battery = ad.serviceData.battery;
335
- this.debugLog(`Meter: ${this.accessory.displayName} serviceData: ${JSON.stringify(ad.serviceData)}`);
336
- this.debugLog(`Meter: ${this.accessory.displayName} model: ${ad.serviceData.model}, modelName: ${ad.serviceData.modelName}, `
337
- + `temperature: ${JSON.stringify(ad.serviceData.temperature?.c)}, humidity: ${ad.serviceData.humidity}, `
338
- + `battery: ${ad.serviceData.battery}`);
339
-
340
- if (this.serviceData) {
341
- this.connected = true;
342
- this.debugLog(`Meter: ${this.accessory.displayName} connected: ${this.connected}`);
320
+ if (switchbot !== false) {
321
+ switchbot.startScan({
322
+ model: 'T',
323
+ id: this.device.bleMac,
324
+ }).then(() => {
325
+ // Set an event hander
326
+ switchbot.onadvertisement = (ad: ad) => {
327
+ this.serviceData = ad.serviceData;
328
+ this.temperature = ad.serviceData.temperature!.c;
329
+ this.humidity = ad.serviceData.humidity;
330
+ this.battery = ad.serviceData.battery;
331
+ this.debugLog(`Meter: ${this.accessory.displayName} serviceData: ${JSON.stringify(ad.serviceData)}`);
332
+ this.debugLog(`Meter: ${this.accessory.displayName} model: ${ad.serviceData.model}, modelName: ${ad.serviceData.modelName}, `
333
+ + `temperature: ${JSON.stringify(ad.serviceData.temperature?.c)}, humidity: ${ad.serviceData.humidity}, `
334
+ + `battery: ${ad.serviceData.battery}`);
335
+
336
+ if (this.serviceData) {
337
+ this.connected = true;
338
+ this.debugLog(`Meter: ${this.accessory.displayName} connected: ${this.connected}`);
339
+ } else {
340
+ this.connected = false;
341
+ this.debugLog(`Meter: ${this.accessory.displayName} connected: ${this.connected}`);
342
+ }
343
+ };
344
+ // Wait 10 seconds
345
+ return switchbot.wait(this.scanDuration * 1000);
346
+ }).then(async () => {
347
+ // Stop to monitor
348
+ switchbot.stopScan();
349
+ if (this.connected) {
350
+ this.CurrentTemperatureCached = this.temperature;
351
+ this.accessory.context.CurrentTemperature = this.CurrentTemperatureCached;
352
+ this.CurrentRelativeHumidityCached = this.humidity!;
353
+ this.accessory.context.CurrentRelativeHumidity = this.CurrentRelativeHumidityCached;
354
+ this.BatteryLevelCached = this.battery!;
355
+ this.accessory.context.BatteryLevel = this.BatteryLevelCached;
356
+ this.parseStatus();
357
+ this.updateHomeKitCharacteristics();
343
358
  } else {
344
- this.connected = false;
345
- this.debugLog(`Meter: ${this.accessory.displayName} connected: ${this.connected}`);
359
+ await this.BLEconnection();
360
+ }
361
+ }).catch(async (e: any) => {
362
+ this.errorLog(`Meter: ${this.accessory.displayName} failed refreshStatus with BLE Connection`);
363
+ if (this.deviceLogging === 'debug') {
364
+ this.errorLog(`Meter: ${this.accessory.displayName} failed refreshStatus with BLE Connection,`
365
+ + ` Error Message: ${JSON.stringify(e.message)}`);
366
+ }
367
+ if (this.platform.debugMode) {
368
+ this.errorLog(`Meter: ${this.accessory.displayName} failed refreshStatus with BLE Connection,`
369
+ + ` Error: ${JSON.stringify(e)}`);
346
370
  }
347
- };
348
- // Wait 10 seconds
349
- return switchbot.wait(this.scanDuration * 1000);
350
- }).then(async () => {
351
- // Stop to monitor
352
- switchbot.stopScan();
353
- if (this.connected) {
354
- this.CurrentTemperatureCached = this.temperature;
355
- this.accessory.context.CurrentTemperature = this.CurrentTemperatureCached;
356
- this.CurrentRelativeHumidityCached = this.humidity!;
357
- this.accessory.context.CurrentRelativeHumidity = this.CurrentRelativeHumidityCached;
358
- this.BatteryLevelCached = this.battery!;
359
- this.accessory.context.BatteryLevel = this.BatteryLevelCached;
360
- this.parseStatus();
361
- this.updateHomeKitCharacteristics();
362
- } else {
363
- this.errorLog(`Meter: ${this.accessory.displayName} wasn't able to establish BLE Connection`);
364
371
  if (this.platform.config.credentials?.openToken) {
365
372
  this.warnLog(`Meter: ${this.accessory.displayName} Using OpenAPI Connection`);
366
373
  this.openAPIRefreshStatus();
367
374
  }
368
- }
369
- }).catch(async (e: any) => {
370
- this.errorLog(`Meter: ${this.accessory.displayName} failed refreshStatus with BLE Connection`);
371
- if (this.deviceLogging === 'debug') {
372
- this.errorLog(`Meter: ${this.accessory.displayName} failed refreshStatus with BLE Connection,`
373
- + ` Error Message: ${JSON.stringify(e.message)}`);
374
- }
375
- if (this.platform.debugMode) {
376
- this.errorLog(`Meter: ${this.accessory.displayName} failed refreshStatus with BLE Connection,`
377
- + ` Error: ${JSON.stringify(e)}`);
378
- }
379
- if (this.platform.config.credentials?.openToken) {
380
- this.warnLog(`Meter: ${this.accessory.displayName} Using OpenAPI Connection`);
381
- this.openAPIRefreshStatus();
382
- }
383
- this.apiError(e);
384
- });
375
+ this.apiError(e);
376
+ });
377
+ } else {
378
+ await this.BLEconnection();
379
+ }
380
+ }
381
+
382
+ private async BLEconnection() {
383
+ this.errorLog(`Meter: ${this.accessory.displayName} wasn't able to establish BLE Connection`);
384
+ if (this.platform.config.credentials?.openToken) {
385
+ this.warnLog(`Meter: ${this.accessory.displayName} Using OpenAPI Connection`);
386
+ await this.openAPIRefreshStatus();
387
+ }
385
388
  }
386
389
 
387
390
  private async openAPIRefreshStatus() {
@@ -130,12 +130,12 @@ export class Motion {
130
130
  if (this.device.scanDuration) {
131
131
  this.scanDuration = this.accessory.context.scanDuration = this.device.scanDuration;
132
132
  if (this.platform.debugMode || (this.deviceLogging === 'debug')) {
133
- 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}`);
134
134
  }
135
135
  } else {
136
136
  this.scanDuration = this.accessory.context.scanDuration = 1;
137
137
  if (this.platform.debugMode || (this.deviceLogging === 'debug')) {
138
- this.warnLog(`Bot: ${this.accessory.displayName} Using Default scanDuration: ${this.scanDuration}`);
138
+ this.warnLog(`Motion Sensor: ${this.accessory.displayName} Using Default scanDuration: ${this.scanDuration}`);
139
139
  }
140
140
  }
141
141
  }
@@ -227,16 +227,11 @@ export class Motion {
227
227
  }
228
228
 
229
229
  private connectBLE() {
230
- let Switchbot: any;
231
- try {
232
- Switchbot = require('node-switchbot');
233
- const switchbot = new Switchbot();
234
- this.device.bleMac = ((this.device.deviceId!.match(/.{1,2}/g))!.join(':')).toLowerCase();
235
- this.debugLog(`Bot: ${this.accessory.displayName} BLE Address: ${this.device.bleMac}`);
236
- return switchbot;
237
- } catch (e) {
238
- Switchbot = false;
239
- }
230
+ // Convert to BLE Address
231
+ this.device.bleMac = ((this.device.deviceId!.match(/.{1,2}/g))!.join(':')).toLowerCase();
232
+ this.debugLog(`Motion Sensor: ${this.accessory.displayName} BLE Address: ${this.device.bleMac}`);
233
+ const switchbot = this.platform.connectBLE();
234
+ return switchbot;
240
235
  }
241
236
 
242
237
  private async BLERefreshStatus() {
@@ -244,59 +239,67 @@ export class Motion {
244
239
  // eslint-disable-next-line @typescript-eslint/no-var-requires
245
240
  const switchbot = this.connectBLE();
246
241
  // Start to monitor advertisement packets
247
- switchbot.startScan({
248
- model: 's',
249
- id: this.device.bleMac,
250
- }).then(() => {
251
- // Set an event hander
252
- switchbot.onadvertisement = (ad: any) => {
253
- this.serviceData = ad.serviceData;
254
- this.movement = ad.serviceData.movement;
255
- this.battery = ad.serviceData.battery;
256
- this.lightLevel = ad.serviceData.lightLevel;
257
- this.debugLog(`Motion Sensor: ${this.accessory.displayName} serviceData: ${JSON.stringify(ad.serviceData)}`);
258
- this.debugLog(`Motion Sensor: ${this.accessory.displayName} movement: ${ad.serviceData.movement}, lightLevel: `
259
- + `${ad.serviceData.lightLevel}, battery: ${ad.serviceData.battery}`);
260
-
261
- if (this.serviceData) {
262
- this.connected = true;
263
- this.debugLog(`Motion Sensor: ${this.accessory.displayName} connected: ${this.connected}`);
242
+ if (switchbot !== false) {
243
+ switchbot.startScan({
244
+ model: 's',
245
+ id: this.device.bleMac,
246
+ }).then(() => {
247
+ // Set an event hander
248
+ switchbot.onadvertisement = (ad: any) => {
249
+ this.serviceData = ad.serviceData;
250
+ this.movement = ad.serviceData.movement;
251
+ this.battery = ad.serviceData.battery;
252
+ this.lightLevel = ad.serviceData.lightLevel;
253
+ this.debugLog(`Motion Sensor: ${this.accessory.displayName} serviceData: ${JSON.stringify(ad.serviceData)}`);
254
+ this.debugLog(`Motion Sensor: ${this.accessory.displayName} movement: ${ad.serviceData.movement}, lightLevel: `
255
+ + `${ad.serviceData.lightLevel}, battery: ${ad.serviceData.battery}`);
256
+
257
+ if (this.serviceData) {
258
+ this.connected = true;
259
+ this.debugLog(`Motion Sensor: ${this.accessory.displayName} connected: ${this.connected}`);
260
+ } else {
261
+ this.connected = false;
262
+ this.debugLog(`Motion Sensor: ${this.accessory.displayName} connected: ${this.connected}`);
263
+ }
264
+ };
265
+ // Wait 10 seconds
266
+ return switchbot.wait(this.scanDuration * 1000);
267
+ }).then(async () => {
268
+ // Stop to monitor
269
+ switchbot.stopScan();
270
+ if (this.connected) {
271
+ this.parseStatus();
272
+ this.updateHomeKitCharacteristics();
264
273
  } else {
265
- this.connected = false;
266
- this.debugLog(`Motion Sensor: ${this.accessory.displayName} connected: ${this.connected}`);
274
+ await this.BLEconnection();
275
+ }
276
+ }).catch(async (e: any) => {
277
+ this.errorLog(`Motion Sensor: ${this.accessory.displayName} failed refreshStatus with BLE Connection`);
278
+ if (this.deviceLogging === 'debug') {
279
+ this.errorLog(`Motion Sensor: ${this.accessory.displayName} failed refreshStatus with BLE Connection,`
280
+ + ` Error Message: ${JSON.stringify(e.message)}`);
281
+ }
282
+ if (this.platform.debugMode) {
283
+ this.errorLog(`Motion Sensor: ${this.accessory.displayName} failed refreshStatus with BLE Connection,`
284
+ + ` Error: ${JSON.stringify(e)}`);
267
285
  }
268
- };
269
- // Wait 10 seconds
270
- return switchbot.wait(this.scanDuration * 1000);
271
- }).then(async () => {
272
- // Stop to monitor
273
- switchbot.stopScan();
274
- if (this.connected) {
275
- this.parseStatus();
276
- this.updateHomeKitCharacteristics();
277
- } else {
278
- this.errorLog(`Motion Sensor: ${this.accessory.displayName} wasn't able to establish BLE Connection`);
279
286
  if (this.platform.config.credentials?.openToken) {
280
287
  this.warnLog(`Motion Sensor: ${this.accessory.displayName} Using OpenAPI Connection`);
281
288
  await this.openAPIRefreshStatus();
282
289
  }
283
- }
284
- }).catch(async (e: any) => {
285
- this.errorLog(`Motion Sensor: ${this.accessory.displayName} failed refreshStatus with BLE Connection`);
286
- if (this.deviceLogging === 'debug') {
287
- this.errorLog(`Motion Sensor: ${this.accessory.displayName} failed refreshStatus with BLE Connection,`
288
- + ` Error Message: ${JSON.stringify(e.message)}`);
289
- }
290
- if (this.platform.debugMode) {
291
- this.errorLog(`Motion Sensor: ${this.accessory.displayName} failed refreshStatus with BLE Connection,`
292
- + ` Error: ${JSON.stringify(e)}`);
293
- }
294
- if (this.platform.config.credentials?.openToken) {
295
- this.warnLog(`Motion Sensor: ${this.accessory.displayName} Using OpenAPI Connection`);
296
- await this.openAPIRefreshStatus();
297
- }
298
- this.apiError(e);
299
- });
290
+ this.apiError(e);
291
+ });
292
+ } else {
293
+ await this.BLEconnection();
294
+ }
295
+ }
296
+
297
+ private async BLEconnection() {
298
+ this.errorLog(`Motion Sensor: ${this.accessory.displayName} wasn't able to establish BLE Connection`);
299
+ if (this.platform.config.credentials?.openToken) {
300
+ this.warnLog(`Motion Sensor: ${this.accessory.displayName} Using OpenAPI Connection`);
301
+ await this.openAPIRefreshStatus();
302
+ }
300
303
  }
301
304
 
302
305
  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
  */