@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
@@ -301,7 +301,7 @@ export class StripLight extends deviceBase {
301
301
  async refreshStatus(): Promise<void> {
302
302
  if (!this.device.enableCloudService && this.OpenAPI) {
303
303
  await this.errorLog(`refreshStatus enableCloudService: ${this.device.enableCloudService}`);
304
- } else if (this.BLE) {
304
+ } else if (this.BLE || this.config.options?.BLE) {
305
305
  await this.BLERefreshStatus();
306
306
  } else if (this.OpenAPI && this.platform.config.credentials?.token) {
307
307
  await this.openAPIRefreshStatus();
@@ -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
 
@@ -297,7 +297,7 @@ export class Lock extends deviceBase {
297
297
  async refreshStatus(): Promise<void> {
298
298
  if (!this.device.enableCloudService && this.OpenAPI) {
299
299
  await this.errorLog(`refreshStatus enableCloudService: ${this.device.enableCloudService}`);
300
- } else if (this.BLE) {
300
+ } else if (this.BLE || this.config.options?.BLE) {
301
301
  await this.BLERefreshStatus();
302
302
  } else if (this.OpenAPI && this.platform.config.credentials?.token) {
303
303
  await this.openAPIRefreshStatus();
@@ -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
 
@@ -265,7 +265,7 @@ export class Meter extends deviceBase {
265
265
  async refreshStatus(): Promise<void> {
266
266
  if (!this.device.enableCloudService && this.OpenAPI) {
267
267
  await this.errorLog(`refreshStatus enableCloudService: ${this.device.enableCloudService}`);
268
- } else if (this.BLE) {
268
+ } else if (this.BLE || this.config.options?.BLE) {
269
269
  await this.BLERefreshStatus();
270
270
  } else if (this.OpenAPI && this.platform.config.credentials?.token) {
271
271
  await this.openAPIRefreshStatus();
@@ -277,25 +277,41 @@ export class Meter extends deviceBase {
277
277
 
278
278
  async BLERefreshStatus(): Promise<void> {
279
279
  await this.debugLog('BLERefreshStatus');
280
- const switchbot = await this.switchbotBLE();
281
-
282
- if (switchbot === undefined) {
283
- await this.BLERefreshConnection(switchbot);
284
- } else {
285
- // Start to monitor advertisement packets
286
- (async () => {
287
- // Start to monitor advertisement packets
288
- const serviceData = await this.monitorAdvertisementPackets(switchbot) as meterServiceData;
289
- // Update HomeKit
290
- if (serviceData.model === SwitchBotBLEModel.Meter && serviceData.modelName === SwitchBotBLEModelName.Meter) {
291
- this.serviceData = serviceData;
280
+ if (this.config.options?.BLE) {
281
+ await this.debugLog('is listening to Platform BLE.');
282
+ this.device.bleMac = this.device.deviceId!.match(/.{1,2}/g)!.join(':').toLowerCase();
283
+ await this.debugLog(`bleMac: ${this.device.bleMac}`);
284
+ this.platform.bleEventHandler[this.device.bleMac] = async (context: meterServiceData) => {
285
+ try {
286
+ await this.debugLog(`received BLE: ${JSON.stringify(context)}`);
287
+ this.serviceData = context;
292
288
  await this.BLEparseStatus();
293
289
  await this.updateHomeKitCharacteristics();
294
- } else {
295
- await this.errorLog(`failed to get serviceData, serviceData: ${serviceData}`);
296
- await this.BLERefreshConnection(switchbot);
290
+ } catch (e: any) {
291
+ await this.errorLog(`failed to handle BLE. Received: ${JSON.stringify(context)} Error: ${e}`);
297
292
  }
298
- })();
293
+ };
294
+ } 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
+ }
299
315
  }
300
316
  }
301
317
 
@@ -266,7 +266,7 @@ export class MeterPlus extends deviceBase {
266
266
  async refreshStatus(): Promise<void> {
267
267
  if (!this.device.enableCloudService && this.OpenAPI) {
268
268
  await this.errorLog(`refreshStatus enableCloudService: ${this.device.enableCloudService}`);
269
- } else if (this.BLE) {
269
+ } else if (this.BLE || this.config.options?.BLE) {
270
270
  await this.BLERefreshStatus();
271
271
  } else if (this.OpenAPI && this.platform.config.credentials?.token) {
272
272
  await this.openAPIRefreshStatus();
@@ -278,25 +278,41 @@ export class MeterPlus extends deviceBase {
278
278
 
279
279
  async BLERefreshStatus(): Promise<void> {
280
280
  await this.debugLog('BLERefreshStatus');
281
- const switchbot = await this.switchbotBLE();
282
-
283
- if (switchbot === undefined) {
284
- await this.BLERefreshConnection(switchbot);
285
- } else {
286
- // Start to monitor advertisement packets
287
- (async () => {
288
- // Start to monitor advertisement packets
289
- const serviceData = await this.monitorAdvertisementPackets(switchbot) as meterPlusServiceData;
290
- // Update HomeKit
291
- if (serviceData.model === SwitchBotBLEModel.MeterPlus && serviceData.modelName === SwitchBotBLEModelName.MeterPlus) {
292
- this.serviceData = serviceData;
281
+ if (this.config.options?.BLE) {
282
+ await this.debugLog('is listening to Platform BLE.');
283
+ this.device.bleMac = this.device.deviceId!.match(/.{1,2}/g)!.join(':').toLowerCase();
284
+ await this.debugLog(`bleMac: ${this.device.bleMac}`);
285
+ this.platform.bleEventHandler[this.device.bleMac] = async (context: meterPlusServiceData) => {
286
+ try {
287
+ await this.debugLog(`received BLE: ${JSON.stringify(context)}`);
288
+ this.serviceData = context;
293
289
  await this.BLEparseStatus();
294
290
  await this.updateHomeKitCharacteristics();
295
- } else {
296
- await this.errorLog(`failed to get serviceData, serviceData: ${serviceData}`);
297
- await this.BLERefreshConnection(switchbot);
291
+ } catch (e: any) {
292
+ await this.errorLog(`failed to handle BLE. Received: ${JSON.stringify(context)} Error: ${e}`);
298
293
  }
299
- })();
294
+ };
295
+ } 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
+ }
300
316
  }
301
317
  }
302
318
 
@@ -237,7 +237,7 @@ export class Motion extends deviceBase {
237
237
  async refreshStatus(): Promise<void> {
238
238
  if (!this.device.enableCloudService && this.OpenAPI) {
239
239
  await this.errorLog(`refreshStatus enableCloudService: ${this.device.enableCloudService}`);
240
- } else if (this.BLE) {
240
+ } else if (this.BLE || this.config.options?.BLE) {
241
241
  await this.BLERefreshStatus();
242
242
  } else if (this.OpenAPI && this.platform.config.credentials?.token) {
243
243
  await this.openAPIRefreshStatus();
@@ -249,25 +249,41 @@ export class Motion extends deviceBase {
249
249
 
250
250
  async BLERefreshStatus(): Promise<void> {
251
251
  await this.debugLog('BLERefreshStatus');
252
- const switchbot = await this.switchbotBLE();
253
-
254
- if (switchbot === undefined) {
255
- await this.BLERefreshConnection(switchbot);
256
- } else {
257
- // Start to monitor advertisement packets
258
- (async () => {
259
- // Start to monitor advertisement packets
260
- const serviceData = await this.monitorAdvertisementPackets(switchbot) as motionSensorServiceData;
261
- // Update HomeKit
262
- if (serviceData.model === SwitchBotBLEModel.MotionSensor && serviceData.modelName === SwitchBotBLEModelName.MotionSensor) {
263
- this.serviceData = serviceData;
252
+ if (this.config.options?.BLE) {
253
+ await this.debugLog('is listening to Platform BLE.');
254
+ this.device.bleMac = this.device.deviceId!.match(/.{1,2}/g)!.join(':').toLowerCase();
255
+ await this.debugLog(`bleMac: ${this.device.bleMac}`);
256
+ this.platform.bleEventHandler[this.device.bleMac] = async (context: motionSensorServiceData) => {
257
+ try {
258
+ await this.debugLog(`received BLE: ${JSON.stringify(context)}`);
259
+ this.serviceData = context;
264
260
  await this.BLEparseStatus();
265
261
  await this.updateHomeKitCharacteristics();
266
- } else {
267
- await this.errorLog(`failed to get serviceData, serviceData: ${serviceData}`);
268
- await this.BLERefreshConnection(switchbot);
262
+ } catch (e: any) {
263
+ await this.errorLog(`failed to handle BLE. Received: ${JSON.stringify(context)} Error: ${e}`);
269
264
  }
270
- })();
265
+ };
266
+ } else {
267
+ await this.debugLog('is using Device BLE Scanning.');
268
+ const switchbot = await this.switchbotBLE();
269
+ if (switchbot === undefined) {
270
+ await this.BLERefreshConnection(switchbot);
271
+ } else {
272
+ // Start to monitor advertisement packets
273
+ (async () => {
274
+ // Start to monitor advertisement packets
275
+ const serviceData = await this.monitorAdvertisementPackets(switchbot) as motionSensorServiceData;
276
+ // Update HomeKit
277
+ if (serviceData.model === SwitchBotBLEModel.MotionSensor && serviceData.modelName === SwitchBotBLEModelName.MotionSensor) {
278
+ this.serviceData = serviceData;
279
+ await this.BLEparseStatus();
280
+ await this.updateHomeKitCharacteristics();
281
+ } else {
282
+ await this.errorLog(`failed to get serviceData, serviceData: ${serviceData}`);
283
+ await this.BLERefreshConnection(switchbot);
284
+ }
285
+ })();
286
+ }
271
287
  }
272
288
  }
273
289
 
@@ -148,7 +148,7 @@ export class Plug extends deviceBase {
148
148
  async refreshStatus(): Promise<void> {
149
149
  if (!this.device.enableCloudService && this.OpenAPI) {
150
150
  await this.errorLog(`refreshStatus enableCloudService: ${this.device.enableCloudService}`);
151
- } else if (this.BLE) {
151
+ } else if (this.BLE || this.config.options?.BLE) {
152
152
  await this.BLERefreshStatus();
153
153
  } else if (this.OpenAPI && this.platform.config.credentials?.token) {
154
154
  await this.openAPIRefreshStatus();
@@ -160,26 +160,42 @@ export class Plug extends deviceBase {
160
160
 
161
161
  async BLERefreshStatus(): Promise<void> {
162
162
  await this.debugLog('BLERefreshStatus');
163
- const switchbot = await this.switchbotBLE();
164
-
165
- if (switchbot === undefined) {
166
- await this.BLERefreshConnection(switchbot);
167
- } else {
168
- // Start to monitor advertisement packets
169
- (async () => {
170
- // Start to monitor advertisement packets
171
- const serviceData = await this.monitorAdvertisementPackets(switchbot) as plugMiniUSServiceData | plugMiniJPServiceData;
172
- // Update HomeKit
173
- if ((serviceData.model === SwitchBotBLEModel.PlugMiniUS || SwitchBotBLEModel.PlugMiniJP)
174
- && serviceData.modelName === (SwitchBotBLEModelName.PlugMini || SwitchBotBLEModelName.PlugMini)) {
175
- this.serviceData = serviceData;
163
+ if (this.config.options?.BLE) {
164
+ await this.debugLog('is listening to Platform BLE.');
165
+ this.device.bleMac = this.device.deviceId!.match(/.{1,2}/g)!.join(':').toLowerCase();
166
+ await this.debugLog(`bleMac: ${this.device.bleMac}`);
167
+ this.platform.bleEventHandler[this.device.bleMac] = async (context: plugMiniUSServiceData | plugMiniJPServiceData) => {
168
+ try {
169
+ await this.debugLog(`received BLE: ${JSON.stringify(context)}`);
170
+ this.serviceData = context;
176
171
  await this.BLEparseStatus();
177
172
  await this.updateHomeKitCharacteristics();
178
- } else {
179
- await this.errorLog(`failed to get serviceData, serviceData: ${serviceData}`);
180
- await this.BLERefreshConnection(switchbot);
173
+ } catch (e: any) {
174
+ await this.errorLog(`failed to handle BLE. Received: ${JSON.stringify(context)} Error: ${e}`);
181
175
  }
182
- })();
176
+ };
177
+ } 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
+ }
183
199
  }
184
200
  }
185
201
 
@@ -249,7 +249,7 @@ export class RobotVacuumCleaner extends deviceBase {
249
249
  async refreshStatus(): Promise<void> {
250
250
  if (!this.device.enableCloudService && this.OpenAPI) {
251
251
  await this.errorLog(`refreshStatus enableCloudService: ${this.device.enableCloudService}`);
252
- } else if (this.BLE) {
252
+ } else if (this.BLE || this.config.options?.BLE) {
253
253
  await this.BLERefreshStatus();
254
254
  } else if (this.OpenAPI && this.platform.config.credentials?.token) {
255
255
  await this.openAPIRefreshStatus();
@@ -261,25 +261,41 @@ export class RobotVacuumCleaner extends deviceBase {
261
261
 
262
262
  async BLERefreshStatus(): Promise<void> {
263
263
  await this.debugLog('BLERefreshStatus');
264
- const switchbot = await this.switchbotBLE();
265
-
266
- if (switchbot === undefined) {
267
- await this.BLERefreshConnection(switchbot);
268
- } else {
269
- // Start to monitor advertisement packets
270
- (async () => {
271
- // Start to monitor advertisement packets
272
- const serviceData = await this.monitorAdvertisementPackets(switchbot) as unknown as robotVacuumCleanerServiceData;
273
- // Update HomeKit
274
- if (serviceData.model === SwitchBotBLEModel.Unknown && serviceData.modelName === SwitchBotBLEModelName.Unknown) {
275
- this.serviceData = serviceData;
264
+ if (this.config.options?.BLE) {
265
+ await this.debugLog('is listening to Platform BLE.');
266
+ this.device.bleMac = this.device.deviceId!.match(/.{1,2}/g)!.join(':').toLowerCase();
267
+ await this.debugLog(`bleMac: ${this.device.bleMac}`);
268
+ this.platform.bleEventHandler[this.device.bleMac] = async (context: robotVacuumCleanerServiceData) => {
269
+ try {
270
+ await this.debugLog(`received BLE: ${JSON.stringify(context)}`);
271
+ this.serviceData = context;
276
272
  await this.BLEparseStatus();
277
273
  await this.updateHomeKitCharacteristics();
278
- } else {
279
- await this.errorLog(`failed to get serviceData, serviceData: ${serviceData}`);
280
- await this.BLERefreshConnection(switchbot);
274
+ } catch (e: any) {
275
+ await this.errorLog(`failed to handle BLE. Received: ${JSON.stringify(context)} Error: ${e}`);
281
276
  }
282
- })();
277
+ };
278
+ } else {
279
+ await this.debugLog('is using Device BLE Scanning.');
280
+ const switchbot = await this.switchbotBLE();
281
+ if (switchbot === undefined) {
282
+ await this.BLERefreshConnection(switchbot);
283
+ } else {
284
+ // Start to monitor advertisement packets
285
+ (async () => {
286
+ // Start to monitor advertisement packets
287
+ const serviceData = await this.monitorAdvertisementPackets(switchbot) as unknown as robotVacuumCleanerServiceData;
288
+ // Update HomeKit
289
+ if (serviceData.model === SwitchBotBLEModel.Unknown && serviceData.modelName === SwitchBotBLEModelName.Unknown) {
290
+ this.serviceData = serviceData;
291
+ await this.BLEparseStatus();
292
+ await this.updateHomeKitCharacteristics();
293
+ } else {
294
+ await this.errorLog(`failed to get serviceData, serviceData: ${serviceData}`);
295
+ await this.BLERefreshConnection(switchbot);
296
+ }
297
+ })();
298
+ }
283
299
  }
284
300
  }
285
301
 
@@ -234,7 +234,7 @@ export class WaterDetector extends deviceBase {
234
234
  async refreshStatus(): Promise<void> {
235
235
  if (!this.device.enableCloudService && this.OpenAPI) {
236
236
  await this.errorLog(`refreshStatus enableCloudService: ${this.device.enableCloudService}`);
237
- } else if (this.BLE) {
237
+ } else if (this.BLE || this.config.options?.BLE) {
238
238
  await this.BLERefreshStatus();
239
239
  } else if (this.OpenAPI && this.platform.config.credentials?.token) {
240
240
  await this.openAPIRefreshStatus();
@@ -246,25 +246,41 @@ export class WaterDetector extends deviceBase {
246
246
 
247
247
  async BLERefreshStatus(): Promise<void> {
248
248
  await this.debugLog('BLERefreshStatus');
249
- const switchbot = await this.switchbotBLE();
250
-
251
- if (switchbot === undefined) {
252
- await this.BLERefreshConnection(switchbot);
253
- } else {
254
- // Start to monitor advertisement packets
255
- (async () => {
256
- // Start to monitor advertisement packets
257
- const serviceData = await this.monitorAdvertisementPackets(switchbot) as waterLeakDetectorServiceData;
258
- // Update HomeKit
259
- if (serviceData.model === SwitchBotBLEModel.Unknown && serviceData.modelName === SwitchBotBLEModelName.Unknown) {
260
- this.serviceData = serviceData;
249
+ if (this.config.options?.BLE) {
250
+ await this.debugLog('is listening to Platform BLE.');
251
+ this.device.bleMac = this.device.deviceId!.match(/.{1,2}/g)!.join(':').toLowerCase();
252
+ await this.debugLog(`bleMac: ${this.device.bleMac}`);
253
+ this.platform.bleEventHandler[this.device.bleMac] = async (context: waterLeakDetectorServiceData) => {
254
+ try {
255
+ await this.debugLog(`received BLE: ${JSON.stringify(context)}`);
256
+ this.serviceData = context;
261
257
  await this.BLEparseStatus();
262
258
  await this.updateHomeKitCharacteristics();
263
- } else {
264
- await this.errorLog(`failed to get serviceData, serviceData: ${serviceData}`);
265
- await this.BLERefreshConnection(switchbot);
259
+ } catch (e: any) {
260
+ await this.errorLog(`failed to handle BLE. Received: ${JSON.stringify(context)} Error: ${e}`);
266
261
  }
267
- })();
262
+ };
263
+ } else {
264
+ await this.debugLog('is using Device BLE Scanning.');
265
+ const switchbot = await this.switchbotBLE();
266
+ if (switchbot === undefined) {
267
+ await this.BLERefreshConnection(switchbot);
268
+ } else {
269
+ // Start to monitor advertisement packets
270
+ (async () => {
271
+ // Start to monitor advertisement packets
272
+ const serviceData = await this.monitorAdvertisementPackets(switchbot) as waterLeakDetectorServiceData;
273
+ // Update HomeKit
274
+ if (serviceData.model === SwitchBotBLEModel.Unknown && serviceData.modelName === SwitchBotBLEModelName.Unknown) {
275
+ this.serviceData = serviceData;
276
+ await this.BLEparseStatus();
277
+ await this.updateHomeKitCharacteristics();
278
+ } else {
279
+ await this.errorLog(`failed to get serviceData, serviceData: ${serviceData}`);
280
+ await this.BLERefreshConnection(switchbot);
281
+ }
282
+ })();
283
+ }
268
284
  }
269
285
  }
270
286