@switchbot/homebridge-switchbot 3.8.1 → 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 (85) 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/utils.d.ts +8 -0
  62. package/dist/utils.d.ts.map +1 -1
  63. package/dist/utils.js +23 -0
  64. package/dist/utils.js.map +1 -1
  65. package/package.json +4 -4
  66. package/src/device/blindtilt.ts +47 -36
  67. package/src/device/bot.ts +71 -59
  68. package/src/device/ceilinglight.ts +50 -39
  69. package/src/device/colorbulb.ts +128 -99
  70. package/src/device/contact.ts +16 -10
  71. package/src/device/curtain.ts +84 -69
  72. package/src/device/device.ts +24 -23
  73. package/src/device/fan.ts +49 -37
  74. package/src/device/hub.ts +16 -11
  75. package/src/device/humidifier.ts +41 -30
  76. package/src/device/iosensor.ts +16 -11
  77. package/src/device/lightstrip.ts +102 -79
  78. package/src/device/lock.ts +49 -37
  79. package/src/device/meter.ts +16 -11
  80. package/src/device/meterplus.ts +16 -11
  81. package/src/device/motion.ts +16 -10
  82. package/src/device/plug.ts +50 -38
  83. package/src/device/robotvacuumcleaner.ts +50 -38
  84. package/src/device/waterdetector.ts +16 -10
  85. package/src/utils.ts +29 -0
