@switchbot/homebridge-switchbot 3.8.1-beta.2 → 3.8.2-beta.0

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 (84) hide show
  1. package/CHANGELOG.md +16 -0
  2. package/dist/device/blindtilt.d.ts.map +1 -1
  3. package/dist/device/blindtilt.js +51 -38
  4. package/dist/device/blindtilt.js.map +1 -1
  5. package/dist/device/bot.d.ts.map +1 -1
  6. package/dist/device/bot.js +78 -64
  7. package/dist/device/bot.js.map +1 -1
  8. package/dist/device/ceilinglight.d.ts.map +1 -1
  9. package/dist/device/ceilinglight.js +55 -42
  10. package/dist/device/ceilinglight.js.map +1 -1
  11. package/dist/device/colorbulb.d.ts.map +1 -1
  12. package/dist/device/colorbulb.js +133 -99
  13. package/dist/device/colorbulb.js.map +1 -1
  14. package/dist/device/contact.d.ts.map +1 -1
  15. package/dist/device/contact.js +20 -13
  16. package/dist/device/contact.js.map +1 -1
  17. package/dist/device/curtain.d.ts.map +1 -1
  18. package/dist/device/curtain.js +88 -70
  19. package/dist/device/curtain.js.map +1 -1
  20. package/dist/device/device.d.ts +0 -1
  21. package/dist/device/device.d.ts.map +1 -1
  22. package/dist/device/device.js +26 -22
  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 +54 -40
  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 +20 -14
  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 +45 -32
  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 +20 -14
  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 +107 -80
  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 +54 -40
  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 +20 -14
  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 +20 -14
  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 +20 -13
  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 +55 -41
  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 +55 -41
  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 +20 -13
  59. package/dist/device/waterdetector.js.map +1 -1
  60. package/dist/utils.d.ts +8 -0
  61. package/dist/utils.d.ts.map +1 -1
  62. package/dist/utils.js +23 -0
  63. package/dist/utils.js.map +1 -1
  64. package/package.json +7 -7
  65. package/src/device/blindtilt.ts +47 -36
  66. package/src/device/bot.ts +71 -59
  67. package/src/device/ceilinglight.ts +50 -39
  68. package/src/device/colorbulb.ts +128 -99
  69. package/src/device/contact.ts +16 -10
  70. package/src/device/curtain.ts +84 -69
  71. package/src/device/device.ts +24 -23
  72. package/src/device/fan.ts +49 -37
  73. package/src/device/hub.ts +16 -11
  74. package/src/device/humidifier.ts +41 -30
  75. package/src/device/iosensor.ts +16 -11
  76. package/src/device/lightstrip.ts +102 -79
  77. package/src/device/lock.ts +49 -37
  78. package/src/device/meter.ts +16 -11
  79. package/src/device/meterplus.ts +16 -11
  80. package/src/device/motion.ts +16 -10
  81. package/src/device/plug.ts +50 -38
  82. package/src/device/robotvacuumcleaner.ts +50 -38
  83. package/src/device/waterdetector.ts +16 -10
  84. package/src/utils.ts +29 -0
package/src/device/bot.ts CHANGED
@@ -18,6 +18,7 @@ import type { botWebhookContext } from '../types/devicewebhookstatus.js'
18
18
  import { SwitchBotBLEModel, SwitchBotBLEModelName } from 'node-switchbot'
19
19
  import { debounceTime, interval, skipWhile, Subject, take, tap } from 'rxjs'
20
20
 
21
+ import { formatDeviceIdAsMac } from '../utils.js'
21
22
  import { deviceBase } from './device.js'
22
23
 
