@switchbot/homebridge-switchbot 3.8.1 → 3.8.2-beta.1

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 (89) hide show
  1. package/.github/npm-version-script.cjs +81 -0
  2. package/CHANGELOG.md +8 -0
  3. package/dist/device/blindtilt.d.ts.map +1 -1
  4. package/dist/device/blindtilt.js +51 -38
  5. package/dist/device/blindtilt.js.map +1 -1
  6. package/dist/device/bot.d.ts.map +1 -1
  7. package/dist/device/bot.js +78 -64
  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 +55 -42
  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 +133 -99
  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 +20 -13
  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 +88 -70
  20. package/dist/device/curtain.js.map +1 -1
  21. package/dist/device/device.d.ts +0 -1
  22. package/dist/device/device.d.ts.map +1 -1
  23. package/dist/device/device.js +26 -22
  24. package/dist/device/device.js.map +1 -1
  25. package/dist/device/fan.d.ts.map +1 -1
  26. package/dist/device/fan.js +54 -40
  27. package/dist/device/fan.js.map +1 -1
  28. package/dist/device/hub.d.ts.map +1 -1
  29. package/dist/device/hub.js +20 -14
  30. package/dist/device/hub.js.map +1 -1
  31. package/dist/device/humidifier.d.ts.map +1 -1
  32. package/dist/device/humidifier.js +45 -32
  33. package/dist/device/humidifier.js.map +1 -1
  34. package/dist/device/iosensor.d.ts.map +1 -1
  35. package/dist/device/iosensor.js +20 -14
  36. package/dist/device/iosensor.js.map +1 -1
  37. package/dist/device/lightstrip.d.ts.map +1 -1
  38. package/dist/device/lightstrip.js +107 -80
  39. package/dist/device/lightstrip.js.map +1 -1
  40. package/dist/device/lock.d.ts.map +1 -1
  41. package/dist/device/lock.js +54 -40
  42. package/dist/device/lock.js.map +1 -1
  43. package/dist/device/meter.d.ts.map +1 -1
  44. package/dist/device/meter.js +20 -14
  45. package/dist/device/meter.js.map +1 -1
  46. package/dist/device/meterplus.d.ts.map +1 -1
  47. package/dist/device/meterplus.js +20 -14
  48. package/dist/device/meterplus.js.map +1 -1
  49. package/dist/device/motion.d.ts.map +1 -1
  50. package/dist/device/motion.js +20 -13
  51. package/dist/device/motion.js.map +1 -1
  52. package/dist/device/plug.d.ts.map +1 -1
  53. package/dist/device/plug.js +55 -41
  54. package/dist/device/plug.js.map +1 -1
  55. package/dist/device/robotvacuumcleaner.d.ts.map +1 -1
  56. package/dist/device/robotvacuumcleaner.js +55 -41
  57. package/dist/device/robotvacuumcleaner.js.map +1 -1
  58. package/dist/device/waterdetector.d.ts.map +1 -1
  59. package/dist/device/waterdetector.js +20 -13
  60. package/dist/device/waterdetector.js.map +1 -1
  61. package/dist/platform.d.ts.map +1 -1
  62. package/dist/platform.js +10 -3
  63. package/dist/platform.js.map +1 -1
  64. package/dist/utils.d.ts +10 -0
  65. package/dist/utils.d.ts.map +1 -1
  66. package/dist/utils.js +27 -0
  67. package/dist/utils.js.map +1 -1
  68. package/package.json +4 -4
  69. package/src/device/blindtilt.ts +47 -36
  70. package/src/device/bot.ts +71 -59
  71. package/src/device/ceilinglight.ts +50 -39
  72. package/src/device/colorbulb.ts +128 -99
  73. package/src/device/contact.ts +16 -10
  74. package/src/device/curtain.ts +84 -69
  75. package/src/device/device.ts +24 -23
  76. package/src/device/fan.ts +49 -37
  77. package/src/device/hub.ts +16 -11
  78. package/src/device/humidifier.ts +41 -30
  79. package/src/device/iosensor.ts +16 -11
  80. package/src/device/lightstrip.ts +102 -79
  81. package/src/device/lock.ts +49 -37
  82. package/src/device/meter.ts +16 -11
  83. package/src/device/meterplus.ts +16 -11
  84. package/src/device/motion.ts +16 -10
  85. package/src/device/plug.ts +50 -38
  86. package/src/device/robotvacuumcleaner.ts +50 -38
  87. package/src/device/waterdetector.ts +16 -10
  88. package/src/platform.ts +9 -3
  89. package/src/utils.ts +33 -0
