@switchbot/homebridge-switchbot 1.6.4-beta.2 → 1.6.4-beta.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.
@@ -15,6 +15,7 @@ export class Bot {
15
15
  // Services
16
16
  private switchService?: Service;
17
17
  private doorService?: Service;
18
+ private lockService?: Service;
18
19
  private garageDoorService?: Service;
19
20
  private outletService?: Service;
20
21
  private batteryService?: Service;
@@ -97,6 +98,7 @@ export class Bot {
97
98
  // get the LightBulb service if it exists, otherwise create a new LightBulb service
98
99
  // you can create multiple services for each accessory
99
100
  if (device.bot?.deviceType === 'switch') {
101
+ this.removeLockService(accessory);
100
102
  this.removeDoorService(accessory);
101
103
  this.removeOutletService(accessory);
102
104
  this.removeGarageDoorService(accessory);
@@ -110,6 +112,7 @@ export class Bot {
110
112
  this.switchService.setCharacteristic(this.platform.Characteristic.Name, accessory.displayName);
111
113
  this.switchService.getCharacteristic(this.platform.Characteristic.On).onSet(this.handleOnSet.bind(this));
112
114
  } else if (device.bot?.deviceType === 'garagedoor') {
115
+ this.removeLockService(accessory);
113
116
  this.removeDoorService(accessory);
114
117
  this.removeOutletService(accessory);
115
118
  this.removeSwitchService(accessory);
@@ -124,6 +127,7 @@ export class Bot {
124
127
  this.garageDoorService.getCharacteristic(this.platform.Characteristic.TargetDoorState).onSet(this.handleOnSet.bind(this));
125
128
  this.garageDoorService.setCharacteristic(this.platform.Characteristic.ObstructionDetected, false);
126
129
  } else if (device.bot?.deviceType === 'door') {
130
+ this.removeLockService(accessory);
127
131
  this.removeOutletService(accessory);
128
132
  this.removeSwitchService(accessory);
129
133
  this.removeGarageDoorService(accessory);
@@ -132,7 +136,7 @@ export class Bot {
132
136
  (this.doorService =
133
137
  accessory.getService(this.platform.Service.Door) ||
134
138
  accessory.addService(this.platform.Service.Door)), `${accessory.displayName} Door`;
135
- this.infoLog(`Bot: ${accessory.displayName} Displaying as Garage Door Opener`);
139
+ this.infoLog(`Bot: ${accessory.displayName} Displaying as Door`);
136
140
 
137
141
  this.doorService.setCharacteristic(this.platform.Characteristic.Name, accessory.displayName);
138
142
  this.doorService.getCharacteristic(this.platform.Characteristic.TargetPosition)
@@ -143,7 +147,22 @@ export class Bot {
143
147
  minStep: 100,
144
148
  }).onSet(this.handleOnSet.bind(this));
145
149
  this.doorService.setCharacteristic(this.platform.Characteristic.PositionState, this.platform.Characteristic.PositionState.STOPPED);
150
+ } else if (device.bot?.deviceType === 'lock') {
151
+ this.removeDoorService(accessory);
152
+ this.removeOutletService(accessory);
153
+ this.removeSwitchService(accessory);
154
+ this.removeGarageDoorService(accessory);
155
+
156
+ // Add switchService
157
+ (this.lockService =
158
+ accessory.getService(this.platform.Service.LockMechanism) ||
159
+ accessory.addService(this.platform.Service.LockMechanism)), `${accessory.displayName} Lock`;
160
+ this.infoLog(`Bot: ${accessory.displayName} Displaying as Lock`);
161
+
162
+ this.lockService.setCharacteristic(this.platform.Characteristic.Name, accessory.displayName);
163
+ this.lockService.getCharacteristic(this.platform.Characteristic.LockTargetState).onSet(this.handleOnSet.bind(this));
146
164
  } else {
165
+ this.removeLockService(accessory);
147
166
  this.removeDoorService(accessory);
148
167
  this.removeSwitchService(accessory);
149
168
  this.removeGarageDoorService(accessory);
@@ -234,6 +253,15 @@ export class Bot {
234
253
  accessory.removeService(this.doorService!);
235
254
  }
236
255
 
256
+ private removeLockService(accessory: PlatformAccessory) {
257
+ // If lockService still pressent, then remove first
258
+ if (this.lockService) {
259
+ this.debugLog(`Bot: ${accessory.displayName} Removing Leftover lockService first`);
260
+ }
261
+ this.lockService = this.accessory.getService(this.platform.Service.Door);
262
+ accessory.removeService(this.lockService!);
263
+ }
264
+
237
265
  private removeSwitchService(accessory: PlatformAccessory) {
238
266
  // If switchService still pressent, then remove first
239
267
  if (this.switchService) {
@@ -379,11 +407,27 @@ export class Bot {
379
407
  }
380
408
  }
381
409
 
382
- private connectBLE() {
383
- // Convert to BLE Address
384
- this.device.bleMac = ((this.device.deviceId!.match(/.{1,2}/g))!.join(':')).toLowerCase();
385
- this.debugLog(`Bot: ${this.accessory.displayName} BLE Address: ${this.device.bleMac}`);
386
- const switchbot = this.platform.connectBLE();
410
+ public async connectBLE() {
411
+ let switchbot: any;
412
+ try {
413
+ // eslint-disable-next-line @typescript-eslint/no-var-requires
414
+ const Switchbot = require('node-switchbot');
415
+ switchbot = new Switchbot();
416
+ // Convert to BLE Address
417
+ this.device.bleMac = ((this.device.deviceId!.match(/.{1,2}/g))!.join(':')).toLowerCase();
418
+ this.debugLog(`Bot: ${this.accessory.displayName} BLE Address: ${this.device.bleMac}`);
419
+ } catch (e: any) {
420
+ switchbot = false;
421
+ this.errorLog(`Bot: ${this.accessory.displayName} 'node-switchbot' found: ${switchbot}`);
422
+ if (this.deviceLogging === 'debug') {
423
+ this.errorLog(`Bot: ${this.accessory.displayName} 'node-switchbot' found: ${switchbot},`
424
+ + ` Error Message: ${JSON.stringify(e.message)}`);
425
+ }
426
+ if (this.platform.debugMode) {
427
+ this.errorLog(`Bot: ${this.accessory.displayName} 'node-switchbot' found: ${switchbot},`
428
+ + ` Error: ${JSON.stringify(e)}`);
429
+ }
430
+ }
387
431
  return switchbot;
388
432
  }
389
433
 
@@ -391,7 +435,7 @@ export class Bot {
391
435
  this.debugLog(`Bot: ${this.accessory.displayName} BLE refreshStatus`);
392
436
 
393
437
  // Convert to BLE Address
394
- const switchbot = this.connectBLE();
438
+ const switchbot = await this.connectBLE();
395
439
  // Start to monitor advertisement packets
396
440
  if (switchbot !== false) {
397
441
  switchbot.startScan({
@@ -425,7 +469,7 @@ export class Bot {
425
469
  this.parseStatus();
426
470
  this.updateHomeKitCharacteristics();
427
471
  } else {
428
- await this.BLEconnection();
472
+ await this.BLEconnection(switchbot);
429
473
  }
430
474
  }).catch(async (e: any) => {
431
475
  this.errorLog(`Bot: ${this.accessory.displayName} failed refreshStatus with BLE Connection`);
@@ -444,12 +488,12 @@ export class Bot {
444
488
  this.apiError(e);
445
489
  });
446
490
  } else {
447
- await this.BLEconnection();
491
+ await this.BLEconnection(switchbot);
448
492
  }
449
493
  }
450
494
 
451
- private async BLEconnection() {
452
- this.errorLog(`Bot: ${this.accessory.displayName} wasn't able to establish BLE Connection`);
495
+ public async BLEconnection(switchbot: any) {
496
+ this.errorLog(`Bot: ${this.accessory.displayName} wasn't able to establish BLE Connection, node-switchbot: ${switchbot}`);
453
497
  if (this.platform.config.credentials?.openToken) {
454
498
  this.warnLog(`Bot: ${this.accessory.displayName} Using OpenAPI Connection`);
455
499
  await this.openAPIRefreshStatus();
@@ -503,7 +547,7 @@ export class Bot {
503
547
  private async BLEpushChanges() {
504
548
  if (this.On !== this.OnCached) {
505
549
  this.debugLog(`Bot: ${this.accessory.displayName} BLE pushChanges`);
506
- const switchbot = this.connectBLE();
550
+ const switchbot = await this.connectBLE();
507
551
  if (switchbot !== false) {
508
552
  if (this.botMode === 'press') {
509
553
  this.debugLog(`Bot: ${this.accessory.displayName} Bot Mode: ${this.botMode}`);
@@ -661,7 +705,7 @@ export class Bot {
661
705
  this.debugLog(`Bot: ${this.accessory.displayName} updateCharacteristic TargetDoorState: Open, CurrentDoorState: Open`);
662
706
  }
663
707
  }
664
- this.debugLog(`Bot: ${this.accessory.displayName} updateCharacteristic On: ${this.On}`);
708
+ this.debugLog(`Bot: ${this.accessory.displayName} Garage Door On: ${this.On}`);
665
709
  } else if (this.device.bot?.deviceType === 'door') {
666
710
  if (this.On === undefined) {
667
711
  this.debugLog(`Bot: ${this.accessory.displayName} On: ${this.On}`);
@@ -680,7 +724,26 @@ export class Bot {
680
724
  this.debugLog(`Bot: ${this.accessory.displayName} updateCharacteristic TargetPosition: 0, CurrentPosition: 0`);
681
725
  }
682
726
  }
683
- this.debugLog(`Bot: ${this.accessory.displayName} updateCharacteristic On: ${this.On}`);
727
+ this.debugLog(`Bot: ${this.accessory.displayName} Door On: ${this.On}`);
728
+ } else if (this.device.bot?.deviceType === 'lock') {
729
+ if (this.On === undefined) {
730
+ this.debugLog(`Bot: ${this.accessory.displayName} On: ${this.On}`);
731
+ } else {
732
+ if (this.On) {
733
+ this.doorService?.updateCharacteristic(this.platform.Characteristic.LockTargetState,
734
+ this.platform.Characteristic.LockTargetState.UNSECURED);
735
+ this.doorService?.updateCharacteristic(this.platform.Characteristic.LockCurrentState,
736
+ this.platform.Characteristic.LockCurrentState.UNSECURED);
737
+ this.debugLog(`Bot: ${this.accessory.displayName} updateCharacteristic LockTargetState: UNSECURED, LockCurrentState: UNSECURED`);
738
+ } else {
739
+ this.doorService?.updateCharacteristic(this.platform.Characteristic.LockTargetState,
740
+ this.platform.Characteristic.LockTargetState.SECURED);
741
+ this.doorService?.updateCharacteristic(this.platform.Characteristic.LockCurrentState,
742
+ this.platform.Characteristic.LockCurrentState.SECURED);
743
+ this.debugLog(`Bot: ${this.accessory.displayName} updateCharacteristic LockTargetState: SECURED, LockCurrentState: SECURED`);
744
+ }
745
+ }
746
+ this.debugLog(`Bot: ${this.accessory.displayName} Lock On: ${this.On}`);
684
747
  } else if (this.device.bot?.deviceType === 'switch') {
685
748
  if (this.On === undefined) {
686
749
  this.debugLog(`Bot: ${this.accessory.displayName} On: ${this.On}`);
@@ -696,8 +759,6 @@ export class Bot {
696
759
  this.debugLog(`Bot: ${this.accessory.displayName} updateCharacteristic On: ${this.On}`);
697
760
  }
698
761
  }
699
-
700
-
701
762
  if (this.device.ble) {
702
763
  if (this.BatteryLevel === undefined) {
703
764
  this.debugLog(`Bot: ${this.accessory.displayName} BatteryLevel: ${this.BatteryLevel}`);
@@ -723,6 +784,9 @@ export class Bot {
723
784
  this.doorService?.updateCharacteristic(this.platform.Characteristic.TargetPosition, e);
724
785
  this.doorService?.updateCharacteristic(this.platform.Characteristic.CurrentPosition, e);
725
786
  this.doorService?.updateCharacteristic(this.platform.Characteristic.PositionState, e);
787
+ } else if (this.device.bot?.deviceType === 'lock') {
788
+ this.doorService?.updateCharacteristic(this.platform.Characteristic.LockTargetState, e);
789
+ this.doorService?.updateCharacteristic(this.platform.Characteristic.LockCurrentState, e);
726
790
  } else if (this.device.bot?.deviceType === 'switch') {
727
791
  this.switchService?.updateCharacteristic(this.platform.Characteristic.On, e);
728
792
  } else {
@@ -795,6 +859,13 @@ export class Bot {
795
859
  } else {
796
860
  this.On = true;
797
861
  }
862
+ } else if (this.device.bot?.deviceType === 'lock') {
863
+ this.debugLog(`Bot: ${this.accessory.displayName} LockTargetState: ${value}`);
864
+ if (value === this.platform.Characteristic.LockTargetState.SECURED) {
865
+ this.On = false;
866
+ } else {
867
+ this.On = true;
868
+ }
798
869
  } else {
799
870
  this.debugLog(`Bot: ${this.accessory.displayName} On: ${value}`);
800
871
  this.On = value;
@@ -273,18 +273,34 @@ export class Contact {
273
273
  }
274
274
  }
275
275
 
276
- private connectBLE() {
277
- // Convert to BLE Address
278
- this.device.bleMac = ((this.device.deviceId!.match(/.{1,2}/g))!.join(':')).toLowerCase();
279
- this.debugLog(`Contact Sensor: ${this.accessory.displayName} BLE Address: ${this.device.bleMac}`);
280
- const switchbot = this.platform.connectBLE();
276
+ public async connectBLE() {
277
+ let switchbot: any;
278
+ try {
279
+ // eslint-disable-next-line @typescript-eslint/no-var-requires
280
+ const Switchbot = require('node-switchbot');
281
+ switchbot = new Switchbot();
282
+ // Convert to BLE Address
283
+ this.device.bleMac = ((this.device.deviceId!.match(/.{1,2}/g))!.join(':')).toLowerCase();
284
+ this.debugLog(`Contact Sensor: ${this.accessory.displayName} BLE Address: ${this.device.bleMac}`);
285
+ } catch (e: any) {
286
+ switchbot = false;
287
+ this.errorLog(`Contact Sensor: ${this.accessory.displayName} 'node-switchbot' found: ${switchbot}`);
288
+ if (this.deviceLogging === 'debug') {
289
+ this.errorLog(`Contact Sensor: ${this.accessory.displayName} 'node-switchbot' found: ${switchbot},`
290
+ + ` Error Message: ${JSON.stringify(e.message)}`);
291
+ }
292
+ if (this.platform.debugMode) {
293
+ this.errorLog(`Contact Sensor: ${this.accessory.displayName} 'node-switchbot' found: ${switchbot},`
294
+ + ` Error: ${JSON.stringify(e)}`);
295
+ }
296
+ }
281
297
  return switchbot;
282
298
  }
283
299
 
284
300
  private async BLERefreshStatus() {
285
301
  this.debugLog(`Contact Sensor: ${this.accessory.displayName} BLE refreshStatus`);
286
302
  // eslint-disable-next-line @typescript-eslint/no-var-requires
287
- const switchbot = this.connectBLE();
303
+ const switchbot = await this.connectBLE();
288
304
  // Start to monitor advertisement packets
289
305
  if (switchbot !== false) {
290
306
  switchbot.startScan({
@@ -342,12 +358,12 @@ export class Contact {
342
358
  this.apiError(e);
343
359
  });
344
360
  } else {
345
- await this.BLEconnection();
361
+ await this.BLEconnection(switchbot);
346
362
  }
347
363
  }
348
364
 
349
- private async BLEconnection() {
350
- this.errorLog(`Contact Sensor: ${this.accessory.displayName} wasn't able to establish BLE Connection`);
365
+ public async BLEconnection(switchbot: any) {
366
+ this.errorLog(`Contact Sensor: ${this.accessory.displayName} wasn't able to establish BLE Connection, node-switchbot: ${switchbot}`);
351
367
  if (this.platform.config.credentials?.openToken) {
352
368
  this.warnLog(`Contact Sensor: ${this.accessory.displayName} Using OpenAPI Connection`);
353
369
  await this.openAPIRefreshStatus();
@@ -253,11 +253,27 @@ export class Curtain {
253
253
  }
254
254
  }
255
255
 
256
- private connectBLE() {
257
- // Convert to BLE Address
258
- this.device.bleMac = ((this.device.deviceId!.match(/.{1,2}/g))!.join(':')).toLowerCase();
259
- this.debugLog(`Curtain: ${this.accessory.displayName} BLE Address: ${this.device.bleMac}`);
260
- const switchbot = this.platform.connectBLE();
256
+ public async connectBLE() {
257
+ let switchbot: any;
258
+ try {
259
+ // eslint-disable-next-line @typescript-eslint/no-var-requires
260
+ const Switchbot = require('node-switchbot');
261
+ switchbot = new Switchbot();
262
+ // Convert to BLE Address
263
+ this.device.bleMac = ((this.device.deviceId!.match(/.{1,2}/g))!.join(':')).toLowerCase();
264
+ this.debugLog(`Curtain: ${this.accessory.displayName} BLE Address: ${this.device.bleMac}`);
265
+ } catch (e: any) {
266
+ switchbot = false;
267
+ this.errorLog(`Curtain: ${this.accessory.displayName} 'node-switchbot' found: ${switchbot}`);
268
+ if (this.deviceLogging === 'debug') {
269
+ this.errorLog(`Curtain: ${this.accessory.displayName} 'node-switchbot' found: ${switchbot},`
270
+ + ` Error Message: ${JSON.stringify(e.message)}`);
271
+ }
272
+ if (this.platform.debugMode) {
273
+ this.errorLog(`Curtain: ${this.accessory.displayName} 'node-switchbot' found: ${switchbot},`
274
+ + ` Error: ${JSON.stringify(e)}`);
275
+ }
276
+ }
261
277
  return switchbot;
262
278
  }
263
279
 
@@ -452,7 +468,7 @@ export class Curtain {
452
468
  private async BLERefreshStatus() {
453
469
  this.debugLog(`Curtain: ${this.accessory.displayName} BLE refreshStatus`);
454
470
  // eslint-disable-next-line @typescript-eslint/no-var-requires
455
- const switchbot = this.connectBLE();
471
+ const switchbot = await this.connectBLE();
456
472
  // Start to monitor advertisement packets
457
473
  if (switchbot !== false) {
458
474
  switchbot.startScan({
@@ -510,12 +526,12 @@ export class Curtain {
510
526
  this.apiError(e);
511
527
  });
512
528
  } else {
513
- await this.BLEconnection();
529
+ await this.BLEconnection(switchbot);
514
530
  }
515
531
  }
516
532
 
517
- private async BLEconnection() {
518
- this.errorLog(`Curtain: ${this.accessory.displayName} wasn't able to establish BLE Connection`);
533
+ public async BLEconnection(switchbot: any) {
534
+ this.errorLog(`Curtain: ${this.accessory.displayName} wasn't able to establish BLE Connection, node-switchbot: ${switchbot}`);
519
535
  if (this.platform.config.credentials?.openToken) {
520
536
  this.warnLog(`Curtain: ${this.accessory.displayName} Using OpenAPI Connection`);
521
537
  await this.openAPIRefreshStatus();
@@ -562,7 +578,7 @@ export class Curtain {
562
578
  private async BLEpushChanges() {
563
579
  if (this.TargetPosition !== this.CurrentPosition) {
564
580
  this.debugLog(`Curtain: ${this.accessory.displayName} BLE pushChanges`);
565
- const switchbot = this.connectBLE();
581
+ const switchbot = await this.connectBLE();
566
582
  if (switchbot !== false) {
567
583
  switchbot.discover({ model: 'c', quick: true, id: this.device.bleMac }).then((device_list) => {
568
584
  this.infoLog(`${this.accessory.displayName} Target Position: ${this.TargetPosition}`);
@@ -339,18 +339,34 @@ export class Humidifier {
339
339
  }
340
340
  }
341
341
 
342
- private connectBLE() {
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();
342
+ public async connectBLE() {
343
+ let switchbot: any;
344
+ try {
345
+ // eslint-disable-next-line @typescript-eslint/no-var-requires
346
+ const Switchbot = require('node-switchbot');
347
+ switchbot = new Switchbot();
348
+ // Convert to BLE Address
349
+ this.device.bleMac = ((this.device.deviceId!.match(/.{1,2}/g))!.join(':')).toLowerCase();
350
+ this.debugLog(`Humidifier: ${this.accessory.displayName} BLE Address: ${this.device.bleMac}`);
351
+ } catch (e: any) {
352
+ switchbot = false;
353
+ this.errorLog(`Humidifier: ${this.accessory.displayName} 'node-switchbot' found: ${switchbot}`);
354
+ if (this.deviceLogging === 'debug') {
355
+ this.errorLog(`Humidifier: ${this.accessory.displayName} 'node-switchbot' found: ${switchbot},`
356
+ + ` Error Message: ${JSON.stringify(e.message)}`);
357
+ }
358
+ if (this.platform.debugMode) {
359
+ this.errorLog(`Humidifier: ${this.accessory.displayName} 'node-switchbot' found: ${switchbot},`
360
+ + ` Error: ${JSON.stringify(e)}`);
361
+ }
362
+ }
347
363
  return switchbot;
348
364
  }
349
365
 
350
366
  private async BLERefreshStatus() {
351
367
  this.debugLog(`Humidifier: ${this.accessory.displayName} BLE refreshStatus`);
352
368
  // eslint-disable-next-line @typescript-eslint/no-var-requires
353
- const switchbot = this.connectBLE();
369
+ const switchbot = await this.connectBLE();
354
370
  // Start to monitor advertisement packets
355
371
  if (switchbot !== false) {
356
372
  switchbot.startScan({
@@ -384,7 +400,7 @@ export class Humidifier {
384
400
  this.parseStatus();
385
401
  this.updateHomeKitCharacteristics();
386
402
  } else {
387
- await this.BLEconnection();
403
+ await this.BLEconnection(switchbot);
388
404
  }
389
405
  }).catch(async (e: any) => {
390
406
  this.errorLog(`Humidifier: ${this.accessory.displayName} failed refreshStatus with BLE Connection`);
@@ -403,12 +419,12 @@ export class Humidifier {
403
419
  this.apiError(e);
404
420
  });
405
421
  } else {
406
- await this.BLEconnection();
422
+ await this.BLEconnection(switchbot);
407
423
  }
408
424
  }
409
425
 
410
- private async BLEconnection() {
411
- this.errorLog(`Humidifier: ${this.accessory.displayName} wasn't able to establish BLE Connection`);
426
+ public async BLEconnection(switchbot: any) {
427
+ this.errorLog(`Humidifier: ${this.accessory.displayName} wasn't able to establish BLE Connection, node-switchbot: ${switchbot}`);
412
428
  if (this.platform.config.credentials?.openToken) {
413
429
  this.warnLog(`Humidifier: ${this.accessory.displayName} Using OpenAPI Connection`);
414
430
  await this.openAPIRefreshStatus();
@@ -461,7 +477,7 @@ export class Humidifier {
461
477
 
462
478
  private async BLEpushChanges() {
463
479
  this.debugLog(`Humidifier: ${this.accessory.displayName} BLE pushChanges`);
464
- const switchbot = this.connectBLE();
480
+ const switchbot = await this.connectBLE();
465
481
  if (switchbot !== false) {
466
482
  switchbot.discover({ model: 'e', quick: true, id: this.device.bleMac }).then((device_list) => {
467
483
  this.infoLog(`${this.accessory.displayName} Target Position: ${this.Active}`);
@@ -303,19 +303,34 @@ export class Meter {
303
303
  }
304
304
  }
305
305
 
306
- private connectBLE() {
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();
306
+ private async connectBLE() {
307
+ let switchbot: any;
308
+ try {
309
+ // eslint-disable-next-line @typescript-eslint/no-var-requires
310
+ const Switchbot = require('node-switchbot');
311
+ switchbot = new Switchbot();
312
+ // Convert to BLE Address
313
+ this.device.bleMac = ((this.device.deviceId!.match(/.{1,2}/g))!.join(':')).toLowerCase();
314
+ this.debugLog(`Meter: ${this.accessory.displayName} BLE Address: ${this.device.bleMac}`);
315
+ } catch (e: any) {
316
+ switchbot = false;
317
+ this.errorLog(`Meter: ${this.accessory.displayName} 'node-switchbot' found: ${switchbot}`);
318
+ if (this.deviceLogging === 'debug') {
319
+ this.errorLog(`Meter: ${this.accessory.displayName} 'node-switchbot' found: ${switchbot},`
320
+ + ` Error Message: ${JSON.stringify(e.message)}`);
321
+ }
322
+ if (this.platform.debugMode) {
323
+ this.errorLog(`Meter: ${this.accessory.displayName} 'node-switchbot' found: ${switchbot},`
324
+ + ` Error: ${JSON.stringify(e)}`);
325
+ }
326
+ }
311
327
  return switchbot;
312
328
  }
313
329
 
314
-
315
330
  private async BLErefreshStatus() {
316
331
  this.debugLog(`Meter: ${this.accessory.displayName} BLE RefreshStatus`);
317
332
  // eslint-disable-next-line @typescript-eslint/no-var-requires
318
- const switchbot = this.connectBLE();
333
+ const switchbot = await this.connectBLE();
319
334
  // Start to monitor advertisement packets
320
335
  if (switchbot !== false) {
321
336
  switchbot.startScan({
@@ -356,7 +371,7 @@ export class Meter {
356
371
  this.parseStatus();
357
372
  this.updateHomeKitCharacteristics();
358
373
  } else {
359
- await this.BLEconnection();
374
+ await this.BLEconnection(switchbot);
360
375
  }
361
376
  }).catch(async (e: any) => {
362
377
  this.errorLog(`Meter: ${this.accessory.displayName} failed refreshStatus with BLE Connection`);
@@ -375,12 +390,12 @@ export class Meter {
375
390
  this.apiError(e);
376
391
  });
377
392
  } else {
378
- await this.BLEconnection();
393
+ await this.BLEconnection(switchbot);
379
394
  }
380
395
  }
381
396
 
382
- private async BLEconnection() {
383
- this.errorLog(`Meter: ${this.accessory.displayName} wasn't able to establish BLE Connection`);
397
+ private async BLEconnection(switchbot: any) {
398
+ this.errorLog(`Meter: ${this.accessory.displayName} wasn't able to establish BLE Connection, node-switchbot: ${switchbot}`);
384
399
  if (this.platform.config.credentials?.openToken) {
385
400
  this.warnLog(`Meter: ${this.accessory.displayName} Using OpenAPI Connection`);
386
401
  await this.openAPIRefreshStatus();
@@ -226,18 +226,34 @@ export class Motion {
226
226
  }
227
227
  }
228
228
 
229
- private connectBLE() {
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();
229
+ public async connectBLE() {
230
+ let switchbot: any;
231
+ try {
232
+ // eslint-disable-next-line @typescript-eslint/no-var-requires
233
+ const Switchbot = require('node-switchbot');
234
+ switchbot = new Switchbot();
235
+ // Convert to BLE Address
236
+ this.device.bleMac = ((this.device.deviceId!.match(/.{1,2}/g))!.join(':')).toLowerCase();
237
+ this.debugLog(`Motion Sensor: ${this.accessory.displayName} BLE Address: ${this.device.bleMac}`);
238
+ } catch (e: any) {
239
+ switchbot = false;
240
+ this.errorLog(`Motion Sensor: ${this.accessory.displayName} 'node-switchbot' found: ${switchbot}`);
241
+ if (this.deviceLogging === 'debug') {
242
+ this.errorLog(`Motion Sensor: ${this.accessory.displayName} 'node-switchbot' found: ${switchbot},`
243
+ + ` Error Message: ${JSON.stringify(e.message)}`);
244
+ }
245
+ if (this.platform.debugMode) {
246
+ this.errorLog(`Motion Sensor: ${this.accessory.displayName} 'node-switchbot' found: ${switchbot},`
247
+ + ` Error: ${JSON.stringify(e)}`);
248
+ }
249
+ }
234
250
  return switchbot;
235
251
  }
236
252
 
237
253
  private async BLERefreshStatus() {
238
254
  this.debugLog(`Motion Sensor: ${this.accessory.displayName} BLE RefreshStatus`);
239
255
  // eslint-disable-next-line @typescript-eslint/no-var-requires
240
- const switchbot = this.connectBLE();
256
+ const switchbot = await this.connectBLE();
241
257
  // Start to monitor advertisement packets
242
258
  if (switchbot !== false) {
243
259
  switchbot.startScan({
@@ -271,7 +287,7 @@ export class Motion {
271
287
  this.parseStatus();
272
288
  this.updateHomeKitCharacteristics();
273
289
  } else {
274
- await this.BLEconnection();
290
+ await this.BLEconnection(switchbot);
275
291
  }
276
292
  }).catch(async (e: any) => {
277
293
  this.errorLog(`Motion Sensor: ${this.accessory.displayName} failed refreshStatus with BLE Connection`);
@@ -290,12 +306,12 @@ export class Motion {
290
306
  this.apiError(e);
291
307
  });
292
308
  } else {
293
- await this.BLEconnection();
309
+ await this.BLEconnection(switchbot);
294
310
  }
295
311
  }
296
312
 
297
- private async BLEconnection() {
298
- this.errorLog(`Motion Sensor: ${this.accessory.displayName} wasn't able to establish BLE Connection`);
313
+ public async BLEconnection(switchbot: any) {
314
+ this.errorLog(`Motion Sensor: ${this.accessory.displayName} wasn't able to establish BLE Connection, node-switchbot: ${switchbot}`);
299
315
  if (this.platform.config.credentials?.openToken) {
300
316
  this.warnLog(`Motion Sensor: ${this.accessory.displayName} Using OpenAPI Connection`);
301
317
  await this.openAPIRefreshStatus();
package/src/platform.ts CHANGED
@@ -41,7 +41,6 @@ export class SwitchBotPlatform implements DynamicPlatformPlugin {
41
41
  registeringDevice!: boolean;
42
42
  debugMode!: boolean;
43
43
  platformLogging?: string;
44
- switchbot: any;
45
44
 
46
45
  constructor(public readonly log: Logger, public readonly config: SwitchBotPlatformConfig, public readonly api: API) {
47
46
  this.logs();
@@ -193,18 +192,6 @@ export class SwitchBotPlatform implements DynamicPlatformPlugin {
193
192
  }
194
193
  }
195
194
 
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
-
208
195
  /**
209
196
  * this method discovers the Locations
210
197
  */