@switchbot/homebridge-switchbot 1.6.3 → 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.
@@ -13,10 +13,14 @@ import { DeviceURL, device, devicesConfig, serviceData, ad, switchbot, deviceSta
13
13
  */
14
14
  export class Bot {
15
15
  // Services
16
- private outletService?: Service;
17
16
  private switchService?: Service;
17
+ private doorService?: Service;
18
+ private lockService?: Service;
19
+ private garageDoorService?: Service;
20
+ private outletService?: Service;
18
21
  private batteryService?: Service;
19
22
 
23
+
20
24
  // Characteristic Values
21
25
  On!: CharacteristicValue;
22
26
  OnCached!: CharacteristicValue;
@@ -94,29 +98,74 @@ export class Bot {
94
98
  // get the LightBulb service if it exists, otherwise create a new LightBulb service
95
99
  // you can create multiple services for each accessory
96
100
  if (device.bot?.deviceType === 'switch') {
97
- // If outletService still pressent, then remove first
98
- if (this.outletService) {
99
- this.debugLog(`Bot: ${accessory.displayName} Removing Leftover outletService first`);
100
- }
101
- this.outletService = this.accessory.getService(this.platform.Service.Outlet);
102
- accessory.removeService(this.outletService!);
101
+ this.removeLockService(accessory);
102
+ this.removeDoorService(accessory);
103
+ this.removeOutletService(accessory);
104
+ this.removeGarageDoorService(accessory);
103
105
 
104
106
  // Add switchService
105
107
  (this.switchService =
106
108
  accessory.getService(this.platform.Service.Switch) ||
107
109
  accessory.addService(this.platform.Service.Switch)), `${accessory.displayName} Switch`;
108
- if (this.deviceLogging !== 'none') {
109
- this.infoLog(`Bot: ${accessory.displayName} Displaying as Switch`);
110
- }
110
+ this.infoLog(`Bot: ${accessory.displayName} Displaying as Switch`);
111
+
112
+ this.switchService.setCharacteristic(this.platform.Characteristic.Name, accessory.displayName);
113
+ this.switchService.getCharacteristic(this.platform.Characteristic.On).onSet(this.handleOnSet.bind(this));
114
+ } else if (device.bot?.deviceType === 'garagedoor') {
115
+ this.removeLockService(accessory);
116
+ this.removeDoorService(accessory);
117
+ this.removeOutletService(accessory);
118
+ this.removeSwitchService(accessory);
119
+
120
+ // Add switchService
121
+ (this.garageDoorService =
122
+ accessory.getService(this.platform.Service.GarageDoorOpener) ||
123
+ accessory.addService(this.platform.Service.GarageDoorOpener)), `${accessory.displayName} Garage Door Opener`;
124
+ this.infoLog(`Bot: ${accessory.displayName} Displaying as Garage Door Opener`);
125
+
126
+ this.garageDoorService.setCharacteristic(this.platform.Characteristic.Name, accessory.displayName);
127
+ this.garageDoorService.getCharacteristic(this.platform.Characteristic.TargetDoorState).onSet(this.handleOnSet.bind(this));
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);
111
155
 
112
- this.switchService?.setCharacteristic(this.platform.Characteristic.Name, accessory.displayName);
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));
113
164
  } else {
114
- // If switchService still pressent, then remove first
115
- if (this.switchService) {
116
- this.debugLog(`Bot: ${accessory.displayName} Removing Leftover switchService first`);
117
- }
118
- this.switchService = this.accessory.getService(this.platform.Service.Switch);
119
- accessory.removeService(this.switchService!);
165
+ this.removeLockService(accessory);
166
+ this.removeDoorService(accessory);
167
+ this.removeSwitchService(accessory);
168
+ this.removeGarageDoorService(accessory);
120
169
 
121
170
  // Add outletService
122
171
  (this.outletService =
@@ -124,7 +173,8 @@ export class Bot {
124
173
  accessory.addService(this.platform.Service.Outlet)), `${accessory.displayName} Outlet`;
125
174
  this.infoLog(`Bot: ${accessory.displayName} Displaying as Outlet`);
126
175
 
127
- this.outletService?.setCharacteristic(this.platform.Characteristic.Name, accessory.displayName);
176
+ this.outletService.setCharacteristic(this.platform.Characteristic.Name, accessory.displayName);
177
+ this.outletService.getCharacteristic(this.platform.Characteristic.On).onSet(this.handleOnSet.bind(this));
128
178
  }
129
179
 
130
180
  if (device.ble) {
@@ -135,15 +185,6 @@ export class Bot {
135
185
  this.batteryService.setCharacteristic(this.platform.Characteristic.Name, `${accessory.displayName} Battery`);
136
186
  }
137
187
 
138
- // each service must implement at-minimum the "required characteristics" for the given service type
139
- // see https://developers.homebridge.io/#/service/Outlet
140
-
141
- if (device.bot?.deviceType === 'switch') {
142
- this.switchService!.getCharacteristic(this.platform.Characteristic.On).onSet(this.handleOnSet.bind(this));
143
- } else {
144
- this.outletService!.getCharacteristic(this.platform.Characteristic.On).onSet(this.handleOnSet.bind(this));
145
- }
146
-
147
188
  // Retrieve initial values and updateHomekit
148
189
  this.updateHomeKitCharacteristics();
149
190
 
@@ -185,6 +226,51 @@ export class Bot {
185
226
  });
186
227
  }