@@ -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) {
package/src/device/fan.ts CHANGED
@@ -18,6 +18,7 @@ import type { batteryCirculatorFanWebhookContext } from '../types/devicewebhooks
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
  export class Fan extends deviceBase {
@@ -346,17 +347,22 @@ export class Fan extends deviceBase {
346
347
  await this.debugLog('registerPlatformBLE')
347
348
  if (this.config.options?.BLE) {
348
349
  await this.debugLog('is listening to Platform BLE.')
349
- this.device.bleMac = this.device.deviceId!.match(/.{1,2}/g)!.join(':').toLowerCase()
350
- await this.debugLog(`bleMac: ${this.device.bleMac}`)
351
- this.platform.bleEventHandler[this.device.bleMac] = async (context: batteryCirculatorFanServiceData) => {
352
- try {
353
- await this.debugLog(`received BLE: ${JSON.stringify(context)}`)
354
- this.serviceData = context
355
- await this.BLEparseStatus()
356
- await this.updateHomeKitCharacteristics()
357
- } catch (e: any) {
358
- await this.errorLog(`failed to handle BLE. Received: ${JSON.stringify(context)} Error: ${e}`)
350
+ try {
351
+ const formattedDeviceId = formatDeviceIdAsMac(this.device.deviceId!)
352
+ this.device.bleMac = formattedDeviceId
353
+ await this.debugLog(`bleMac: ${this.device.bleMac}`)
354
+ this.platform.bleEventHandler[this.device.bleMac] = async (context: batteryCirculatorFanServiceData) => {
355
+ try {
356
+ await this.debugLog(`received BLE: ${JSON.stringify(context)}`)
357
+ this.serviceData = context
358
+ await this.BLEparseStatus()
359
+ await this.updateHomeKitCharacteristics()
360
+ } catch (e: any) {
361
+ await this.errorLog(`failed to handle BLE. Received: ${JSON.stringify(context)} Error: ${e}`)
362
+ }
359
363
  }
364
+ } catch (error) {
365
+ await this.errorLog(`failed to format device ID as MAC, Error: ${error}`)
360
366
  }
361
367
  } else {
362
368
  await this.debugLog('is not listening to Platform BLE')
@@ -448,34 +454,40 @@ export class Fan extends deviceBase {
448
454
  if (this.Fan.Active !== this.accessory.context.Active) {
449
455
  await this.debugLog(`BLEpushChanges On: ${this.Fan.Active} OnCached: ${this.accessory.context.Active}`)
450
456
  const switchbot = await this.platform.connectBLE(this.accessory, this.device)
451
- await this.convertBLEAddress()
452
- if (switchbot !== false) {
453
- switchbot
454
- .discover({ model: this.device.bleModel, id: this.device.bleMac })
455
- .then(async (device_list: any) => {
456
- return await this.retryBLE({
457
- max: await this.maxRetryBLE(),
458
- fn: async () => {
459
- if (this.Fan.Active) {
460
- return await device_list[0].turnOn({ id: this.device.bleMac })
461
- } else {
462
- return await device_list[0].turnOff({ id: this.device.bleMac })
463
- }
464
- },
457
+ try {
458
+ const formattedDeviceId = formatDeviceIdAsMac(this.device.deviceId!)
459
+ this.device.bleMac = formattedDeviceId
460
+ await this.debugLog(`bleMac: ${this.device.bleMac}`)
461
+ if (switchbot !== false) {
462
+ switchbot
463
+ .discover({ model: this.device.bleModel, id: this.device.bleMac })
464
+ .then(async (device_list: any) => {
465
+ return await this.retryBLE({
466
+ max: await this.maxRetryBLE(),
467
+ fn: async () => {
468
+ if (this.Fan.Active) {
469
+ return await device_list[0].turnOn({ id: this.device.bleMac })
470
+ } else {
471
+ return await device_list[0].turnOff({ id: this.device.bleMac })
472
+ }
473
+ },
474
+ })
465
475
  })
466
- })
467
- .then(async () => {
468
- await this.successLog(`Active: ${this.Fan.Active} sent over SwitchBot BLE, sent successfully`)
469
- await this.updateHomeKitCharacteristics()
470
- })
471
- .catch(async (e: any) => {
472
- await this.apiError(e)
473
- await this.errorLog(`failed BLEpushChanges with ${this.device.connectionType} Connection, Error Message: ${JSON.stringify(e.message)}`)
474
- await this.BLEPushConnection()
475
- })
476
- } else {
477
- await this.errorLog(`wasn't able to establish BLE Connection, node-switchbot: ${switchbot}`)
478
- await this.BLEPushConnection()
476
+ .then(async () => {
477
+ await this.successLog(`Active: ${this.Fan.Active} sent over SwitchBot BLE, sent successfully`)
478
+ await this.updateHomeKitCharacteristics()
479
+ })
480
+ .catch(async (e: any) => {
481
+ await this.apiError(e)
482
+ await this.errorLog(`failed BLEpushChanges with ${this.device.connectionType} Connection, Error Message: ${JSON.stringify(e.message)}`)
483
+ await this.BLEPushConnection()
484
+ })
485
+ } else {
486
+ await this.errorLog(`wasn't able to establish BLE Connection, node-switchbot: ${switchbot}`)
487
+ await this.BLEPushConnection()
488
+ }
489
+ } catch (error) {
490
+ await this.errorLog(`failed to format device ID as MAC, Error: ${error}`)
479
491
  }
480
492
  } else {
481
493
  await this.debugLog(`No change (BLEpushChanges), Active: ${this.Fan.Active}, ActiveCached: ${this.accessory.context.Active}`)
package/src/device/hub.ts CHANGED
@@ -19,7 +19,7 @@ import { Units } from 'homebridge'
19
19
  import { SwitchBotBLEModel, SwitchBotBLEModelName } from 'node-switchbot'
20
20
  import { interval, skipWhile, Subject } from 'rxjs'
21
21
 
22
- import { convertUnits, validHumidity } from '../utils.js'
22
+ import { convertUnits, formatDeviceIdAsMac, validHumidity } from '../utils.js'
23
23
  import { deviceBase } from './device.js'
24
24
 
25
25
  export class Hub extends deviceBase {
@@ -315,17 +315,22 @@ export class Hub extends deviceBase {
315
315
  await this.debugLog('registerPlatformBLE')
316
316
  if (this.config.options?.BLE) {
317
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: hub2ServiceData) => {
321
- try {
322
- await this.debugLog(`received BLE: ${JSON.stringify(context)}`)
323
- this.serviceData = context
324
- await this.BLEparseStatus()
325
- await this.updateHomeKitCharacteristics()
326
- } catch (e: any) {
327
- await this.errorLog(`failed to handle BLE. Received: ${JSON.stringify(context)} Error: ${e}`)
318
+ try {
319
+ const formattedDeviceId = formatDeviceIdAsMac(this.device.deviceId!)
320
+ this.device.bleMac = formattedDeviceId
321
+ await this.debugLog(`bleMac: ${this.device.bleMac}`)
322
+ this.platform.bleEventHandler[this.device.bleMac] = async (context: hub2ServiceData) => {
323
+ try {
324
+ await this.debugLog(`received BLE: ${JSON.stringify(context)}`)
325
+ this.serviceData = context
326
+ await this.BLEparseStatus()
327
+ await this.updateHomeKitCharacteristics()
328
+ } catch (e: any) {
329
+ await this.errorLog(`failed to handle BLE. Received: ${JSON.stringify(context)} Error: ${e}`)
330
+ }
328
331
  }
332
+ } catch (error) {
333
+ await this.errorLog(`failed to format device ID as MAC, Error: ${error}`)
329
334
  }
330
335
  } else {
331
336
  await this.debugLog('is not listening to Platform BLE')
@@ -18,7 +18,7 @@ import type { humidifierWebhookContext } 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 { convertUnits, validHumidity } from '../utils.js'
21
+ import { convertUnits, formatDeviceIdAsMac, validHumidity } from '../utils.js'
22
22
  import { deviceBase } from './device.js'
23
23
 
24
24
  /**
@@ -345,17 +345,22 @@ export class Humidifier extends deviceBase {
345
345
  await this.debugLog('registerPlatformBLE')
346
346
  if (this.config.options?.BLE) {
347
347
  await this.debugLog('is listening to Platform BLE.')
348
- this.device.bleMac = this.device.deviceId!.match(/.{1,2}/g)!.join(':').toLowerCase()
349
- await this.debugLog(`bleMac: ${this.device.bleMac}`)
350
- this.platform.bleEventHandler[this.device.bleMac] = async (context: humidifierServiceData) => {
351
- try {
352
- await this.debugLog(`received BLE: ${JSON.stringify(context)}`)
353
- this.serviceData = context
354
- await this.BLEparseStatus()
355
- await this.updateHomeKitCharacteristics()
356
- } catch (e: any) {
357
- await this.errorLog(`failed to handle BLE. Received: ${JSON.stringify(context)} Error: ${e}`)
348
+ try {
349
+ const formattedDeviceId = formatDeviceIdAsMac(this.device.deviceId!)
350
+ this.device.bleMac = formattedDeviceId
351
+ await this.debugLog(`bleMac: ${this.device.bleMac}`)
352
+ this.platform.bleEventHandler[this.device.bleMac] = async (context: humidifierServiceData) => {
353
+ try {
354
+ await this.debugLog(`received BLE: ${JSON.stringify(context)}`)
355
+ this.serviceData = context
356
+ await this.BLEparseStatus()
357
+ await this.updateHomeKitCharacteristics()
358
+ } catch (e: any) {
359
+ await this.errorLog(`failed to handle BLE. Received: ${JSON.stringify(context)} Error: ${e}`)
360
+ }
358
361
  }
362
+ } catch (error) {
363
+ await this.errorLog(`failed to format device ID as MAC, Error: ${error}`)
359
364
  }
360
365
  } else {
361
366
  await this.debugLog('is not listening to Platform BLE')
@@ -428,25 +433,31 @@ export class Humidifier extends deviceBase {
428
433
  && (this.HumidifierDehumidifier.Active === this.hap.Characteristic.Active.ACTIVE)
429
434
  && (this.HumidifierDehumidifier.RelativeHumidityHumidifierThreshold !== this.HumidifierDehumidifier.CurrentRelativeHumidity)) {
430
435
  const switchbot = await this.platform.connectBLE(this.accessory, this.device)
431
- await this.convertBLEAddress()
432
- if (switchbot !== false) {
433
- switchbot
434
- .discover({ model: this.device.bleModel, quick: true, id: this.device.bleMac })
435
- .then(async (device_list: any) => {
436
- return await device_list[0].percentage(this.HumidifierDehumidifier.RelativeHumidityHumidifierThreshold)
437
- })
438
- .then(async () => {
439
- await this.successLog(`RelativeHumidityHumidifierThreshold: ${this.HumidifierDehumidifier.RelativeHumidityHumidifierThreshold} sent over BLE, sent successfully`)
440
- await this.updateHomeKitCharacteristics()
441
- })
442
- .catch(async (e: any) => {
443
- await this.apiError(e)
444
- await this.errorLog(`failed BLEpushChanges with ${this.device.connectionType} Connection, Error Message: ${JSON.stringify(e.message)}`)
445
- await this.BLEPushConnection()
446
- })
447
- } else {
448
- await this.errorLog(`wasn't able to establish BLE Connection, node-switchbot: ${switchbot}`)
449
- await this.BLEPushConnection()
436
+ try {
437
+ const formattedDeviceId = formatDeviceIdAsMac(this.device.deviceId!)
438
+ this.device.bleMac = formattedDeviceId
439
+ await this.debugLog(`bleMac: ${this.device.bleMac}`)
440
+ if (switchbot !== false) {
441
+ switchbot
442
+ .discover({ model: this.device.bleModel, quick: true, id: this.device.bleMac })
443
+ .then(async (device_list: any) => {
444
+ return await device_list[0].percentage(this.HumidifierDehumidifier.RelativeHumidityHumidifierThreshold)
445
+ })
446
+ .then(async () => {
447
+ await this.successLog(`RelativeHumidityHumidifierThreshold: ${this.HumidifierDehumidifier.RelativeHumidityHumidifierThreshold} sent over BLE, sent successfully`)
448
+ await this.updateHomeKitCharacteristics()
449
+ })
450
+ .catch(async (e: any) => {
451
+ await this.apiError(e)
452
+ await this.errorLog(`failed BLEpushChanges with ${this.device.connectionType} Connection, Error Message: ${JSON.stringify(e.message)}`)
453
+ await this.BLEPushConnection()
454
+ })
455
+ } else {
456
+ await this.errorLog(`wasn't able to establish BLE Connection, node-switchbot: ${switchbot}`)
457
+ await this.BLEPushConnection()
458
+ }
459
+ } catch (error) {
460
+ await this.errorLog(`failed to format device ID as MAC, Error: ${error}`)
450
461
  }
451
462
  } else {
452
463
  await this.debugLog(`No changes (BLEpushChanges), Active: ${this.HumidifierDehumidifier.Active}, RelativeHumidityHumidifierThreshold: ${this.HumidifierDehumidifier.RelativeHumidityHumidifierThreshold}, CurrentRelativeHumidity: ${this.HumidifierDehumidifier.CurrentRelativeHumidity}`)
@@ -19,7 +19,7 @@ import { Units } from 'homebridge'
19
19
  import { SwitchBotBLEModel, SwitchBotBLEModelName } from 'node-switchbot'
20
20
  import { interval, skipWhile, Subject } from 'rxjs'
21
21
 
22
- import { convertUnits, validHumidity } from '../utils.js'
22
+ import { convertUnits, formatDeviceIdAsMac, validHumidity } from '../utils.js'
23
23
  import { deviceBase } from './device.js'
24
24
 
25
25
  /**
@@ -310,17 +310,22 @@ export class IOSensor 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: outdoorMeterServiceData) => {
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: outdoorMeterServiceData) => {
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')