@switchbot/homebridge-switchbot 1.6.4-beta.1 → 1.6.4-beta.5

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);
@@ -209,37 +228,46 @@ export class Bot {
209
228
 
210
229
  private removeOutletService(accessory: PlatformAccessory) {
211
230
  // If outletService still pressent, then remove first
231
+ this.outletService = this.accessory.getService(this.platform.Service.Outlet);
212
232
  if (this.outletService) {
213
- this.debugLog(`Bot: ${accessory.displayName} Removing Leftover outletService first`);
233
+ this.warnLog(`Bot: ${accessory.displayName} Removing Leftover Outlet Service`);
214
234
  }
215
- this.outletService = this.accessory.getService(this.platform.Service.Outlet);
216
235
  accessory.removeService(this.outletService!);
217
236
  }
218
237
 
219
238
  private removeGarageDoorService(accessory: PlatformAccessory) {
220
239
  // If garageDoorService still pressent, then remove first
240
+ this.garageDoorService = this.accessory.getService(this.platform.Service.GarageDoorOpener);
221
241
  if (this.garageDoorService) {
222
- this.debugLog(`Bot: ${accessory.displayName} Removing Leftover garageDoorService first`);
242
+ this.warnLog(`Bot: ${accessory.displayName} Removing Leftover Garage Door Service`);
223
243
  }
224
- this.garageDoorService = this.accessory.getService(this.platform.Service.GarageDoorOpener);
225
244
  accessory.removeService(this.garageDoorService!);
226
245
  }
227
246
 
228
247
  private removeDoorService(accessory: PlatformAccessory) {
229
248
  // If doorService still pressent, then remove first
249
+ this.doorService = this.accessory.getService(this.platform.Service.Door);
230
250
  if (this.doorService) {
231
- this.debugLog(`Bot: ${accessory.displayName} Removing Leftover doorService first`);
251
+ this.warnLog(`Bot: ${accessory.displayName} Removing Leftover Door Service`);
232
252
  }
233
- this.doorService = this.accessory.getService(this.platform.Service.Door);
234
253
  accessory.removeService(this.doorService!);
235
254
  }
236
255
 
256
+ private removeLockService(accessory: PlatformAccessory) {
257
+ // If lockService still pressent, then remove first
258
+ this.lockService = this.accessory.getService(this.platform.Service.LockMechanism);
259
+ if (this.lockService) {
260
+ this.warnLog(`Bot: ${accessory.displayName} Removing Leftover Lock Service`);
261
+ }
262
+ accessory.removeService(this.lockService!);
263
+ }
264
+
237
265
  private removeSwitchService(accessory: PlatformAccessory) {
238
266
  // If switchService still pressent, then remove first
267
+ this.switchService = this.accessory.getService(this.platform.Service.Switch);
239
268
  if (this.switchService) {
240
- this.debugLog(`Bot: ${accessory.displayName} Removing Leftover switchService first`);
269
+ this.warnLog(`Bot: ${accessory.displayName} Removing Leftover Switch Service`);
241
270
  }
242
- this.switchService = this.accessory.getService(this.platform.Service.Switch);
243
271
  accessory.removeService(this.switchService!);
244
272
  }
245
273
 
@@ -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,69 +435,64 @@ 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
- if (switchbot !== false) {
397
- switchbot.startScan({
398
- model: 'H',
399
- id: this.device.bleMac,
400
- }).then(() => {
401
- // Set an event hander
402
- switchbot.onadvertisement = (ad: ad) => {
403
- this.serviceData = ad.serviceData;
404
- this.mode = ad.serviceData.mode;
405
- this.state = ad.serviceData.state;
406
- this.battery = ad.serviceData.battery;
407
- this.debugLog(`Bot: ${this.accessory.displayName} serviceData: ${JSON.stringify(ad.serviceData)}`);
408
- this.debugLog(`Bot: ${this.accessory.displayName}, model: ${ad.serviceData.model}, modelName: ${ad.serviceData.modelName},`
409
- + ` mode: ${ad.serviceData.mode}, state: ${ad.serviceData.state}, battery: ${ad.serviceData.battery}`);
410
-
411
- if (this.serviceData) {
412
- this.connected = true;
413
- this.debugLog(`Bot: ${this.accessory.displayName} connected: ${this.connected}`);
414
- } else {
415
- this.connected = false;
416
- this.debugLog(`Bot: ${this.accessory.displayName} connected: ${this.connected}`);
417
- }
418
- };
419
- // Wait 2 seconds
420
- return switchbot.wait(this.scanDuration * 1000);
421
- }).then(async () => {
422
- // Stop to monitor
423
- switchbot.stopScan();
424
- if (this.connected) {
425
- this.parseStatus();
426
- this.updateHomeKitCharacteristics();
440
+ if (switchbot === false) {
441
+ this.errorLog(`Bot: ${this.accessory.displayName} wasn't able to establish BLE Connection: ${switchbot}`);
442
+ }
443
+ switchbot.startScan({
444
+ model: 'H',
445
+ id: this.device.bleMac,
446
+ }).then(() => {
447
+ // Set an event hander
448
+ switchbot.onadvertisement = (ad: ad) => {
449
+ this.serviceData = ad.serviceData;
450
+ this.mode = ad.serviceData.mode;
451
+ this.state = ad.serviceData.state;
452
+ this.battery = ad.serviceData.battery;
453
+ this.debugLog(`Bot: ${this.accessory.displayName} serviceData: ${JSON.stringify(ad.serviceData)}`);
454
+ this.debugLog(`Bot: ${this.accessory.displayName}, model: ${ad.serviceData.model}, modelName: ${ad.serviceData.modelName},`
455
+ + ` mode: ${ad.serviceData.mode}, state: ${ad.serviceData.state}, battery: ${ad.serviceData.battery}`);
456
+
457
+ if (this.serviceData) {
458
+ this.connected = true;
459
+ this.debugLog(`Bot: ${this.accessory.displayName} connected: ${this.connected}`);
427
460
  } else {
428
- await this.BLEconnection();
429
- }
430
- }).catch(async (e: any) => {
431
- this.errorLog(`Bot: ${this.accessory.displayName} failed refreshStatus with BLE Connection`);
432
- if (this.deviceLogging === 'debug') {
433
- this.errorLog(`Bot: ${this.accessory.displayName} failed refreshStatus with BLE Connection,`
434
- + ` Error Message: ${JSON.stringify(e.message)}`);
435
- }
436
- if (this.platform.debugMode) {
437
- this.errorLog(`Bot: ${this.accessory.displayName} failed refreshStatus with BLE Connection,`
438
- + ` Error: ${JSON.stringify(e)}`);
461
+ this.connected = false;
462
+ this.debugLog(`Bot: ${this.accessory.displayName} connected: ${this.connected}`);
439
463
  }
464
+ };
465
+ // Wait 2 seconds
466
+ return switchbot.wait(this.scanDuration * 1000);
467
+ }).then(async () => {
468
+ // Stop to monitor
469
+ switchbot.stopScan();
470
+ if (this.connected) {
471
+ this.parseStatus();
472
+ this.updateHomeKitCharacteristics();
473
+ } else {
474
+ this.errorLog(`Bot: ${this.accessory.displayName} wasn't able to establish BLE Connection`);
440
475
  if (this.platform.config.credentials?.openToken) {
441
476
  this.warnLog(`Bot: ${this.accessory.displayName} Using OpenAPI Connection`);
442
477
  await this.openAPIRefreshStatus();
443
478
  }
444
- this.apiError(e);
445
- });
446
- } else {
447
- await this.BLEconnection();
448
- }
449
- }
450
-
451
- private async BLEconnection() {
452
- this.errorLog(`Bot: ${this.accessory.displayName} wasn't able to establish BLE Connection`);
453
- if (this.platform.config.credentials?.openToken) {
454
- this.warnLog(`Bot: ${this.accessory.displayName} Using OpenAPI Connection`);
455
- await this.openAPIRefreshStatus();
456
- }
479
+ }
480
+ }).catch(async (e: any) => {
481
+ this.errorLog(`Bot: ${this.accessory.displayName} failed refreshStatus with BLE Connection`);
482
+ if (this.deviceLogging === 'debug') {
483
+ this.errorLog(`Bot: ${this.accessory.displayName} failed refreshStatus with BLE Connection,`
484
+ + ` Error Message: ${JSON.stringify(e.message)}`);
485
+ }
486
+ if (this.platform.debugMode) {
487
+ this.errorLog(`Bot: ${this.accessory.displayName} failed refreshStatus with BLE Connection,`
488
+ + ` Error: ${JSON.stringify(e)}`);
489
+ }
490
+ if (this.platform.config.credentials?.openToken) {
491
+ this.warnLog(`Bot: ${this.accessory.displayName} Using OpenAPI Connection`);
492
+ await this.openAPIRefreshStatus();
493
+ }
494
+ this.apiError(e);
495
+ });
457
496
  }
458
497
 
459
498
  private async openAPIRefreshStatus() {
@@ -501,54 +540,30 @@ export class Bot {
501
540
  }
502
541
 
503
542
  private async BLEpushChanges() {
543
+ this.debugLog(`Bot: ${this.accessory.displayName} BLE pushChanges On: ${this.On} OnCached: ${this.OnCached}`);
504
544
  if (this.On !== this.OnCached) {
505
545
  this.debugLog(`Bot: ${this.accessory.displayName} BLE pushChanges`);
506
- const switchbot = this.connectBLE();
507
- if (switchbot !== false) {
508
- if (this.botMode === 'press') {
509
- this.debugLog(`Bot: ${this.accessory.displayName} Bot Mode: ${this.botMode}`);
510
- switchbot.discover({ model: 'H', quick: true, id: this.device.bleMac })
511
- .then((device_list: { press: (arg0: { id: string | undefined; }) => any; }[]) => {
512
- this.infoLog(`Bot: ${this.accessory.displayName} On: ${this.On}`);
513
- return device_list[0].press({ id: this.device.bleMac });
514
- }).then(() => {
515
- this.debugLog(`Bot: ${this.accessory.displayName} Done.`);
516
- this.On = false;
517
- this.OnCached = this.On;
518
- this.accessory.context.On = this.OnCached;
519
- setTimeout(() => {
520
- if (this.device.bot?.deviceType === 'switch') {
521
- this.switchService?.getCharacteristic(this.platform.Characteristic.On).updateValue(this.On);
522
- } else {
523
- this.outletService?.getCharacteristic(this.platform.Characteristic.On).updateValue(this.On);
524
- }
525
- this.debugLog(`Bot: ${this.accessory.displayName} On: ${this.On}, Switch Timeout`);
526
- }, 500);
527
- }).catch(async (e: any) => {
528
- this.errorLog(`Bot: ${this.accessory.displayName} failed pushChanges with BLE Connection`);
529
- if (this.deviceLogging === 'debug') {
530
- this.errorLog(`Bot: ${this.accessory.displayName} failed pushChanges with BLE Connection,`
531
- + ` Error Message: ${JSON.stringify(e.message)}`);
532
- }
533
- if (this.platform.debugMode) {
534
- this.errorLog(`Bot: ${this.accessory.displayName} failed pushChanges with BLE Connection,`
535
- + ` Error: ${JSON.stringify(e)}`);
536
- }
537
- if (this.platform.config.credentials?.openToken) {
538
- this.warnLog(`Bot: ${this.accessory.displayName} Using OpenAPI Connection`);
539
- await this.openAPIpushChanges();
540
- }
541
- this.apiError(e);
542
- });
543
- } else if (this.botMode === 'switch') {
544
- this.debugLog(`Bot: ${this.accessory.displayName} Press Mode: ${this.botMode}`);
545
- switchbot.discover({ model: 'H', quick: true, id: this.device.bleMac }).then((device_list: any) => {
546
+ const switchbot = await this.connectBLE();
547
+ //if (switchbot !== false) {
548
+ if (this.botMode === 'press') {
549
+ this.debugLog(`Bot: ${this.accessory.displayName} Bot Mode: ${this.botMode}`);
550
+ switchbot.discover({ model: 'H', quick: true, id: this.device.bleMac })
551
+ .then((device_list: { press: (arg0: { id: string | undefined; }) => any; }[]) => {
546
552
  this.infoLog(`Bot: ${this.accessory.displayName} On: ${this.On}`);
547
- return this.turnOnOff(device_list);
553
+ return device_list[0].press({ id: this.device.bleMac });
548
554
  }).then(() => {
549
555
  this.debugLog(`Bot: ${this.accessory.displayName} Done.`);
556
+ this.On = false;
550
557
  this.OnCached = this.On;
551
558
  this.accessory.context.On = this.OnCached;
559
+ setTimeout(() => {
560
+ if (this.device.bot?.deviceType === 'switch') {
561
+ this.switchService?.getCharacteristic(this.platform.Characteristic.On).updateValue(this.On);
562
+ } else {
563
+ this.outletService?.getCharacteristic(this.platform.Characteristic.On).updateValue(this.On);
564
+ }
565
+ this.debugLog(`Bot: ${this.accessory.displayName} On: ${this.On}, Switch Timeout`);
566
+ }, 500);
552
567
  }).catch(async (e: any) => {
553
568
  this.errorLog(`Bot: ${this.accessory.displayName} failed pushChanges with BLE Connection`);
554
569
  if (this.deviceLogging === 'debug') {
@@ -565,16 +580,41 @@ export class Bot {
565
580
  }
566
581
  this.apiError(e);
567
582
  });
568
- } else {
569
- throw new Error(`Bot: ${this.accessory.displayName} Bot Mode: ${this.botMode}`);
570
- }
583
+ } else if (this.botMode === 'switch') {
584
+ this.debugLog(`Bot: ${this.accessory.displayName} Press Mode: ${this.botMode}`);
585
+ switchbot.discover({ model: 'H', quick: true, id: this.device.bleMac }).then((device_list: any) => {
586
+ this.infoLog(`Bot: ${this.accessory.displayName} On: ${this.On}`);
587
+ return this.turnOnOff(device_list);
588
+ }).then(() => {
589
+ this.debugLog(`Bot: ${this.accessory.displayName} Done.`);
590
+ this.OnCached = this.On;
591
+ this.accessory.context.On = this.OnCached;
592
+ }).catch(async (e: any) => {
593
+ this.errorLog(`Bot: ${this.accessory.displayName} failed pushChanges with BLE Connection`);
594
+ if (this.deviceLogging === 'debug') {
595
+ this.errorLog(`Bot: ${this.accessory.displayName} failed pushChanges with BLE Connection,`
596
+ + ` Error Message: ${JSON.stringify(e.message)}`);
597
+ }
598
+ if (this.platform.debugMode) {
599
+ this.errorLog(`Bot: ${this.accessory.displayName} failed pushChanges with BLE Connection,`
600
+ + ` Error: ${JSON.stringify(e)}`);
601
+ }
602
+ if (this.platform.config.credentials?.openToken) {
603
+ this.warnLog(`Bot: ${this.accessory.displayName} Using OpenAPI Connection`);
604
+ await this.openAPIpushChanges();
605
+ }
606
+ this.apiError(e);
607
+ });
571
608
  } else {
572
- this.errorLog(`Bot: ${this.accessory.displayName} wasn't able to establish BLE Connection`);
573
- if (this.platform.config.credentials?.openToken) {
574
- this.warnLog(`Bot: ${this.accessory.displayName} Using OpenAPI Connection`);
575
- await this.openAPIpushChanges();
576
- }
609
+ throw new Error(`Bot: ${this.accessory.displayName} Bot Mode: ${this.botMode}`);
577
610
  }
611
+ /* } else {
612
+ this.errorLog(`Bot: ${this.accessory.displayName} wasn't able to establish BLE Connection`);
613
+ if (this.platform.config.credentials?.openToken) {
614
+ this.warnLog(`Bot: ${this.accessory.displayName} Using OpenAPI Connection`);
615
+ await this.openAPIpushChanges();
616
+ }
617
+ }*/
578
618
  }
579
619
  this.OnCached = this.On;
580
620
  this.accessory.context.On = this.OnCached;
@@ -661,22 +701,45 @@ export class Bot {
661
701
  this.debugLog(`Bot: ${this.accessory.displayName} updateCharacteristic TargetDoorState: Open, CurrentDoorState: Open`);
662
702
  }
663
703
  }
664
- this.debugLog(`Bot: ${this.accessory.displayName} updateCharacteristic On: ${this.On}`);
704
+ this.debugLog(`Bot: ${this.accessory.displayName} Garage Door On: ${this.On}`);
665
705
  } else if (this.device.bot?.deviceType === 'door') {
666
706
  if (this.On === undefined) {
667
707
  this.debugLog(`Bot: ${this.accessory.displayName} On: ${this.On}`);
668
708
  } else {
669
709
  if (this.On) {
670
- this.garageDoorService?.updateCharacteristic(this.platform.Characteristic.TargetPosition, 100);
671
- this.garageDoorService?.updateCharacteristic(this.platform.Characteristic.CurrentPosition, 100);
710
+ this.doorService?.updateCharacteristic(this.platform.Characteristic.TargetPosition, 100);
711
+ this.doorService?.updateCharacteristic(this.platform.Characteristic.CurrentPosition, 100);
712
+ this.doorService?.updateCharacteristic(this.platform.Characteristic.PositionState,
713
+ this.platform.Characteristic.PositionState.STOPPED);
672
714
  this.debugLog(`Bot: ${this.accessory.displayName} updateCharacteristic TargetPosition: 100, CurrentPosition: 100`);
673
715
  } else {
674
- this.garageDoorService?.updateCharacteristic(this.platform.Characteristic.TargetPosition, 0);
675
- this.garageDoorService?.updateCharacteristic(this.platform.Characteristic.CurrentPosition, 0);
716
+ this.doorService?.updateCharacteristic(this.platform.Characteristic.TargetPosition, 0);
717
+ this.doorService?.updateCharacteristic(this.platform.Characteristic.CurrentPosition, 0);
718
+ this.doorService?.updateCharacteristic(this.platform.Characteristic.PositionState,
719
+ this.platform.Characteristic.PositionState.STOPPED);
676
720
  this.debugLog(`Bot: ${this.accessory.displayName} updateCharacteristic TargetPosition: 0, CurrentPosition: 0`);
677
721
  }
678
722
  }
679
- this.debugLog(`Bot: ${this.accessory.displayName} updateCharacteristic On: ${this.On}`);
723
+ this.debugLog(`Bot: ${this.accessory.displayName} Door On: ${this.On}`);
724
+ } else if (this.device.bot?.deviceType === 'lock') {
725
+ if (this.On === undefined) {
726
+ this.debugLog(`Bot: ${this.accessory.displayName} On: ${this.On}`);
727
+ } else {
728
+ if (this.On) {
729
+ this.lockService?.updateCharacteristic(this.platform.Characteristic.LockTargetState,
730
+ this.platform.Characteristic.LockTargetState.UNSECURED);
731
+ this.lockService?.updateCharacteristic(this.platform.Characteristic.LockCurrentState,
732
+ this.platform.Characteristic.LockCurrentState.UNSECURED);
733
+ this.debugLog(`Bot: ${this.accessory.displayName} updateCharacteristic LockTargetState: UNSECURED, LockCurrentState: UNSECURED`);
734
+ } else {
735
+ this.lockService?.updateCharacteristic(this.platform.Characteristic.LockTargetState,
736
+ this.platform.Characteristic.LockTargetState.SECURED);
737
+ this.lockService?.updateCharacteristic(this.platform.Characteristic.LockCurrentState,
738
+ this.platform.Characteristic.LockCurrentState.SECURED);
739
+ this.debugLog(`Bot: ${this.accessory.displayName} updateCharacteristic LockTargetState: SECURED, LockCurrentState: SECURED`);
740
+ }
741
+ }
742
+ this.debugLog(`Bot: ${this.accessory.displayName} Lock On: ${this.On}`);
680
743
  } else if (this.device.bot?.deviceType === 'switch') {
681
744
  if (this.On === undefined) {
682
745
  this.debugLog(`Bot: ${this.accessory.displayName} On: ${this.On}`);
@@ -692,8 +755,6 @@ export class Bot {
692
755
  this.debugLog(`Bot: ${this.accessory.displayName} updateCharacteristic On: ${this.On}`);
693
756
  }
694
757
  }
695
-
696
-
697
758
  if (this.device.ble) {
698
759
  if (this.BatteryLevel === undefined) {
699
760
  this.debugLog(`Bot: ${this.accessory.displayName} BatteryLevel: ${this.BatteryLevel}`);
@@ -711,7 +772,18 @@ export class Bot {
711
772
  }
712
773
 
713
774
  public apiError(e: any) {
714
- if (this.device.bot?.deviceType === 'switch') {
775
+ if (this.device.bot?.deviceType === 'garagedoor') {
776
+ this.garageDoorService?.updateCharacteristic(this.platform.Characteristic.TargetDoorState, e);
777
+ this.garageDoorService?.updateCharacteristic(this.platform.Characteristic.CurrentDoorState, e);
778
+ this.garageDoorService?.updateCharacteristic(this.platform.Characteristic.ObstructionDetected, e);
779
+ } else if (this.device.bot?.deviceType === 'door') {
780
+ this.doorService?.updateCharacteristic(this.platform.Characteristic.TargetPosition, e);
781
+ this.doorService?.updateCharacteristic(this.platform.Characteristic.CurrentPosition, e);
782
+ this.doorService?.updateCharacteristic(this.platform.Characteristic.PositionState, e);
783
+ } else if (this.device.bot?.deviceType === 'lock') {
784
+ this.doorService?.updateCharacteristic(this.platform.Characteristic.LockTargetState, e);
785
+ this.doorService?.updateCharacteristic(this.platform.Characteristic.LockCurrentState, e);
786
+ } else if (this.device.bot?.deviceType === 'switch') {
715
787
  this.switchService?.updateCharacteristic(this.platform.Characteristic.On, e);
716
788
  } else {
717
789
  this.outletService?.updateCharacteristic(this.platform.Characteristic.On, e);
@@ -778,7 +850,14 @@ export class Bot {
778
850
  }
779
851
  } else if (this.device.bot?.deviceType === 'door') {
780
852
  this.debugLog(`Bot: ${this.accessory.displayName} TargetPosition: ${value}`);
781
- if (value === 100) {
853
+ if (value === 0) {
854
+ this.On = false;
855
+ } else {
856
+ this.On = true;
857
+ }
858
+ } else if (this.device.bot?.deviceType === 'lock') {
859
+ this.debugLog(`Bot: ${this.accessory.displayName} LockTargetState: ${value}`);
860
+ if (value === this.platform.Characteristic.LockTargetState.SECURED) {
782
861
  this.On = false;
783
862
  } else {
784
863
  this.On = true;
@@ -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}`);