@switchbot/homebridge-switchbot 3.7.1-beta.0 → 3.7.1-beta.10

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 (110) hide show
  1. package/CHANGELOG.md +8 -0
  2. package/config.schema.json +7 -1
  3. package/dist/device/blindtilt.d.ts +3 -1
  4. package/dist/device/blindtilt.d.ts.map +1 -1
  5. package/dist/device/blindtilt.js +61 -28
  6. package/dist/device/blindtilt.js.map +1 -1
  7. package/dist/device/bot.d.ts +1 -0
  8. package/dist/device/bot.d.ts.map +1 -1
  9. package/dist/device/bot.js +102 -80
  10. package/dist/device/bot.js.map +1 -1
  11. package/dist/device/ceilinglight.d.ts +1 -0
  12. package/dist/device/ceilinglight.d.ts.map +1 -1
  13. package/dist/device/ceilinglight.js +53 -28
  14. package/dist/device/ceilinglight.js.map +1 -1
  15. package/dist/device/colorbulb.d.ts +1 -0
  16. package/dist/device/colorbulb.d.ts.map +1 -1
  17. package/dist/device/colorbulb.js +53 -28
  18. package/dist/device/colorbulb.js.map +1 -1
  19. package/dist/device/contact.d.ts +1 -0
  20. package/dist/device/contact.d.ts.map +1 -1
  21. package/dist/device/contact.js +53 -28
  22. package/dist/device/contact.js.map +1 -1
  23. package/dist/device/curtain.d.ts +2 -0
  24. package/dist/device/curtain.d.ts.map +1 -1
  25. package/dist/device/curtain.js +74 -43
  26. package/dist/device/curtain.js.map +1 -1
  27. package/dist/device/device.d.ts.map +1 -1
  28. package/dist/device/device.js +247 -394
  29. package/dist/device/device.js.map +1 -1
  30. package/dist/device/fan.d.ts +1 -0
  31. package/dist/device/fan.d.ts.map +1 -1
  32. package/dist/device/fan.js +53 -28
  33. package/dist/device/fan.js.map +1 -1
  34. package/dist/device/hub.d.ts +1 -0
  35. package/dist/device/hub.d.ts.map +1 -1
  36. package/dist/device/hub.js +53 -28
  37. package/dist/device/hub.js.map +1 -1
  38. package/dist/device/humidifier.d.ts +1 -0
  39. package/dist/device/humidifier.d.ts.map +1 -1
  40. package/dist/device/humidifier.js +53 -28
  41. package/dist/device/humidifier.js.map +1 -1
  42. package/dist/device/iosensor.d.ts +1 -0
  43. package/dist/device/iosensor.d.ts.map +1 -1
  44. package/dist/device/iosensor.js +53 -28
  45. package/dist/device/iosensor.js.map +1 -1
  46. package/dist/device/lightstrip.d.ts +1 -0
  47. package/dist/device/lightstrip.d.ts.map +1 -1
  48. package/dist/device/lightstrip.js +53 -28
  49. package/dist/device/lightstrip.js.map +1 -1
  50. package/dist/device/lock.d.ts +1 -0
  51. package/dist/device/lock.d.ts.map +1 -1
  52. package/dist/device/lock.js +54 -29
  53. package/dist/device/lock.js.map +1 -1
  54. package/dist/device/meter.d.ts +1 -0
  55. package/dist/device/meter.d.ts.map +1 -1
  56. package/dist/device/meter.js +53 -28
  57. package/dist/device/meter.js.map +1 -1
  58. package/dist/device/meterplus.d.ts +1 -0
  59. package/dist/device/meterplus.d.ts.map +1 -1
  60. package/dist/device/meterplus.js +53 -28
  61. package/dist/device/meterplus.js.map +1 -1
  62. package/dist/device/motion.d.ts +1 -0
  63. package/dist/device/motion.d.ts.map +1 -1
  64. package/dist/device/motion.js +53 -28
  65. package/dist/device/motion.js.map +1 -1
  66. package/dist/device/plug.d.ts +1 -0
  67. package/dist/device/plug.d.ts.map +1 -1
  68. package/dist/device/plug.js +54 -29
  69. package/dist/device/plug.js.map +1 -1
  70. package/dist/device/robotvacuumcleaner.d.ts +1 -0
  71. package/dist/device/robotvacuumcleaner.d.ts.map +1 -1
  72. package/dist/device/robotvacuumcleaner.js +53 -28
  73. package/dist/device/robotvacuumcleaner.js.map +1 -1
  74. package/dist/device/waterdetector.d.ts +1 -0
  75. package/dist/device/waterdetector.d.ts.map +1 -1
  76. package/dist/device/waterdetector.js +53 -28
  77. package/dist/device/waterdetector.js.map +1 -1
  78. package/dist/irdevice/irdevice.d.ts.map +1 -1
  79. package/dist/irdevice/irdevice.js +30 -133
  80. package/dist/irdevice/irdevice.js.map +1 -1
  81. package/dist/platform.d.ts +19 -18
  82. package/dist/platform.d.ts.map +1 -1
  83. package/dist/platform.js +369 -524
  84. package/dist/platform.js.map +1 -1
  85. package/dist/settings.d.ts +1 -0
  86. package/dist/settings.d.ts.map +1 -1
  87. package/dist/settings.js.map +1 -1
  88. package/package.json +13 -13
  89. package/src/device/blindtilt.ts +59 -27
  90. package/src/device/bot.ts +98 -77
  91. package/src/device/ceilinglight.ts +51 -27
  92. package/src/device/colorbulb.ts +50 -26
  93. package/src/device/contact.ts +50 -26
  94. package/src/device/curtain.ts +71 -40
  95. package/src/device/device.ts +283 -395
  96. package/src/device/fan.ts +50 -26
  97. package/src/device/hub.ts +50 -26
  98. package/src/device/humidifier.ts +50 -26
  99. package/src/device/iosensor.ts +50 -26
  100. package/src/device/lightstrip.ts +50 -26
  101. package/src/device/lock.ts +51 -27
  102. package/src/device/meter.ts +50 -26
  103. package/src/device/meterplus.ts +50 -26
  104. package/src/device/motion.ts +50 -26
  105. package/src/device/plug.ts +51 -27
  106. package/src/device/robotvacuumcleaner.ts +50 -26
  107. package/src/device/waterdetector.ts +50 -26
  108. package/src/irdevice/irdevice.ts +54 -134
  109. package/src/platform.ts +379 -529
  110. package/src/settings.ts +1 -0
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
 
