@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,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
 
@@ -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';
@@ -148,12 +152,28 @@ export class Meter extends deviceBase {
148
152
  }
149
153
 
150
154
  // Retrieve initial values and updateHomekit
151
- this.debugLog('Retrieve initial values and update Homekit');
152
- this.refreshStatus();
155
+ try {
156
+ this.debugLog('Retrieve initial values and update Homekit');
157
+ this.refreshStatus();
158
+ } catch (e: any) {
159
+ this.errorLog(`failed to retrieve initial values and update Homekit, Error: ${e}`);
160
+ }
153
161
 
154
- //regisiter webhook event handler
155
- this.debugLog('Registering Webhook Event Handler');
156
- this.registerWebhook();
162
+ //regisiter webhook event handler if enabled
163
+ try {
164
+ this.debugLog('Registering Webhook Event Handler');
165
+ this.registerWebhook();
166
+ } catch (e: any) {
167
+ this.errorLog(`failed to registerWebhook, Error: ${e}`);
168
+ }
169
+
170
+ //regisiter platform BLE event handler if enabled
171
+ try {
172
+ this.debugLog('Registering Platform BLE Event Handler');
173
+ this.registerPlatformBLE();
174
+ } catch (e: any) {
175
+ this.errorLog(`failed to registerPlatformBLE, Error: ${e}`);
176
+ }
157
177
 
158
178
  // Start an update interval
159
179
  interval(this.deviceRefreshRate * 1000)