@@ -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')
@@ -20,6 +20,7 @@ import { hostname } from 'node:os'
20
20
  import { SwitchBotBLEModel, SwitchBotBLEModelName } from 'node-switchbot'
21
21
  import { debounceTime, interval, skipWhile, Subject, take, tap } from 'rxjs'
22
22
 
23
+ import { formatDeviceIdAsMac } from '../utils.js'
23
24
  import { deviceBase } from './device.js'
24
25
 
25
26
  export class Curtain extends deviceBase {
@@ -324,42 +325,45 @@ export class Curtain extends deviceBase {
324
325
  return
325
326
  }
326
327
 
327
- const mac = this.device
328
- .deviceId!.match(/.{1,2}/g)!
329
- .join(':')
330
- .toLowerCase()
331
- this.historyService = new this.platform.fakegatoAPI('custom', this.accessory, {
332
- log: this.platform.log,
333
- storage: 'fs',
334
- filename: `${hostname().split('.')[0]}_${mac}_persist.json`,
335
- })
336
- const motion: Service
328
+ try {
329
+ const formattedDeviceId = formatDeviceIdAsMac(this.device.deviceId)
330
+ this.device.bleMac = formattedDeviceId
331
+ await this.debugLog(`bleMac: ${this.device.bleMac}`)
332
+ this.historyService = new this.platform.fakegatoAPI('custom', this.accessory, {
333
+ log: this.platform.log,
334
+ storage: 'fs',
335
+ filename: `${hostname().split('.')[0]}_${this.device.bleMac}_persist.json`,
336
+ })
337
+ const motion: Service
337
338
  = this.accessory.getService(this.hap.Service.MotionSensor)
338
339
  || this.accessory.addService(this.hap.Service.MotionSensor, 'Motion')
339
- motion.addOptionalCharacteristic(this.platform.eve.Characteristics.LastActivation)
340
- motion.getCharacteristic(this.platform.eve.Characteristics.LastActivation).onGet(() => {
341
- const lastActivation = this.accessory.context.lastActivation
342
- ? Math.max(0, this.accessory.context.lastActivation - this.historyService.getInitialTime())
343
- : 0
344
- return lastActivation
345
- })
346
- await this.setMinMax()
347
- motion.getCharacteristic(this.hap.Characteristic.MotionDetected).on('change', (event: CharacteristicChange) => {
348
- if (event.newValue !== event.oldValue) {
349
- const sensor = this.accessory.getService(this.hap.Service.MotionSensor)
350
- const entry = {
351
- time: Math.round(new Date().valueOf() / 1000),
352
- motion: event.newValue,
340
+ motion.addOptionalCharacteristic(this.platform.eve.Characteristics.LastActivation)
341
+ motion.getCharacteristic(this.platform.eve.Characteristics.LastActivation).onGet(() => {
342
+ const lastActivation = this.accessory.context.lastActivation
343
+ ? Math.max(0, this.accessory.context.lastActivation - this.historyService.getInitialTime())
344
+ : 0
345
+ return lastActivation
346
+ })
347
+ await this.setMinMax()
348
+ motion.getCharacteristic(this.hap.Characteristic.MotionDetected).on('change', (event: CharacteristicChange) => {
349
+ if (event.newValue !== event.oldValue) {
350
+ const sensor = this.accessory.getService(this.hap.Service.MotionSensor)
351
+ const entry = {
352
+ time: Math.round(new Date().valueOf() / 1000),
353
+ motion: event.newValue,
354
+ }
355
+ this.accessory.context.lastActivation = entry.time
356
+ sensor?.updateCharacteristic(
357
+ this.platform.eve.Characteristics.LastActivation,
358
+ Math.max(0, this.accessory.context.lastActivation - this.historyService.getInitialTime()),
359
+ )
360
+ this.historyService.addEntry(entry)
353
361
  }
354
- this.accessory.context.lastActivation = entry.time
355
- sensor?.updateCharacteristic(
356
- this.platform.eve.Characteristics.LastActivation,
357
- Math.max(0, this.accessory.context.lastActivation - this.historyService.getInitialTime()),
358
- )
359
- this.historyService.addEntry(entry)
360
- }
361
- })
362
- this.updateHistory()
362
+ })
363
+ this.updateHistory()
364
+ } catch (error) {
365
+ await this.errorLog(`failed to format device ID as MAC, Error: ${error}`)
366
+ }
363
367
  }
364
368
 
365
369
  async updateHistory(): Promise<void> {
@@ -541,17 +545,22 @@ export class Curtain extends deviceBase {
541
545
  await this.debugLog('registerPlatformBLE')
542
546
  if (this.config.options?.BLE) {
543
547
  await this.debugLog('is listening to Platform BLE.')
544
- this.device.bleMac = this.device.deviceId!.match(/.{1,2}/g)!.join(':').toLowerCase()
545
- await this.debugLog(`bleMac: ${this.device.bleMac}`)
546
- this.platform.bleEventHandler[this.device.bleMac] = async (context: curtainServiceData | curtain3ServiceData) => {
547
- try {
548
- await this.debugLog(`received BLE: ${JSON.stringify(context)}`)
549
- this.serviceData = context
550
- await this.BLEparseStatus()
551
- await this.updateHomeKitCharacteristics()
552
- } catch (e: any) {
553
- await this.errorLog(`failed to handle BLE. Received: ${JSON.stringify(context)} Error: ${e}`)
548
+ try {
549
+ const formattedDeviceId = formatDeviceIdAsMac(this.device.deviceId)
550
+ this.device.bleMac = formattedDeviceId
551
+ await this.debugLog(`bleMac: ${this.device.bleMac}`)
552
+ this.platform.bleEventHandler[this.device.bleMac] = async (context: curtainServiceData | curtain3ServiceData) => {
553
+ try {
554
+ await this.debugLog(`received BLE: ${JSON.stringify(context)}`)
555
+ this.serviceData = context
556
+ await this.BLEparseStatus()
557
+ await this.updateHomeKitCharacteristics()
558
+ } catch (e: any) {
559
+ await this.errorLog(`failed to handle BLE. Received: ${JSON.stringify(context)} Error: ${e}`)
560
+ }
554
561
  }
562
+ } catch (error) {
563
+ await this.errorLog(`failed to format device ID as MAC, Error: ${error}`)
555
564
  }
556
565
  } else {
557
566
  await this.debugLog('is not listening to Platform BLE')
@@ -585,33 +594,39 @@ export class Curtain extends deviceBase {
585
594
  await this.debugLog('BLEpushChanges')
586
595
  if (this.WindowCovering.TargetPosition !== this.WindowCovering.CurrentPosition) {
587
596
  const switchbot = await this.platform.connectBLE(this.accessory, this.device)
588
- await this.convertBLEAddress()
589
- const { setPositionMode, Mode }: { setPositionMode: number, Mode: string } = await this.setPerformance()
590
- const adjustedMode = setPositionMode === 1 ? 0x01 : 0xFF
591
- await this.debugLog(`Mode: ${Mode}, setPositionMode: ${setPositionMode}`)
592
- if (switchbot !== false) {
593
- switchbot
594
- .discover({ model: this.device.bleModel, quick: true, id: this.device.bleMac })
595
- .then(async (device_list: any) => {
596
- return await this.retryBLE({
597
- max: await this.maxRetryBLE(),
598
- fn: async () => {
599
- return await device_list[0].runToPos(100 - Number(this.WindowCovering.TargetPosition), adjustedMode)
600
- },
597
+ try {
598
+ const formattedDeviceId = formatDeviceIdAsMac(this.device.deviceId)
599
+ this.device.bleMac = formattedDeviceId
600
+ await this.debugLog(`bleMac: ${this.device.bleMac}`)
601
+ const { setPositionMode, Mode }: { setPositionMode: number, Mode: string } = await this.setPerformance()
602
+ const adjustedMode = setPositionMode === 1 ? 0x01 : 0xFF
603
+ await this.debugLog(`Mode: ${Mode}, setPositionMode: ${setPositionMode}`)
604
+ if (switchbot !== false) {
605
+ switchbot
606
+ .discover({ model: this.device.bleModel, quick: true, id: this.device.bleMac })
607
+ .then(async (device_list: any) => {
608
+ return await this.retryBLE({
609
+ max: await this.maxRetryBLE(),
610
+ fn: async () => {
611
+ return await device_list[0].runToPos(100 - Number(this.WindowCovering.TargetPosition), adjustedMode)
612
+ },
613
+ })
601
614
  })
602
- })
603
- .then(async () => {
604
- await this.successLog(`TargetPostion: ${this.WindowCovering.TargetPosition} sent over SwitchBot BLE, sent successfully`)
605
- await this.updateHomeKitCharacteristics()
606
- })
607
- .catch(async (e: any) => {
608
- await this.apiError(e)
609
- await this.errorLog(`failed BLEpushChanges with ${this.device.connectionType} Connection, Error Message: ${JSON.stringify(e.message)}`)
610
- await this.BLEPushConnection()
611
- })
612
- } else {
613
- await this.errorLog(`wasn't able to establish BLE Connection, node-switchbot: ${switchbot}`)
614
- await this.BLEPushConnection()
615
+ .then(async () => {
616
+ await this.successLog(`TargetPostion: ${this.WindowCovering.TargetPosition} sent over SwitchBot BLE, sent successfully`)
617
+ await this.updateHomeKitCharacteristics()
618
+ })
619
+ .catch(async (e: any) => {
620
+ await this.apiError(e)
621
+ await this.errorLog(`failed BLEpushChanges with ${this.device.connectionType} Connection, Error Message: ${JSON.stringify(e.message)}`)
622
+ await this.BLEPushConnection()
623
+ })
624
+ } else {
625
+ await this.errorLog(`wasn't able to establish BLE Connection, node-switchbot: ${switchbot}`)
626
+ await this.BLEPushConnection()
627
+ }
628
+ } catch (error) {
629
+ await this.errorLog(`failed to format device ID as MAC, Error: ${error}`)
615
630
  }
616
631
  } else {
617
632
  await this.debugLog(`No changes (BLEpushChanges), TargetPosition: ${this.WindowCovering.TargetPosition}, CurrentPosition: ${this.WindowCovering.CurrentPosition}`)
@@ -17,7 +17,7 @@ import { SwitchBotBLEModel, SwitchBotBLEModelFriendlyName, SwitchBotBLEModelName
17
17
  import { request } from 'undici'
18
18
 
19
19
  import { Devices } from '../settings.js'
20
- import { BlindTiltMappingMode, sleep } from '../utils.js'
20
+ import { BlindTiltMappingMode, formatDeviceIdAsMac, sleep } from '../utils.js'
21
21
 
22
22
  export abstract class deviceBase {
23
23
  public readonly api: API
@@ -261,33 +261,34 @@ export abstract class deviceBase {
261
261
  * Setup EVE history graph feature if enabled.
262
262
  */
263
263
  async setupHistoryService(accessory: PlatformAccessory, device: device & devicesConfig): Promise<void> {
264
- const mac = this.device
265
- .deviceId!.match(/.{1,2}/g)!
266
- .join(':')
267
- .toLowerCase()
268
- this.historyService = device.history
269
- ? new this.platform.fakegatoAPI('room', accessory, {
270
- log: this.platform.log,
271
- storage: 'fs',
272
- filename: `${hostname().split('.')[0]}_${mac}_persist.json`,
273
- })
274
- : null
264
+ try {
265
+ const formattedDeviceId = formatDeviceIdAsMac(this.device.deviceId)
266
+ this.device.bleMac = formattedDeviceId
267
+ await this.debugLog(`bleMac: ${this.device.bleMac}`)
268
+ this.historyService = device.history
269
+ ? new this.platform.fakegatoAPI('room', accessory, {
270
+ log: this.platform.log,
271
+ storage: 'fs',
272
+ filename: `${hostname().split('.')[0]}_${this.device.bleMac}_persist.json`,
273
+ })
274
+ : null
275
+ } catch (error) {
276
+ await this.errorLog(`failed to format device ID as MAC, Error: ${error}`)
277
+ }
275
278
  }
276
279
 
277
280
  async switchbotBLE(): Promise<any> {
278
281
  const switchbot = await this.platform.connectBLE(this.accessory, this.device)
279
282
  // Convert to BLE Address
280
- await this.convertBLEAddress()
281
- await this.getCustomBLEAddress(switchbot)
282
- return switchbot
283
- }
284
-
285
- async convertBLEAddress() {
286
- this.device.bleMac = this.device
287
- .deviceId!.match(/.{1,2}/g)!
288
- .join(':')
289
- .toLowerCase()
290
- await this.debugLog(`BLE Address: ${this.device.bleMac}`)
283
+ try {
284
+ const formattedDeviceId = formatDeviceIdAsMac(this.device.deviceId)
285
+ this.device.bleMac = formattedDeviceId
286
+ await this.debugLog(`bleMac: ${this.device.bleMac}`)
287
+ await this.getCustomBLEAddress(switchbot)
288
+ return switchbot
289
+ } catch (error) {
290
+ await this.errorLog(`failed to format device ID as MAC, Error: ${error}`)
291
+ }
291
292
  }
292
293
 
293
294
  async monitorAdvertisementPackets(switchbot: any) {