187
228
 
229
+ private removeOutletService(accessory: PlatformAccessory) {
230
+ // If outletService still pressent, then remove first
231
+ if (this.outletService) {
232
+ this.debugLog(`Bot: ${accessory.displayName} Removing Leftover outletService first`);
233
+ }
234
+ this.outletService = this.accessory.getService(this.platform.Service.Outlet);
235
+ accessory.removeService(this.outletService!);
236
+ }
237
+
238
+ private removeGarageDoorService(accessory: PlatformAccessory) {
239
+ // If garageDoorService still pressent, then remove first
240
+ if (this.garageDoorService) {
241
+ this.debugLog(`Bot: ${accessory.displayName} Removing Leftover garageDoorService first`);
242
+ }
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!);
263
+ }
264
+
265
+ private removeSwitchService(accessory: PlatformAccessory) {
266
+ // If switchService still pressent, then remove first
267
+ if (this.switchService) {
268
+ this.debugLog(`Bot: ${accessory.displayName} Removing Leftover switchService first`);
269
+ }
270
+ this.switchService = this.accessory.getService(this.platform.Service.Switch);
271
+ accessory.removeService(this.switchService!);
272
+ }
273
+
188
274
  refreshRate() {
189
275
  if (this.device.refreshRate) {
190
276
  this.deviceRefreshRate = this.accessory.context.refreshRate = this.device.refreshRate;
@@ -321,11 +407,27 @@ export class Bot {
321
407
  }
322
408
  }
323
409
 
324
- private connectBLE() {
325
- // Convert to BLE Address
326
- this.device.bleMac = ((this.device.deviceId!.match(/.{1,2}/g))!.join(':')).toLowerCase();
327
- this.debugLog(`Bot: ${this.accessory.displayName} BLE Address: ${this.device.bleMac}`);
328
- 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
+ }
329
431
  return switchbot;
330
432
  }
331
433
 
