@switchbot/homebridge-switchbot 3.6.1-beta.0 → 3.6.1-beta.2

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 (93) hide show
  1. package/CHANGELOG.md +11 -0
  2. package/config.schema.json +11 -0
  3. package/dist/device/blindtilt.d.ts.map +1 -1
  4. package/dist/device/blindtilt.js +36 -18
  5. package/dist/device/blindtilt.js.map +1 -1
  6. package/dist/device/bot.d.ts +1 -2
  7. package/dist/device/bot.d.ts.map +1 -1
  8. package/dist/device/bot.js +186 -396
  9. package/dist/device/bot.js.map +1 -1
  10. package/dist/device/ceilinglight.d.ts.map +1 -1
  11. package/dist/device/ceilinglight.js +36 -19
  12. package/dist/device/ceilinglight.js.map +1 -1
  13. package/dist/device/colorbulb.d.ts.map +1 -1
  14. package/dist/device/colorbulb.js +36 -19
  15. package/dist/device/colorbulb.js.map +1 -1
  16. package/dist/device/contact.d.ts.map +1 -1
  17. package/dist/device/contact.js +36 -19
  18. package/dist/device/contact.js.map +1 -1
  19. package/dist/device/curtain.d.ts.map +1 -1
  20. package/dist/device/curtain.js +37 -18
  21. package/dist/device/curtain.js.map +1 -1
  22. package/dist/device/device.js +2 -2
  23. package/dist/device/device.js.map +1 -1
  24. package/dist/device/fan.d.ts.map +1 -1
  25. package/dist/device/fan.js +36 -19
  26. package/dist/device/fan.js.map +1 -1
  27. package/dist/device/hub.d.ts.map +1 -1
  28. package/dist/device/hub.js +36 -17
  29. package/dist/device/hub.js.map +1 -1
  30. package/dist/device/humidifier.d.ts.map +1 -1
  31. package/dist/device/humidifier.js +36 -17
  32. package/dist/device/humidifier.js.map +1 -1
  33. package/dist/device/iosensor.d.ts.map +1 -1
  34. package/dist/device/iosensor.js +36 -17
  35. package/dist/device/iosensor.js.map +1 -1
  36. package/dist/device/lightstrip.d.ts.map +1 -1
  37. package/dist/device/lightstrip.js +36 -17
  38. package/dist/device/lightstrip.js.map +1 -1
  39. package/dist/device/lock.d.ts.map +1 -1
  40. package/dist/device/lock.js +37 -18
  41. package/dist/device/lock.js.map +1 -1
  42. package/dist/device/meter.d.ts.map +1 -1
  43. package/dist/device/meter.js +36 -17
  44. package/dist/device/meter.js.map +1 -1
  45. package/dist/device/meterplus.d.ts.map +1 -1
  46. package/dist/device/meterplus.js +36 -17
  47. package/dist/device/meterplus.js.map +1 -1
  48. package/dist/device/motion.d.ts.map +1 -1
  49. package/dist/device/motion.js +36 -17
  50. package/dist/device/motion.js.map +1 -1
  51. package/dist/device/plug.d.ts.map +1 -1
  52. package/dist/device/plug.js +37 -18
  53. package/dist/device/plug.js.map +1 -1
  54. package/dist/device/robotvacuumcleaner.d.ts.map +1 -1
  55. package/dist/device/robotvacuumcleaner.js +36 -17
  56. package/dist/device/robotvacuumcleaner.js.map +1 -1
  57. package/dist/device/waterdetector.d.ts.map +1 -1
  58. package/dist/device/waterdetector.js +36 -17
  59. package/dist/device/waterdetector.js.map +1 -1
  60. package/dist/irdevice/other.d.ts +1 -0
  61. package/dist/irdevice/other.d.ts.map +1 -1
  62. package/dist/irdevice/other.js +186 -228
  63. package/dist/irdevice/other.js.map +1 -1
  64. package/dist/platform.d.ts +4 -0
  65. package/dist/platform.d.ts.map +1 -1
  66. package/dist/platform.js +32 -0
  67. package/dist/platform.js.map +1 -1
  68. package/dist/settings.d.ts +1 -0
  69. package/dist/settings.d.ts.map +1 -1
  70. package/dist/settings.js.map +1 -1
  71. package/package.json +3 -3
  72. package/src/device/blindtilt.ts +33 -18
  73. package/src/device/bot.ts +179 -406
  74. package/src/device/ceilinglight.ts +33 -20
  75. package/src/device/colorbulb.ts +33 -20
  76. package/src/device/contact.ts +33 -20
  77. package/src/device/curtain.ts +34 -18
  78. package/src/device/device.ts +2 -2
  79. package/src/device/fan.ts +33 -20
  80. package/src/device/hub.ts +33 -17
  81. package/src/device/humidifier.ts +33 -17
  82. package/src/device/iosensor.ts +33 -17
  83. package/src/device/lightstrip.ts +33 -17
  84. package/src/device/lock.ts +34 -18
  85. package/src/device/meter.ts +33 -17
  86. package/src/device/meterplus.ts +33 -17
  87. package/src/device/motion.ts +33 -17
  88. package/src/device/plug.ts +34 -18
  89. package/src/device/robotvacuumcleaner.ts +33 -17
  90. package/src/device/waterdetector.ts +33 -17
  91. package/src/irdevice/other.ts +214 -283
  92. package/src/platform.ts +30 -0
  93. package/src/settings.ts +1 -0