23
24
  /**
@@ -575,17 +576,22 @@ export class Bot extends deviceBase {
575
576
  await this.debugLog('registerPlatformBLE')
576
577
  if (this.config.options?.BLE) {
577
578
  await this.debugLog('is listening to Platform BLE.')
578
- this.device.bleMac = this.device.deviceId!.match(/.{1,2}/g)!.join(':').toLowerCase()
579
- await this.debugLog(`bleMac: ${this.device.bleMac}`)
580
- this.platform.bleEventHandler[this.device.bleMac] = async (context: botServiceData) => {
581
- try {
582
- await this.debugLog(`received BLE: ${JSON.stringify(context)}`)
583
- this.serviceData = context
584
- await this.BLEparseStatus()
585
- await this.updateHomeKitCharacteristics()
586
- } catch (e: any) {
587
- await this.errorLog(`failed to handle BLE. Received: ${JSON.stringify(context)} Error: ${e}`)
579
+ try {
580
+ const formattedDeviceId = formatDeviceIdAsMac(this.device.deviceId!)
581
+ this.device.bleMac = formattedDeviceId
582
+ await this.debugLog(`bleMac: ${this.device.bleMac}`)
583
+ this.platform.bleEventHandler[this.device.bleMac] = async (context: botServiceData) => {
584
+ try {
585
+ await this.debugLog(`received BLE: ${JSON.stringify(context)}`)
586
+ this.serviceData = context
587
+ await this.BLEparseStatus()
588
+ await this.updateHomeKitCharacteristics()
589
+ } catch (e: any) {
590
+ await this.errorLog(`failed to handle BLE. Received: ${JSON.stringify(context)} Error: ${e}`)
591
+ }
588
592
  }
593
+ } catch (error) {
594
+ await this.errorLog(`failed to format device ID as MAC, Error: ${error}`)
589
595
  }
590
596
  } else {
591
597
  await this.debugLog('is not listening to Platform BLE')
@@ -663,57 +669,63 @@ export class Bot extends deviceBase {
663
669
  if (this.On !== this.accessory.context.On || this.allowPush) {
664
670
  await this.debugLog(`BLEpushChanges On: ${this.On} OnCached: ${this.accessory.context.On}`)
665
671
  const switchbot = await this.platform.connectBLE(this.accessory, this.device)
666
- await this.convertBLEAddress()
667
- // if (switchbot !== false) {
668
- await this.debugLog(`Bot Mode: ${this.botMode}`)
669
- if (this.botMode === 'press') {
670
- switchbot
671
- .discover({ model: 'H', quick: true, id: this.device.bleMac })
672
- .then(async (device_list: { press: (arg0: { id: string | undefined }) => any }[]) => {
673
- await this.infoLog(`On: ${this.On}`)
674
- return await device_list[0].press({ id: this.device.bleMac })
675
- })
676
- .then(async () => {
677
- await this.successLog(`On: ${this.On} sent over SwitchBot BLE, sent successfully`)
678
- await this.updateHomeKitCharacteristics()
679
- setTimeout(async () => {
680
- this.On = false
672
+ try {
673
+ const formattedDeviceId = formatDeviceIdAsMac(this.device.deviceId!)
674
+ this.device.bleMac = formattedDeviceId
675
+ await this.debugLog(`bleMac: ${this.device.bleMac}`)
676
+ // if (switchbot !== false) {
677
+ await this.debugLog(`Bot Mode: ${this.botMode}`)
678
+ if (this.botMode === 'press') {
679
+ switchbot
680
+ .discover({ model: 'H', quick: true, id: this.device.bleMac })
681
+ .then(async (device_list: { press: (arg0: { id: string | undefined }) => any }[]) => {
682
+ await this.infoLog(`On: ${this.On}`)
683
+ return await device_list[0].press({ id: this.device.bleMac })
684
+ })
685
+ .then(async () => {
686
+ await this.successLog(`On: ${this.On} sent over SwitchBot BLE, sent successfully`)
681
687
  await this.updateHomeKitCharacteristics()
682
- this.debugLog(`On: ${this.On}, Switch Timeout`)
683
- }, 500)
684
- })
685
- .catch(async (e: any) => {
686
- await this.apiError(e)
687
- await this.errorLog(`failed BLEpushChanges with ${this.device.connectionType} Connection, Error Message: ${JSON.stringify(e.message)}`)
688
- await this.BLEPushConnection()
689
- })
690
- } else if (this.botMode === 'switch') {
691
- switchbot
692
- .discover({ model: this.device.bleModel, quick: true, id: this.device.bleMac })
693
- .then(async (device_list: any) => {
694
- this.infoLog(`On: ${this.On}`)
695
- return await this.retryBLE({
696
- max: await this.maxRetryBLE(),
697
- fn: async () => {
698
- if (this.On) {
699
- return await device_list[0].turnOn({ id: this.device.bleMac })
700
- } else {
701
- return await device_list[0].turnOff({ id: this.device.bleMac })
702
- }
703
- },
688
+ setTimeout(async () => {
689
+ this.On = false
690
+ await this.updateHomeKitCharacteristics()
691
+ this.debugLog(`On: ${this.On}, Switch Timeout`)
692
+ }, 500)
704
693
  })
705
- })
706
- .then(async () => {
707
- await this.successLog(`On: ${this.On} sent over SwitchBot BLE, sent successfully`)
708
- await this.updateHomeKitCharacteristics()
709
- })
710
- .catch(async (e: any) => {
711
- await this.apiError(e)
712
- await this.errorLog(`failed BLEpushChanges with ${this.device.connectionType} Connection, Error Message: ${JSON.stringify(e.message)}`)
713
- await this.BLEPushConnection()
714
- })
715
- } else {
716
- await this.errorLog(`Device Parameters not set for this Bot, please check the device configuration. Bot Mode: ${this.botMode}`)
694
+ .catch(async (e: any) => {
695
+ await this.apiError(e)
696
+ await this.errorLog(`failed BLEpushChanges with ${this.device.connectionType} Connection, Error Message: ${JSON.stringify(e.message)}`)
697
+ await this.BLEPushConnection()
698
+ })
699
+ } else if (this.botMode === 'switch') {
700
+ switchbot
701
+ .discover({ model: this.device.bleModel, quick: true, id: this.device.bleMac })
702
+ .then(async (device_list: any) => {
703
+ this.infoLog(`On: ${this.On}`)
704
+ return await this.retryBLE({
705
+ max: await this.maxRetryBLE(),
706
+ fn: async () => {
707
+ if (this.On) {
708
+ return await device_list[0].turnOn({ id: this.device.bleMac })
709
+ } else {
710
+ return await device_list[0].turnOff({ id: this.device.bleMac })
711
+ }
712
+ },
713
+ })
714
+ })
715
+ .then(async () => {
716
+ await this.successLog(`On: ${this.On} sent over SwitchBot BLE, sent successfully`)
717
+ await this.updateHomeKitCharacteristics()
718
+ })
719
+ .catch(async (e: any) => {
720
+ await this.apiError(e)
721
+ await this.errorLog(`failed BLEpushChanges with ${this.device.connectionType} Connection, Error Message: ${JSON.stringify(e.message)}`)
722
+ await this.BLEPushConnection()
723
+ })
724
+ } else {
725
+ await this.errorLog(`Device Parameters not set for this Bot, please check the device configuration. Bot Mode: ${this.botMode}`)
726
+ }
727
+ } catch (error) {
728
+ await this.errorLog(`failed to format device ID as MAC, Error: ${error}`)
717
729
  }
718
730
  } else {
719
731
  await this.debugLog(`No Changes (BLEpushChanges), On: ${this.On} OnCached: ${this.accessory.context.On}`)
@@ -18,7 +18,7 @@ import type { ceilingLightProWebhookContext, ceilingLightWebhookContext } from '
18
18
  import { SwitchBotBLEModel, SwitchBotBLEModelName } from 'node-switchbot'
19
19
  import { debounceTime, interval, skipWhile, Subject, take, tap } from 'rxjs'
20
20
 
21
- import { hs2rgb, m2hs } from '../utils.js'
21
+ import { formatDeviceIdAsMac, hs2rgb, m2hs } from '../utils.js'
22
22
  import { deviceBase } from './device.js'
23
23
 
24
24
  /**
@@ -310,17 +310,22 @@ export class CeilingLight extends deviceBase {
310
310
  await this.debugLog('registerPlatformBLE')
311
311
  if (this.config.options?.BLE) {
312
312
  await this.debugLog('is listening to Platform BLE.')
313
- this.device.bleMac = this.device.deviceId!.match(/.{1,2}/g)!.join(':').toLowerCase()
314
- await this.debugLog(`bleMac: ${this.device.bleMac}`)
315
- this.platform.bleEventHandler[this.device.bleMac] = async (context: ceilingLightServiceData | ceilingLightProServiceData) => {
316
- try {
317
- await this.debugLog(`received BLE: ${JSON.stringify(context)}`)
318
- this.serviceData = context
319
- await this.BLEparseStatus()
320
- await this.updateHomeKitCharacteristics()
321
- } catch (e: any) {
322
- await this.errorLog(`failed to handle BLE. Received: ${JSON.stringify(context)} Error: ${e}`)
313
+ try {
314
+ const formattedDeviceId = formatDeviceIdAsMac(this.device.deviceId!)
315
+ this.device.bleMac = formattedDeviceId
316
+ await this.debugLog(`bleMac: ${this.device.bleMac}`)
317
+ this.platform.bleEventHandler[this.device.bleMac] = async (context: ceilingLightServiceData | ceilingLightProServiceData) => {
318
+ try {
319
+ await this.debugLog(`received BLE: ${JSON.stringify(context)}`)
320
+ this.serviceData = context
321
+ await this.BLEparseStatus()
322
+ await this.updateHomeKitCharacteristics()
323
+ } catch (e: any) {
324
+ await this.errorLog(`failed to handle BLE. Received: ${JSON.stringify(context)} Error: ${e}`)
325
+ }
323
326
  }
327
+ } catch (error) {
328
+ await this.errorLog(`failed to format device ID as MAC, Error: ${error}`)
324
329
  }
325
330
  } else {
326
331
  await this.debugLog('is not listening to Platform BLE')
@@ -416,35 +421,41 @@ export class CeilingLight extends deviceBase {
416
421
  if (this.LightBulb.On !== this.accessory.context.On) {
417
422
  await this.debugLog(`BLEpushChanges On: ${this.LightBulb.On} OnCached: ${this.accessory.context.On}`)
418
423
  const switchbot = await this.platform.connectBLE(this.accessory, this.device)
419
- await this.convertBLEAddress()
420
- if (switchbot !== false) {
421
- switchbot
422
- .discover({ model: this.device.bleModel, id: this.device.bleMac })
423
- .then(async (device_list: any) => {
424
- this.infoLog(`On: ${this.LightBulb.On}`)
425
- return await this.retryBLE({
426
- max: await this.maxRetryBLE(),
427
- fn: async () => {
428
- if (this.LightBulb.On) {
429
- return await device_list[0].turnOn({ id: this.device.bleMac })
430
- } else {
431
- return await device_list[0].turnOff({ id: this.device.bleMac })
432
- }
433
- },
424
+ try {
425
+ const formattedDeviceId = formatDeviceIdAsMac(this.device.deviceId!)
426
+ this.device.bleMac = formattedDeviceId
427
+ await this.debugLog(`bleMac: ${this.device.bleMac}`)
428
+ if (switchbot !== false) {
429
+ switchbot
430
+ .discover({ model: this.device.bleModel, id: this.device.bleMac })
431
+ .then(async (device_list: any) => {
432
+ this.infoLog(`On: ${this.LightBulb.On}`)
433
+ return await this.retryBLE({
434
+ max: await this.maxRetryBLE(),
435
+ fn: async () => {
436
+ if (this.LightBulb.On) {
437
+ return await device_list[0].turnOn({ id: this.device.bleMac })
438
+ } else {
439
+ return await device_list[0].turnOff({ id: this.device.bleMac })
440
+ }
441
+ },
442
+ })
434
443
  })
435
- })
436
- .then(async () => {
437
- await this.successLog(`On: ${this.LightBulb.On} sent over SwitchBot BLE, sent successfully`)
438
- this.LightBulb.On = false
439
- })
440
- .catch(async (e: any) => {
441
- await this.apiError(e)
442
- await this.errorLog(`failed BLEpushChanges with ${this.device.connectionType} Connection, Error Message: ${JSON.stringify(e.message)}`)
443
- await this.BLEPushConnection()
444
- })
445
- } else {
446
- await this.errorLog(`wasn't able to establish BLE Connection, node-switchbot: ${switchbot}`)
447
- await this.BLEPushConnection()
444
+ .then(async () => {
445
+ await this.successLog(`On: ${this.LightBulb.On} sent over SwitchBot BLE, sent successfully`)
446
+ this.LightBulb.On = false
447
+ })
448
+ .catch(async (e: any) => {
449
+ await this.apiError(e)
450
+ await this.errorLog(`failed BLEpushChanges with ${this.device.connectionType} Connection, Error Message: ${JSON.stringify(e.message)}`)
451
+ await this.BLEPushConnection()
452
+ })
453
+ } else {
454
+ await this.errorLog(`wasn't able to establish BLE Connection, node-switchbot: ${switchbot}`)
455
+ await this.BLEPushConnection()
456
+ }
457
+ } catch (error) {
458
+ await this.errorLog(`failed to format device ID as MAC, Error: ${error}`)
448
459
  }
449
460
  } else {
450
461
  await this.debugLog(`No changes (BLEpushChanges): On: ${this.LightBulb.On}, OnCached: ${this.accessory.context.On}`)
@@ -18,7 +18,7 @@ import type { colorBulbWebhookContext } from '../types/devicewebhookstatus.js'
18
18
  import { SwitchBotBLEModel, SwitchBotBLEModelName } from 'node-switchbot'
19
19
  import { debounceTime, interval, skipWhile, Subject, take, tap } from 'rxjs'
20
20
 
21
- import { hs2rgb, m2hs, rgb2hs } from '../utils.js'
21
+ import { formatDeviceIdAsMac, hs2rgb, m2hs, rgb2hs } from '../utils.js'
22
22
  import { deviceBase } from './device.js'
23
23
 
24
24
  /**
@@ -333,17 +333,22 @@ export class ColorBulb extends deviceBase {
333
333
  await this.debugLog('registerPlatformBLE')
334
334
  if (this.config.options?.BLE) {
335
335
  await this.debugLog('is listening to Platform BLE.')
336
- this.device.bleMac = this.device.deviceId!.match(/.{1,2}/g)!.join(':').toLowerCase()
337
- await this.debugLog(`bleMac: ${this.device.bleMac}`)
338
- this.platform.bleEventHandler[this.device.bleMac] = async (context: colorBulbServiceData) => {
339
- try {
340
- await this.debugLog(`received BLE: ${JSON.stringify(context)}`)
341
- this.serviceData = context
342
- await this.BLEparseStatus()
343
- await this.updateHomeKitCharacteristics()
344
- } catch (e: any) {
345
- await this.errorLog(`failed to handle BLE. Received: ${JSON.stringify(context)} Error: ${e}`)
336
+ try {
337
+ const formattedDeviceId = formatDeviceIdAsMac(this.device.deviceId!)
338
+ this.device.bleMac = formattedDeviceId
339
+ await this.debugLog(`bleMac: ${this.device.bleMac}`)
340
+ this.platform.bleEventHandler[this.device.bleMac] = async (context: colorBulbServiceData) => {
341
+ try {
342
+ await this.debugLog(`received BLE: ${JSON.stringify(context)}`)
343
+ this.serviceData = context
344
+ await this.BLEparseStatus()
345
+ await this.updateHomeKitCharacteristics()
346
+ } catch (e: any) {
347
+ await this.errorLog(`failed to handle BLE. Received: ${JSON.stringify(context)} Error: ${e}`)
348
+ }
346
349
  }
350
+ } catch (error) {
351
+ await this.errorLog(`failed to format device ID as MAC, Error: ${error}`)
347
352
  }
348
353
  } else {
349
354
  await this.debugLog('is not listening to Platform BLE')
@@ -451,35 +456,41 @@ export class ColorBulb extends deviceBase {
451
456
  if (this.LightBulb.On !== this.accessory.context.On) {
452
457
  await this.debugLog(`BLEpushChanges On: ${this.LightBulb.On}, OnCached: ${this.accessory.context.On}`)
453
458
  const switchbot = await this.platform.connectBLE(this.accessory, this.device)
454
- await this.convertBLEAddress()
455
- if (switchbot !== false) {
456
- switchbot
457
- .discover({ model: this.device.bleModel, id: this.device.bleMac })
458
- .then(async (device_list: any) => {
459
- await this.infoLog(`On: ${this.LightBulb.On}`)
460
- return await this.retryBLE({
461
- max: await this.maxRetryBLE(),
462
- fn: async () => {
463
- if (this.LightBulb.On) {
464
- return await device_list[0].turnOn({ id: this.device.bleMac })
465
- } else {
466
- return await device_list[0].turnOff({ id: this.device.bleMac })
467
- }
468
- },
459
+ try {
460
+ const formattedDeviceId = formatDeviceIdAsMac(this.device.deviceId!)
461
+ this.device.bleMac = formattedDeviceId
462
+ await this.debugLog(`bleMac: ${this.device.bleMac}`)
463
+ if (switchbot !== false) {
464
+ switchbot
465
+ .discover({ model: this.device.bleModel, id: this.device.bleMac })
466
+ .then(async (device_list: any) => {
467
+ await this.infoLog(`On: ${this.LightBulb.On}`)
468
+ return await this.retryBLE({
469
+ max: await this.maxRetryBLE(),
470
+ fn: async () => {
471
+ if (this.LightBulb.On) {
472
+ return await device_list[0].turnOn({ id: this.device.bleMac })
473
+ } else {
474
+ return await device_list[0].turnOff({ id: this.device.bleMac })
475
+ }
476
+ },
477
+ })
469
478
  })
470
- })
471
- .then(async () => {
472
- await this.successLog(`On: ${this.LightBulb.On} sent over SwitchBot BLE, sent successfully`)
473
- await this.updateHomeKitCharacteristics()
474
- })
475
- .catch(async (e: any) => {
476
- await this.apiError(e)
477
- await this.errorLog(`failed BLEpushChanges with ${this.device.connectionType} Connection, Error Message: ${JSON.stringify(e.message)}`)
478
- await this.BLEPushConnection()
479
- })
480
- } else {
481
- await this.errorLog(`wasn't able to establish BLE Connection, node-switchbot: ${switchbot}`)
482
- await this.BLEPushConnection()
479
+ .then(async () => {
480
+ await this.successLog(`On: ${this.LightBulb.On} sent over SwitchBot BLE, sent successfully`)
481
+ await this.updateHomeKitCharacteristics()
482
+ })
483
+ .catch(async (e: any) => {
484
+ await this.apiError(e)
485
+ await this.errorLog(`failed BLEpushChanges with ${this.device.connectionType} Connection, Error Message: ${JSON.stringify(e.message)}`)
486
+ await this.BLEPushConnection()
487
+ })
488
+ } else {
489
+ await this.errorLog(`wasn't able to establish BLE Connection, node-switchbot: ${switchbot}`)
490
+ await this.BLEPushConnection()
491
+ }
492
+ } catch (error) {
493
+ await this.errorLog(`failed to format device ID as MAC, Error: ${error}`)
483
494
  }
484
495
  } else {
485
496
  await this.debugLog(`No changes (BLEpushChanges), On: ${this.LightBulb.On}, OnCached: ${this.accessory.context.On}`)
@@ -490,26 +501,32 @@ export class ColorBulb extends deviceBase {
490
501
  await this.debugLog('BLEpushBrightnessChanges')
491
502
  if (this.LightBulb.Brightness !== this.accessory.context.Brightness) {
492
503
  const switchbot = await this.platform.connectBLE(this.accessory, this.device)
493
- await this.convertBLEAddress()
494
- if (switchbot !== false) {
495
- switchbot
496
- .discover({ model: this.device.bleModel, id: this.device.bleMac })
497
- .then(async (device_list: any) => {
498
- await this.infoLog(`Target Brightness: ${this.LightBulb.Brightness}`)
499
- return await device_list[0].setBrightness(this.LightBulb.Brightness)
500
- })
501
- .then(async () => {
502
- await this.successLog(`Brightness: ${this.LightBulb.Brightness} sent over SwitchBot BLE, sent successfully`)
503
- await this.updateHomeKitCharacteristics()
504
- })
505
- .catch(async (e: any) => {
506
- await this.apiError(e)
507
- await this.errorLog(`failed BLEpushChanges with ${this.device.connectionType} Connection, Error Message: ${JSON.stringify(e.message)}`)
508
- await this.BLEPushConnection()
509
- })
510
- } else {
511
- await this.errorLog(`wasn't able to establish BLE Connection, node-switchbot: ${switchbot}`)
512
- await this.BLEPushConnection()
504
+ try {
505
+ const formattedDeviceId = formatDeviceIdAsMac(this.device.deviceId!)
506
+ this.device.bleMac = formattedDeviceId
507
+ await this.debugLog(`bleMac: ${this.device.bleMac}`)
508
+ if (switchbot !== false) {
509
+ switchbot
510
+ .discover({ model: this.device.bleModel, id: this.device.bleMac })
511
+ .then(async (device_list: any) => {
512
+ await this.infoLog(`Target Brightness: ${this.LightBulb.Brightness}`)
513
+ return await device_list[0].setBrightness(this.LightBulb.Brightness)
514
+ })
515
+ .then(async () => {
516
+ await this.successLog(`Brightness: ${this.LightBulb.Brightness} sent over SwitchBot BLE, sent successfully`)
517
+ await this.updateHomeKitCharacteristics()
518
+ })
519
+ .catch(async (e: any) => {
520
+ await this.apiError(e)
521
+ await this.errorLog(`failed BLEpushChanges with ${this.device.connectionType} Connection, Error Message: ${JSON.stringify(e.message)}`)
522
+ await this.BLEPushConnection()
523
+ })
524
+ } else {
525
+ await this.errorLog(`wasn't able to establish BLE Connection, node-switchbot: ${switchbot}`)
526
+ await this.BLEPushConnection()
527
+ }
528
+ } catch (error) {
529
+ await this.errorLog(`failed to format device ID as MAC, Error: ${error}`)
513
530
  }
514
531
  } else {
515
532
  await this.debugLog(`No changes (BLEpushBrightnessChanges), Brightness: ${this.LightBulb.Brightness}, BrightnessCached: ${this.accessory.context.Brightness}`)
@@ -522,26 +539,32 @@ export class ColorBulb extends deviceBase {
522
539
  const kelvin = Math.round(1000000 / Number(this.LightBulb.ColorTemperature))
523
540
  this.accessory.context.kelvin = kelvin
524
541
  const switchbot = await this.platform.connectBLE(this.accessory, this.device)
525
- await this.convertBLEAddress()
526
- if (switchbot !== false) {
527
- switchbot
528
- .discover({ model: this.device.bleModel, id: this.device.bleMac })
529
- .then(async (device_list: any) => {
530
- await this.infoLog(`ColorTemperature: ${this.LightBulb.ColorTemperature}`)
531
- return await device_list[0].setColorTemperature(kelvin)
532
- })
533
- .then(async () => {
534
- await this.successLog(`ColorTemperature: ${this.LightBulb.ColorTemperature} sent over SwitchBot BLE, sent successfully`)
535
- await this.updateHomeKitCharacteristics()
536
- })
537
- .catch(async (e: any) => {
538
- await this.apiError(e)
539
- await this.errorLog(`failed BLEpushRGBChanges with ${this.device.connectionType} Connection, Error Message: ${JSON.stringify(e.message)}`)
540
- await this.BLEPushConnection()
541
- })
542
- } else {
543
- await this.errorLog(`wasn't able to establish BLE Connection, node-switchbot: ${switchbot}`)
544
- await this.BLEPushConnection()
542
+ try {
543
+ const formattedDeviceId = formatDeviceIdAsMac(this.device.deviceId!)
544
+ this.device.bleMac = formattedDeviceId
545
+ await this.debugLog(`bleMac: ${this.device.bleMac}`)
546
+ if (switchbot !== false) {
547
+ switchbot
548
+ .discover({ model: this.device.bleModel, id: this.device.bleMac })
549
+ .then(async (device_list: any) => {
550
+ await this.infoLog(`ColorTemperature: ${this.LightBulb.ColorTemperature}`)
551
+ return await device_list[0].setColorTemperature(kelvin)
552
+ })
553
+ .then(async () => {
554
+ await this.successLog(`ColorTemperature: ${this.LightBulb.ColorTemperature} sent over SwitchBot BLE, sent successfully`)
555
+ await this.updateHomeKitCharacteristics()
556
+ })
557
+ .catch(async (e: any) => {
558
+ await this.apiError(e)
559
+ await this.errorLog(`failed BLEpushRGBChanges with ${this.device.connectionType} Connection, Error Message: ${JSON.stringify(e.message)}`)
560
+ await this.BLEPushConnection()
561
+ })
562
+ } else {
563
+ await this.errorLog(`wasn't able to establish BLE Connection, node-switchbot: ${switchbot}`)
564
+ await this.BLEPushConnection()
565
+ }
566
+ } catch (error) {
567
+ await this.errorLog(`failed to format device ID as MAC, Error: ${error}`)
545
568
  }
546
569
  } else {
547
570
  await this.debugLog(`No changes (BLEpushColorTemperatureChanges), ColorTemperature: ${this.LightBulb.ColorTemperature}, ColorTemperatureCached: ${this.accessory.context.ColorTemperature}`)
@@ -555,26 +578,32 @@ export class ColorBulb extends deviceBase {
555
578
  const [red, green, blue] = hs2rgb(this.LightBulb.Hue, this.LightBulb.Saturation)
556
579
  await this.debugLog(`rgb: ${JSON.stringify([red, green, blue])}`)
557
580
  const switchbot = await this.platform.connectBLE(this.accessory, this.device)
558
- await this.convertBLEAddress()
559
- if (switchbot !== false) {
560
- switchbot
561
- .discover({ model: this.device.bleModel, id: this.device.bleMac })
562
- .then(async (device_list: any) => {
563
- await this.infoLog(`RGB: ${(this.LightBulb.Brightness, red, green, blue)}`)
564
- return await device_list[0].setRGB(this.LightBulb.Brightness, red, green, blue)
565
- })
566
- .then(async () => {
567
- await this.successLog(`RGB: ${(this.LightBulb.Brightness, red, green, blue)} sent over SwitchBot BLE, sent successfully`)
568
- await this.updateHomeKitCharacteristics()
569
- })
570
- .catch(async (e: any) => {
571
- await this.apiError(e)
572
- await this.errorLog(`failed BLEpushRGBChanges with ${this.device.connectionType} Connection, Error Message: ${JSON.stringify(e.message)}`)
573
- await this.BLEPushConnection()
574
- })
575
- } else {
576
- await this.errorLog(`wasn't able to establish BLE Connection, node-switchbot: ${switchbot}`)
577
- await this.BLEPushConnection()
581
+ try {
582
+ const formattedDeviceId = formatDeviceIdAsMac(this.device.deviceId!)
583
+ this.device.bleMac = formattedDeviceId
584
+ await this.debugLog(`bleMac: ${this.device.bleMac}`)
585
+ if (switchbot !== false) {
586
+ switchbot
587
+ .discover({ model: this.device.bleModel, id: this.device.bleMac })
588
+ .then(async (device_list: any) => {
589
+ await this.infoLog(`RGB: ${(this.LightBulb.Brightness, red, green, blue)}`)
590
+ return await device_list[0].setRGB(this.LightBulb.Brightness, red, green, blue)
591
+ })
592
+ .then(async () => {
593
+ await this.successLog(`RGB: ${(this.LightBulb.Brightness, red, green, blue)} sent over SwitchBot BLE, sent successfully`)
594
+ await this.updateHomeKitCharacteristics()
595
+ })
596
+ .catch(async (e: any) => {
597
+ await this.apiError(e)
598
+ await this.errorLog(`failed BLEpushRGBChanges with ${this.device.connectionType} Connection, Error Message: ${JSON.stringify(e.message)}`)
599
+ await this.BLEPushConnection()
600
+ })
601
+ } else {
602
+ await this.errorLog(`wasn't able to establish BLE Connection, node-switchbot: ${switchbot}`)
603
+ await this.BLEPushConnection()
604
+ }
605
+ } catch (error) {
606
+ await this.errorLog(`failed to format device ID as MAC, Error: ${error}`)
578
607
  }
579
608
  } else {
580
609
  await this.debugLog(`No changes (BLEpushRGBChanges), Hue: ${this.LightBulb.Hue}, HueCached: ${this.accessory.context.Hue}, Saturation: ${this.LightBulb.Saturation}, SaturationCached: ${this.accessory.context.Saturation}`)
@@ -18,6 +18,7 @@ import type { contactSensorWebhookContext } from '../types/devicewebhookstatus.j
18
18
  import { SwitchBotBLEModel, SwitchBotBLEModelName } from 'node-switchbot'
19
19
  import { interval, skipWhile, Subject } from 'rxjs'
20
20
 
21
+ import { formatDeviceIdAsMac } from '../utils.js'
21
22
  import { deviceBase } from './device.js'
22
23
 
23
24
  /**
@@ -326,17 +327,22 @@ export class Contact extends deviceBase {
326
327
  await this.debugLog('registerPlatformBLE')
327
328
  if (this.config.options?.BLE) {
328
329
  await this.debugLog('is listening to Platform BLE.')
329
- this.device.bleMac = this.device.deviceId!.match(/.{1,2}/g)!.join(':').toLowerCase()
330
- await this.debugLog(`bleMac: ${this.device.bleMac}`)
331
- this.platform.bleEventHandler[this.device.bleMac] = async (context: contactSensorServiceData) => {
332
- try {
333
- await this.debugLog(`received BLE: ${JSON.stringify(context)}`)
334
- this.serviceData = context
335
- await this.BLEparseStatus()
336
- await this.updateHomeKitCharacteristics()
337
- } catch (e: any) {
338
- await this.errorLog(`failed to handle BLE. Received: ${JSON.stringify(context)} Error: ${e}`)
330
+ try {
331
+ const formattedDeviceId = formatDeviceIdAsMac(this.device.deviceId!)
332
+ this.device.bleMac = formattedDeviceId
333
+ await this.debugLog(`bleMac: ${this.device.bleMac}`)
334
+ this.platform.bleEventHandler[this.device.bleMac] = async (context: contactSensorServiceData) => {
335
+ try {
336
+ await this.debugLog(`received BLE: ${JSON.stringify(context)}`)
337
+ this.serviceData = context
338
+ await this.BLEparseStatus()
339
+ await this.updateHomeKitCharacteristics()
340
+ } catch (e: any) {
341
+ await this.errorLog(`failed to handle BLE. Received: ${JSON.stringify(context)} Error: ${e}`)
342
+ }
339
343
  }
344
+ } catch (error) {
345
+ await this.errorLog(`failed to format device ID as MAC, Error: ${error}`)
340
346
  }
341
347
  } else {
342
348
  await this.debugLog('is not listening to Platform BLE')