@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
@@ -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 Meter extends deviceBase {
@@ -305,17 +305,22 @@ export class Meter extends deviceBase {
305
305
  await this.debugLog('registerPlatformBLE')
306
306
  if (this.config.options?.BLE) {
307
307
  await this.debugLog('is listening to Platform BLE.')
308
- this.device.bleMac = this.device.deviceId!.match(/.{1,2}/g)!.join(':').toLowerCase()
309
- await this.debugLog(`bleMac: ${this.device.bleMac}`)
310
- this.platform.bleEventHandler[this.device.bleMac] = async (context: meterServiceData) => {
311
- try {
312
- await this.debugLog(`received BLE: ${JSON.stringify(context)}`)
313
- this.serviceData = context
314
- await this.BLEparseStatus()
315
- await this.updateHomeKitCharacteristics()
316
- } catch (e: any) {
317
- await this.errorLog(`failed to handle BLE. Received: ${JSON.stringify(context)} Error: ${e}`)
308
+ try {
309
+ const formattedDeviceId = formatDeviceIdAsMac(this.device.deviceId)
310
+ this.device.bleMac = formattedDeviceId
311
+ await this.debugLog(`bleMac: ${this.device.bleMac}`)
312
+ this.platform.bleEventHandler[this.device.bleMac] = async (context: meterServiceData) => {
313
+ try {
314
+ await this.debugLog(`received BLE: ${JSON.stringify(context)}`)
315
+ this.serviceData = context
316
+ await this.BLEparseStatus()
317
+ await this.updateHomeKitCharacteristics()
318
+ } catch (e: any) {
319
+ await this.errorLog(`failed to handle BLE. Received: ${JSON.stringify(context)} Error: ${e}`)
320
+ }
318
321
  }
322
+ } catch (error) {
323
+ await this.errorLog(`failed to format device ID as MAC, Error: ${error}`)
319
324
  }
320
325
  } else {
321
326
  await this.debugLog('is not listening to Platform BLE')
@@ -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
  /**
@@ -307,17 +307,22 @@ export class MeterPlus extends deviceBase {
307
307
  await this.debugLog('registerPlatformBLE')
308
308
  if (this.config.options?.BLE) {
309
309
  await this.debugLog('is listening to Platform BLE.')
310
- this.device.bleMac = this.device.deviceId!.match(/.{1,2}/g)!.join(':').toLowerCase()
311
- await this.debugLog(`bleMac: ${this.device.bleMac}`)
312
- this.platform.bleEventHandler[this.device.bleMac] = async (context: meterPlusServiceData) => {
313
- try {
314
- await this.debugLog(`received BLE: ${JSON.stringify(context)}`)
315
- this.serviceData = context
316
- await this.BLEparseStatus()
317
- await this.updateHomeKitCharacteristics()
318
- } catch (e: any) {
319
- await this.errorLog(`failed to handle BLE. Received: ${JSON.stringify(context)} Error: ${e}`)
310
+ try {
311
+ const formattedDeviceId = formatDeviceIdAsMac(this.device.deviceId)
312
+ this.device.bleMac = formattedDeviceId
313
+ await this.debugLog(`bleMac: ${this.device.bleMac}`)
314
+ this.platform.bleEventHandler[this.device.bleMac] = async (context: meterPlusServiceData) => {
315
+ try {
316
+ await this.debugLog(`received BLE: ${JSON.stringify(context)}`)
317
+ this.serviceData = context
318
+ await this.BLEparseStatus()
319
+ await this.updateHomeKitCharacteristics()
320
+ } catch (e: any) {
321
+ await this.errorLog(`failed to handle BLE. Received: ${JSON.stringify(context)} Error: ${e}`)
322
+ }
320
323
  }
324
+ } catch (error) {
325
+ await this.errorLog(`failed to format device ID as MAC, Error: ${error}`)
321
326
  }
322
327
  } else {
323
328
  await this.debugLog('is not listening to Platform BLE')
@@ -18,6 +18,7 @@ import type { motionSensorWebhookContext } from '../types/devicewebhookstatus.js
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
  /**
@@ -281,17 +282,22 @@ export class Motion extends deviceBase {
281
282
  await this.debugLog('registerPlatformBLE')
282
283
  if (this.config.options?.BLE) {
283
284
  await this.debugLog('is listening to Platform BLE.')
284
- this.device.bleMac = this.device.deviceId!.match(/.{1,2}/g)!.join(':').toLowerCase()
285
- await this.debugLog(`bleMac: ${this.device.bleMac}`)
286
- this.platform.bleEventHandler[this.device.bleMac] = async (context: motionSensorServiceData) => {
287
- try {
288
- await this.debugLog(`received BLE: ${JSON.stringify(context)}`)
289
- this.serviceData = context
290
- await this.BLEparseStatus()
291
- await this.updateHomeKitCharacteristics()
292
- } catch (e: any) {
293
- await this.errorLog(`failed to handle BLE. Received: ${JSON.stringify(context)} Error: ${e}`)
285
+ try {
286
+ const formattedDeviceId = formatDeviceIdAsMac(this.device.deviceId)
287
+ this.device.bleMac = formattedDeviceId
288
+ await this.debugLog(`bleMac: ${this.device.bleMac}`)
289
+ this.platform.bleEventHandler[this.device.bleMac] = async (context: motionSensorServiceData) => {
290
+ try {
291
+ await this.debugLog(`received BLE: ${JSON.stringify(context)}`)
292
+ this.serviceData = context
293
+ await this.BLEparseStatus()
294
+ await this.updateHomeKitCharacteristics()
295
+ } catch (e: any) {
296
+ await this.errorLog(`failed to handle BLE. Received: ${JSON.stringify(context)} Error: ${e}`)
297
+ }
294
298
  }
299
+ } catch (error) {
300
+ await this.errorLog(`failed to format device ID as MAC, Error: ${error}`)
295
301
  }
296
302
  } else {
297
303
  await this.debugLog('is not listening to Platform BLE')
@@ -18,6 +18,7 @@ import type { plugMiniJPWebhookContext, plugMiniUSWebhookContext, plugWebhookCon
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 Plug extends deviceBase {
@@ -205,17 +206,22 @@ export class Plug extends deviceBase {
205
206
  await this.debugLog('registerPlatformBLE')
206
207
  if (this.config.options?.BLE) {
207
208
  await this.debugLog('is listening to Platform BLE.')
208
- this.device.bleMac = this.device.deviceId!.match(/.{1,2}/g)!.join(':').toLowerCase()
209
- await this.debugLog(`bleMac: ${this.device.bleMac}`)
210
- this.platform.bleEventHandler[this.device.bleMac] = async (context: plugMiniUSServiceData | plugMiniJPServiceData) => {
211
- try {
212
- await this.debugLog(`received BLE: ${JSON.stringify(context)}`)
213
- this.serviceData = context
214
- await this.BLEparseStatus()
215
- await this.updateHomeKitCharacteristics()
216
- } catch (e: any) {
217
- await this.errorLog(`failed to handle BLE. Received: ${JSON.stringify(context)} Error: ${e}`)
209
+ try {
210
+ const formattedDeviceId = formatDeviceIdAsMac(this.device.deviceId)
211
+ this.device.bleMac = formattedDeviceId
212
+ await this.debugLog(`bleMac: ${this.device.bleMac}`)
213
+ this.platform.bleEventHandler[this.device.bleMac] = async (context: plugMiniUSServiceData | plugMiniJPServiceData) => {
214
+ try {
215
+ await this.debugLog(`received BLE: ${JSON.stringify(context)}`)
216
+ this.serviceData = context
217
+ await this.BLEparseStatus()
218
+ await this.updateHomeKitCharacteristics()
219
+ } catch (e: any) {
220
+ await this.errorLog(`failed to handle BLE. Received: ${JSON.stringify(context)} Error: ${e}`)
221
+ }
218
222
  }
223
+ } catch (error) {
224
+ await this.errorLog(`failed to format device ID as MAC, Error: ${error}`)
219
225
  }
220
226
  } else {
221
227
  await this.debugLog('is not listening to Platform BLE')
@@ -296,35 +302,41 @@ export class Plug extends deviceBase {
296
302
  if (this.Outlet.On !== this.accessory.context.On) {
297
303
  await this.debugLog(`BLEpushChanges On: ${this.Outlet.On}, OnCached: ${this.accessory.context.On}`)
298
304
  const switchbot = await this.platform.connectBLE(this.accessory, this.device)
299
- await this.convertBLEAddress()
300
- if (switchbot !== false) {
301
- switchbot
302
- .discover({ model: this.device.bleModel, id: this.device.bleMac })
303
- .then(async (device_list: any) => {
304
- await this.infoLog(`On: ${this.Outlet.On}`)
305
- return await this.retryBLE({
306
- max: await this.maxRetryBLE(),
307
- fn: async () => {
308
- if (this.Outlet.On) {
309
- return await device_list[0].turnOn({ id: this.device.bleMac })
310
- } else {
311
- return await device_list[0].turnOff({ id: this.device.bleMac })
312
- }
313
- },
305
+ try {
306
+ const formattedDeviceId = formatDeviceIdAsMac(this.device.deviceId)
307
+ this.device.bleMac = formattedDeviceId
308
+ await this.debugLog(`bleMac: ${this.device.bleMac}`)
309
+ if (switchbot !== false) {
310
+ switchbot
311
+ .discover({ model: this.device.bleModel, id: this.device.bleMac })
312
+ .then(async (device_list: any) => {
313
+ await this.infoLog(`On: ${this.Outlet.On}`)
314
+ return await this.retryBLE({
315
+ max: await this.maxRetryBLE(),
316
+ fn: async () => {
317
+ if (this.Outlet.On) {
318
+ return await device_list[0].turnOn({ id: this.device.bleMac })
319
+ } else {
320
+ return await device_list[0].turnOff({ id: this.device.bleMac })
321
+ }
322
+ },
323
+ })
314
324
  })
315
- })
316
- .then(async () => {
317
- await this.successLog(`On: ${this.Outlet.On} sent over SwitchBot BLE, sent successfully`)
318
- await this.updateHomeKitCharacteristics()
319
- })
320
- .catch(async (e: any) => {
321
- await this.apiError(e)
322
- await this.errorLog(`failed BLEpushChanges with ${this.device.connectionType} Connection, Error Message: ${JSON.stringify(e.message)}`)
323
- await this.BLEPushConnection()
324
- })
325
- } else {
326
- await this.errorLog(`wasn't able to establish BLE Connection, node-switchbot: ${switchbot}`)
327
- await this.BLEPushConnection()
325
+ .then(async () => {
326
+ await this.successLog(`On: ${this.Outlet.On} sent over SwitchBot BLE, sent successfully`)
327
+ await this.updateHomeKitCharacteristics()
328
+ })
329
+ .catch(async (e: any) => {
330
+ await this.apiError(e)
331
+ await this.errorLog(`failed BLEpushChanges with ${this.device.connectionType} Connection, Error Message: ${JSON.stringify(e.message)}`)
332
+ await this.BLEPushConnection()
333
+ })
334
+ } else {
335
+ await this.errorLog(`wasn't able to establish BLE Connection, node-switchbot: ${switchbot}`)
336
+ await this.BLEPushConnection()
337
+ }
338
+ } catch (error) {
339
+ await this.errorLog(`failed to format device ID as MAC, Error: ${error}`)
328
340
  }
329
341
  } else {
330
342
  await this.debugLog(`No changes (BLEpushChanges), On: ${this.Outlet.On}, OnCached: ${this.accessory.context.On}`)
@@ -22,6 +22,7 @@ import type {
22
22
  import { SwitchBotBLEModel, SwitchBotBLEModelName } from 'node-switchbot'
23
23
  import { debounceTime, interval, skipWhile, Subject, take, tap } from 'rxjs'
24
24
 
25
+ import { formatDeviceIdAsMac } from '../utils.js'
25
26
  import { deviceBase } from './device.js'
26
27
 
27
28
  export class RobotVacuumCleaner extends deviceBase {
@@ -295,17 +296,22 @@ export class RobotVacuumCleaner extends deviceBase {
295
296
  await this.debugLog('registerPlatformBLE')
296
297
  if (this.config.options?.BLE) {
297
298
  await this.debugLog('is listening to Platform BLE.')
298
- this.device.bleMac = this.device.deviceId!.match(/.{1,2}/g)!.join(':').toLowerCase()
299
- await this.debugLog(`bleMac: ${this.device.bleMac}`)
300
- this.platform.bleEventHandler[this.device.bleMac] = async (context: robotVacuumCleanerServiceData) => {
301
- try {
302
- await this.debugLog(`received BLE: ${JSON.stringify(context)}`)
303
- this.serviceData = context
304
- await this.BLEparseStatus()
305
- await this.updateHomeKitCharacteristics()
306
- } catch (e: any) {
307
- await this.errorLog(`failed to handle BLE. Received: ${JSON.stringify(context)} Error: ${e}`)
299
+ try {
300
+ const formattedDeviceId = formatDeviceIdAsMac(this.device.deviceId)
301
+ this.device.bleMac = formattedDeviceId
302
+ await this.debugLog(`bleMac: ${this.device.bleMac}`)
303
+ this.platform.bleEventHandler[this.device.bleMac] = async (context: robotVacuumCleanerServiceData) => {
304
+ try {
305
+ await this.debugLog(`received BLE: ${JSON.stringify(context)}`)
306
+ this.serviceData = context
307
+ await this.BLEparseStatus()
308
+ await this.updateHomeKitCharacteristics()
309
+ } catch (e: any) {
310
+ await this.errorLog(`failed to handle BLE. Received: ${JSON.stringify(context)} Error: ${e}`)
311
+ }
308
312
  }
313
+ } catch (error) {
314
+ await this.errorLog(`failed to format device ID as MAC, Error: ${error}`)
309
315
  }
310
316
  } else {
311
317
  await this.debugLog('is not listening to Platform BLE')
@@ -383,35 +389,41 @@ export class RobotVacuumCleaner extends deviceBase {
383
389
  await this.debugLog('BLEpushChanges')
384
390
  if (this.LightBulb.On !== this.accessory.context.On) {
385
391
  const switchbot = await this.platform.connectBLE(this.accessory, this.device)
386
- await this.convertBLEAddress()
387
- if (switchbot !== false) {
388
- switchbot
389
- .discover({ model: this.device.bleModel, id: this.device.bleMac })
390
- .then(async (device_list: any) => {
391
- await this.infoLog(`On: ${this.LightBulb.On}`)
392
- return await this.retryBLE({
393
- max: await this.maxRetryBLE(),
394
- fn: async () => {
395
- if (this.LightBulb.On) {
396
- return await device_list[0].turnOn({ id: this.device.bleMac })
397
- } else {
398
- return await device_list[0].turnOff({ id: this.device.bleMac })
399
- }
400
- },
392
+ try {
393
+ const formattedDeviceId = formatDeviceIdAsMac(this.device.deviceId)
394
+ this.device.bleMac = formattedDeviceId
395
+ await this.debugLog(`bleMac: ${this.device.bleMac}`)
396
+ if (switchbot !== false) {
397
+ switchbot
398
+ .discover({ model: this.device.bleModel, id: this.device.bleMac })
399
+ .then(async (device_list: any) => {
400
+ await this.infoLog(`On: ${this.LightBulb.On}`)
401
+ return await this.retryBLE({
402
+ max: await this.maxRetryBLE(),
403
+ fn: async () => {
404
+ if (this.LightBulb.On) {
405
+ return await device_list[0].turnOn({ id: this.device.bleMac })
406
+ } else {
407
+ return await device_list[0].turnOff({ id: this.device.bleMac })
408
+ }
409
+ },
410
+ })
401
411
  })
402
- })
403
- .then(async () => {
404
- await this.successLog(`On: ${this.LightBulb.On} sent over SwitchBot BLE, sent successfully`)
405
- await this.updateHomeKitCharacteristics()
406
- })
407
- .catch(async (e: any) => {
408
- await this.apiError(e)
409
- await this.errorLog(`failed BLEpushChanges with ${this.device.connectionType} Connection, Error Message: ${JSON.stringify(e.message)}`)
410
- await this.BLEPushConnection()
411
- })
412
- } else {
413
- await this.errorLog(`wasn't able to establish BLE Connection, node-switchbot: ${switchbot}`)
414
- await this.BLEPushConnection()
412
+ .then(async () => {
413
+ await this.successLog(`On: ${this.LightBulb.On} sent over SwitchBot BLE, sent successfully`)
414
+ await this.updateHomeKitCharacteristics()
415
+ })
416
+ .catch(async (e: any) => {
417
+ await this.apiError(e)
418
+ await this.errorLog(`failed BLEpushChanges with ${this.device.connectionType} Connection, Error Message: ${JSON.stringify(e.message)}`)
419
+ await this.BLEPushConnection()
420
+ })
421
+ } else {
422
+ await this.errorLog(`wasn't able to establish BLE Connection, node-switchbot: ${switchbot}`)
423
+ await this.BLEPushConnection()
424
+ }
425
+ } catch (error) {
426
+ await this.errorLog(`failed to format device ID as MAC, Error: ${error}`)
415
427
  }
416
428
  } else {
417
429
  await this.debugLog(`No changes (BLEpushChanges), On: ${this.LightBulb.On}, OnCached: ${this.accessory.context.On}`)
@@ -18,6 +18,7 @@ import type { waterLeakDetectorWebhookContext } from '../types/devicewebhookstat
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
  /**
@@ -295,17 +296,22 @@ export class WaterDetector extends deviceBase {
295
296
  await this.debugLog('registerPlatformBLE')
296
297
  if (this.config.options?.BLE) {
297
298
  await this.debugLog('is listening to Platform BLE.')
298
- this.device.bleMac = this.device.deviceId!.match(/.{1,2}/g)!.join(':').toLowerCase()
299
- await this.debugLog(`bleMac: ${this.device.bleMac}`)
300
- this.platform.bleEventHandler[this.device.bleMac] = async (context: waterLeakDetectorServiceData) => {
301
- try {
302
- await this.debugLog(`received BLE: ${JSON.stringify(context)}`)
303
- this.serviceData = context
304
- await this.BLEparseStatus()
305
- await this.updateHomeKitCharacteristics()
306
- } catch (e: any) {
307
- await this.errorLog(`failed to handle BLE. Received: ${JSON.stringify(context)} Error: ${e}`)
299
+ try {
300
+ const formattedDeviceId = formatDeviceIdAsMac(this.device.deviceId)
301
+ this.device.bleMac = formattedDeviceId
302
+ await this.debugLog(`bleMac: ${this.device.bleMac}`)
303
+ this.platform.bleEventHandler[this.device.bleMac] = async (context: waterLeakDetectorServiceData) => {
304
+ try {
305
+ await this.debugLog(`received BLE: ${JSON.stringify(context)}`)
306
+ this.serviceData = context
307
+ await this.BLEparseStatus()
308
+ await this.updateHomeKitCharacteristics()
309
+ } catch (e: any) {
310
+ await this.errorLog(`failed to handle BLE. Received: ${JSON.stringify(context)} Error: ${e}`)
311
+ }
308
312
  }
313
+ } catch (error) {
314
+ await this.errorLog(`failed to format device ID as MAC, Error: ${error}`)
309
315
  }
310
316
  } else {
311
317
  await this.debugLog('is not listening to Platform BLE')
package/src/platform.ts CHANGED
@@ -58,7 +58,7 @@ import { TV } from './irdevice/tv.js'
58
58
  import { VacuumCleaner } from './irdevice/vacuumcleaner.js'
59
59
  import { WaterHeater } from './irdevice/waterheater.js'
60
60
  import { deleteWebhook, Devices, PLATFORM_NAME, PLUGIN_NAME, queryWebhook, setupWebhook, updateWebhook } from './settings.js'
61
- import { isBlindTiltDevice, isCurtainDevice, sleep } from './utils.js'
61
+ import { formatDeviceIdAsMac, isBlindTiltDevice, isCurtainDevice, sleep } from './utils.js'
62
62
 
63
63
  /**
64
64
  * HomebridgePlatform
@@ -593,8 +593,14 @@ export class SwitchBotPlatform implements DynamicPlatformPlugin {
593
593
  for (const device of devices) {
594
594
  device.deviceType = device.configDeviceType
595
595
  device.deviceName = device.configDeviceName
596
- if (device.deviceType) {
597
- await this.createDevice(device)
596
+ try {
597
+ device.deviceId = formatDeviceIdAsMac(device.deviceId, true)
598
+ await this.debugLog(`deviceId: ${device.deviceId}`)
599
+ if (device.deviceType) {
600
+ await this.createDevice(device)
601
+ }
602
+ } catch (error) {
603
+ await this.errorLog(`failed to format device ID as MAC, Error: ${error}`)
598
604
  }
599
605
  }
600
606
  } else {
package/src/utils.ts CHANGED
@@ -158,6 +158,39 @@ export function convertUnits(value: number, unit: string, convert?: string): num
158
158
  return value
159
159
  }
160
160
 
161
+ /**
162
+ * Formats a device ID as a MAC address.
163
+ * Ensures the device ID does not already contain colons.
164
+ *
165
+ * @param deviceId - The device ID to format.
166
+ * @param cassSensative - If the MAC address should be case sensitive. Default is false, which will return the MAC address in lowercase.
167
+ * @returns The formatted MAC address.
168
+ * @throws Will throw an error if the device ID is not a valid MAC address or a 12-character hexadecimal string.
169
+ */
170
+ export function formatDeviceIdAsMac(deviceId: string, cassSensative?: boolean): string {
171
+ if (typeof deviceId !== 'string') {
172
+ throw new TypeError('Invalid device ID format. Device ID must be a string.')
173
+ }
174
+
175
+ deviceId = deviceId.trim()
176
+
177
+ const macAddressRegex = /^(?:[0-9a-f]{2}:){5}[0-9a-f]{2}$/i
178
+ const hexRegex = /^[0-9a-f]{12}$/i
179
+
180
+ // Check if the deviceId is already in a valid MAC address format
181
+ if (macAddressRegex.test(deviceId)) {
182
+ return cassSensative ? deviceId : deviceId.toLowerCase()
183
+ }
184
+
185
+ // Check if the deviceId is a valid 12-character hexadecimal string
186
+ if (hexRegex.test(deviceId)) {
187
+ const formattedDeviceId = deviceId.match(/.{1,2}/g)!.join(':')
188
+ return cassSensative ? formattedDeviceId : formattedDeviceId.toLowerCase()
189
+ }
190
+
191
+ throw new Error('Invalid device ID format. Must be a valid MAC address or a 12-character hexadecimal string.')
192
+ }
193
+
161
194
  export function rgb2hs(r: any, g: any, b: any) {
162
195
  /**
163
196
  * Credit: