@switchbot/homebridge-switchbot 3.7.1-beta.2 → 3.7.1-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.
Files changed (96) hide show
  1. package/CHANGELOG.md +8 -0
  2. package/dist/device/blindtilt.d.ts +3 -1
  3. package/dist/device/blindtilt.d.ts.map +1 -1
  4. package/dist/device/blindtilt.js +61 -28
  5. package/dist/device/blindtilt.js.map +1 -1
  6. package/dist/device/bot.d.ts +1 -0
  7. package/dist/device/bot.d.ts.map +1 -1
  8. package/dist/device/bot.js +53 -28
  9. package/dist/device/bot.js.map +1 -1
  10. package/dist/device/ceilinglight.d.ts +1 -0
  11. package/dist/device/ceilinglight.d.ts.map +1 -1
  12. package/dist/device/ceilinglight.js +53 -28
  13. package/dist/device/ceilinglight.js.map +1 -1
  14. package/dist/device/colorbulb.d.ts +1 -0
  15. package/dist/device/colorbulb.d.ts.map +1 -1
  16. package/dist/device/colorbulb.js +53 -28
  17. package/dist/device/colorbulb.js.map +1 -1
  18. package/dist/device/contact.d.ts +1 -0
  19. package/dist/device/contact.d.ts.map +1 -1
  20. package/dist/device/contact.js +53 -28
  21. package/dist/device/contact.js.map +1 -1
  22. package/dist/device/curtain.d.ts +2 -0
  23. package/dist/device/curtain.d.ts.map +1 -1
  24. package/dist/device/curtain.js +74 -43
  25. package/dist/device/curtain.js.map +1 -1
  26. package/dist/device/fan.d.ts +1 -0
  27. package/dist/device/fan.d.ts.map +1 -1
  28. package/dist/device/fan.js +53 -28
  29. package/dist/device/fan.js.map +1 -1
  30. package/dist/device/hub.d.ts +1 -0
  31. package/dist/device/hub.d.ts.map +1 -1
  32. package/dist/device/hub.js +53 -28
  33. package/dist/device/hub.js.map +1 -1
  34. package/dist/device/humidifier.d.ts +1 -0
  35. package/dist/device/humidifier.d.ts.map +1 -1
  36. package/dist/device/humidifier.js +53 -28
  37. package/dist/device/humidifier.js.map +1 -1
  38. package/dist/device/iosensor.d.ts +1 -0
  39. package/dist/device/iosensor.d.ts.map +1 -1
  40. package/dist/device/iosensor.js +53 -28
  41. package/dist/device/iosensor.js.map +1 -1
  42. package/dist/device/lightstrip.d.ts +1 -0
  43. package/dist/device/lightstrip.d.ts.map +1 -1
  44. package/dist/device/lightstrip.js +53 -28
  45. package/dist/device/lightstrip.js.map +1 -1
  46. package/dist/device/lock.d.ts +1 -0
  47. package/dist/device/lock.d.ts.map +1 -1
  48. package/dist/device/lock.js +54 -29
  49. package/dist/device/lock.js.map +1 -1
  50. package/dist/device/meter.d.ts +1 -0
  51. package/dist/device/meter.d.ts.map +1 -1
  52. package/dist/device/meter.js +53 -28
  53. package/dist/device/meter.js.map +1 -1
  54. package/dist/device/meterplus.d.ts +1 -0
  55. package/dist/device/meterplus.d.ts.map +1 -1
  56. package/dist/device/meterplus.js +53 -28
  57. package/dist/device/meterplus.js.map +1 -1
  58. package/dist/device/motion.d.ts +1 -0
  59. package/dist/device/motion.d.ts.map +1 -1
  60. package/dist/device/motion.js +53 -28
  61. package/dist/device/motion.js.map +1 -1
  62. package/dist/device/plug.d.ts +1 -0
  63. package/dist/device/plug.d.ts.map +1 -1
  64. package/dist/device/plug.js +54 -29
  65. package/dist/device/plug.js.map +1 -1
  66. package/dist/device/robotvacuumcleaner.d.ts +1 -0
  67. package/dist/device/robotvacuumcleaner.d.ts.map +1 -1
  68. package/dist/device/robotvacuumcleaner.js +53 -28
  69. package/dist/device/robotvacuumcleaner.js.map +1 -1
  70. package/dist/device/waterdetector.d.ts +1 -0
  71. package/dist/device/waterdetector.d.ts.map +1 -1
  72. package/dist/device/waterdetector.js +53 -28
  73. package/dist/device/waterdetector.js.map +1 -1
  74. package/dist/platform.d.ts.map +1 -1
  75. package/dist/platform.js +22 -3
  76. package/dist/platform.js.map +1 -1
  77. package/package.json +7 -7
  78. package/src/device/blindtilt.ts +59 -27
  79. package/src/device/bot.ts +50 -26
  80. package/src/device/ceilinglight.ts +51 -27
  81. package/src/device/colorbulb.ts +50 -26
  82. package/src/device/contact.ts +50 -26
  83. package/src/device/curtain.ts +71 -40
  84. package/src/device/fan.ts +50 -26
  85. package/src/device/hub.ts +50 -26
  86. package/src/device/humidifier.ts +50 -26
  87. package/src/device/iosensor.ts +50 -26
  88. package/src/device/lightstrip.ts +50 -26
  89. package/src/device/lock.ts +51 -27
  90. package/src/device/meter.ts +50 -26
  91. package/src/device/meterplus.ts +50 -26
  92. package/src/device/motion.ts +50 -26
  93. package/src/device/plug.ts +51 -27
  94. package/src/device/robotvacuumcleaner.ts +50 -26
  95. package/src/device/waterdetector.ts +50 -26
  96. package/src/platform.ts +19 -3
