@switchbot/homebridge-switchbot 1.6.4-beta.0 → 1.6.4-beta.4

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.
@@ -14,6 +14,8 @@ import { DeviceURL, device, devicesConfig, serviceData, ad, switchbot, deviceSta
14
14
  export class Bot {
15
15
  // Services
16
16
  private switchService?: Service;
17
+ private doorService?: Service;
18
+ private lockService?: Service;
17
19
  private garageDoorService?: Service;
18
20
  private outletService?: Service;
19
21
  private batteryService?: Service;
@@ -96,6 +98,8 @@ export class Bot {
96
98
  // get the LightBulb service if it exists, otherwise create a new LightBulb service
97
99
  // you can create multiple services for each accessory
98
100
  if (device.bot?.deviceType === 'switch') {
101
+ this.removeLockService(accessory);
102
+ this.removeDoorService(accessory);
99
103
  this.removeOutletService(accessory);
100
104
  this.removeGarageDoorService(accessory);
101
105
 
@@ -108,6 +112,8 @@ export class Bot {
108
112
  this.switchService.setCharacteristic(this.platform.Characteristic.Name, accessory.displayName);
109
113
  this.switchService.getCharacteristic(this.platform.Characteristic.On).onSet(this.handleOnSet.bind(this));
110
114
  } else if (device.bot?.deviceType === 'garagedoor') {
115
+ this.removeLockService(accessory);
116
+ this.removeDoorService(accessory);
111
117
  this.removeOutletService(accessory);
112
118
  this.removeSwitchService(accessory);
113
119
 
@@ -120,7 +126,44 @@ export class Bot {
120
126
  this.garageDoorService.setCharacteristic(this.platform.Characteristic.Name, accessory.displayName);
121
127
  this.garageDoorService.getCharacteristic(this.platform.Characteristic.TargetDoorState).onSet(this.handleOnSet.bind(this));
122
128
  this.garageDoorService.setCharacteristic(this.platform.Characteristic.ObstructionDetected, false);
129
+ } else if (device.bot?.deviceType === 'door') {
130
+ this.removeLockService(accessory);
131
+ this.removeOutletService(accessory);
132
+ this.removeSwitchService(accessory);
133
+ this.removeGarageDoorService(accessory);
134
+
135
+ // Add switchService
136
+ (this.doorService =
137
+ accessory.getService(this.platform.Service.Door) ||
138
+ accessory.addService(this.platform.Service.Door)), `${accessory.displayName} Door`;
139
+ this.infoLog(`Bot: ${accessory.displayName} Displaying as Door`);
140
+
141
+ this.doorService.setCharacteristic(this.platform.Characteristic.Name, accessory.displayName);
142
+ this.doorService.getCharacteristic(this.platform.Characteristic.TargetPosition)
143
+ .setProps({
144
+ validValues: [0, 100],
145
+ minValue: 0,
146
+ maxValue: 100,
147
+ minStep: 100,
148
+ }).onSet(this.handleOnSet.bind(this));
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));
123
164
  } else {
165
+ this.removeLockService(accessory);
166
+ this.removeDoorService(accessory);
124
167
  this.removeSwitchService(accessory);
125
168
  this.removeGarageDoorService(accessory);
126
169
 
@@ -193,12 +236,30 @@ export class Bot {
193
236
  }
194
237
 
195
238
  private removeGarageDoorService(accessory: PlatformAccessory) {
196
- // If outletService still pressent, then remove first
197
- if (this.outletService) {
198
- this.debugLog(`Bot: ${accessory.displayName} Removing Leftover outletService first`);
239
+ // If garageDoorService still pressent, then remove first
240
+ if (this.garageDoorService) {
241
+ this.debugLog(`Bot: ${accessory.displayName} Removing Leftover garageDoorService first`);
199
242
  }
200
- this.outletService = this.accessory.getService(this.platform.Service.Outlet);
201
- accessory.removeService(this.outletService!);
243
+ this.garageDoorService = this.accessory.getService(this.platform.Service.GarageDoorOpener);
244
+ accessory.removeService(this.garageDoorService!);
245
+ }
246
+
247
+ private removeDoorService(accessory: PlatformAccessory) {
248
+ // If doorService still pressent, then remove first
249
+ if (this.doorService) {
250
+ this.debugLog(`Bot: ${accessory.displayName} Removing Leftover doorService first`);
251
+ }
252
+ this.doorService = this.accessory.getService(this.platform.Service.Door);
253
+ accessory.removeService(this.doorService!);
254
+ }
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!);
202
263
  }
203
264
 
204
265
  private removeSwitchService(accessory: PlatformAccessory) {
@@ -346,11 +407,27 @@ export class Bot {
346
407
  }
347
408
  }
348
409
 
349
- private connectBLE() {
350
- // Convert to BLE Address
351
- this.device.bleMac = ((this.device.deviceId!.match(/.{1,2}/g))!.join(':')).toLowerCase();
352
- this.debugLog(`Bot: ${this.accessory.displayName} BLE Address: ${this.device.bleMac}`);
353
- 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
+ }
354
431
  return switchbot;
355
432
  }
356
433
 
@@ -358,69 +435,64 @@ export class Bot {
358
435
  this.debugLog(`Bot: ${this.accessory.displayName} BLE refreshStatus`);
359
436
 
360
437
  // Convert to BLE Address
361
- const switchbot = this.connectBLE();
438
+ const switchbot = await this.connectBLE();
362
439
  // Start to monitor advertisement packets
363
- if (switchbot !== false) {
364
- switchbot.startScan({
365
- model: 'H',
366
- id: this.device.bleMac,
367
- }).then(() => {
368
- // Set an event hander
369
- switchbot.onadvertisement = (ad: ad) => {
370
- this.serviceData = ad.serviceData;
371
- this.mode = ad.serviceData.mode;
372
- this.state = ad.serviceData.state;
373
- this.battery = ad.serviceData.battery;
374
- this.debugLog(`Bot: ${this.accessory.displayName} serviceData: ${JSON.stringify(ad.serviceData)}`);
375
- this.debugLog(`Bot: ${this.accessory.displayName}, model: ${ad.serviceData.model}, modelName: ${ad.serviceData.modelName},`
376
- + ` mode: ${ad.serviceData.mode}, state: ${ad.serviceData.state}, battery: ${ad.serviceData.battery}`);
377
-
378
- if (this.serviceData) {
379
- this.connected = true;
380
- this.debugLog(`Bot: ${this.accessory.displayName} connected: ${this.connected}`);
381
- } else {
382
- this.connected = false;
383
- this.debugLog(`Bot: ${this.accessory.displayName} connected: ${this.connected}`);
384
- }
385
- };
386
- // Wait 2 seconds
387
- return switchbot.wait(this.scanDuration * 1000);
388
- }).then(async () => {
389
- // Stop to monitor
390
- switchbot.stopScan();
391
- if (this.connected) {
392
- this.parseStatus();
393
- 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}`);
394
460
  } else {
395
- await this.BLEconnection();
396
- }
397
- }).catch(async (e: any) => {
398
- this.errorLog(`Bot: ${this.accessory.displayName} failed refreshStatus with BLE Connection`);
399
- if (this.deviceLogging === 'debug') {
400
- this.errorLog(`Bot: ${this.accessory.displayName} failed refreshStatus with BLE Connection,`
401
- + ` Error Message: ${JSON.stringify(e.message)}`);
402
- }
403
- if (this.platform.debugMode) {
404
- this.errorLog(`Bot: ${this.accessory.displayName} failed refreshStatus with BLE Connection,`
405
- + ` Error: ${JSON.stringify(e)}`);
461
+ this.connected = false;
462
+ this.debugLog(`Bot: ${this.accessory.displayName} connected: ${this.connected}`);
406
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`);
407
475
  if (this.platform.config.credentials?.openToken) {
408
476
  this.warnLog(`Bot: ${this.accessory.displayName} Using OpenAPI Connection`);
409
477
  await this.openAPIRefreshStatus();
410
478
  }
411
- this.apiError(e);
412
- });
413
- } else {
414
- await this.BLEconnection();
415
- }
416
- }
417
-
418
- private async BLEconnection() {
419
- this.errorLog(`Bot: ${this.accessory.displayName} wasn't able to establish BLE Connection`);
420
- if (this.platform.config.credentials?.openToken) {
421
- this.warnLog(`Bot: ${this.accessory.displayName} Using OpenAPI Connection`);
422
- await this.openAPIRefreshStatus();
423
- }
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
+ });
424
496
  }