@@ -265,7 +285,7 @@ export class Meter extends deviceBase {
265
285
  async refreshStatus(): Promise<void> {
266
286
  if (!this.device.enableCloudService && this.OpenAPI) {
267
287
  await this.errorLog(`refreshStatus enableCloudService: ${this.device.enableCloudService}`);
268
- } else if (this.BLE || this.config.options?.BLE) {
288
+ } else if (this.BLE) {
269
289
  await this.BLERefreshStatus();
270
290
  } else if (this.OpenAPI && this.platform.config.credentials?.token) {
271
291
  await this.openAPIRefreshStatus();
@@ -277,6 +297,29 @@ export class Meter extends deviceBase {
277
297
 
278
298
  async BLERefreshStatus(): Promise<void> {
279
299
  await this.debugLog('BLERefreshStatus');
300
+ const switchbot = await this.switchbotBLE();
301
+ if (switchbot === undefined) {
302
+ await this.BLERefreshConnection(switchbot);
303
+ } else {
304
+ // Start to monitor advertisement packets
305
+ (async () => {
306
+ // Start to monitor advertisement packets
307
+ const serviceData = await this.monitorAdvertisementPackets(switchbot) as meterServiceData;
308
+ // Update HomeKit
309
+ if (serviceData.model === SwitchBotBLEModel.Meter && serviceData.modelName === SwitchBotBLEModelName.Meter) {
310
+ this.serviceData = serviceData;
311
+ await this.BLEparseStatus();
312
+ await this.updateHomeKitCharacteristics();
313
+ } else {
314
+ await this.errorLog(`failed to get serviceData, serviceData: ${serviceData}`);
315
+ await this.BLERefreshConnection(switchbot);
316
+ }
317
+ })();
318
+ }
319
+ }
320
+
321
+ async registerPlatformBLE(): Promise<void> {
322
+ await this.debugLog('registerPlatformBLE');
280
323
  if (this.config.options?.BLE) {
281
324
  await this.debugLog('is listening to Platform BLE.');
282
325
  this.device.bleMac = this.device.deviceId!.match(/.{1,2}/g)!.join(':').toLowerCase();
@@ -292,26 +335,7 @@ export class Meter extends deviceBase {
292
335
  }
293
336
  };
294
337
  } else {
295
- await this.debugLog('is using Device BLE Scanning.');
296
- const switchbot = await this.switchbotBLE();
297
- if (switchbot === undefined) {
298
- await this.BLERefreshConnection(switchbot);
299
- } else {
300
- // Start to monitor advertisement packets
301
- (async () => {
302
- // Start to monitor advertisement packets
303
- const serviceData = await this.monitorAdvertisementPackets(switchbot) as meterServiceData;
304
- // Update HomeKit
305
- if (serviceData.model === SwitchBotBLEModel.Meter && serviceData.modelName === SwitchBotBLEModelName.Meter) {
306
- this.serviceData = serviceData;
307
- await this.BLEparseStatus();
308
- await this.updateHomeKitCharacteristics();
309
- } else {
310
- await this.errorLog(`failed to get serviceData, serviceData: ${serviceData}`);
311
- await this.BLERefreshConnection(switchbot);
312
- }
313
- })();
314
- }
338
+ await this.debugLog('is not listening to Platform BLE');
315
339
  }
316
340
  }
317
341
 
@@ -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';
@@ -153,12 +157,28 @@ export class MeterPlus extends deviceBase {
153
157
  }
154
158
 
155
159
  // Retrieve initial values and updateHomekit
156
- this.debugLog('Retrieve initial values and update Homekit');
157
- this.refreshStatus();
160
+ try {
161
+ this.debugLog('Retrieve initial values and update Homekit');
162
+ this.refreshStatus();
163
+ } catch (e: any) {
164
+ this.errorLog(`failed to retrieve initial values and update Homekit, Error: ${e}`);
165
+ }
158
166
 
159
- //regisiter webhook event handler
160
- this.debugLog('Registering Webhook Event Handler');
161
- this.registerWebhook();
167
+ //regisiter webhook event handler if enabled
168
+ try {
169
+ this.debugLog('Registering Webhook Event Handler');
170
+ this.registerWebhook();
171
+ } catch (e: any) {
172
+ this.errorLog(`failed to registerWebhook, Error: ${e}`);
173
+ }
174
+
175
+ //regisiter platform BLE event handler if enabled
176
+ try {
177
+ this.debugLog('Registering Platform BLE Event Handler');
178
+ this.registerPlatformBLE();
179
+ } catch (e: any) {
180
+ this.errorLog(`failed to registerPlatformBLE, Error: ${e}`);
181
+ }
162
182
 
163
183
  // Start an update interval
164
184
  interval(this.deviceRefreshRate * 1000)
@@ -266,7 +286,7 @@ export class MeterPlus extends deviceBase {
266
286
  async refreshStatus(): Promise<void> {
267
287
  if (!this.device.enableCloudService && this.OpenAPI) {
268
288
  await this.errorLog(`refreshStatus enableCloudService: ${this.device.enableCloudService}`);
269
- } else if (this.BLE || this.config.options?.BLE) {
289
+ } else if (this.BLE) {
270
290
  await this.BLERefreshStatus();
271
291
  } else if (this.OpenAPI && this.platform.config.credentials?.token) {
272
292
  await this.openAPIRefreshStatus();
@@ -278,6 +298,29 @@ export class MeterPlus extends deviceBase {
278
298
 
279
299
  async BLERefreshStatus(): Promise<void> {
280
300
  await this.debugLog('BLERefreshStatus');
301
+ const switchbot = await this.switchbotBLE();
302
+ if (switchbot === undefined) {
303
+ await this.BLERefreshConnection(switchbot);
304
+ } else {
305
+ // Start to monitor advertisement packets
306
+ (async () => {
307
+ // Start to monitor advertisement packets
308
+ const serviceData = await this.monitorAdvertisementPackets(switchbot) as meterPlusServiceData;
309
+ // Update HomeKit
310
+ if (serviceData.model === SwitchBotBLEModel.MeterPlus && serviceData.modelName === SwitchBotBLEModelName.MeterPlus) {
311
+ this.serviceData = serviceData;
312
+ await this.BLEparseStatus();
313
+ await this.updateHomeKitCharacteristics();
314
+ } else {
315
+ await this.errorLog(`failed to get serviceData, serviceData: ${serviceData}`);
316
+ await this.BLERefreshConnection(switchbot);
317
+ }
318
+ })();
319
+ }
320
+ }
321
+
322
+ async registerPlatformBLE(): Promise<void> {
323
+ await this.debugLog('registerPlatformBLE');
281
324
  if (this.config.options?.BLE) {
282
325
  await this.debugLog('is listening to Platform BLE.');
283
326
  this.device.bleMac = this.device.deviceId!.match(/.{1,2}/g)!.join(':').toLowerCase();
@@ -293,26 +336,7 @@ export class MeterPlus extends deviceBase {
293
336
  }
294
337
  };
295
338
  } else {
296
- await this.debugLog('is using Device BLE Scanning.');
297
- const switchbot = await this.switchbotBLE();
298
- if (switchbot === undefined) {
299
- await this.BLERefreshConnection(switchbot);
300
- } else {
301
- // Start to monitor advertisement packets
302
- (async () => {
303
- // Start to monitor advertisement packets
304
- const serviceData = await this.monitorAdvertisementPackets(switchbot) as meterPlusServiceData;
305
- // Update HomeKit
306
- if (serviceData.model === SwitchBotBLEModel.MeterPlus && serviceData.modelName === SwitchBotBLEModelName.MeterPlus) {
307
- this.serviceData = serviceData;
308
- await this.BLEparseStatus();
309
- await this.updateHomeKitCharacteristics();
310
- } else {
311
- await this.errorLog(`failed to get serviceData, serviceData: ${serviceData}`);
312
- await this.BLERefreshConnection(switchbot);
313
- }
314
- })();
315
- }
339
+ await this.debugLog('is not listening to Platform BLE');
316
340
  }
317
341
  }
318
342
 
@@ -5,6 +5,10 @@
5
5
  */
6
6
  import { deviceBase } from './device.js';
7
7
  import { Subject, interval, skipWhile } from 'rxjs';
8
+ /*
9
+ * For Testing Locally:
10
+ * import { SwitchBotBLEModel, SwitchBotBLEModelName } from '/Users/Shared/GitHub/OpenWonderLabs/node-switchbot/dist/index.js';
11
+ */
8
12
  import { SwitchBotBLEModel, SwitchBotBLEModelName } from 'node-switchbot';
9
13
 
10
14
  import type { devicesConfig } from '../settings.js';
@@ -138,12 +142,28 @@ export class Motion extends deviceBase {
138
142
  };
139
143
 
140
144
  // Retrieve initial values and updateHomekit
141
- this.debugLog('Retrieve initial values and update Homekit');
142
- this.refreshStatus();
145
+ try {
146
+ this.debugLog('Retrieve initial values and update Homekit');
147
+ this.refreshStatus();
148
+ } catch (e: any) {
149
+ this.errorLog(`failed to retrieve initial values and update Homekit, Error: ${e}`);
150
+ }
143
151
 
144
- //regisiter webhook event handler
145
- this.debugLog('Registering Webhook Event Handler');
146
- this.registerWebhook();
152
+ //regisiter webhook event handler if enabled
153
+ try {
154
+ this.debugLog('Registering Webhook Event Handler');
155
+ this.registerWebhook();
156
+ } catch (e: any) {
157
+ this.errorLog(`failed to registerWebhook, Error: ${e}`);
158
+ }
159
+
160
+ //regisiter platform BLE event handler if enabled
161
+ try {
162
+ this.debugLog('Registering Platform BLE Event Handler');
163
+ this.registerPlatformBLE();
164
+ } catch (e: any) {
165
+ this.errorLog(`failed to registerPlatformBLE, Error: ${e}`);
166
+ }
147
167
 
148
168
  // Start an update interval
149
169
  interval(this.deviceRefreshRate * 1000)
@@ -236,7 +256,7 @@ export class Motion extends deviceBase {
236
256
  async refreshStatus(): Promise<void> {
237
257
  if (!this.device.enableCloudService && this.OpenAPI) {
238
258
  await this.errorLog(`refreshStatus enableCloudService: ${this.device.enableCloudService}`);
239
- } else if (this.BLE || this.config.options?.BLE) {
259
+ } else if (this.BLE) {
240
260
  await this.BLERefreshStatus();
241
261
  } else if (this.OpenAPI && this.platform.config.credentials?.token) {
242
262
  await this.openAPIRefreshStatus();
@@ -248,6 +268,29 @@ export class Motion extends deviceBase {
248
268
 
249
269
  async BLERefreshStatus(): Promise<void> {
250
270
  await this.debugLog('BLERefreshStatus');
271
+ const switchbot = await this.switchbotBLE();
272
+ if (switchbot === undefined) {
273
+ await this.BLERefreshConnection(switchbot);
274
+ } else {
275
+ // Start to monitor advertisement packets
276
+ (async () => {
277
+ // Start to monitor advertisement packets
278
+ const serviceData = await this.monitorAdvertisementPackets(switchbot) as motionSensorServiceData;
279
+ // Update HomeKit
280
+ if (serviceData.model === SwitchBotBLEModel.MotionSensor && serviceData.modelName === SwitchBotBLEModelName.MotionSensor) {
281
+ this.serviceData = serviceData;
282
+ await this.BLEparseStatus();
283
+ await this.updateHomeKitCharacteristics();
284
+ } else {
285
+ await this.errorLog(`failed to get serviceData, serviceData: ${serviceData}`);
286
+ await this.BLERefreshConnection(switchbot);
287
+ }
288
+ })();
289
+ }
290
+ }
291
+
292
+ async registerPlatformBLE(): Promise<void> {
293
+ await this.debugLog('registerPlatformBLE');
251
294
  if (this.config.options?.BLE) {
252
295
  await this.debugLog('is listening to Platform BLE.');
253
296
  this.device.bleMac = this.device.deviceId!.match(/.{1,2}/g)!.join(':').toLowerCase();
@@ -263,26 +306,7 @@ export class Motion extends deviceBase {
263
306
  }
264
307
  };
265
308
  } else {
266
- await this.debugLog('is using Device BLE Scanning.');
267
- const switchbot = await this.switchbotBLE();
268
- if (switchbot === undefined) {
269
- await this.BLERefreshConnection(switchbot);
270
- } else {
271
- // Start to monitor advertisement packets
272
- (async () => {
273
- // Start to monitor advertisement packets
274
- const serviceData = await this.monitorAdvertisementPackets(switchbot) as motionSensorServiceData;
275
- // Update HomeKit
276
- if (serviceData.model === SwitchBotBLEModel.MotionSensor && serviceData.modelName === SwitchBotBLEModelName.MotionSensor) {
277
- this.serviceData = serviceData;
278
- await this.BLEparseStatus();
279
- await this.updateHomeKitCharacteristics();
280
- } else {
281
- await this.errorLog(`failed to get serviceData, serviceData: ${serviceData}`);
282
- await this.BLERefreshConnection(switchbot);
283
- }
284
- })();
285
- }
309
+ await this.debugLog('is not listening to Platform BLE');
286
310
  }
287
311
  }
288
312
 
@@ -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
 
@@ -66,12 +70,28 @@ export class Plug extends deviceBase {
66
70
  .onSet(this.OnSet.bind(this));
67
71
 
68
72
  // Retrieve initial values and updateHomekit
69
- this.debugLog('Retrieve initial values and update Homekit');
70
- this.refreshStatus();
73
+ try {
74
+ this.debugLog('Retrieve initial values and update Homekit');
75
+ this.refreshStatus();
76
+ } catch (e: any) {
77
+ this.errorLog(`failed to retrieve initial values and update Homekit, Error: ${e}`);
78
+ }
71
79
 
72
- //regisiter webhook event handler
73
- this.debugLog('Registering Webhook Event Handler');
74
- this.registerWebhook();
80
+ //regisiter webhook event handler if enabled
81
+ try {
82
+ this.debugLog('Registering Webhook Event Handler');
83
+ this.registerWebhook();
84
+ } catch (e: any) {
85
+ this.errorLog(`failed to registerWebhook, Error: ${e}`);
86
+ }
87
+
88
+ //regisiter platform BLE event handler if enabled
89
+ try {
90
+ this.debugLog('Registering Platform BLE Event Handler');
91
+ this.registerPlatformBLE();
92
+ } catch (e: any) {
93
+ this.errorLog(`failed to registerPlatformBLE, Error: ${e}`);
94
+ }
75
95
 
76
96
  // Start an update interval
77
97
  interval(this.deviceRefreshRate * 1000)
@@ -148,7 +168,7 @@ export class Plug extends deviceBase {
148
168
  async refreshStatus(): Promise<void> {
149
169
  if (!this.device.enableCloudService && this.OpenAPI) {
150
170
  await this.errorLog(`refreshStatus enableCloudService: ${this.device.enableCloudService}`);
151
- } else if (this.BLE || this.config.options?.BLE) {
171
+ } else if (this.BLE) {
152
172
  await this.BLERefreshStatus();
153
173
  } else if (this.OpenAPI && this.platform.config.credentials?.token) {
154
174
  await this.openAPIRefreshStatus();
@@ -160,6 +180,30 @@ export class Plug extends deviceBase {
160
180
 
161
181
  async BLERefreshStatus(): Promise<void> {
162
182
  await this.debugLog('BLERefreshStatus');
183
+ const switchbot = await this.switchbotBLE();
184
+ if (switchbot === undefined) {
185
+ await this.BLERefreshConnection(switchbot);
186
+ } else {
187
+ // Start to monitor advertisement packets
188
+ (async () => {
189
+ // Start to monitor advertisement packets
190
+ const serviceData = await this.monitorAdvertisementPackets(switchbot) as plugMiniUSServiceData | plugMiniJPServiceData;
191
+ // Update HomeKit
192
+ if ((serviceData.model === SwitchBotBLEModel.PlugMiniUS || SwitchBotBLEModel.PlugMiniJP)
193
+ && serviceData.modelName === (SwitchBotBLEModelName.PlugMini || SwitchBotBLEModelName.PlugMini)) {
194
+ this.serviceData = serviceData;
195
+ await this.BLEparseStatus();
196
+ await this.updateHomeKitCharacteristics();
197
+ } else {
198
+ await this.errorLog(`failed to get serviceData, serviceData: ${serviceData}`);
199
+ await this.BLERefreshConnection(switchbot);
200
+ }
201
+ })();
202
+ }
203
+ }
204
+
205
+ async registerPlatformBLE(): Promise<void> {
206
+ await this.debugLog('registerPlatformBLE');
163
207
  if (this.config.options?.BLE) {
164
208
  await this.debugLog('is listening to Platform BLE.');
165
209
  this.device.bleMac = this.device.deviceId!.match(/.{1,2}/g)!.join(':').toLowerCase();
@@ -175,27 +219,7 @@ export class Plug extends deviceBase {
175
219
  }
176
220
  };
177
221
  } else {
178
- await this.debugLog('is using Device BLE Scanning.');
179
- const switchbot = await this.switchbotBLE();
180
- if (switchbot === undefined) {
181
- await this.BLERefreshConnection(switchbot);
182
- } else {
183
- // Start to monitor advertisement packets
184
- (async () => {
185
- // Start to monitor advertisement packets
186
- const serviceData = await this.monitorAdvertisementPackets(switchbot) as plugMiniUSServiceData | plugMiniJPServiceData;
187
- // Update HomeKit
188
- if ((serviceData.model === SwitchBotBLEModel.PlugMiniUS || SwitchBotBLEModel.PlugMiniJP)
189
- && serviceData.modelName === (SwitchBotBLEModelName.PlugMini || SwitchBotBLEModelName.PlugMini)) {
190
- this.serviceData = serviceData;
191
- await this.BLEparseStatus();
192
- await this.updateHomeKitCharacteristics();
193
- } else {
194
- await this.errorLog(`failed to get serviceData, serviceData: ${serviceData}`);
195
- await this.BLERefreshConnection(switchbot);
196
- }
197
- })();
198
- }
222
+ await this.debugLog('is not listening to Platform BLE');
199
223
  }
200
224
  }
201
225