@switchbot/homebridge-switchbot 3.6.1-beta.1 → 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 (83) hide show
  1. package/CHANGELOG.md +11 -0
  2. package/dist/device/blindtilt.d.ts.map +1 -1
  3. package/dist/device/blindtilt.js +35 -17
  4. package/dist/device/blindtilt.js.map +1 -1
  5. package/dist/device/bot.d.ts +1 -2
  6. package/dist/device/bot.d.ts.map +1 -1
  7. package/dist/device/bot.js +186 -396
  8. package/dist/device/bot.js.map +1 -1
  9. package/dist/device/ceilinglight.d.ts.map +1 -1
  10. package/dist/device/ceilinglight.js +35 -18
  11. package/dist/device/ceilinglight.js.map +1 -1
  12. package/dist/device/colorbulb.d.ts.map +1 -1
  13. package/dist/device/colorbulb.js +35 -18
  14. package/dist/device/colorbulb.js.map +1 -1
  15. package/dist/device/contact.d.ts.map +1 -1
  16. package/dist/device/contact.js +35 -18
  17. package/dist/device/contact.js.map +1 -1
  18. package/dist/device/curtain.d.ts.map +1 -1
  19. package/dist/device/curtain.js +7 -5
  20. package/dist/device/curtain.js.map +1 -1
  21. package/dist/device/fan.d.ts.map +1 -1
  22. package/dist/device/fan.js +35 -18
  23. package/dist/device/fan.js.map +1 -1
  24. package/dist/device/hub.d.ts.map +1 -1
  25. package/dist/device/hub.js +35 -16
  26. package/dist/device/hub.js.map +1 -1
  27. package/dist/device/humidifier.d.ts.map +1 -1
  28. package/dist/device/humidifier.js +35 -16
  29. package/dist/device/humidifier.js.map +1 -1
  30. package/dist/device/iosensor.d.ts.map +1 -1
  31. package/dist/device/iosensor.js +35 -16
  32. package/dist/device/iosensor.js.map +1 -1
  33. package/dist/device/lightstrip.d.ts.map +1 -1
  34. package/dist/device/lightstrip.js +35 -16
  35. package/dist/device/lightstrip.js.map +1 -1
  36. package/dist/device/lock.d.ts.map +1 -1
  37. package/dist/device/lock.js +36 -17
  38. package/dist/device/lock.js.map +1 -1
  39. package/dist/device/meter.d.ts.map +1 -1
  40. package/dist/device/meter.js +35 -16
  41. package/dist/device/meter.js.map +1 -1
  42. package/dist/device/meterplus.d.ts.map +1 -1
  43. package/dist/device/meterplus.js +35 -16
  44. package/dist/device/meterplus.js.map +1 -1
  45. package/dist/device/motion.d.ts.map +1 -1
  46. package/dist/device/motion.js +35 -16
  47. package/dist/device/motion.js.map +1 -1
  48. package/dist/device/plug.d.ts.map +1 -1
  49. package/dist/device/plug.js +36 -17
  50. package/dist/device/plug.js.map +1 -1
  51. package/dist/device/robotvacuumcleaner.d.ts.map +1 -1
  52. package/dist/device/robotvacuumcleaner.js +35 -16
  53. package/dist/device/robotvacuumcleaner.js.map +1 -1
  54. package/dist/device/waterdetector.d.ts.map +1 -1
  55. package/dist/device/waterdetector.js +35 -16
  56. package/dist/device/waterdetector.js.map +1 -1
  57. package/dist/irdevice/other.d.ts +1 -0
  58. package/dist/irdevice/other.d.ts.map +1 -1
  59. package/dist/irdevice/other.js +186 -228
  60. package/dist/irdevice/other.js.map +1 -1
  61. package/dist/platform.js +2 -2
  62. package/dist/platform.js.map +1 -1
  63. package/package.json +1 -1
  64. package/src/device/blindtilt.ts +32 -17
  65. package/src/device/bot.ts +179 -406
  66. package/src/device/ceilinglight.ts +32 -19
  67. package/src/device/colorbulb.ts +32 -19
  68. package/src/device/contact.ts +32 -19
  69. package/src/device/curtain.ts +7 -6
  70. package/src/device/fan.ts +32 -19
  71. package/src/device/hub.ts +32 -16
  72. package/src/device/humidifier.ts +32 -16
  73. package/src/device/iosensor.ts +32 -16
  74. package/src/device/lightstrip.ts +32 -16
  75. package/src/device/lock.ts +33 -17
  76. package/src/device/meter.ts +32 -16
  77. package/src/device/meterplus.ts +32 -16
  78. package/src/device/motion.ts +32 -16
  79. package/src/device/plug.ts +33 -17
  80. package/src/device/robotvacuumcleaner.ts +32 -16
  81. package/src/device/waterdetector.ts +32 -16
  82. package/src/irdevice/other.ts +214 -283
  83. package/src/platform.ts +2 -2
@@ -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))
@@ -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))
@@ -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))
@@ -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
 