425
497
 
426
498
  private async openAPIRefreshStatus() {
@@ -468,54 +540,30 @@ export class Bot {
468
540
  }
469
541
 
470
542
  private async BLEpushChanges() {
543
+ this.debugLog(`Bot: ${this.accessory.displayName} BLE pushChanges On: ${this.On} OnCached: ${this.OnCached}`);
471
544
  if (this.On !== this.OnCached) {
472
545
  this.debugLog(`Bot: ${this.accessory.displayName} BLE pushChanges`);
473
- const switchbot = this.connectBLE();
474
- if (switchbot !== false) {
475
- if (this.botMode === 'press') {
476
- this.debugLog(`Bot: ${this.accessory.displayName} Bot Mode: ${this.botMode}`);
477
- switchbot.discover({ model: 'H', quick: true, id: this.device.bleMac })
478
- .then((device_list: { press: (arg0: { id: string | undefined; }) => any; }[]) => {
479
- this.infoLog(`Bot: ${this.accessory.displayName} On: ${this.On}`);
480
- return device_list[0].press({ id: this.device.bleMac });
481
- }).then(() => {
482
- this.debugLog(`Bot: ${this.accessory.displayName} Done.`);
483
- this.On = false;
484
- this.OnCached = this.On;
485
- this.accessory.context.On = this.OnCached;
486
- setTimeout(() => {
487
- if (this.device.bot?.deviceType === 'switch') {
488
- this.switchService?.getCharacteristic(this.platform.Characteristic.On).updateValue(this.On);
489
- } else {
490
- this.outletService?.getCharacteristic(this.platform.Characteristic.On).updateValue(this.On);
491
- }
492
- this.debugLog(`Bot: ${this.accessory.displayName} On: ${this.On}, Switch Timeout`);
493
- }, 500);
494
- }).catch(async (e: any) => {
495
- this.errorLog(`Bot: ${this.accessory.displayName} failed pushChanges with BLE Connection`);
496
- if (this.deviceLogging === 'debug') {
497
- this.errorLog(`Bot: ${this.accessory.displayName} failed pushChanges with BLE Connection,`
498
- + ` Error Message: ${JSON.stringify(e.message)}`);
499
- }
500
- if (this.platform.debugMode) {
501
- this.errorLog(`Bot: ${this.accessory.displayName} failed pushChanges with BLE Connection,`
502
- + ` Error: ${JSON.stringify(e)}`);
503
- }
504
- if (this.platform.config.credentials?.openToken) {
505
- this.warnLog(`Bot: ${this.accessory.displayName} Using OpenAPI Connection`);
506
- await this.openAPIpushChanges();
507
- }
508
- this.apiError(e);
509
- });
510
- } else if (this.botMode === 'switch') {
511
- this.debugLog(`Bot: ${this.accessory.displayName} Press Mode: ${this.botMode}`);
512
- 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; }[]) => {
513
552
  this.infoLog(`Bot: ${this.accessory.displayName} On: ${this.On}`);
514
- return this.turnOnOff(device_list);
553
+ return device_list[0].press({ id: this.device.bleMac });
515
554
  }).then(() => {
516
555
  this.debugLog(`Bot: ${this.accessory.displayName} Done.`);
556
+ this.On = false;
517
557
  this.OnCached = this.On;
518
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);
519
567
  }).catch(async (e: any) => {
520
568
  this.errorLog(`Bot: ${this.accessory.displayName} failed pushChanges with BLE Connection`);
521
569
  if (this.deviceLogging === 'debug') {
@@ -532,16 +580,41 @@ export class Bot {
532
580
  }
533
581
  this.apiError(e);
534
582
  });
535
- } else {
536
- throw new Error(`Bot: ${this.accessory.displayName} Bot Mode: ${this.botMode}`);
537
- }
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
+ });
538
608
  } else {
539
- this.errorLog(`Bot: ${this.accessory.displayName} wasn't able to establish BLE Connection`);
540
- if (this.platform.config.credentials?.openToken) {
541
- this.warnLog(`Bot: ${this.accessory.displayName} Using OpenAPI Connection`);
542
- await this.openAPIpushChanges();
543
- }
609
+ throw new Error(`Bot: ${this.accessory.displayName} Bot Mode: ${this.botMode}`);
544
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
+ }*/
545
618
  }