@@ -333,7 +435,7 @@ export class Bot {
333
435
  this.debugLog(`Bot: ${this.accessory.displayName} BLE refreshStatus`);
334
436
 
335
437
  // Convert to BLE Address
336
- const switchbot = this.connectBLE();
438
+ const switchbot = await this.connectBLE();
337
439
  // Start to monitor advertisement packets
338
440
  if (switchbot !== false) {
339
441
  switchbot.startScan({
@@ -367,7 +469,7 @@ export class Bot {
367
469
  this.parseStatus();
368
470
  this.updateHomeKitCharacteristics();
369
471
  } else {
370
- await this.BLEconnection();
472
+ await this.BLEconnection(switchbot);
371
473
  }
372
474
  }).catch(async (e: any) => {
373
475
  this.errorLog(`Bot: ${this.accessory.displayName} failed refreshStatus with BLE Connection`);
@@ -386,12 +488,12 @@ export class Bot {
386
488
  this.apiError(e);
387
489
  });
388
490
  } else {
389
- await this.BLEconnection();
491
+ await this.BLEconnection(switchbot);
390
492
  }
391
493
  }
392
494
 
393
- private async BLEconnection() {
394
- 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}`);
395
497
  if (this.platform.config.credentials?.openToken) {
396
498
  this.warnLog(`Bot: ${this.accessory.displayName} Using OpenAPI Connection`);
397
499
  await this.openAPIRefreshStatus();
@@ -445,7 +547,7 @@ export class Bot {
445
547
  private async BLEpushChanges() {
446
548
  if (this.On !== this.OnCached) {
447
549
  this.debugLog(`Bot: ${this.accessory.displayName} BLE pushChanges`);
448
- const switchbot = this.connectBLE();
550
+ const switchbot = await this.connectBLE();
449
551
  if (switchbot !== false) {
450
552
  if (this.botMode === 'press') {
451
553
  this.debugLog(`Bot: ${this.accessory.displayName} Bot Mode: ${this.botMode}`);
@@ -585,15 +687,77 @@ export class Bot {
585
687
  * Updates the status for each of the HomeKit Characteristics
586
688
  */
587
689
  updateHomeKitCharacteristics() {
588
- if (this.On === undefined) {
589
- this.debugLog(`Bot: ${this.accessory.displayName} On: ${this.On}`);
590
- } else {
591
- if (this.device.bot?.deviceType === 'switch') {
690
+ if (this.device.bot?.deviceType === 'garagedoor') {
691
+ if (this.On === undefined) {
692
+ this.debugLog(`Bot: ${this.accessory.displayName} On: ${this.On}`);
693
+ } else {
694
+ if (this.On) {
695
+ this.garageDoorService?.updateCharacteristic(this.platform.Characteristic.TargetDoorState,
696
+ this.platform.Characteristic.TargetDoorState.OPEN);
697
+ this.garageDoorService?.updateCharacteristic(this.platform.Characteristic.CurrentDoorState,
698
+ this.platform.Characteristic.CurrentDoorState.OPEN);
699
+ this.debugLog(`Bot: ${this.accessory.displayName} updateCharacteristic TargetDoorState: Open, CurrentDoorState: Open`);
700
+ } else {
701
+ this.garageDoorService?.updateCharacteristic(this.platform.Characteristic.TargetDoorState,
702
+ this.platform.Characteristic.TargetDoorState.CLOSED);
703
+ this.garageDoorService?.updateCharacteristic(this.platform.Characteristic.CurrentDoorState,
704
+ this.platform.Characteristic.CurrentDoorState.CLOSED);
705
+ this.debugLog(`Bot: ${this.accessory.displayName} updateCharacteristic TargetDoorState: Open, CurrentDoorState: Open`);
706
+ }
707
+ }
708
+ this.debugLog(`Bot: ${this.accessory.displayName} Garage Door On: ${this.On}`);
709
+ } else if (this.device.bot?.deviceType === 'door') {
710
+ if (this.On === undefined) {
711
+ this.debugLog(`Bot: ${this.accessory.displayName} On: ${this.On}`);
712
+ } else {
713
+ if (this.On) {
714
+ this.doorService?.updateCharacteristic(this.platform.Characteristic.TargetPosition, 100);
715
+ this.doorService?.updateCharacteristic(this.platform.Characteristic.CurrentPosition, 100);
716
+ this.doorService?.updateCharacteristic(this.platform.Characteristic.PositionState,
717
+ this.platform.Characteristic.PositionState.STOPPED);
718
+ this.debugLog(`Bot: ${this.accessory.displayName} updateCharacteristic TargetPosition: 100, CurrentPosition: 100`);
719
+ } else {
720
+ this.doorService?.updateCharacteristic(this.platform.Characteristic.TargetPosition, 0);
721
+ this.doorService?.updateCharacteristic(this.platform.Characteristic.CurrentPosition, 0);
722
+ this.doorService?.updateCharacteristic(this.platform.Characteristic.PositionState,
723
+ this.platform.Characteristic.PositionState.STOPPED);
724
+ this.debugLog(`Bot: ${this.accessory.displayName} updateCharacteristic TargetPosition: 0, CurrentPosition: 0`);
725
+ }
726
+ }
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}`);
747
+ } else if (this.device.bot?.deviceType === 'switch') {
748
+ if (this.On === undefined) {
749
+ this.debugLog(`Bot: ${this.accessory.displayName} On: ${this.On}`);
750
+ } else {
592
751
  this.switchService?.updateCharacteristic(this.platform.Characteristic.On, this.On);
752
+ this.debugLog(`Bot: ${this.accessory.displayName} updateCharacteristic On: ${this.On}`);
753
+ }
754
+ } else {
755
+ if (this.On === undefined) {
756
+ this.debugLog(`Bot: ${this.accessory.displayName} On: ${this.On}`);
593
757
  } else {
594
758
  this.outletService?.updateCharacteristic(this.platform.Characteristic.On, this.On);
759
+ this.debugLog(`Bot: ${this.accessory.displayName} updateCharacteristic On: ${this.On}`);
595
760
  }
596
- this.debugLog(`Bot: ${this.accessory.displayName} updateCharacteristic On: ${this.On}`);
597
761
  }