@@ -166,9 +166,6 @@ export class CeilingLight extends deviceBase {
166
166
  this.debugLog('Registering Webhook Event Handler');
167
167
  this.registerWebhook();
168
168
 
169
- //regisiter webhook event handler
170
- this.registerWebhook();
171
-
172
169
  // Start an update interval
173
170
  interval(this.deviceRefreshRate * 1000)
174
171
  .pipe(skipWhile(() => this.ceilingLightUpdateInProgress))
@@ -276,7 +273,7 @@ export class CeilingLight extends deviceBase {
276
273
  async refreshStatus(): Promise<void> {
277
274
  if (!this.device.enableCloudService && this.OpenAPI) {
278
275
  await this.errorLog(`refreshStatus enableCloudService: ${this.device.enableCloudService}`);
279
- } else if (this.BLE) {
276
+ } else if (this.BLE || this.config.options?.BLE) {
280
277
  await this.BLERefreshStatus();
281
278
  } else if (this.OpenAPI && this.platform.config.credentials?.token) {
282
279
  await this.openAPIRefreshStatus();
@@ -288,25 +285,41 @@ export class CeilingLight extends deviceBase {
288
285
 
289
286
  async BLERefreshStatus(): Promise<void> {
290
287
  await this.debugLog('BLERefreshStatus');
291
- const switchbot = await this.switchbotBLE();
292
-
293
- if (switchbot === undefined) {
294
- await this.BLERefreshConnection(switchbot);
295
- } else {
296
- (async () => {
297
- // Start to monitor advertisement packets
298
- const serviceData = await this.monitorAdvertisementPackets(switchbot) as unknown as ceilingLightServiceData;
299
- // Update HomeKit
300
- if ((serviceData.model === SwitchBotBLEModel.CeilingLight || SwitchBotBLEModel.CeilingLightPro)
301
- && serviceData.modelName === SwitchBotBLEModelName.CeilingLight || SwitchBotBLEModelName.CeilingLightPro) {
302
- this.serviceData = serviceData;
288
+ if (this.config.options?.BLE) {
289
+ await this.debugLog('is listening to Platform BLE.');
290
+ this.device.bleMac = this.device.deviceId!.match(/.{1,2}/g)!.join(':').toLowerCase();
291
+ await this.debugLog(`bleMac: ${this.device.bleMac}`);
292
+ this.platform.bleEventHandler[this.device.bleMac] = async (context: ceilingLightServiceData) => {
293
+ try {
294
+ await this.debugLog(`received BLE: ${JSON.stringify(context)}`);
295
+ this.serviceData = context;
303
296
  await this.BLEparseStatus();
304
297
  await this.updateHomeKitCharacteristics();
305
- } else {
306
- await this.errorLog(`failed to get serviceData, serviceData: ${serviceData}`);
307
- await this.BLERefreshConnection(switchbot);
298
+ } catch (e: any) {
299
+ await this.errorLog(`failed to handle BLE. Received: ${JSON.stringify(context)} Error: ${e}`);
308
300
  }
309
- })();
301
+ };
302
+ } else {
303
+ await this.debugLog('is using Device BLE Scanning.');
304
+ const switchbot = await this.switchbotBLE();
305
+ if (switchbot === undefined) {
306
+ await this.BLERefreshConnection(switchbot);
307
+ } else {
308
+ (async () => {
309
+ // Start to monitor advertisement packets
310
+ const serviceData = await this.monitorAdvertisementPackets(switchbot) as unknown as ceilingLightServiceData;
311
+ // Update HomeKit
312
+ if ((serviceData.model === SwitchBotBLEModel.CeilingLight || SwitchBotBLEModel.CeilingLightPro)
313
+ && serviceData.modelName === SwitchBotBLEModelName.CeilingLight || SwitchBotBLEModelName.CeilingLightPro) {
314
+ this.serviceData = serviceData;
315
+ await this.BLEparseStatus();
316
+ await this.updateHomeKitCharacteristics();
317
+ } else {
318
+ await this.errorLog(`failed to get serviceData, serviceData: ${serviceData}`);
319
+ await this.BLERefreshConnection(switchbot);
320
+ }
321
+ })();
322
+ }
310
323
  }
311
324
  }
312
325
 
@@ -162,9 +162,6 @@ export class ColorBulb extends deviceBase {
162
162
  this.debugLog('Registering Webhook Event Handler');
163
163
  this.registerWebhook();
164
164
 
165
- //regisiter webhook event handler
166
- this.registerWebhook();
167
-
168
165
  // Start an update interval
169
166
  interval(this.deviceRefreshRate * 1000)
170
167
  .pipe(skipWhile(() => this.colorBulbUpdateInProgress))
@@ -297,7 +294,7 @@ export class ColorBulb extends deviceBase {
297
294
  async refreshStatus(): Promise<void> {
298
295
  if (!this.device.enableCloudService && this.OpenAPI) {
299
296
  await this.errorLog(`refreshStatus enableCloudService: ${this.device.enableCloudService}`);
300
- } else if (this.BLE) {
297
+ } else if (this.BLE || this.config.options?.BLE) {
301
298
  await this.BLERefreshStatus();
302
299
  } else if (this.OpenAPI && this.platform.config.credentials?.token) {
303
300
  await this.openAPIRefreshStatus();
@@ -309,25 +306,41 @@ export class ColorBulb extends deviceBase {
309
306
 
310
307
  async BLERefreshStatus(): Promise<void> {
311
308
  await this.debugLog('BLERefreshStatus');
312
- const switchbot = await this.switchbotBLE();
313
-
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 colorBulbServiceData;
321
- // Update HomeKit
322
- if (serviceData.model === SwitchBotBLEModel.ColorBulb && serviceData.modelName === SwitchBotBLEModelName.ColorBulb) {
323
- this.serviceData = serviceData;
309
+ if (this.config.options?.BLE) {
310
+ await this.debugLog('is listening to Platform BLE.');
311
+ this.device.bleMac = this.device.deviceId!.match(/.{1,2}/g)!.join(':').toLowerCase();
312
+ await this.debugLog(`bleMac: ${this.device.bleMac}`);
313
+ this.platform.bleEventHandler[this.device.bleMac] = async (context: colorBulbServiceData) => {
314
+ try {
315
+ await this.debugLog(`received BLE: ${JSON.stringify(context)}`);
316
+ this.serviceData = context;
324
317
  await this.BLEparseStatus();
325
318
  await this.updateHomeKitCharacteristics();
326
- } else {
327
- await this.errorLog(`failed to get serviceData, serviceData: ${serviceData}`);
328
- await this.BLERefreshConnection(switchbot);
319
+ } catch (e: any) {
320
+ await this.errorLog(`failed to handle BLE. Received: ${JSON.stringify(context)} Error: ${e}`);
329
321
  }
330
- })();
322
+ };
323
+ } else {
324
+ await this.debugLog('is using Device BLE Scanning.');
325
+ const switchbot = await this.switchbotBLE();
326
+ if (switchbot === undefined) {
327
+ await this.BLERefreshConnection(switchbot);
328
+ } else {
329
+ // Start to monitor advertisement packets
330
+ (async () => {
331
+ // Start to monitor advertisement packets
332
+ const serviceData = await this.monitorAdvertisementPackets(switchbot) as colorBulbServiceData;
333
+ // Update HomeKit
334
+ if (serviceData.model === SwitchBotBLEModel.ColorBulb && serviceData.modelName === SwitchBotBLEModelName.ColorBulb) {
335
+ this.serviceData = serviceData;
336
+ await this.BLEparseStatus();
337
+ await this.updateHomeKitCharacteristics();
338
+ } else {
339
+ await this.errorLog(`failed to get serviceData, serviceData: ${serviceData}`);
340
+ await this.BLERefreshConnection(switchbot);
341
+ }
342
+ })();
343
+ }
331
344
  }
332
345
  }
333
346
 
@@ -176,9 +176,6 @@ export class Contact extends deviceBase {
176
176
  this.debugLog('Registering Webhook Event Handler');
177
177
  this.registerWebhook();
178
178
 
179
- //regisiter webhook event handler
180
- this.registerWebhook();
181
-
182
179
  // Start an update interval
183
180
  interval(this.deviceRefreshRate * 1000)
184
181
  .pipe(skipWhile(() => this.contactUpdateInProgress))
@@ -287,7 +284,7 @@ export class Contact extends deviceBase {
287
284
  async refreshStatus(): Promise<void> {
288
285
  if (!this.device.enableCloudService && this.OpenAPI) {
289
286
  await this.errorLog(`refreshStatus enableCloudService: ${this.device.enableCloudService}`);
290
- } else if (this.BLE) {
287
+ } else if (this.BLE || this.config.options?.BLE) {
291
288
  await this.BLERefreshStatus();
292
289
  } else if (this.OpenAPI && this.platform.config.credentials?.token) {
293
290
  await this.openAPIRefreshStatus();
@@ -299,25 +296,41 @@ export class Contact extends deviceBase {
299
296
 
300
297
  async BLERefreshStatus(): Promise<void> {
301
298
  await this.debugLog('BLERefreshStatus');
302
- const switchbot = await this.switchbotBLE();
303
-
304
- if (switchbot === undefined) {
305
- await this.BLERefreshConnection(switchbot);
306
- } else {
307
- // Start to monitor advertisement packets
308
- (async () => {
309
- // Start to monitor advertisement packets
310
- const serviceData = await this.monitorAdvertisementPackets(switchbot) as contactSensorServiceData;
311
- // Update HomeKit
312
- if (serviceData.model === SwitchBotBLEModel.ContactSensor && serviceData.modelName === SwitchBotBLEModelName.ContactSensor) {
313
- this.serviceData = serviceData;
299
+ if (this.config.options?.BLE) {
300
+ await this.debugLog('is listening to Platform BLE.');
301
+ this.device.bleMac = this.device.deviceId!.match(/.{1,2}/g)!.join(':').toLowerCase();
302
+ await this.debugLog(`bleMac: ${this.device.bleMac}`);
303
+ this.platform.bleEventHandler[this.device.bleMac] = async (context: contactSensorServiceData) => {
304
+ try {
305
+ await this.debugLog(`received BLE: ${JSON.stringify(context)}`);
306
+ this.serviceData = context;
314
307
  await this.BLEparseStatus();
315
308
  await this.updateHomeKitCharacteristics();
316
- } else {
317
- await this.errorLog(`failed to get serviceData, serviceData: ${serviceData}`);
318
- await this.BLERefreshConnection(switchbot);
309
+ } catch (e: any) {
310
+ await this.errorLog(`failed to handle BLE. Received: ${JSON.stringify(context)} Error: ${e}`);
319
311
  }
320
- })();
312
+ };
313
+ } else {
314
+ await this.debugLog('is using Device BLE Scanning.');
315
+ const switchbot = await this.switchbotBLE();
316
+ if (switchbot === undefined) {
317
+ await this.BLERefreshConnection(switchbot);
318
+ } else {
319
+ // Start to monitor advertisement packets
320
+ (async () => {
321
+ // Start to monitor advertisement packets
322
+ const serviceData = await this.monitorAdvertisementPackets(switchbot) as contactSensorServiceData;
323
+ // Update HomeKit
324
+ if (serviceData.model === SwitchBotBLEModel.ContactSensor && serviceData.modelName === SwitchBotBLEModelName.ContactSensor) {
325
+ this.serviceData = serviceData;
326
+ await this.BLEparseStatus();
327
+ await this.updateHomeKitCharacteristics();
328
+ } else {
329
+ await this.errorLog(`failed to get serviceData, serviceData: ${serviceData}`);
330
+ await this.BLERefreshConnection(switchbot);
331
+ }
332
+ })();
333
+ }
321
334
  }
322
335
  }
323
336
 
@@ -425,7 +425,7 @@ export class Curtain extends deviceBase {
425
425
  async refreshStatus(): Promise<void> {
426
426
  if (!this.device.enableCloudService && this.OpenAPI) {
427
427
  await this.errorLog(`refreshStatus enableCloudService: ${this.device.enableCloudService}`);
428
- } else if (this.BLE) {
428
+ } else if (this.BLE || this.config.options?.BLE) {
429
429
  await this.BLERefreshStatus();
430
430
  } else if (this.OpenAPI && this.platform.config.credentials?.token) {
431
431
  await this.openAPIRefreshStatus();
@@ -437,26 +437,42 @@ export class Curtain extends deviceBase {
437
437
 
438
438
  async BLERefreshStatus(): Promise<void> {
439
439
  await this.debugLog('BLERefreshStatus');
440
- const switchbot = await this.switchbotBLE();
441
-
442
- if (switchbot === undefined) {
443
- await this.BLERefreshConnection(switchbot);
444
- } else {
445
- // Start to monitor advertisement packets
446
- (async () => {
447
- // Start to monitor advertisement packets
448
- const serviceData = await this.monitorAdvertisementPackets(switchbot) as curtainServiceData | curtain3ServiceData;
449
- // Update HomeKit
450
- if ((serviceData.model === SwitchBotBLEModel.Curtain || SwitchBotBLEModel.Curtain3)
451
- && (serviceData.modelName === SwitchBotBLEModelName.Curtain || SwitchBotBLEModelName.Curtain3)) {
452
- this.serviceData = serviceData;
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;
453
448
  await this.BLEparseStatus();
454
449
  await this.updateHomeKitCharacteristics();
455
- } else {
456
- await this.errorLog(`failed to get serviceData, serviceData: ${serviceData}`);
457
- await this.BLERefreshConnection(switchbot);
450
+ } catch (e: any) {
451
+ await this.errorLog(`failed to handle BLE. Received: ${JSON.stringify(context)} Error: ${e}`);
458
452
  }
459
- })();
453
+ };
454
+ } 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 {
460
+ // 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)
466
+ && (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
+ }
460
476
  }
461
477
  }
462
478
 
@@ -355,11 +355,11 @@ export abstract class deviceBase {
355
355
  .deviceId!.match(/.{1,2}/g)!
356
356
  .join(':')
357
357
  .toLowerCase();
358
- this.debugLog(`BLE Address: ${this.device.bleMac}`);
358
+ await this.debugLog(`BLE Address: ${this.device.bleMac}`);
359
359
  }
360
360
 
361
361
  async monitorAdvertisementPackets(switchbot: any) {
362
- this.debugLog(`Scanning for ${this.device.bleModelName} devices...`);
362
+ await this.debugLog(`Scanning for ${this.device.bleModelName} devices...`);
363
363
  await switchbot.startScan({ model: this.device.bleModel, id: this.device.bleMac });
364
364
  // Set an event handler
365
365
  let serviceData = { model: this.device.bleModel, modelName: this.device.bleModelName } as ad['serviceData'];
package/src/device/fan.ts CHANGED
@@ -168,9 +168,6 @@ export class Fan extends deviceBase {
168
168
  this.debugLog('Registering Webhook Event Handler');
169
169
  this.registerWebhook();
170
170
 
171
- //regisiter webhook event handler
172
- this.registerWebhook();
173
-
174
171
  // Start an update interval
175
172
  interval(this.deviceRefreshRate * 1000)
176
173
  .pipe(skipWhile(() => this.fanUpdateInProgress))
@@ -314,7 +311,7 @@ export class Fan extends deviceBase {
314
311
  async refreshStatus(): Promise<void> {
315
312
  if (!this.device.enableCloudService && this.OpenAPI) {
316
313
  await this.errorLog(`refreshStatus enableCloudService: ${this.device.enableCloudService}`);
317
- } else if (this.BLE) {
314
+ } else if (this.BLE || this.config.options?.BLE) {
318
315
  await this.BLERefreshStatus();
319
316
  } else if (this.OpenAPI && this.platform.config.credentials?.token) {
320
317
  await this.openAPIRefreshStatus();
@@ -326,25 +323,41 @@ export class Fan extends deviceBase {
326
323
 
327
324
  async BLERefreshStatus(): Promise<void> {
328
325
  await this.debugLog('BLERefreshStatus');
329
- const switchbot = await this.switchbotBLE();
330
-
331
- if (switchbot === undefined) {
332
- await this.BLERefreshConnection(switchbot);
333
- } else {
334
- // Start to monitor advertisement packets
335
- (async () => {
336
- // Start to monitor advertisement packets
337
- const serviceData = await this.monitorAdvertisementPackets(switchbot) as batteryCirculatorFanServiceData;
338
- // Update HomeKit
339
- if (serviceData.model === SwitchBotBLEModel.Unknown && SwitchBotBLEModelName.Unknown) {
340
- this.serviceData = serviceData;
326
+ if (this.config.options?.BLE) {
327
+ await this.debugLog('is listening to Platform BLE.');
328
+ this.device.bleMac = this.device.deviceId!.match(/.{1,2}/g)!.join(':').toLowerCase();
329
+ await this.debugLog(`bleMac: ${this.device.bleMac}`);
330
+ this.platform.bleEventHandler[this.device.bleMac] = async (context: batteryCirculatorFanServiceData) => {
331
+ try {
332
+ await this.debugLog(`received BLE: ${JSON.stringify(context)}`);
333
+ this.serviceData = context;
341
334
  await this.BLEparseStatus();
342
335
  await this.updateHomeKitCharacteristics();
343
- } else {
344
- await this.errorLog(`failed to get serviceData, serviceData: ${serviceData}`);
345
- await this.BLERefreshConnection(switchbot);
336
+ } catch (e: any) {
337
+ await this.errorLog(`failed to handle BLE. Received: ${JSON.stringify(context)} Error: ${e}`);
346
338
  }
347
- })();
339
+ };
340
+ } 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
+ }
348
361
  }
349
362
  }
350
363
 
package/src/device/hub.ts CHANGED
@@ -278,7 +278,7 @@ export class Hub extends deviceBase {
278
278
  async refreshStatus(): Promise<void> {
279
279
  if (!this.device.enableCloudService && this.OpenAPI) {
280
280
  await this.errorLog(`refreshStatus enableCloudService: ${this.device.enableCloudService}`);
281
- } else if (this.BLE) {
281
+ } else if (this.BLE || this.config.options?.BLE) {
282
282
  await this.BLERefreshStatus();
283
283
  } else if (this.OpenAPI && this.platform.config.credentials?.token) {
284
284
  await this.openAPIRefreshStatus();
@@ -290,25 +290,41 @@ export class Hub extends deviceBase {
290
290
 
291
291
  async BLERefreshStatus(): Promise<void> {
292
292
  await this.debugLog('BLERefreshStatus');
293
- const switchbot = await this.switchbotBLE();
294
-
295
- if (switchbot === undefined) {
296
- await this.BLERefreshConnection(switchbot);
297
- } else {
298
- // Start to monitor advertisement packets
299
- (async () => {
300
- // Start to monitor advertisement packets
301
- const serviceData = await this.monitorAdvertisementPackets(switchbot) as hub2ServiceData;
302
- // Update HomeKit
303
- if (serviceData.model === SwitchBotBLEModel.Hub2 && serviceData.modelName === SwitchBotBLEModelName.Hub2) {
304
- this.serviceData = serviceData;
293
+ if (this.config.options?.BLE) {
294
+ await this.debugLog('is listening to Platform BLE.');
295
+ this.device.bleMac = this.device.deviceId!.match(/.{1,2}/g)!.join(':').toLowerCase();
296
+ await this.debugLog(`bleMac: ${this.device.bleMac}`);
297
+ this.platform.bleEventHandler[this.device.bleMac] = async (context: hub2ServiceData) => {
298
+ try {
299
+ await this.debugLog(`received BLE: ${JSON.stringify(context)}`);
300
+ this.serviceData = context;
305
301
  await this.BLEparseStatus();
306
302
  await this.updateHomeKitCharacteristics();
307
- } else {
308
- await this.errorLog(`failed to get serviceData, serviceData: ${serviceData}`);
309
- await this.BLERefreshConnection(switchbot);
303
+ } catch (e: any) {
304
+ await this.errorLog(`failed to handle BLE. Received: ${JSON.stringify(context)} Error: ${e}`);
310
305
  }
311
- })();
306
+ };
307
+ } 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
+ }
312
328
  }
313
329
  }
314
330
 
@@ -318,7 +318,7 @@ export class Humidifier extends deviceBase {
318
318
  async refreshStatus(): Promise<void> {
319
319
  if (!this.device.enableCloudService && this.OpenAPI) {
320
320
  await this.errorLog(`refreshStatus enableCloudService: ${this.device.enableCloudService}`);
321
- } else if (this.BLE) {
321
+ } else if (this.BLE || this.config.options?.BLE) {
322
322
  await this.BLERefreshStatus();
323
323
  } else if (this.OpenAPI && this.platform.config.credentials?.token) {
324
324
  await this.openAPIRefreshStatus();
@@ -330,25 +330,41 @@ export class Humidifier extends deviceBase {
330
330
 
331
331
  async BLERefreshStatus(): Promise<void> {
332
332
  await this.debugLog('BLERefreshStatus');
333
- const switchbot = await this.switchbotBLE();
334
-
335
- if (switchbot === undefined) {
336
- await this.BLERefreshConnection(switchbot);
337
- } else {
338
- // Start to monitor advertisement packets
339
- (async () => {
340
- // Start to monitor advertisement packets
341
- const serviceData = await this.monitorAdvertisementPackets(switchbot) as humidifierServiceData;
342
- // Update HomeKit
343
- if (serviceData.model === SwitchBotBLEModel.Humidifier && serviceData.modelName === SwitchBotBLEModelName.Humidifier) {
344
- this.serviceData = serviceData;
333
+ if (this.config.options?.BLE) {
334
+ await this.debugLog('is listening to Platform BLE.');
335
+ this.device.bleMac = this.device.deviceId!.match(/.{1,2}/g)!.join(':').toLowerCase();
336
+ await this.debugLog(`bleMac: ${this.device.bleMac}`);
337
+ this.platform.bleEventHandler[this.device.bleMac] = async (context: humidifierServiceData) => {
338
+ try {
339
+ await this.debugLog(`received BLE: ${JSON.stringify(context)}`);
340
+ this.serviceData = context;
345
341
  await this.BLEparseStatus();
346
342
  await this.updateHomeKitCharacteristics();
347
- } else {
348
- await this.errorLog(`failed to get serviceData, serviceData: ${serviceData}`);
349
- await this.BLERefreshConnection(switchbot);
343
+ } catch (e: any) {
344
+ await this.errorLog(`failed to handle BLE. Received: ${JSON.stringify(context)} Error: ${e}`);
350
345
  }
351
- })();
346
+ };
347
+ } 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
+ }
352
368
  }
353
369
  }
354
370
 
@@ -271,7 +271,7 @@ export class IOSensor extends deviceBase {
271
271
  async refreshStatus(): Promise<void> {
272
272
  if (!this.device.enableCloudService && this.OpenAPI) {
273
273
  await this.errorLog(`refreshStatus enableCloudService: ${this.device.enableCloudService}`);
274
- } else if (this.BLE) {
274
+ } else if (this.BLE || this.config.options?.BLE) {
275
275
  await this.BLERefreshStatus();
276
276
  } else if (this.OpenAPI && this.platform.config.credentials?.token) {
277
277
  await this.openAPIRefreshStatus();
@@ -283,25 +283,41 @@ export class IOSensor extends deviceBase {
283
283
 
284
284
  async BLERefreshStatus(): Promise<void> {
285
285
  await this.debugLog('BLERefreshStatus');
286
- const switchbot = await this.switchbotBLE();
287
-
288
- if (switchbot === undefined) {
289
- await this.BLERefreshConnection(switchbot);
290
- } else {
291
- // Start to monitor advertisement packets
292
- (async () => {
293
- // Start to monitor advertisement packets
294
- const serviceData = await this.monitorAdvertisementPackets(switchbot) as outdoorMeterServiceData;
295
- // Update HomeKit
296
- if (serviceData.model === SwitchBotBLEModel.OutdoorMeter && serviceData.modelName === SwitchBotBLEModelName.OutdoorMeter) {
297
- this.serviceData = serviceData;
286
+ if (this.config.options?.BLE) {
287
+ await this.debugLog('is listening to Platform BLE.');
288
+ this.device.bleMac = this.device.deviceId!.match(/.{1,2}/g)!.join(':').toLowerCase();
289
+ await this.debugLog(`bleMac: ${this.device.bleMac}`);
290
+ this.platform.bleEventHandler[this.device.bleMac] = async (context: outdoorMeterServiceData) => {
291
+ try {
292
+ await this.debugLog(`received BLE: ${JSON.stringify(context)}`);
293
+ this.serviceData = context;
298
294
  await this.BLEparseStatus();
299
295
  await this.updateHomeKitCharacteristics();
300
- } else {
301
- await this.errorLog(`failed to get serviceData, serviceData: ${serviceData}`);
302
- await this.BLERefreshConnection(switchbot);
296
+ } catch (e: any) {
297
+ await this.errorLog(`failed to handle BLE. Received: ${JSON.stringify(context)} Error: ${e}`);
303
298
  }
304
- })();
299
+ };
300
+ } 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
+ }
305
321
  }
306
322
  }
307
323