@@ -4,6 +4,10 @@
4
4
  */
5
5
  import { deviceBase } from './device.js';
6
6
  import { hs2rgb, rgb2hs, m2hs } 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
 
@@ -161,12 +165,28 @@ export class StripLight extends deviceBase {
161
165
  .onSet(this.SaturationSet.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
+ }
174
+
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
+ }
166
182
 
167
- //regisiter webhook event handler
168
- this.debugLog('Registering Webhook Event Handler');
169
- this.registerWebhook();
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)
@@ -304,7 +324,7 @@ export class StripLight extends deviceBase {
304
324
  async refreshStatus(): Promise<void> {
305
325
  if (!this.device.enableCloudService && this.OpenAPI) {
306
326
  await this.errorLog(`refreshStatus enableCloudService: ${this.device.enableCloudService}`);
307
- } else if (this.BLE || this.config.options?.BLE) {
327
+ } else if (this.BLE) {
308
328
  await this.BLERefreshStatus();
309
329
  } else if (this.OpenAPI && this.platform.config.credentials?.token) {
310
330
  await this.openAPIRefreshStatus();
@@ -316,6 +336,29 @@ export class StripLight extends deviceBase {
316
336
 
317
337
  async BLERefreshStatus(): Promise<void> {
318
338
  await this.debugLog('BLERefreshStatus');
339
+ const switchbot = await this.switchbotBLE();
340
+ if (switchbot === undefined) {
341
+ await this.BLERefreshConnection(switchbot);
342
+ } else {
343
+ // Start to monitor advertisement packets
344
+ (async () => {
345
+ // Start to monitor advertisement packets
346
+ const serviceData = await this.monitorAdvertisementPackets(switchbot) as stripLightServiceData;
347
+ // Update HomeKit
348
+ if (serviceData.model === SwitchBotBLEModel.StripLight && serviceData.modelName === SwitchBotBLEModelName.StripLight) {
349
+ this.serviceData = serviceData;
350
+ await this.BLEparseStatus();
351
+ await this.updateHomeKitCharacteristics();
352
+ } else {
353
+ await this.errorLog(`failed to get serviceData, serviceData: ${serviceData}`);
354
+ await this.BLERefreshConnection(switchbot);
355
+ }
356
+ })();
357
+ }
358
+ }
359
+
360
+ async registerPlatformBLE(): Promise<void> {
361
+ await this.debugLog('registerPlatformBLE');
319
362
  if (this.config.options?.BLE) {
320
363
  await this.debugLog('is listening to Platform BLE.');
321
364
  this.device.bleMac = this.device.deviceId!.match(/.{1,2}/g)!.join(':').toLowerCase();
@@ -331,26 +374,7 @@ export class StripLight extends deviceBase {
331
374
  }
332
375
  };
333
376
  } else {
334
- await this.debugLog('is using Device BLE Scanning.');
335
- const switchbot = await this.switchbotBLE();
336
- if (switchbot === undefined) {
337
- await this.BLERefreshConnection(switchbot);
338
- } else {
339
- // Start to monitor advertisement packets
340
- (async () => {
341
- // Start to monitor advertisement packets
342
- const serviceData = await this.monitorAdvertisementPackets(switchbot) as stripLightServiceData;
343
- // Update HomeKit
344
- if (serviceData.model === SwitchBotBLEModel.StripLight && serviceData.modelName === SwitchBotBLEModelName.StripLight) {
345
- this.serviceData = serviceData;
346
- await this.BLEparseStatus();
347
- await this.updateHomeKitCharacteristics();
348
- } else {
349
- await this.errorLog(`failed to get serviceData, serviceData: ${serviceData}`);
350
- await this.BLERefreshConnection(switchbot);
351
- }
352
- })();
353
- }
377
+ await this.debugLog('is not listening to Platform BLE');
354
378
  }
355
379
  }
356
380
 
@@ -3,6 +3,10 @@
3
3
  * lock.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
 
@@ -165,12 +169,28 @@ export class Lock extends deviceBase {
165
169
  }
166
170
 
167
171
  // Retrieve initial values and updateHomekit
168
- this.debugLog('Retrieve initial values and update Homekit');
169
- this.refreshStatus();
172
+ try {
173
+ this.debugLog('Retrieve initial values and update Homekit');
174
+ this.refreshStatus();
175
+ } catch (e: any) {
176
+ this.errorLog(`failed to retrieve initial values and update Homekit, Error: ${e}`);
177
+ }
170
178
 
171
- //regisiter webhook event handler
172
- this.debugLog('Registering Webhook Event Handler');
173
- this.registerWebhook();
179
+ //regisiter webhook event handler if enabled
180
+ try {
181
+ this.debugLog('Registering Webhook Event Handler');
182
+ this.registerWebhook();
183
+ } catch (e: any) {
184
+ this.errorLog(`failed to registerWebhook, Error: ${e}`);
185
+ }
186
+
187
+ //regisiter platform BLE event handler if enabled
188
+ try {
189
+ this.debugLog('Registering Platform BLE Event Handler');
190
+ this.registerPlatformBLE();
191
+ } catch (e: any) {
192
+ this.errorLog(`failed to registerPlatformBLE, Error: ${e}`);
193
+ }
174
194
 
175
195
  // Start an update interval
176
196
  interval(this.deviceRefreshRate * 1000)
@@ -297,7 +317,7 @@ export class Lock extends deviceBase {
297
317
  async refreshStatus(): Promise<void> {
298
318
  if (!this.device.enableCloudService && this.OpenAPI) {
299
319
  await this.errorLog(`refreshStatus enableCloudService: ${this.device.enableCloudService}`);
300
- } else if (this.BLE || this.config.options?.BLE) {
320
+ } else if (this.BLE) {
301
321
  await this.BLERefreshStatus();
302
322
  } else if (this.OpenAPI && this.platform.config.credentials?.token) {
303
323
  await this.openAPIRefreshStatus();
@@ -309,6 +329,30 @@ export class Lock extends deviceBase {
309
329
 
310
330
  async BLERefreshStatus(): Promise<void> {
311
331
  await this.debugLog('BLERefreshStatus');
332
+ const switchbot = await this.switchbotBLE();
333
+ if (switchbot === undefined) {
334
+ await this.BLERefreshConnection(switchbot);
335
+ } else {
336
+ // Start to monitor advertisement packets
337
+ (async () => {
338
+ // Start to monitor advertisement packets
339
+ const serviceData = await this.monitorAdvertisementPackets(switchbot) as lockServiceData | lockProServiceData;
340
+ // Update HomeKit
341
+ if ((serviceData.model === SwitchBotBLEModel.Lock || SwitchBotBLEModel.LockPro)
342
+ && (serviceData.modelName === SwitchBotBLEModelName.Lock || SwitchBotBLEModelName.LockPro)) {
343
+ this.serviceData = serviceData;
344
+ await this.BLEparseStatus();
345
+ await this.updateHomeKitCharacteristics();
346
+ } else {
347
+ await this.errorLog(`failed to get serviceData, serviceData: ${serviceData}`);
348
+ await this.BLERefreshConnection(switchbot);
349
+ }
350
+ })();
351
+ }
352
+ }
353
+
354
+ async registerPlatformBLE(): Promise<void> {
355
+ await this.debugLog('registerPlatformBLE');
312
356
  if (this.config.options?.BLE) {
313
357
  await this.debugLog('is listening to Platform BLE.');
314
358
  this.device.bleMac = this.device.deviceId!.match(/.{1,2}/g)!.join(':').toLowerCase();
@@ -324,27 +368,7 @@ export class Lock extends deviceBase {
324
368
  }
325
369
  };
326
370
  } else {
327
- await this.debugLog('is using Device BLE Scanning.');
328
- const switchbot = await this.switchbotBLE();
329
- if (switchbot === undefined) {
330
- await this.BLERefreshConnection(switchbot);
331
- } else {
332
- // Start to monitor advertisement packets
333
- (async () => {
334
- // Start to monitor advertisement packets
335
- const serviceData = await this.monitorAdvertisementPackets(switchbot) as lockServiceData | lockProServiceData;
336
- // Update HomeKit
337
- if ((serviceData.model === SwitchBotBLEModel.Lock || SwitchBotBLEModel.LockPro)
338
- && (serviceData.modelName === SwitchBotBLEModelName.Lock || SwitchBotBLEModelName.LockPro)) {
339
- this.serviceData = serviceData;
340
- await this.BLEparseStatus();
341
- await this.updateHomeKitCharacteristics();
342
- } else {
343
- await this.errorLog(`failed to get serviceData, serviceData: ${serviceData}`);
344
- await this.BLERefreshConnection(switchbot);
345
- }
346
- })();
347
- }
371
+ await this.debugLog('is not listening to Platform BLE');
348
372
  }
349
373
  }
350
374