598
762
  if (this.device.ble) {
599
763
  if (this.BatteryLevel === undefined) {
@@ -612,7 +776,18 @@ export class Bot {
612
776
  }
613
777
 
614
778
  public apiError(e: any) {
615
- if (this.device.bot?.deviceType === 'switch') {
779
+ if (this.device.bot?.deviceType === 'garagedoor') {
780
+ this.garageDoorService?.updateCharacteristic(this.platform.Characteristic.TargetDoorState, e);
781
+ this.garageDoorService?.updateCharacteristic(this.platform.Characteristic.CurrentDoorState, e);
782
+ this.garageDoorService?.updateCharacteristic(this.platform.Characteristic.ObstructionDetected, e);
783
+ } else if (this.device.bot?.deviceType === 'door') {
784
+ this.doorService?.updateCharacteristic(this.platform.Characteristic.TargetPosition, e);
785
+ this.doorService?.updateCharacteristic(this.platform.Characteristic.CurrentPosition, e);
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);
790
+ } else if (this.device.bot?.deviceType === 'switch') {
616
791
  this.switchService?.updateCharacteristic(this.platform.Characteristic.On, e);
617
792
  } else {
618
793
  this.outletService?.updateCharacteristic(this.platform.Characteristic.On, e);
@@ -670,8 +845,31 @@ export class Bot {
670
845
  * Handle requests to set the "On" characteristic
671
846
  */
672
847
  private handleOnSet(value: CharacteristicValue) {
673
- this.debugLog(`Bot: ${this.accessory.displayName} On: ${value}`);
674
- this.On = value;
848
+ if (this.device.bot?.deviceType === 'garagedoor') {
849
+ this.debugLog(`Bot: ${this.accessory.displayName} TargetDoorState: ${value}`);
850
+ if (value === this.platform.Characteristic.TargetDoorState.CLOSED) {
851
+ this.On = false;
852
+ } else {
853
+ this.On = true;
854
+ }
855
+ } else if (this.device.bot?.deviceType === 'door') {
856
+ this.debugLog(`Bot: ${this.accessory.displayName} TargetPosition: ${value}`);
857
+ if (value === 100) {
858
+ this.On = false;
859
+ } else {
860
+ this.On = true;
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
+ }
869
+ } else {
870
+ this.debugLog(`Bot: ${this.accessory.displayName} On: ${value}`);
871
+ this.On = value;
872
+ }
675
873
  this.doBotUpdate.next();
676
874
  }
677
875
 
@@ -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();