@@ -4,7 +4,12 @@
4
4
  */
5
5
  import { hostname } from 'os';
6
6
  import { deviceBase } from './device.js';
7
+ /*
8
+ * For Testing Locally:
9
+ * import { SwitchBotBLEModel, SwitchBotBLEModelName } from '/Users/Shared/GitHub/OpenWonderLabs/node-switchbot/dist/index.js';
10
+ */
7
11
  import { SwitchBotBLEModel, SwitchBotBLEModelName } from 'node-switchbot';
12
+
8
13
  import { Subject, debounceTime, interval, skipWhile, take, tap } from 'rxjs';
9
14
 
10
15
  import type { devicesConfig } from '../settings.js';
@@ -54,6 +59,7 @@ export class Curtain extends deviceBase {
54
59
  setNewTargetTimer!: NodeJS.Timeout;
55
60
 
56
61
  // Updates
62
+ curtainMoving!: boolean;
57
63
  curtainUpdateInProgress!: boolean;
58
64
  doCurtainUpdate!: Subject<void>;
59
65
 
@@ -68,6 +74,7 @@ export class Curtain extends deviceBase {
68
74
 
69
75
  // this is subject we use to track when we need to POST changes to the SwitchBot API
70
76
  this.doCurtainUpdate = new Subject();
77
+ this.curtainMoving = false;
71
78
  this.curtainUpdateInProgress = false;
72
79
  this.setNewTarget = false;
73
80
 
@@ -186,12 +193,28 @@ export class Curtain extends deviceBase {
186
193
  }
187
194
 
188
195
  // Retrieve initial values and updateHomekit
189
- this.debugLog('Retrieve initial values and update Homekit');
190
- this.refreshStatus();
196
+ try {
197
+ this.debugLog('Retrieve initial values and update Homekit');
198
+ this.refreshStatus();
199
+ } catch (e: any) {
200
+ this.errorLog(`failed to retrieve initial values and update Homekit, Error: ${e}`);
201
+ }
191
202
 
192
- //regisiter webhook event handler
193
- this.debugLog('Registering Webhook Event Handler');
194
- this.registerWebhook();
203
+ //regisiter webhook event handler if enabled
204
+ try {
205
+ this.debugLog('Registering Webhook Event Handler');
206
+ this.registerWebhook();
207
+ } catch (e: any) {
208
+ this.errorLog(`failed to registerWebhook, Error: ${e}`);
209
+ }
210
+
211
+ //regisiter platform BLE event handler if enabled
212
+ try {
213
+ this.debugLog('Registering Platform BLE Event Handler');
214
+ this.registerPlatformBLE();
215
+ } catch (e: any) {
216
+ this.errorLog(`failed to registerPlatformBLE, Error: ${e}`);
217
+ }
195
218
 
196
219
  // History
197
220
  this.history();
@@ -205,7 +228,7 @@ export class Curtain extends deviceBase {
205
228
 
206
229
  // update slide progress
207
230
  interval(this.deviceUpdateRate * 1000)
208
- //.pipe(skipWhile(() => this.curtainUpdateInProgress))
231
+ .pipe(skipWhile(() => !this.curtainMoving))
209
232
  .subscribe(async () => {
210
233
  if (this.WindowCovering.PositionState === this.hap.Characteristic.PositionState.STOPPED) {
211
234
  return;
@@ -340,6 +363,7 @@ export class Curtain extends deviceBase {
340
363
  await this.infoLog('Checking Status ...');
341
364
  }
342
365
  if (this.setNewTarget && this.deviceStatus.moving) {
366
+ this.curtainMoving = true;
343
367
  await this.setMinMax();
344
368
  if (Number(this.WindowCovering.TargetPosition) > this.WindowCovering.CurrentPosition) {
345
369
  await this.debugLog(`Closing, CurrentPosition: ${this.WindowCovering.CurrentPosition}`);
@@ -358,6 +382,7 @@ export class Curtain extends deviceBase {
358
382
  await this.debugLog(`Stopped, PositionState: ${this.WindowCovering.PositionState}`);
359
383
  }
360
384
  } else {
385
+ this.curtainMoving = false;
361
386
  await this.debugLog(`Standby, CurrentPosition: ${this.WindowCovering.CurrentPosition}`);
362
387
  this.WindowCovering.TargetPosition = this.WindowCovering.CurrentPosition;
363
388
  this.WindowCovering.PositionState = this.hap.Characteristic.PositionState.STOPPED;
@@ -425,7 +450,7 @@ export class Curtain extends deviceBase {
425
450
  async refreshStatus(): Promise<void> {
426
451
  if (!this.device.enableCloudService && this.OpenAPI) {
427
452
  await this.errorLog(`refreshStatus enableCloudService: ${this.device.enableCloudService}`);
428
- } else if (this.BLE || this.config.options?.BLE) {
453
+ } else if (this.BLE) {
429
454
  await this.BLERefreshStatus();
430
455
  } else if (this.OpenAPI && this.platform.config.credentials?.token) {
431
456
  await this.openAPIRefreshStatus();
@@ -437,42 +462,25 @@ export class Curtain extends deviceBase {
437
462
 
438
463
  async BLERefreshStatus(): Promise<void> {
439
464
  await this.debugLog('BLERefreshStatus');
440
- if (this.config.options?.BLE) {
441
- await this.debugLog('is listening to Platform BLE.');
442
- this.device.bleMac = this.device.deviceId!.match(/.{1,2}/g)!.join(':').toLowerCase();
443
- await this.debugLog(`bleMac: ${this.device.bleMac}`);
444
- this.platform.bleEventHandler[this.device.bleMac] = async (context: curtainServiceData | curtain3ServiceData) => {
445
- try {
446
- await this.debugLog(`received BLE: ${JSON.stringify(context)}`);
447
- this.serviceData = context;
448
- await this.BLEparseStatus();
449
- await this.updateHomeKitCharacteristics();
450
- } catch (e: any) {
451
- await this.errorLog(`failed to handle BLE. Received: ${JSON.stringify(context)} Error: ${e}`);
452
- }
453
- };
465
+ const switchbot = await this.switchbotBLE();
466
+ if (switchbot === undefined) {
467
+ await this.BLERefreshConnection(switchbot);
454
468
  } else {
455
- await this.debugLog('is using Device BLE Scanning.');
456
- const switchbot = await this.switchbotBLE();
457
- if (switchbot === undefined) {
458
- await this.BLERefreshConnection(switchbot);
459
- } else {
469
+ // Start to monitor advertisement packets
470
+ (async () => {
460
471
  // Start to monitor advertisement packets
461
- (async () => {
462
- // Start to monitor advertisement packets
463
- const serviceData = await this.monitorAdvertisementPackets(switchbot) as curtainServiceData | curtain3ServiceData;
464
- // Update HomeKit
465
- if ((serviceData.model === SwitchBotBLEModel.Curtain || SwitchBotBLEModel.Curtain3)
472
+ const serviceData = await this.monitorAdvertisementPackets(switchbot) as curtainServiceData | curtain3ServiceData;
473
+ // Update HomeKit
474
+ if ((serviceData.model === SwitchBotBLEModel.Curtain || SwitchBotBLEModel.Curtain3)
466
475
  && (serviceData.modelName === SwitchBotBLEModelName.Curtain || SwitchBotBLEModelName.Curtain3)) {
467
- this.serviceData = serviceData;
468
- await this.BLEparseStatus();
469
- await this.updateHomeKitCharacteristics();
470
- } else {
471
- await this.errorLog(`failed to get serviceData, serviceData: ${serviceData}`);
472
- await this.BLERefreshConnection(switchbot);
473
- }
474
- })();
475
- }
476
+ this.serviceData = serviceData;
477
+ await this.BLEparseStatus();
478
+ await this.updateHomeKitCharacteristics();
479
+ } else {
480
+ await this.errorLog(`failed to get serviceData, serviceData: ${serviceData}`);
481
+ await this.BLERefreshConnection(switchbot);
482
+ }
483
+ })();
476
484
  }
477
485
  }
478
486
 
@@ -515,6 +523,27 @@ export class Curtain extends deviceBase {
515
523
  }
516
524
  }
517
525
 
526
+ async registerPlatformBLE(): Promise<void> {
527
+ await this.debugLog('registerPlatformBLE');
528
+ if (this.config.options?.BLE) {
529
+ await this.debugLog('is listening to Platform BLE.');
530
+ this.device.bleMac = this.device.deviceId!.match(/.{1,2}/g)!.join(':').toLowerCase();
531
+ await this.debugLog(`bleMac: ${this.device.bleMac}`);
532
+ this.platform.bleEventHandler[this.device.bleMac] = async (context: curtainServiceData | curtain3ServiceData) => {
533
+ try {
534
+ await this.debugLog(`received BLE: ${JSON.stringify(context)}`);
535
+ this.serviceData = context;
536
+ await this.BLEparseStatus();
537
+ await this.updateHomeKitCharacteristics();
538
+ } catch (e: any) {
539
+ await this.errorLog(`failed to handle BLE. Received: ${JSON.stringify(context)} Error: ${e}`);
540
+ }
541
+ };
542
+ } else {
543
+ await this.debugLog('is not listening to Platform BLE');
544
+ }
545
+ }
546
+
518
547
  /**
519
548
  * Pushes the requested changes to the SwitchBot API
520
549
  */
@@ -761,6 +790,7 @@ export class Curtain extends deviceBase {
761
790
  await this.setMinMax();
762
791
  await this.debugLog(`CurrentPosition ${this.WindowCovering.CurrentPosition}`);
763
792
  if (this.setNewTarget) {
793
+ this.curtainMoving = true;
764
794
  this.infoLog('Checking Status ...');
765
795
  await this.setMinMax();
766
796
  if (this.WindowCovering.TargetPosition > this.WindowCovering.CurrentPosition) {
@@ -780,6 +810,7 @@ export class Curtain extends deviceBase {
780
810
  await this.debugLog(`Stopped, PositionState: ${this.WindowCovering.PositionState}`);
781
811
  }
782
812
  } else {
813
+ this.curtainMoving = false;
783
814
  await this.debugLog(`Standby, CurrentPosition: ${this.WindowCovering.CurrentPosition}`);
784
815
  this.WindowCovering.TargetPosition = this.WindowCovering.CurrentPosition;
785
816
  this.WindowCovering.PositionState = this.hap.Characteristic.PositionState.STOPPED;
package/src/device/fan.ts CHANGED
@@ -3,6 +3,10 @@
3
3
  * plug.ts: @switchbot/homebridge-switchbot.
4
4
  */
5
5
  import { deviceBase } from './device.js';
6
+ /*
7
+ * For Testing Locally:
8
+ * import { SwitchBotBLEModel, SwitchBotBLEModelName } from '/Users/Shared/GitHub/OpenWonderLabs/node-switchbot/dist/index.js';
9
+ */
6
10
  import { SwitchBotBLEModel, SwitchBotBLEModelName } from 'node-switchbot';
7
11
  import { Subject, debounceTime, interval, skipWhile, take, tap } from 'rxjs';
8
12
 
@@ -161,12 +165,28 @@ export class Fan extends deviceBase {
161
165
  .onSet(this.BrightnessSet.bind(this));
162
166
 
163
167
  // Retrieve initial values and updateHomekit
164
- this.debugLog('Retrieve initial values and update Homekit');
165
- this.refreshStatus();
168
+ try {
169
+ this.debugLog('Retrieve initial values and update Homekit');
170
+ this.refreshStatus();
171
+ } catch (e: any) {
172
+ this.errorLog(`failed to retrieve initial values and update Homekit, Error: ${e}`);
173
+ }
166
174
 
167
- //regisiter webhook event handler
168
- this.debugLog('Registering Webhook Event Handler');
169
- this.registerWebhook();
175
+ //regisiter webhook event handler if enabled
176
+ try {
177
+ this.debugLog('Registering Webhook Event Handler');
178
+ this.registerWebhook();
179
+ } catch (e: any) {
180
+ this.errorLog(`failed to registerWebhook, Error: ${e}`);
181
+ }
182
+
183
+ //regisiter platform BLE event handler if enabled
184
+ try {
185
+ this.debugLog('Registering Platform BLE Event Handler');
186
+ this.registerPlatformBLE();
187
+ } catch (e: any) {
188
+ this.errorLog(`failed to registerPlatformBLE, Error: ${e}`);
189
+ }
170
190
 
171
191
  // Start an update interval
172
192
  interval(this.deviceRefreshRate * 1000)
@@ -311,7 +331,7 @@ export class Fan extends deviceBase {
311
331
  async refreshStatus(): Promise<void> {
312
332
  if (!this.device.enableCloudService && this.OpenAPI) {
313
333
  await this.errorLog(`refreshStatus enableCloudService: ${this.device.enableCloudService}`);
314
- } else if (this.BLE || this.config.options?.BLE) {
334
+ } else if (this.BLE) {
315
335
  await this.BLERefreshStatus();
316
336
  } else if (this.OpenAPI && this.platform.config.credentials?.token) {
317
337
  await this.openAPIRefreshStatus();
@@ -323,6 +343,29 @@ export class Fan extends deviceBase {
323
343
 
324
344
  async BLERefreshStatus(): Promise<void> {
325
345
  await this.debugLog('BLERefreshStatus');
346
+ const switchbot = await this.switchbotBLE();
347
+ if (switchbot === undefined) {
348
+ await this.BLERefreshConnection(switchbot);
349
+ } else {
350
+ // Start to monitor advertisement packets
351
+ (async () => {
352
+ // Start to monitor advertisement packets
353
+ const serviceData = await this.monitorAdvertisementPackets(switchbot) as batteryCirculatorFanServiceData;
354
+ // Update HomeKit
355
+ if (serviceData.model === SwitchBotBLEModel.Unknown && SwitchBotBLEModelName.Unknown) {
356
+ this.serviceData = serviceData;
357
+ await this.BLEparseStatus();
358
+ await this.updateHomeKitCharacteristics();
359
+ } else {
360
+ await this.errorLog(`failed to get serviceData, serviceData: ${serviceData}`);
361
+ await this.BLERefreshConnection(switchbot);
362
+ }
363
+ })();
364
+ }
365
+ }
366
+
367
+ async registerPlatformBLE(): Promise<void> {
368
+ await this.debugLog('registerPlatformBLE');
326
369
  if (this.config.options?.BLE) {
327
370
  await this.debugLog('is listening to Platform BLE.');
328
371
  this.device.bleMac = this.device.deviceId!.match(/.{1,2}/g)!.join(':').toLowerCase();
@@ -338,26 +381,7 @@ export class Fan extends deviceBase {
338
381
  }
339
382
  };
340
383
  } else {
341
- await this.debugLog('is using Device BLE Scanning.');
342
- const switchbot = await this.switchbotBLE();
343
- if (switchbot === undefined) {
344
- await this.BLERefreshConnection(switchbot);
345
- } else {
346
- // Start to monitor advertisement packets
347
- (async () => {
348
- // Start to monitor advertisement packets
349
- const serviceData = await this.monitorAdvertisementPackets(switchbot) as batteryCirculatorFanServiceData;
350
- // Update HomeKit
351
- if (serviceData.model === SwitchBotBLEModel.Unknown && SwitchBotBLEModelName.Unknown) {
352
- this.serviceData = serviceData;
353
- await this.BLEparseStatus();
354
- await this.updateHomeKitCharacteristics();
355
- } else {
356
- await this.errorLog(`failed to get serviceData, serviceData: ${serviceData}`);
357
- await this.BLERefreshConnection(switchbot);
358
- }
359
- })();
360
- }
384
+ await this.debugLog('is not listening to Platform BLE');
361
385
  }
362
386
  }
363
387
 
package/src/device/hub.ts CHANGED
@@ -6,6 +6,10 @@ import { Units } from 'homebridge';
6
6
  import { deviceBase } from './device.js';
7
7
  import { Subject, interval, skipWhile} from 'rxjs';
8
8
  import { validHumidity, convertUnits } from '../utils.js';
9
+ /*
10
+ * For Testing Locally:
11
+ * import { SwitchBotBLEModel, SwitchBotBLEModelName } from '/Users/Shared/GitHub/OpenWonderLabs/node-switchbot/dist/index.js';
12
+ */
9
13
  import { SwitchBotBLEModel, SwitchBotBLEModelName } from 'node-switchbot';
10
14
 
11
15
  import type { devicesConfig } from '../settings.js';
@@ -151,12 +155,28 @@ export class Hub extends deviceBase {
151
155
  }
152
156
 
153
157
  // Retrieve initial values and updateHomekit
154
- this.debugLog('Retrieve initial values and update Homekit');
155
- this.refreshStatus();
158
+ try {
159
+ this.debugLog('Retrieve initial values and update Homekit');
160
+ this.refreshStatus();
161
+ } catch (e: any) {
162
+ this.errorLog(`failed to retrieve initial values and update Homekit, Error: ${e}`);
163
+ }
156
164
 
157
- //regisiter webhook event handler
158
- this.debugLog('Registering Webhook Event Handler');
159
- this.registerWebhook();
165
+ //regisiter webhook event handler if enabled
166
+ try {
167
+ this.debugLog('Registering Webhook Event Handler');
168
+ this.registerWebhook();
169
+ } catch (e: any) {
170
+ this.errorLog(`failed to registerWebhook, Error: ${e}`);
171
+ }
172
+
173
+ //regisiter platform BLE event handler if enabled
174
+ try {
175
+ this.debugLog('Registering Platform BLE Event Handler');
176
+ this.registerPlatformBLE();
177
+ } catch (e: any) {
178
+ this.errorLog(`failed to registerPlatformBLE, Error: ${e}`);
179
+ }
160
180
 
161
181
  // Start an update interval
162
182
  interval(this.deviceRefreshRate * 1000)
@@ -278,7 +298,7 @@ export class Hub extends deviceBase {
278
298
  async refreshStatus(): Promise<void> {
279
299
  if (!this.device.enableCloudService && this.OpenAPI) {
280
300
  await this.errorLog(`refreshStatus enableCloudService: ${this.device.enableCloudService}`);
281
- } else if (this.BLE || this.config.options?.BLE) {
301
+ } else if (this.BLE) {
282
302
  await this.BLERefreshStatus();
283
303
  } else if (this.OpenAPI && this.platform.config.credentials?.token) {
284
304
  await this.openAPIRefreshStatus();
@@ -290,6 +310,29 @@ export class Hub extends deviceBase {
290
310
 
291
311
  async BLERefreshStatus(): Promise<void> {
292
312
  await this.debugLog('BLERefreshStatus');
313
+ const switchbot = await this.switchbotBLE();
314
+ if (switchbot === undefined) {
315
+ await this.BLERefreshConnection(switchbot);
316
+ } else {
317
+ // Start to monitor advertisement packets
318
+ (async () => {
319
+ // Start to monitor advertisement packets
320
+ const serviceData = await this.monitorAdvertisementPackets(switchbot) as hub2ServiceData;
321
+ // Update HomeKit
322
+ if (serviceData.model === SwitchBotBLEModel.Hub2 && serviceData.modelName === SwitchBotBLEModelName.Hub2) {
323
+ this.serviceData = serviceData;
324
+ await this.BLEparseStatus();
325
+ await this.updateHomeKitCharacteristics();
326
+ } else {
327
+ await this.errorLog(`failed to get serviceData, serviceData: ${serviceData}`);
328
+ await this.BLERefreshConnection(switchbot);
329
+ }
330
+ })();
331
+ }
332
+ }
333
+
334
+ async registerPlatformBLE(): Promise<void> {
335
+ await this.debugLog('registerPlatformBLE');
293
336
  if (this.config.options?.BLE) {
294
337
  await this.debugLog('is listening to Platform BLE.');
295
338
  this.device.bleMac = this.device.deviceId!.match(/.{1,2}/g)!.join(':').toLowerCase();
@@ -305,26 +348,7 @@ export class Hub extends deviceBase {
305
348
  }
306
349
  };
307
350
  } else {
308
- await this.debugLog('is using Device BLE Scanning.');
309
- const switchbot = await this.switchbotBLE();
310
- if (switchbot === undefined) {
311
- await this.BLERefreshConnection(switchbot);
312
- } else {
313
- // Start to monitor advertisement packets
314
- (async () => {
315
- // Start to monitor advertisement packets
316
- const serviceData = await this.monitorAdvertisementPackets(switchbot) as hub2ServiceData;
317
- // Update HomeKit
318
- if (serviceData.model === SwitchBotBLEModel.Hub2 && serviceData.modelName === SwitchBotBLEModelName.Hub2) {
319
- this.serviceData = serviceData;
320
- await this.BLEparseStatus();
321
- await this.updateHomeKitCharacteristics();
322
- } else {
323
- await this.errorLog(`failed to get serviceData, serviceData: ${serviceData}`);
324
- await this.BLERefreshConnection(switchbot);
325
- }
326
- })();
327
- }
351
+ await this.debugLog('is not listening to Platform BLE');
328
352
  }
329
353
  }
330
354
 
@@ -4,6 +4,10 @@
4
4
  */
5
5
  import { deviceBase } from './device.js';
6
6
  import { convertUnits, validHumidity } from '../utils.js';
7
+ /*
8
+ * For Testing Locally:
9
+ * import { SwitchBotBLEModel, SwitchBotBLEModelName } from '/Users/Shared/GitHub/OpenWonderLabs/node-switchbot/dist/index.js';
10
+ */
7
11
  import { SwitchBotBLEModel, SwitchBotBLEModelName } from 'node-switchbot';
8
12
  import { Subject, debounceTime, interval, skipWhile, take, tap } from 'rxjs';
9
13
 
@@ -151,12 +155,28 @@ export class Humidifier extends deviceBase {
151
155
  }
152
156
 
153
157
  // Retrieve initial values and updateHomekit
154
- this.debugLog('Retrieve initial values and update Homekit');
155
- this.refreshStatus();
158
+ try {
159
+ this.debugLog('Retrieve initial values and update Homekit');
160
+ this.refreshStatus();
161
+ } catch (e: any) {
162
+ this.errorLog(`failed to retrieve initial values and update Homekit, Error: ${e}`);
163
+ }
156
164
 
157
- //regisiter webhook event handler
158
- this.debugLog('Registering Webhook Event Handler');
159
- this.registerWebhook();
165
+ //regisiter webhook event handler if enabled
166
+ try {
167
+ this.debugLog('Registering Webhook Event Handler');
168
+ this.registerWebhook();
169
+ } catch (e: any) {
170
+ this.errorLog(`failed to registerWebhook, Error: ${e}`);
171
+ }
172
+
173
+ //regisiter platform BLE event handler if enabled
174
+ try {
175
+ this.debugLog('Registering Platform BLE Event Handler');
176
+ this.registerPlatformBLE();
177
+ } catch (e: any) {
178
+ this.errorLog(`failed to registerPlatformBLE, Error: ${e}`);
179
+ }
160
180
 
161
181
  // Start an update interval
162
182
  interval(this.deviceRefreshRate * 1000)
@@ -318,7 +338,7 @@ export class Humidifier extends deviceBase {
318
338
  async refreshStatus(): Promise<void> {
319
339
  if (!this.device.enableCloudService && this.OpenAPI) {
320
340
  await this.errorLog(`refreshStatus enableCloudService: ${this.device.enableCloudService}`);
321
- } else if (this.BLE || this.config.options?.BLE) {
341
+ } else if (this.BLE) {
322
342
  await this.BLERefreshStatus();
323
343
  } else if (this.OpenAPI && this.platform.config.credentials?.token) {
324
344
  await this.openAPIRefreshStatus();
@@ -330,6 +350,29 @@ export class Humidifier extends deviceBase {
330
350
 
331
351
  async BLERefreshStatus(): Promise<void> {
332
352
  await this.debugLog('BLERefreshStatus');
353
+ const switchbot = await this.switchbotBLE();
354
+ if (switchbot === undefined) {
355
+ await this.BLERefreshConnection(switchbot);
356
+ } else {
357
+ // Start to monitor advertisement packets
358
+ (async () => {
359
+ // Start to monitor advertisement packets
360
+ const serviceData = await this.monitorAdvertisementPackets(switchbot) as humidifierServiceData;
361
+ // Update HomeKit
362
+ if (serviceData.model === SwitchBotBLEModel.Humidifier && serviceData.modelName === SwitchBotBLEModelName.Humidifier) {
363
+ this.serviceData = serviceData;
364
+ await this.BLEparseStatus();
365
+ await this.updateHomeKitCharacteristics();
366
+ } else {
367
+ await this.errorLog(`failed to get serviceData, serviceData: ${serviceData}`);
368
+ await this.BLERefreshConnection(switchbot);
369
+ }
370
+ })();
371
+ }
372
+ }
373
+
374
+ async registerPlatformBLE(): Promise<void> {
375
+ await this.debugLog('registerPlatformBLE');
333
376
  if (this.config.options?.BLE) {
334
377
  await this.debugLog('is listening to Platform BLE.');
335
378
  this.device.bleMac = this.device.deviceId!.match(/.{1,2}/g)!.join(':').toLowerCase();
@@ -345,26 +388,7 @@ export class Humidifier extends deviceBase {
345
388
  }
346
389
  };
347
390
  } else {
348
- await this.debugLog('is using Device BLE Scanning.');
349
- const switchbot = await this.switchbotBLE();
350
- if (switchbot === undefined) {
351
- await this.BLERefreshConnection(switchbot);
352
- } else {
353
- // Start to monitor advertisement packets
354
- (async () => {
355
- // Start to monitor advertisement packets
356
- const serviceData = await this.monitorAdvertisementPackets(switchbot) as humidifierServiceData;
357
- // Update HomeKit
358
- if (serviceData.model === SwitchBotBLEModel.Humidifier && serviceData.modelName === SwitchBotBLEModelName.Humidifier) {
359
- this.serviceData = serviceData;
360
- await this.BLEparseStatus();
361
- await this.updateHomeKitCharacteristics();
362
- } else {
363
- await this.errorLog(`failed to get serviceData, serviceData: ${serviceData}`);
364
- await this.BLERefreshConnection(switchbot);
365
- }
366
- })();
367
- }
391
+ await this.debugLog('is not listening to Platform BLE');
368
392
  }
369
393
  }
370
394
 
@@ -6,6 +6,10 @@ import { Units } from 'homebridge';
6
6
  import { deviceBase } from './device.js';
7
7
  import { Subject, interval, skipWhile } from 'rxjs';
8
8
  import { convertUnits, validHumidity } from '../utils.js';
9
+ /*
10
+ * For Testing Locally:
11
+ * import { SwitchBotBLEModel, SwitchBotBLEModelName } from '/Users/Shared/GitHub/OpenWonderLabs/node-switchbot/dist/index.js';
12
+ */
9
13
  import { SwitchBotBLEModel, SwitchBotBLEModelName } from 'node-switchbot';
10
14
 
11
15
  import type { devicesConfig } from '../settings.js';
@@ -155,12 +159,28 @@ export class IOSensor extends deviceBase {
155
159
  }
156
160
 
157
161
  // Retrieve initial values and updateHomekit
158
- this.debugLog('Retrieve initial values and update Homekit');
159
- this.refreshStatus();
162
+ try {
163
+ this.debugLog('Retrieve initial values and update Homekit');
164
+ this.refreshStatus();
165
+ } catch (e: any) {
166
+ this.errorLog(`failed to retrieve initial values and update Homekit, Error: ${e}`);
167
+ }
160
168
 
161
- //regisiter webhook event handler
162
- this.debugLog('Registering Webhook Event Handler');
163
- this.registerWebhook();
169
+ //regisiter webhook event handler if enabled
170
+ try {
171
+ this.debugLog('Registering Webhook Event Handler');
172
+ this.registerWebhook();
173
+ } catch (e: any) {
174
+ this.errorLog(`failed to registerWebhook, Error: ${e}`);
175
+ }
176
+
177
+ //regisiter platform BLE event handler if enabled
178
+ try {
179
+ this.debugLog('Registering Platform BLE Event Handler');
180
+ this.registerPlatformBLE();
181
+ } catch (e: any) {
182
+ this.errorLog(`failed to registerPlatformBLE, Error: ${e}`);
183
+ }
164
184
 
165
185
  // Start an update interval
166
186
  interval(this.deviceRefreshRate * 1000)
@@ -271,7 +291,7 @@ export class IOSensor extends deviceBase {
271
291
  async refreshStatus(): Promise<void> {
272
292
  if (!this.device.enableCloudService && this.OpenAPI) {
273
293
  await this.errorLog(`refreshStatus enableCloudService: ${this.device.enableCloudService}`);
274
- } else if (this.BLE || this.config.options?.BLE) {
294
+ } else if (this.BLE) {
275
295
  await this.BLERefreshStatus();
276
296
  } else if (this.OpenAPI && this.platform.config.credentials?.token) {
277
297
  await this.openAPIRefreshStatus();
@@ -283,6 +303,29 @@ export class IOSensor extends deviceBase {
283
303
 
284
304
  async BLERefreshStatus(): Promise<void> {
285
305
  await this.debugLog('BLERefreshStatus');
306
+ const switchbot = await this.switchbotBLE();
307
+ if (switchbot === undefined) {
308
+ await this.BLERefreshConnection(switchbot);
309
+ } else {
310
+ // Start to monitor advertisement packets
311
+ (async () => {
312
+ // Start to monitor advertisement packets
313
+ const serviceData = await this.monitorAdvertisementPackets(switchbot) as outdoorMeterServiceData;
314
+ // Update HomeKit
315
+ if (serviceData.model === SwitchBotBLEModel.OutdoorMeter && serviceData.modelName === SwitchBotBLEModelName.OutdoorMeter) {
316
+ this.serviceData = serviceData;
317
+ await this.BLEparseStatus();
318
+ await this.updateHomeKitCharacteristics();
319
+ } else {
320
+ await this.errorLog(`failed to get serviceData, serviceData: ${serviceData}`);
321
+ await this.BLERefreshConnection(switchbot);
322
+ }
323
+ })();
324
+ }
325
+ }
326
+
327
+ async registerPlatformBLE(): Promise<void> {
328
+ await this.debugLog('registerPlatformBLE');
286
329
  if (this.config.options?.BLE) {
287
330
  await this.debugLog('is listening to Platform BLE.');
288
331
  this.device.bleMac = this.device.deviceId!.match(/.{1,2}/g)!.join(':').toLowerCase();
@@ -298,26 +341,7 @@ export class IOSensor extends deviceBase {
298
341
  }
299
342
  };
300
343
  } else {
301
- await this.debugLog('is using Device BLE Scanning.');
302
- const switchbot = await this.switchbotBLE();
303
- if (switchbot === undefined) {
304
- await this.BLERefreshConnection(switchbot);
305
- } else {
306
- // Start to monitor advertisement packets
307
- (async () => {
308
- // Start to monitor advertisement packets
309
- const serviceData = await this.monitorAdvertisementPackets(switchbot) as outdoorMeterServiceData;
310
- // Update HomeKit
311
- if (serviceData.model === SwitchBotBLEModel.OutdoorMeter && serviceData.modelName === SwitchBotBLEModelName.OutdoorMeter) {
312
- this.serviceData = serviceData;
313
- await this.BLEparseStatus();
314
- await this.updateHomeKitCharacteristics();
315
- } else {
316
- await this.errorLog(`failed to get serviceData, serviceData: ${serviceData}`);
317
- await this.BLERefreshConnection(switchbot);
318
- }
319
- })();
320
- }
344
+ await this.debugLog('is not listening to Platform BLE');
321
345
  }
322
346
  }
323
347