@@ -438,21 +438,22 @@ export class Curtain extends deviceBase {
438
438
  async BLERefreshStatus(): Promise<void> {
439
439
  await this.debugLog('BLERefreshStatus');
440
440
  if (this.config.options?.BLE) {
441
- await this.debugLog('is listening to BLE.');
442
- this.platform.bleEventHandler[this.device.deviceId] = async (context: curtainServiceData | curtain3ServiceData) => {
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) => {
443
445
  try {
444
- await this.debugLog(`received Webhook: ${JSON.stringify(context)}`);
446
+ await this.debugLog(`received BLE: ${JSON.stringify(context)}`);
445
447
  this.serviceData = context;
446
- await this.parseStatusWebhook();
448
+ await this.BLEparseStatus();
447
449
  await this.updateHomeKitCharacteristics();
448
450
  } catch (e: any) {
449
451
  await this.errorLog(`failed to handle BLE. Received: ${JSON.stringify(context)} Error: ${e}`);
450
452
  }
451
453
  };
452
454
  } else {
453
- await this.debugLog('is not listening to BLE.');
455
+ await this.debugLog('is using Device BLE Scanning.');
454
456
  const switchbot = await this.switchbotBLE();
455
-
456
457
  if (switchbot === undefined) {
457
458
  await this.BLERefreshConnection(switchbot);
458
459
  } else {
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))
@@ -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
@@ -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
 
@@ -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
 
@@ -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
 
@@ -313,25 +313,41 @@ export class StripLight extends deviceBase {
313
313
 
314
314
  async BLERefreshStatus(): Promise<void> {
315
315
  await this.debugLog('BLERefreshStatus');
316
- const switchbot = await this.switchbotBLE();
317
-
318
- if (switchbot === undefined) {
319
- await this.BLERefreshConnection(switchbot);
320
- } else {
321
- // Start to monitor advertisement packets
322
- (async () => {
323
- // Start to monitor advertisement packets
324
- const serviceData = await this.monitorAdvertisementPackets(switchbot) as stripLightServiceData;
325
- // Update HomeKit
326
- if (serviceData.model === SwitchBotBLEModel.StripLight && serviceData.modelName === SwitchBotBLEModelName.StripLight) {
327
- this.serviceData = serviceData;
316
+ if (this.config.options?.BLE) {
317
+ await this.debugLog('is listening to Platform BLE.');
318
+ this.device.bleMac = this.device.deviceId!.match(/.{1,2}/g)!.join(':').toLowerCase();
319
+ await this.debugLog(`bleMac: ${this.device.bleMac}`);
320
+ this.platform.bleEventHandler[this.device.bleMac] = async (context: stripLightServiceData) => {
321
+ try {
322
+ await this.debugLog(`received BLE: ${JSON.stringify(context)}`);
323
+ this.serviceData = context;
328
324
  await this.BLEparseStatus();
329
325
  await this.updateHomeKitCharacteristics();
330
- } else {
331
- await this.errorLog(`failed to get serviceData, serviceData: ${serviceData}`);
332
- await this.BLERefreshConnection(switchbot);
326
+ } catch (e: any) {
327
+ await this.errorLog(`failed to handle BLE. Received: ${JSON.stringify(context)} Error: ${e}`);
333
328
  }
334
- })();
329
+ };
330
+ } else {
331
+ await this.debugLog('is using Device BLE Scanning.');
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 stripLightServiceData;
340
+ // Update HomeKit
341
+ if (serviceData.model === SwitchBotBLEModel.StripLight && serviceData.modelName === SwitchBotBLEModelName.StripLight) {
342
+ this.serviceData = serviceData;
343
+ await this.BLEparseStatus();
344
+ await this.updateHomeKitCharacteristics();
345
+ } else {
346
+ await this.errorLog(`failed to get serviceData, serviceData: ${serviceData}`);
347
+ await this.BLERefreshConnection(switchbot);
348
+ }
349
+ })();
350
+ }
335
351
  }
336
352
  }
337
353
 
@@ -309,26 +309,42 @@ export class Lock extends deviceBase {
309
309
 
310
310
  async BLERefreshStatus(): Promise<void> {
311
311
  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 lockServiceData | lockProServiceData;
321
- // Update HomeKit
322
- if ((serviceData.model === SwitchBotBLEModel.Lock || SwitchBotBLEModel.LockPro)
323
- && (serviceData.modelName === SwitchBotBLEModelName.Lock || SwitchBotBLEModelName.LockPro)) {
324
- this.serviceData = serviceData;
312
+ if (this.config.options?.BLE) {
313
+ await this.debugLog('is listening to Platform BLE.');
314
+ this.device.bleMac = this.device.deviceId!.match(/.{1,2}/g)!.join(':').toLowerCase();
315
+ await this.debugLog(`bleMac: ${this.device.bleMac}`);
316
+ this.platform.bleEventHandler[this.device.bleMac] = async (context: lockServiceData | lockProServiceData) => {
317
+ try {
318
+ await this.debugLog(`received BLE: ${JSON.stringify(context)}`);
319
+ this.serviceData = context;
325
320
  await this.BLEparseStatus();
326
321
  await this.updateHomeKitCharacteristics();
327
- } else {
328
- await this.errorLog(`failed to get serviceData, serviceData: ${serviceData}`);
329
- await this.BLERefreshConnection(switchbot);
322
+ } catch (e: any) {
323
+ await this.errorLog(`failed to handle BLE. Received: ${JSON.stringify(context)} Error: ${e}`);
330
324
  }
331
- })();
325
+ };
326
+ } 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
+ }
332
348
  }
333
349
  }
334
350