546
619
  this.OnCached = this.On;
547
620
  this.accessory.context.On = this.OnCached;
@@ -628,7 +701,45 @@ export class Bot {
628
701
  this.debugLog(`Bot: ${this.accessory.displayName} updateCharacteristic TargetDoorState: Open, CurrentDoorState: Open`);
629
702
  }
630
703
  }
631
- this.debugLog(`Bot: ${this.accessory.displayName} updateCharacteristic On: ${this.On}`);
704
+ this.debugLog(`Bot: ${this.accessory.displayName} Garage Door On: ${this.On}`);
705
+ } else if (this.device.bot?.deviceType === 'door') {
706
+ if (this.On === undefined) {
707
+ this.debugLog(`Bot: ${this.accessory.displayName} On: ${this.On}`);
708
+ } else {
709
+ if (this.On) {
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);
714
+ this.debugLog(`Bot: ${this.accessory.displayName} updateCharacteristic TargetPosition: 100, CurrentPosition: 100`);
715
+ } else {
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);
720
+ this.debugLog(`Bot: ${this.accessory.displayName} updateCharacteristic TargetPosition: 0, CurrentPosition: 0`);
721
+ }
722
+ }
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}`);
632
743
  } else if (this.device.bot?.deviceType === 'switch') {
633
744
  if (this.On === undefined) {
634
745
  this.debugLog(`Bot: ${this.accessory.displayName} On: ${this.On}`);
@@ -644,8 +755,6 @@ export class Bot {
644
755
  this.debugLog(`Bot: ${this.accessory.displayName} updateCharacteristic On: ${this.On}`);
645
756
  }
646
757
  }
647
-
648
-
649
758
  if (this.device.ble) {
650
759
  if (this.BatteryLevel === undefined) {
651
760
  this.debugLog(`Bot: ${this.accessory.displayName} BatteryLevel: ${this.BatteryLevel}`);
@@ -663,7 +772,18 @@ export class Bot {
663
772
  }
664
773
 
665
774
  public apiError(e: any) {
666
- 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') {
667
787
  this.switchService?.updateCharacteristic(this.platform.Characteristic.On, e);
668
788
  } else {
669
789
  this.outletService?.updateCharacteristic(this.platform.Characteristic.On, e);
@@ -728,6 +848,20 @@ export class Bot {
728
848
  } else {
729
849
  this.On = true;
730
850
  }
851
+ } else if (this.device.bot?.deviceType === 'door') {
852
+ this.debugLog(`Bot: ${this.accessory.displayName} TargetPosition: ${value}`);
853
+ if (value === 100) {
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) {
861
+ this.On = false;
862
+ } else {
863
+ this.On = true;
864
+ }
731
865
  } else {
732
866
  this.debugLog(`Bot: ${this.accessory.displayName} On: ${value}`);
733
867
  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}`);