@switchbot/homebridge-switchbot 5.2.9-beta.0 → 5.2.9

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 (115) hide show
  1. package/config.schema.json +29 -0
  2. package/dist/SwitchBotMatterPlatform.js +1 -1
  3. package/dist/SwitchBotMatterPlatform.js.map +1 -1
  4. package/dist/deviceFactory.js +1 -1
  5. package/dist/deviceFactory.js.map +1 -1
  6. package/dist/devices/genericDevice.d.ts +2 -0
  7. package/dist/devices/genericDevice.d.ts.map +1 -1
  8. package/dist/devices/genericDevice.js +573 -123
  9. package/dist/devices/genericDevice.js.map +1 -1
  10. package/dist/homebridge-ui/endpoints/discovery.d.ts.map +1 -1
  11. package/dist/homebridge-ui/endpoints/discovery.js +2 -0
  12. package/dist/homebridge-ui/endpoints/discovery.js.map +1 -1
  13. package/dist/homebridge-ui/homebridge-ui/endpoints/discovery.js +2 -0
  14. package/dist/homebridge-ui/homebridge-ui/endpoints/discovery.js.map +1 -1
  15. package/dist/homebridge-ui/public/index.html +12 -2
  16. package/dist/homebridge-ui/public/js/advanced-settings.d.ts +4 -1
  17. package/dist/homebridge-ui/public/js/advanced-settings.d.ts.map +1 -1
  18. package/dist/homebridge-ui/public/js/advanced-settings.js +21 -11
  19. package/dist/homebridge-ui/public/js/advanced-settings.js.map +1 -1
  20. package/dist/homebridge-ui/public/js/advanced-settings.ts +23 -11
  21. package/dist/homebridge-ui/public/js/api.d.ts.map +1 -1
  22. package/dist/homebridge-ui/public/js/api.js +21 -7
  23. package/dist/homebridge-ui/public/js/api.js.map +1 -1
  24. package/dist/homebridge-ui/public/js/api.ts +21 -7
  25. package/dist/homebridge-ui/public/js/app.js +78 -20
  26. package/dist/homebridge-ui/public/js/app.js.map +2 -2
  27. package/dist/homebridge-ui/public/js/credentials.d.ts.map +1 -1
  28. package/dist/homebridge-ui/public/js/credentials.js +9 -3
  29. package/dist/homebridge-ui/public/js/credentials.js.map +1 -1
  30. package/dist/homebridge-ui/public/js/credentials.ts +9 -3
  31. package/dist/homebridge-ui/public/js/devices-delete.d.ts.map +1 -1
  32. package/dist/homebridge-ui/public/js/devices-delete.js +6 -2
  33. package/dist/homebridge-ui/public/js/devices-delete.js.map +1 -1
  34. package/dist/homebridge-ui/public/js/devices-delete.ts +6 -2
  35. package/dist/homebridge-ui/public/js/devices.d.ts.map +1 -1
  36. package/dist/homebridge-ui/public/js/devices.js +3 -1
  37. package/dist/homebridge-ui/public/js/devices.js.map +1 -1
  38. package/dist/homebridge-ui/public/js/devices.ts +3 -1
  39. package/dist/homebridge-ui/public/js/discovery.d.ts.map +1 -1
  40. package/dist/homebridge-ui/public/js/discovery.js +89 -25
  41. package/dist/homebridge-ui/public/js/discovery.js.map +1 -1
  42. package/dist/homebridge-ui/public/js/discovery.ts +89 -25
  43. package/dist/homebridge-ui/public/js/modals.d.ts.map +1 -1
  44. package/dist/homebridge-ui/public/js/modals.js +15 -5
  45. package/dist/homebridge-ui/public/js/modals.js.map +1 -1
  46. package/dist/homebridge-ui/public/js/modals.ts +15 -5
  47. package/dist/homebridge-ui/public/js/render.d.ts.map +1 -1
  48. package/dist/homebridge-ui/public/js/render.js +67 -17
  49. package/dist/homebridge-ui/public/js/render.js.map +1 -1
  50. package/dist/homebridge-ui/public/js/render.ts +67 -17
  51. package/dist/homebridge-ui/public/js/toast.d.ts.map +1 -1
  52. package/dist/homebridge-ui/public/js/toast.js +6 -2
  53. package/dist/homebridge-ui/public/js/toast.js.map +1 -1
  54. package/dist/homebridge-ui/public/js/toast.ts +6 -2
  55. package/dist/homebridge-ui/settings.js.map +1 -1
  56. package/dist/homebridge-ui/switchbotClient.js +4 -0
  57. package/dist/homebridge-ui/switchbotClient.js.map +1 -1
  58. package/dist/settings.d.ts +1 -0
  59. package/dist/settings.d.ts.map +1 -1
  60. package/dist/settings.js.map +1 -1
  61. package/dist/switchbotClient.d.ts +1 -0
  62. package/dist/switchbotClient.d.ts.map +1 -1
  63. package/dist/switchbotClient.js +4 -0
  64. package/dist/switchbotClient.js.map +1 -1
  65. package/docs/variables/default.html +1 -1
  66. package/package.json +9 -9
  67. package/src/SwitchBotMatterPlatform.ts +1 -1
  68. package/src/deviceFactory.ts +1 -1
  69. package/src/devices/genericDevice.ts +572 -134
  70. package/src/homebridge-ui/endpoints/discovery.ts +2 -0
  71. package/src/homebridge-ui/public/index.html +12 -2
  72. package/src/homebridge-ui/public/js/advanced-settings.ts +23 -11
  73. package/src/homebridge-ui/public/js/api.ts +21 -7
  74. package/src/homebridge-ui/public/js/credentials.ts +9 -3
  75. package/src/homebridge-ui/public/js/devices-delete.ts +6 -2
  76. package/src/homebridge-ui/public/js/devices.ts +3 -1
  77. package/src/homebridge-ui/public/js/discovery.ts +89 -25
  78. package/src/homebridge-ui/public/js/modals.ts +15 -5
  79. package/src/homebridge-ui/public/js/render.ts +67 -17
  80. package/src/homebridge-ui/public/js/toast.ts +6 -2
  81. package/src/settings.ts +1 -0
  82. package/src/switchbotClient.ts +5 -0
  83. package/test/client/node-switchbot-commands.spec.ts +44 -8
  84. package/test/client/switchbot-client-debounce.spec.ts +24 -3
  85. package/test/client/switchbot-client-discovery.spec.ts +26 -5
  86. package/test/client/switchbot-client-keys.spec.ts +30 -7
  87. package/test/client/switchbot-client-openapi.spec.ts +6 -1
  88. package/test/client/switchbotClient.spec.ts +30 -6
  89. package/test/device/bot-hap.spec.ts +131 -22
  90. package/test/device/curtain-ble-position.spec.ts +46 -0
  91. package/test/device/deviceBase.spec.ts +6 -1
  92. package/test/device/deviceFactory-edge.spec.ts +12 -2
  93. package/test/device/deviceFactory.spec.ts +6 -1
  94. package/test/device/fan-swing.spec.ts +9 -2
  95. package/test/device/genericDevice-blepoll.spec.ts +17 -3
  96. package/test/device/getstate-cache.spec.ts +37 -6
  97. package/test/device/getstate-returns-status.spec.ts +65 -13
  98. package/test/device/lock-users.spec.ts +24 -7
  99. package/test/device/matter-descriptors.spec.ts +6 -1
  100. package/test/device/matter-device-state.spec.ts +6 -1
  101. package/test/device/meter-battery.spec.ts +86 -14
  102. package/test/device/meter-co2.spec.ts +65 -10
  103. package/test/device/model-identities.spec.ts +6 -1
  104. package/test/device/water-detector-battery.spec.ts +19 -3
  105. package/test/device/water-detector-leak.spec.ts +6 -1
  106. package/test/helpers/matter-harness.ts +21 -4
  107. package/test/platform/accessory-restore.spec.ts +18 -3
  108. package/test/platform/hap-cache-restart.spec.ts +66 -13
  109. package/test/platform/matter-childbridge.spec.ts +16 -3
  110. package/test/platform/matter-integration.spec.ts +6 -1
  111. package/test/platform/matter-registration.spec.ts +47 -10
  112. package/test/platform/platform-edge.spec.ts +13 -3
  113. package/test/platform/platform.integration.spec.ts +16 -3
  114. package/test/utils/utils.spec.ts +42 -8
  115. package/.github/npm-version-script-esm.js +0 -103
@@ -42,7 +42,9 @@ import { DeviceBase } from './deviceBase.js'
42
42
 
43
43
  function validateBLEResponseLength(buf: Buffer | Uint8Array | any[], expected: number, context = '', log: import('homebridge').Logger): boolean {
44
44
  if (!buf || typeof buf.length !== 'number' || buf.length !== expected) {
45
- log.warn(`[BLE] Invalid response length${context ? ` for ${context}` : ''}: expected ${expected}, got ${buf?.length}`)
45
+ log.warn(`[BLE] Invalid response length${context
46
+ ? ` for ${context}`
47
+ : ''}: expected ${expected}, got ${buf?.length}`)
46
48
  return false
47
49
  }
48
50
  return true
@@ -211,7 +213,10 @@ export class GenericDevice extends DeviceBase {
211
213
  // at its fallback for the whole window after one transient error.
212
214
  return { id: this.opts.id, type: this.opts.type, unreadable: true }
213
215
  }
214
- this.stateCache = { at: Date.now(), value }
216
+ this.stateCache = {
217
+ at: Date.now(),
218
+ value,
219
+ }
215
220
  return value
216
221
  })
217
222
  .finally(() => {
@@ -229,7 +234,12 @@ export class GenericDevice extends DeviceBase {
229
234
  if (raw && (raw instanceof Buffer || Array.isArray(raw) || raw instanceof Uint8Array)) {
230
235
  // Default to 12, override per device if needed
231
236
  if (!validateBLEResponseLength(raw, 12, this.opts.type, this.log)) {
232
- return { id: this.opts.id, type: this.opts.type, error: 'invalid_ble_response_length', raw }
237
+ return {
238
+ id: this.opts.id,
239
+ type: this.opts.type,
240
+ error: 'invalid_ble_response_length',
241
+ raw,
242
+ }
233
243
  }
234
244
  }
235
245
  // Normalize common response shapes
@@ -249,7 +259,9 @@ export class GenericDevice extends DeviceBase {
249
259
  return device
250
260
  }
251
261
  } catch (e: any) {
252
- this.log?.debug?.(`getState: could not read status for ${this.opts.id}:`, e instanceof Error ? e.message : e)
262
+ this.log?.debug?.(`getState: could not read status for ${this.opts.id}:`, e instanceof Error
263
+ ? e.message
264
+ : e)
253
265
  }
254
266
  } catch (e) {
255
267
  // ignore and fallback
@@ -368,8 +380,14 @@ export class GenericDevice extends DeviceBase {
368
380
  type: 'OnOff',
369
381
  clusterId: MATTER_CLUSTER_IDS.OnOff,
370
382
  attributes: {
371
- onOff: { read: async () => !!(await this.getState()).on || (await this.getState()).power === 'on' || (await this.getState()).state === 'on', write: async (v: any) => this.setState({ on: !!v }) },
372
- [MATTER_ATTRIBUTE_IDS.OnOff.OnOff]: { read: async () => !!(await this.getState()).on || (await this.getState()).power === 'on' || (await this.getState()).state === 'on', write: async (v: any) => this.setState({ on: !!v }) },
383
+ onOff: {
384
+ read: async () => !!(await this.getState()).on || (await this.getState()).power === 'on' || (await this.getState()).state === 'on',
385
+ write: async (v: any) => this.setState({ on: !!v }),
386
+ },
387
+ [MATTER_ATTRIBUTE_IDS.OnOff.OnOff]: {
388
+ read: async () => !!(await this.getState()).on || (await this.getState()).power === 'on' || (await this.getState()).state === 'on',
389
+ write: async (v: any) => this.setState({ on: !!v }),
390
+ },
373
391
  },
374
392
  })
375
393
  }
@@ -380,8 +398,18 @@ export class GenericDevice extends DeviceBase {
380
398
  type: 'LevelControl',
381
399
  clusterId: MATTER_CLUSTER_IDS.LevelControl,
382
400
  attributes: {
383
- currentLevel: { read: async () => (await this.getState()).brightness ?? 100, write: async (v: any) => this.setState({ brightness: Number(v) }) },
384
- [MATTER_ATTRIBUTE_IDS.LevelControl.CurrentLevel]: { read: async () => (await this.getState()).brightness ?? 100, write: async (v: any) => this.setState({ brightness: Number(v) }) },
401
+ currentLevel: {
402
+ read: async () => (await this.getState()).brightness ?? 100,
403
+ write: async (v: any) => this.setState({
404
+ brightness: Number(v),
405
+ }),
406
+ },
407
+ [MATTER_ATTRIBUTE_IDS.LevelControl.CurrentLevel]: {
408
+ read: async () => (await this.getState()).brightness ?? 100,
409
+ write: async (v: any) => this.setState({
410
+ brightness: Number(v),
411
+ }),
412
+ },
385
413
  },
386
414
  })
387
415
  }
@@ -392,15 +420,51 @@ export class GenericDevice extends DeviceBase {
392
420
  type: 'ColorControl',
393
421
  clusterId: MATTER_CLUSTER_IDS.ColorControl,
394
422
  attributes: {
395
- colorMode: { read: async () => 0 },
396
- colorHue: { read: async () => (await this.getState()).hue ?? 0, write: async (v: any) => this.setState({ hue: Number(v) }) },
397
- [MATTER_ATTRIBUTE_IDS.ColorControl.CurrentHue]: { read: async () => (await this.getState()).hue ?? 0, write: async (v: any) => this.setState({ hue: Number(v) }) },
398
- colorSaturation: { read: async () => (await this.getState()).saturation ?? 0, write: async (v: any) => this.setState({ saturation: Number(v) }) },
399
- [MATTER_ATTRIBUTE_IDS.ColorControl.CurrentSaturation]: { read: async () => (await this.getState()).saturation ?? 0, write: async (v: any) => this.setState({ saturation: Number(v) }) },
423
+ colorMode: {
424
+ read: async () => 0,
425
+ },
426
+ colorHue: {
427
+ read: async () => (await this.getState()).hue ?? 0,
428
+ write: async (v: any) => this.setState({
429
+ hue: Number(v),
430
+ }),
431
+ },
432
+ [MATTER_ATTRIBUTE_IDS.ColorControl.CurrentHue]: {
433
+ read: async () => (await this.getState()).hue ?? 0,
434
+ write: async (v: any) => this.setState({
435
+ hue: Number(v),
436
+ }),
437
+ },
438
+ colorSaturation: {
439
+ read: async () => (await this.getState()).saturation ?? 0,
440
+ write: async (v: any) => this.setState({
441
+ saturation: Number(v),
442
+ }),
443
+ },
444
+ [MATTER_ATTRIBUTE_IDS.ColorControl.CurrentSaturation]: {
445
+ read: async () => (await this.getState()).saturation ?? 0,
446
+ write: async (v: any) => this.setState({
447
+ saturation: Number(v),
448
+ }),
449
+ },
400
450
  ...(typeof state.colorTemperature === 'number' || typeof state.kelvin === 'number'
401
451
  ? {
402
- colorTemperature: { read: async () => typeof (await this.getState()).colorTemperature === 'number' ? (await this.getState()).colorTemperature : Math.round(1000000 / ((await this.getState()).kelvin ?? 2500)), write: async (v: any) => this.setState({ colorTemperature: Number(v) }) },
403
- [MATTER_ATTRIBUTE_IDS.ColorControl.ColorTemperatureMireds]: { read: async () => typeof (await this.getState()).colorTemperature === 'number' ? (await this.getState()).colorTemperature : Math.round(1000000 / ((await this.getState()).kelvin ?? 2500)), write: async (v: any) => this.setState({ colorTemperature: Number(v) }) },
452
+ colorTemperature: {
453
+ read: async () => typeof (await this.getState()).colorTemperature === 'number'
454
+ ? (await this.getState()).colorTemperature
455
+ : Math.round(1000000 / ((await this.getState()).kelvin ?? 2500)),
456
+ write: async (v: any) => this.setState({
457
+ colorTemperature: Number(v),
458
+ }),
459
+ },
460
+ [MATTER_ATTRIBUTE_IDS.ColorControl.ColorTemperatureMireds]: {
461
+ read: async () => typeof (await this.getState()).colorTemperature === 'number'
462
+ ? (await this.getState()).colorTemperature
463
+ : Math.round(1000000 / ((await this.getState()).kelvin ?? 2500)),
464
+ write: async (v: any) => this.setState({
465
+ colorTemperature: Number(v),
466
+ }),
467
+ },
404
468
  }
405
469
  : {}),
406
470
  },
@@ -413,7 +477,9 @@ export class GenericDevice extends DeviceBase {
413
477
  type: 'TemperatureMeasurement',
414
478
  // No clusterId, not present in MATTER_CLUSTER_IDS
415
479
  attributes: {
416
- measuredValue: { read: async () => (await this.getState()).temperature ?? 0 },
480
+ measuredValue: {
481
+ read: async () => (await this.getState()).temperature ?? 0,
482
+ },
417
483
  },
418
484
  })
419
485
  }
@@ -424,7 +490,9 @@ export class GenericDevice extends DeviceBase {
424
490
  type: 'RelativeHumidityMeasurement',
425
491
  clusterId: MATTER_CLUSTER_IDS.RelativeHumidityMeasurement,
426
492
  attributes: {
427
- measuredValue: { read: async () => (await this.getState()).humidity ?? 0 },
493
+ measuredValue: {
494
+ read: async () => (await this.getState()).humidity ?? 0,
495
+ },
428
496
  },
429
497
  })
430
498
  }
@@ -434,7 +502,9 @@ export class GenericDevice extends DeviceBase {
434
502
  clusters.push({
435
503
  type: 'AirQuality',
436
504
  attributes: {
437
- CO2: { read: async () => (await this.getState()).CO2 ?? 0 },
505
+ CO2: {
506
+ read: async () => (await this.getState()).CO2 ?? 0,
507
+ },
438
508
  },
439
509
  })
440
510
  }
@@ -445,7 +515,12 @@ export class GenericDevice extends DeviceBase {
445
515
  type: 'DoorLock',
446
516
  clusterId: MATTER_CLUSTER_IDS.DoorLock,
447
517
  attributes: {
448
- lockState: { read: async () => (await this.getState()).lockState ?? (await this.getState()).locked ? 1 : 0, write: async (v: any) => this.setState({ locked: !!v }) },
518
+ lockState: {
519
+ read: async () => (await this.getState()).lockState ?? (await this.getState()).locked
520
+ ? 1
521
+ : 0,
522
+ write: async (v: any) => this.setState({ locked: !!v }),
523
+ },
449
524
  },
450
525
  })
451
526
  }
@@ -456,7 +531,11 @@ export class GenericDevice extends DeviceBase {
456
531
  type: 'OccupancySensing',
457
532
  // No clusterId, not present in MATTER_CLUSTER_IDS
458
533
  attributes: {
459
- occupancy: { read: async () => (await this.getState()).moveDetected === true || (await this.getState()).motion === true ? 1 : 0 },
534
+ occupancy: {
535
+ read: async () => (await this.getState()).moveDetected === true || (await this.getState()).motion === true
536
+ ? 1
537
+ : 0,
538
+ },
460
539
  },
461
540
  })
462
541
  }
@@ -467,7 +546,11 @@ export class GenericDevice extends DeviceBase {
467
546
  type: 'BooleanState',
468
547
  // No clusterId, not present in MATTER_CLUSTER_IDS
469
548
  attributes: {
470
- stateValue: { read: async () => (await this.getState()).openState === 'open' || (await this.getState()).open === true ? 1 : 0 },
549
+ stateValue: {
550
+ read: async () => (await this.getState()).openState === 'open' || (await this.getState()).open === true
551
+ ? 1
552
+ : 0,
553
+ },
471
554
  },
472
555
  })
473
556
  }
@@ -477,7 +560,11 @@ export class GenericDevice extends DeviceBase {
477
560
  clusters.push({
478
561
  type: 'LeakSensor',
479
562
  attributes: {
480
- leakDetected: { read: async () => (await this.getState()).leak === true || (await this.getState()).status === 1 ? 1 : 0 },
563
+ leakDetected: {
564
+ read: async () => (await this.getState()).leak === true || (await this.getState()).status === 1
565
+ ? 1
566
+ : 0,
567
+ },
481
568
  },
482
569
  })
483
570
  }
@@ -488,9 +575,15 @@ export class GenericDevice extends DeviceBase {
488
575
  type: 'ElectricalMeasurement',
489
576
  // No clusterId, not present in MATTER_CLUSTER_IDS
490
577
  attributes: {
491
- voltage: { read: async () => (await this.getState()).voltage ?? 0 },
492
- power: { read: async () => (await this.getState()).power ?? 0 },
493
- electricCurrent: { read: async () => (await this.getState()).electricCurrent ?? 0 },
578
+ voltage: {
579
+ read: async () => (await this.getState()).voltage ?? 0,
580
+ },
581
+ power: {
582
+ read: async () => (await this.getState()).power ?? 0,
583
+ },
584
+ electricCurrent: {
585
+ read: async () => (await this.getState()).electricCurrent ?? 0,
586
+ },
494
587
  },
495
588
  })
496
589
  }
@@ -501,7 +594,12 @@ export class GenericDevice extends DeviceBase {
501
594
  type: 'FanControl',
502
595
  clusterId: MATTER_CLUSTER_IDS.FanControl,
503
596
  attributes: {
504
- speedCurrent: { read: async () => (await this.getState()).speed ?? (await this.getState()).fanSpeed ?? 0, write: async (v: any) => this.setState({ speed: Number(v) }) },
597
+ speedCurrent: {
598
+ read: async () => (await this.getState()).speed ?? (await this.getState()).fanSpeed ?? 0,
599
+ write: async (v: any) => this.setState({
600
+ speed: Number(v),
601
+ }),
602
+ },
505
603
  },
506
604
  })
507
605
  }
@@ -511,7 +609,9 @@ export class GenericDevice extends DeviceBase {
511
609
  clusters.push({
512
610
  type: 'RobotVacuumCleaner',
513
611
  attributes: {
514
- workingStatus: { read: async () => (await this.getState()).workingStatus ?? 'StandBy' },
612
+ workingStatus: {
613
+ read: async () => (await this.getState()).workingStatus ?? 'StandBy',
614
+ },
515
615
  },
516
616
  })
517
617
  }
@@ -553,56 +653,125 @@ export class BotDevice extends GenericDevice {
553
653
  if (pressMode && !on) {
554
654
  return
555
655
  }
556
- const command = pressMode ? 'press' : on ? 'turnOn' : 'turnOff'
656
+ const command = pressMode
657
+ ? 'press'
658
+ : on ? 'turnOn' : 'turnOff'
557
659
  const result = await this.setState({ command, parameter: 'default', commandType: 'command' })
558
660
  if (result !== true && result?.success !== true) {
559
661
  throw new Error(`SwitchBot Bot ${command} failed: ${result?.reason ?? 'unknown error'}`)
560
662
  }
561
663
  if (!pressMode) {
562
- this.lastCommand = { on, at: Date.now() }
664
+ this.lastCommand = {
665
+ on,
666
+ at: Date.now(),
667
+ }
563
668
  }
564
669
  }
565
670
 
566
671
  const control = (name: string, active: number | boolean, inactive: number | boolean, requestedOn: (value: any) => boolean) => ({
567
- get: async () => (await isOn()) ? active : inactive,
672
+ get: async () => (await isOn())
673
+ ? active
674
+ : inactive,
568
675
  set: async (value: any) => setOn(requestedOn(value)),
569
- ...(pressMode ? { refreshAfterSet: [name] } : {}),
676
+ ...(pressMode
677
+ ? {
678
+ refreshAfterSet: [name],
679
+ }
680
+ : {}),
570
681
  })
571
682
  const position = () => ({
572
- CurrentPosition: { get: async () => (await isOn()) ? 100 : 0 },
573
- PositionState: { get: async () => 2 },
683
+ CurrentPosition: {
684
+ get: async () => (await isOn())
685
+ ? 100
686
+ : 0,
687
+ },
688
+ PositionState: {
689
+ get: async () => 2,
690
+ },
574
691
  TargetPosition: control('TargetPosition', 100, 0, value => Number(value) > 0),
575
692
  })
576
693
  const active = (value: any) => Number(value) === 1
577
694
  const presentations: Record<string, { type: string, characteristics: Record<string, any> }> = {
578
- switch: { type: 'Switch', characteristics: { On: control('On', true, false, value => value === true) } },
579
- outlet: { type: 'Outlet', characteristics: { On: control('On', true, false, value => value === true) } },
580
- door: { type: 'Door', characteristics: position() },
581
- window: { type: 'Window', characteristics: position() },
582
- windowcovering: { type: 'WindowCovering', characteristics: position() },
583
- garagedoor: { type: 'GarageDoorOpener', characteristics: {
584
- CurrentDoorState: { get: async () => (await isOn()) ? 0 : 1 },
585
- TargetDoorState: control('TargetDoorState', 0, 1, value => Number(value) === 0),
586
- ObstructionDetected: { get: async () => false },
587
- } },
588
- lock: { type: 'LockMechanism', characteristics: {
589
- LockCurrentState: { get: async () => (await isOn()) ? 0 : 1 },
590
- LockTargetState: control('LockTargetState', 0, 1, value => Number(value) === 0),
591
- } },
592
- faucet: { type: 'Faucet', characteristics: { Active: control('Active', 1, 0, active) } },
593
- fan: { type: 'Fanv2', characteristics: { Active: control('Active', 1, 0, active) } },
594
- stateful: { type: 'StatefulProgrammableSwitch', characteristics: {
595
- ProgrammableSwitchEvent: {},
596
- ProgrammableSwitchOutputState: {
597
- ...control('ProgrammableSwitchOutputState', 1, 0, active),
598
- ...(pressMode ? { refreshAfterSet: ['ProgrammableSwitchOutputState'] } : {}),
599
- afterSet: (value: any, service: any, hap: any) => {
600
- if (!pressMode || active(value)) {
601
- service.getCharacteristic(hap.Characteristic.ProgrammableSwitchEvent).sendEventNotification(0)
602
- }
695
+ switch: {
696
+ type: 'Switch',
697
+ characteristics: {
698
+ On: control('On', true, false, value => value === true),
699
+ },
700
+ },
701
+ outlet: {
702
+ type: 'Outlet',
703
+ characteristics: {
704
+ On: control('On', true, false, value => value === true),
705
+ },
706
+ },
707
+ door: {
708
+ type: 'Door',
709
+ characteristics: position(),
710
+ },
711
+ window: {
712
+ type: 'Window',
713
+ characteristics: position(),
714
+ },
715
+ windowcovering: {
716
+ type: 'WindowCovering',
717
+ characteristics: position(),
718
+ },
719
+ garagedoor: {
720
+ type: 'GarageDoorOpener',
721
+ characteristics: {
722
+ CurrentDoorState: {
723
+ get: async () => (await isOn())
724
+ ? 0
725
+ : 1,
726
+ },
727
+ TargetDoorState: control('TargetDoorState', 0, 1, value => Number(value) === 0),
728
+ ObstructionDetected: {
729
+ get: async () => false,
730
+ },
731
+ },
732
+ },
733
+ lock: {
734
+ type: 'LockMechanism',
735
+ characteristics: {
736
+ LockCurrentState: {
737
+ get: async () => (await isOn())
738
+ ? 0
739
+ : 1,
740
+ },
741
+ LockTargetState: control('LockTargetState', 0, 1, value => Number(value) === 0),
742
+ },
743
+ },
744
+ faucet: {
745
+ type: 'Faucet',
746
+ characteristics: {
747
+ Active: control('Active', 1, 0, active),
748
+ },
749
+ },
750
+ fan: {
751
+ type: 'Fanv2',
752
+ characteristics: {
753
+ Active: control('Active', 1, 0, active),
754
+ },
755
+ },
756
+ stateful: {
757
+ type: 'StatefulProgrammableSwitch',
758
+ characteristics: {
759
+ ProgrammableSwitchEvent: {},
760
+ ProgrammableSwitchOutputState: {
761
+ ...control('ProgrammableSwitchOutputState', 1, 0, active),
762
+ ...(pressMode
763
+ ? {
764
+ refreshAfterSet: ['ProgrammableSwitchOutputState'],
765
+ }
766
+ : {}),
767
+ afterSet: (value: any, service: any, hap: any) => {
768
+ if (!pressMode || active(value)) {
769
+ service.getCharacteristic(hap.Characteristic.ProgrammableSwitchEvent).sendEventNotification(0)
770
+ }
771
+ },
603
772
  },
604
773
  },
605
- } },
774
+ },
606
775
  }
607
776
 
608
777
  return {
@@ -616,6 +785,8 @@ export class CurtainDevice extends GenericDevice {
616
785
  private lastTargetPosition = 0
617
786
  private positionState = 2
618
787
  private preferLocalPositionUntil = 0
788
+ private lastAdvertisement?: object
789
+ private positionBeforeCommand?: number
619
790
 
620
791
  private clampHomeKitPosition(position: number): number {
621
792
  return Math.max(0, Math.min(100, Math.round(Number(position))))
@@ -630,6 +801,21 @@ export class CurtainDevice extends GenericDevice {
630
801
  }
631
802
 
632
803
  private async getPositionForHomeKit(): Promise<number> {
804
+ const device = this.client?.getCachedDevice?.(this.opts.id)
805
+ if (device?.hasBLE?.()) {
806
+ const advertisement = device.getInfo?.()?.bleServiceData
807
+ if (advertisement && advertisement !== this.lastAdvertisement) {
808
+ this.lastAdvertisement = advertisement
809
+ if (typeof advertisement.position === 'number' && Number.isFinite(advertisement.position)) {
810
+ const position = this.toHomeKitPosition(advertisement.position)
811
+ if (this.positionBeforeCommand === undefined || position !== this.positionBeforeCommand) {
812
+ this.lastKnownPosition = position
813
+ this.lastTargetPosition = position
814
+ }
815
+ }
816
+ }
817
+ return this.lastKnownPosition
818
+ }
633
819
  if (Date.now() < this.preferLocalPositionUntil) {
634
820
  return this.lastKnownPosition
635
821
  }
@@ -637,6 +823,9 @@ export class CurtainDevice extends GenericDevice {
637
823
  this.getState(),
638
824
  new Promise(resolve => setTimeout(resolve, 1000)),
639
825
  ])
826
+ if (Date.now() < this.preferLocalPositionUntil) {
827
+ return this.lastKnownPosition
828
+ }
640
829
  if (typeof state?.position === 'number') {
641
830
  this.lastKnownPosition = this.toHomeKitPosition(Number(state.position))
642
831
  this.lastTargetPosition = this.lastKnownPosition
@@ -652,7 +841,8 @@ export class CurtainDevice extends GenericDevice {
652
841
  characteristics: {
653
842
  CurrentPosition: {
654
843
  get: async () => {
655
- return this.getPositionForHomeKit()
844
+ void this.getPositionForHomeKit().catch(() => {})
845
+ return this.lastKnownPosition
656
846
  },
657
847
  },
658
848
  PositionState: {
@@ -660,17 +850,40 @@ export class CurtainDevice extends GenericDevice {
660
850
  },
661
851
  TargetPosition: {
662
852
  get: async () => {
663
- await this.getPositionForHomeKit()
853
+ void this.getPositionForHomeKit().catch(() => {})
664
854
  return this.lastTargetPosition
665
855
  },
666
856
  set: async (v: any) => {
667
857
  const position = this.clampHomeKitPosition(Number(v))
858
+ const bleDevice = this.client?.getCachedDevice?.(this.opts.id)
859
+ if (bleDevice?.hasBLE?.()) {
860
+ await this.getPositionForHomeKit()
861
+ }
862
+ const previousPosition = this.lastKnownPosition
668
863
  this.lastTargetPosition = position
669
- this.positionState = position > this.lastKnownPosition ? 1 : position < this.lastKnownPosition ? 0 : 2
670
- await this.setState({ position: this.toSwitchBotPosition(position) })
671
- this.lastKnownPosition = position
672
- this.preferLocalPositionUntil = Date.now() + 30000
673
- this.positionState = 2
864
+ this.positionState = position > this.lastKnownPosition
865
+ ? 1
866
+ : position < this.lastKnownPosition
867
+ ? 0
868
+ : 2
869
+ try {
870
+ const result = await this.setState({
871
+ position: this.toSwitchBotPosition(position),
872
+ })
873
+ if (result === false || result?.success === false) {
874
+ throw new Error(result?.reason ?? 'Curtain position command failed')
875
+ }
876
+ this.lastKnownPosition = position
877
+ if (bleDevice?.hasBLE?.()) {
878
+ this.positionBeforeCommand = previousPosition
879
+ }
880
+ this.preferLocalPositionUntil = Date.now() + 30000
881
+ } catch (error) {
882
+ this.lastTargetPosition = this.lastKnownPosition
883
+ throw error
884
+ } finally {
885
+ this.positionState = 2
886
+ }
674
887
  },
675
888
  refreshAfterSet: ['CurrentPosition', 'TargetPosition', 'PositionState'],
676
889
  },
@@ -689,16 +902,22 @@ export class CurtainDevice extends GenericDevice {
689
902
  currentPositionLiftPercent100ths: {
690
903
  read: async () => {
691
904
  const s = await this.getState()
692
- return typeof s.position === 'number' ? Math.round(s.position * 100) : 0
905
+ return typeof s.position === 'number'
906
+ ? Math.round(s.position * 100)
907
+ : 0
693
908
  },
694
909
  write: undefined,
695
910
  },
696
911
  targetPositionLiftPercent100ths: {
697
912
  read: async () => {
698
913
  const s = await this.getState()
699
- return typeof s.position === 'number' ? Math.round(s.position * 100) : 0
914
+ return typeof s.position === 'number'
915
+ ? Math.round(s.position * 100)
916
+ : 0
700
917
  },
701
- write: async (v: any) => this.setState({ position: Math.round(Number(v) / 100) }),
918
+ write: async (v: any) => this.setState({
919
+ position: Math.round(Number(v) / 100),
920
+ }),
702
921
  },
703
922
  operationalStatus: {
704
923
  read: async () => state.operationalStatus ?? { global: 0, lift: 0, tilt: 0 },
@@ -726,16 +945,22 @@ export class CurtainDevice extends GenericDevice {
726
945
  attributes.currentPositionTiltPercent100ths = {
727
946
  read: async () => {
728
947
  const s = await this.getState()
729
- return typeof s.tilt === 'number' ? Math.round(s.tilt * 100) : 0
948
+ return typeof s.tilt === 'number'
949
+ ? Math.round(s.tilt * 100)
950
+ : 0
730
951
  },
731
952
  write: undefined,
732
953
  }
733
954
  attributes.targetPositionTiltPercent100ths = {
734
955
  read: async () => {
735
956
  const s = await this.getState()
736
- return typeof s.tilt === 'number' ? Math.round(s.tilt * 100) : 0
957
+ return typeof s.tilt === 'number'
958
+ ? Math.round(s.tilt * 100)
959
+ : 0
737
960
  },
738
- write: async (v: any) => this.setState({ tilt: Math.round(Number(v) / 100) }),
961
+ write: async (v: any) => this.setState({
962
+ tilt: Math.round(Number(v) / 100),
963
+ }),
739
964
  }
740
965
  }
741
966
  const windowCoveringCluster = {
@@ -777,10 +1002,14 @@ export class FanDevice extends GenericDevice {
777
1002
  RotationSpeed: {
778
1003
  get: async () => {
779
1004
  const s = await this.getState()
780
- return typeof s.speed === 'number' ? s.speed : 0
1005
+ return typeof s.speed === 'number'
1006
+ ? s.speed
1007
+ : 0
781
1008
  },
782
1009
  set: async (v: any) => {
783
- await this.setState({ speed: Number(v) })
1010
+ await this.setState({
1011
+ speed: Number(v),
1012
+ })
784
1013
  },
785
1014
  },
786
1015
  },
@@ -794,7 +1023,11 @@ export class FanDevice extends GenericDevice {
794
1023
 
795
1024
  // Oscillation support
796
1025
  if (typeof change.oscillate === 'boolean') {
797
- const body = { command: 'setOscillation', parameter: change.oscillate ? 'on' : 'off', commandType: 'command' }
1026
+ const body = {
1027
+ command: 'setOscillation',
1028
+ parameter: change.oscillate ? 'on' : 'off',
1029
+ commandType: 'command',
1030
+ }
798
1031
  try {
799
1032
  return await this.client.setDeviceState(this.opts.id, body)
800
1033
  } catch (err) {
@@ -840,9 +1073,15 @@ export class FanDevice extends GenericDevice {
840
1073
  type: 'OnOff',
841
1074
  clusterId: MATTER_CLUSTER_IDS.OnOff,
842
1075
  attributes: {
843
- onOff: { read: async () => { const s = await this.getState(); return !!(s && (s.on === true || s.state === 'on')) }, write: async (v: any) => this.setState({ on: !!v }) },
1076
+ onOff: {
1077
+ read: async () => { const s = await this.getState(); return !!(s && (s.on === true || s.state === 'on')) },
1078
+ write: async (v: any) => this.setState({ on: !!v }),
1079
+ },
844
1080
  // numeric attribute id for onOff
845
- [MATTER_ATTRIBUTE_IDS.OnOff.OnOff]: { read: async () => { const s = await this.getState(); return !!(s && (s.on === true || s.state === 'on')) }, write: async (v: any) => this.setState({ on: !!v }) },
1081
+ [MATTER_ATTRIBUTE_IDS.OnOff.OnOff]: {
1082
+ read: async () => { const s = await this.getState(); return !!(s && (s.on === true || s.state === 'on')) },
1083
+ write: async (v: any) => this.setState({ on: !!v }),
1084
+ },
846
1085
  },
847
1086
  },
848
1087
  {
@@ -850,10 +1089,34 @@ export class FanDevice extends GenericDevice {
850
1089
  type: 'FanControl',
851
1090
  clusterId: MATTER_CLUSTER_IDS.FanControl,
852
1091
  attributes: {
853
- rotationSpeed: { read: async () => { const s = await this.getState(); return typeof s.speed === 'number' ? s.speed : 0 }, write: async (v: any) => this.setState({ speed: Number(v) }) },
854
- [MATTER_ATTRIBUTE_IDS.FanControl.SpeedCurrent]: { read: async () => { const s = await this.getState(); return typeof s.speed === 'number' ? s.speed : 0 }, write: async (v: any) => this.setState({ speed: Number(v) }) },
855
- oscillation: { read: async () => { const s = await this.getState(); return !!s?.oscillating }, write: async (v: any) => this.setState({ oscillate: !!v }) },
856
- swingMode: { read: async () => { const s = await this.getState(); return s?.swingMode ?? null }, write: async (v: any) => this.setState({ swingMode: v }) },
1092
+ rotationSpeed: {
1093
+ read: async () => {
1094
+ const s = await this.getState(); return typeof s.speed === 'number'
1095
+ ? s.speed
1096
+ : 0
1097
+ },
1098
+ write: async (v: any) => this.setState({
1099
+ speed: Number(v),
1100
+ }),
1101
+ },
1102
+ [MATTER_ATTRIBUTE_IDS.FanControl.SpeedCurrent]: {
1103
+ read: async () => {
1104
+ const s = await this.getState(); return typeof s.speed === 'number'
1105
+ ? s.speed
1106
+ : 0
1107
+ },
1108
+ write: async (v: any) => this.setState({
1109
+ speed: Number(v),
1110
+ }),
1111
+ },
1112
+ oscillation: {
1113
+ read: async () => { const s = await this.getState(); return !!s?.oscillating },
1114
+ write: async (v: any) => this.setState({ oscillate: !!v }),
1115
+ },
1116
+ swingMode: {
1117
+ read: async () => { const s = await this.getState(); return s?.swingMode ?? null },
1118
+ write: async (v: any) => this.setState({ swingMode: v }),
1119
+ },
857
1120
  },
858
1121
  },
859
1122
  ],
@@ -880,10 +1143,14 @@ export class LightDevice extends GenericDevice {
880
1143
  props: { minValue: 0, maxValue: 100, minStep: 1 },
881
1144
  get: async () => {
882
1145
  const s = await this.getState()
883
- return typeof s.brightness === 'number' ? s.brightness : 100
1146
+ return typeof s.brightness === 'number'
1147
+ ? s.brightness
1148
+ : 100
884
1149
  },
885
1150
  set: async (v: any) => {
886
- await this.setState({ brightness: Number(v) })
1151
+ await this.setState({
1152
+ brightness: Number(v),
1153
+ })
887
1154
  },
888
1155
  },
889
1156
  Hue: {
@@ -920,7 +1187,9 @@ export class LightDevice extends GenericDevice {
920
1187
  return 0
921
1188
  },
922
1189
  set: async (v: any) => {
923
- await this.setState({ hue: Number(v) })
1190
+ await this.setState({
1191
+ hue: Number(v),
1192
+ })
924
1193
  },
925
1194
  },
926
1195
  Saturation: {
@@ -937,13 +1206,17 @@ export class LightDevice extends GenericDevice {
937
1206
  const b = Number.parseInt(cleaned.substr(4, 2), 16) / 255
938
1207
  const mx = Math.max(r, g, b); const mn = Math.min(r, g, b)
939
1208
  const d = mx - mn
940
- const sat = mx === 0 ? 0 : Math.round((d / mx) * 100)
1209
+ const sat = mx === 0
1210
+ ? 0
1211
+ : Math.round((d / mx) * 100)
941
1212
  return sat
942
1213
  }
943
1214
  return 0
944
1215
  },
945
1216
  set: async (v: any) => {
946
- await this.setState({ saturation: Number(v) })
1217
+ await this.setState({
1218
+ saturation: Number(v),
1219
+ })
947
1220
  },
948
1221
  },
949
1222
  ColorTemperature: {
@@ -960,7 +1233,9 @@ export class LightDevice extends GenericDevice {
960
1233
  return 400
961
1234
  },
962
1235
  set: async (v: any) => {
963
- await this.setState({ colorTemperature: Number(v) })
1236
+ await this.setState({
1237
+ colorTemperature: Number(v),
1238
+ })
964
1239
  },
965
1240
  },
966
1241
  },
@@ -1029,8 +1304,14 @@ export class LightDevice extends GenericDevice {
1029
1304
  type: 'OnOff',
1030
1305
  clusterId: MATTER_CLUSTER_IDS.OnOff,
1031
1306
  attributes: {
1032
- onOff: { read: async () => { const s = await this.getState(); return !!(s && (s.on === true || s.state === 'on' || s.power === 'on')) }, write: async (v: any) => this.setState({ on: !!v }) },
1033
- [MATTER_ATTRIBUTE_IDS.OnOff.OnOff]: { read: async () => { const s = await this.getState(); return !!(s && (s.on === true || s.state === 'on' || s.power === 'on')) }, write: async (v: any) => this.setState({ on: !!v }) },
1307
+ onOff: {
1308
+ read: async () => { const s = await this.getState(); return !!(s && (s.on === true || s.state === 'on' || s.power === 'on')) },
1309
+ write: async (v: any) => this.setState({ on: !!v }),
1310
+ },
1311
+ [MATTER_ATTRIBUTE_IDS.OnOff.OnOff]: {
1312
+ read: async () => { const s = await this.getState(); return !!(s && (s.on === true || s.state === 'on' || s.power === 'on')) },
1313
+ write: async (v: any) => this.setState({ on: !!v }),
1314
+ },
1034
1315
  },
1035
1316
  },
1036
1317
  {
@@ -1038,8 +1319,26 @@ export class LightDevice extends GenericDevice {
1038
1319
  type: 'LevelControl',
1039
1320
  clusterId: MATTER_CLUSTER_IDS.LevelControl,
1040
1321
  attributes: {
1041
- currentLevel: { read: async () => { const s = await this.getState(); return typeof s.brightness === 'number' ? s.brightness : 100 }, write: async (v: any) => this.setState({ brightness: Number(v) }) },
1042
- [MATTER_ATTRIBUTE_IDS.LevelControl.CurrentLevel]: { read: async () => { const s = await this.getState(); return typeof s.brightness === 'number' ? s.brightness : 100 }, write: async (v: any) => this.setState({ brightness: Number(v) }) },
1322
+ currentLevel: {
1323
+ read: async () => {
1324
+ const s = await this.getState(); return typeof s.brightness === 'number'
1325
+ ? s.brightness
1326
+ : 100
1327
+ },
1328
+ write: async (v: any) => this.setState({
1329
+ brightness: Number(v),
1330
+ }),
1331
+ },
1332
+ [MATTER_ATTRIBUTE_IDS.LevelControl.CurrentLevel]: {
1333
+ read: async () => {
1334
+ const s = await this.getState(); return typeof s.brightness === 'number'
1335
+ ? s.brightness
1336
+ : 100
1337
+ },
1338
+ write: async (v: any) => this.setState({
1339
+ brightness: Number(v),
1340
+ }),
1341
+ },
1043
1342
  },
1044
1343
  },
1045
1344
  {
@@ -1048,17 +1347,65 @@ export class LightDevice extends GenericDevice {
1048
1347
  clusterId: MATTER_CLUSTER_IDS.ColorControl,
1049
1348
  attributes: {
1050
1349
  // Required colorMode attribute for Matter conformance (0 = currentHueAndSaturation)
1051
- colorMode: { read: async () => 0 },
1052
- colorHue: { read: async () => { const s = await this.getState(); return typeof s.hue === 'number' ? s.hue : 0 }, write: async (v: any) => this.setState({ hue: Number(v) }) },
1053
- [MATTER_ATTRIBUTE_IDS.ColorControl.CurrentHue]: { read: async () => { const s = await this.getState(); return typeof s.hue === 'number' ? s.hue : 0 }, write: async (v: any) => this.setState({ hue: Number(v) }) },
1054
- colorSaturation: { read: async () => { const s = await this.getState(); return typeof s.saturation === 'number' ? s.saturation : 0 }, write: async (v: any) => this.setState({ saturation: Number(v) }) },
1055
- [MATTER_ATTRIBUTE_IDS.ColorControl.CurrentSaturation]: { read: async () => { const s = await this.getState(); return typeof s.saturation === 'number' ? s.saturation : 0 }, write: async (v: any) => this.setState({ saturation: Number(v) }) },
1056
- colorTemperature: { read: async () => {
1057
- const s = await this.getState(); if (typeof s.colorTemperature === 'number') { return s.colorTemperature } if (typeof s.kelvin === 'number') { return Math.round(1000000 / s.kelvin) } return 400
1058
- }, write: async (v: any) => this.setState({ colorTemperature: Number(v) }) },
1059
- [MATTER_ATTRIBUTE_IDS.ColorControl.ColorTemperatureMireds]: { read: async () => {
1060
- const s = await this.getState(); if (typeof s.colorTemperature === 'number') { return s.colorTemperature } if (typeof s.kelvin === 'number') { return Math.round(1000000 / s.kelvin) } return 400
1061
- }, write: async (v: any) => this.setState({ colorTemperature: Number(v) }) },
1350
+ colorMode: {
1351
+ read: async () => 0,
1352
+ },
1353
+ colorHue: {
1354
+ read: async () => {
1355
+ const s = await this.getState(); return typeof s.hue === 'number'
1356
+ ? s.hue
1357
+ : 0
1358
+ },
1359
+ write: async (v: any) => this.setState({
1360
+ hue: Number(v),
1361
+ }),
1362
+ },
1363
+ [MATTER_ATTRIBUTE_IDS.ColorControl.CurrentHue]: {
1364
+ read: async () => {
1365
+ const s = await this.getState(); return typeof s.hue === 'number'
1366
+ ? s.hue
1367
+ : 0
1368
+ },
1369
+ write: async (v: any) => this.setState({
1370
+ hue: Number(v),
1371
+ }),
1372
+ },
1373
+ colorSaturation: {
1374
+ read: async () => {
1375
+ const s = await this.getState(); return typeof s.saturation === 'number'
1376
+ ? s.saturation
1377
+ : 0
1378
+ },
1379
+ write: async (v: any) => this.setState({
1380
+ saturation: Number(v),
1381
+ }),
1382
+ },
1383
+ [MATTER_ATTRIBUTE_IDS.ColorControl.CurrentSaturation]: {
1384
+ read: async () => {
1385
+ const s = await this.getState(); return typeof s.saturation === 'number'
1386
+ ? s.saturation
1387
+ : 0
1388
+ },
1389
+ write: async (v: any) => this.setState({
1390
+ saturation: Number(v),
1391
+ }),
1392
+ },
1393
+ colorTemperature: {
1394
+ read: async () => {
1395
+ const s = await this.getState(); if (typeof s.colorTemperature === 'number') { return s.colorTemperature } if (typeof s.kelvin === 'number') { return Math.round(1000000 / s.kelvin) } return 400
1396
+ },
1397
+ write: async (v: any) => this.setState({
1398
+ colorTemperature: Number(v),
1399
+ }),
1400
+ },
1401
+ [MATTER_ATTRIBUTE_IDS.ColorControl.ColorTemperatureMireds]: {
1402
+ read: async () => {
1403
+ const s = await this.getState(); if (typeof s.colorTemperature === 'number') { return s.colorTemperature } if (typeof s.kelvin === 'number') { return Math.round(1000000 / s.kelvin) } return 400
1404
+ },
1405
+ write: async (v: any) => this.setState({
1406
+ colorTemperature: Number(v),
1407
+ }),
1408
+ },
1062
1409
  },
1063
1410
  },
1064
1411
  ],
@@ -1092,7 +1439,11 @@ export class MotionSensorDevice extends GenericDevice {
1092
1439
 
1093
1440
  // Oscillation support
1094
1441
  if (typeof change.oscillate === 'boolean') {
1095
- const body = { command: 'setOscillation', parameter: change.oscillate ? 'on' : 'off', commandType: 'command' }
1442
+ const body = {
1443
+ command: 'setOscillation',
1444
+ parameter: change.oscillate ? 'on' : 'off',
1445
+ commandType: 'command',
1446
+ }
1096
1447
  try {
1097
1448
  return await this.client.setDeviceState(this.opts.id, body)
1098
1449
  } catch (err) {
@@ -1137,7 +1488,9 @@ export class ContactSensorDevice extends GenericDevice {
1137
1488
  ContactSensorState: {
1138
1489
  get: async () => {
1139
1490
  const s = await this.getState()
1140
- return s && s.open ? 1 : 0
1491
+ return s && s.open
1492
+ ? 1
1493
+ : 0
1141
1494
  },
1142
1495
  },
1143
1496
  },
@@ -1164,13 +1517,17 @@ export class LockDevice extends GenericDevice {
1164
1517
  LockCurrentState: {
1165
1518
  get: async () => {
1166
1519
  const s = await this.getState()
1167
- return s && s.locked ? 1 : 0
1520
+ return s && s.locked
1521
+ ? 1
1522
+ : 0
1168
1523
  },
1169
1524
  },
1170
1525
  LockTargetState: {
1171
1526
  get: async () => {
1172
1527
  const s = await this.getState()
1173
- return s && s.locked ? 1 : 0
1528
+ return s && s.locked
1529
+ ? 1
1530
+ : 0
1174
1531
  },
1175
1532
  set: async (v: any) => {
1176
1533
  await this.setState({ locked: !!v })
@@ -1198,7 +1555,11 @@ export class LockDevice extends GenericDevice {
1198
1555
 
1199
1556
  if (action === 'removeUser' && (change.user || change.userId)) {
1200
1557
  const user = change.user ?? change.userId
1201
- const body = { command: 'removeUserCode', parameter: String(user), commandType: 'command' }
1558
+ const body = {
1559
+ command: 'removeUserCode',
1560
+ parameter: String(user),
1561
+ commandType: 'command',
1562
+ }
1202
1563
  return await this.client.setDeviceState(this.opts.id, body)
1203
1564
  }
1204
1565
 
@@ -1224,7 +1585,11 @@ export class LockDevice extends GenericDevice {
1224
1585
  // Support setting lock PIN/passcode via `pin` or `passcode` (fallback)
1225
1586
  const pin = change?.pin ?? change?.passcode ?? change?.code
1226
1587
  if (typeof pin === 'string' || typeof pin === 'number') {
1227
- const body = { command: 'setLockPin', parameter: String(pin), commandType: 'command' }
1588
+ const body = {
1589
+ command: 'setLockPin',
1590
+ parameter: String(pin),
1591
+ commandType: 'command',
1592
+ }
1228
1593
  try {
1229
1594
  return await this.client.setDeviceState(this.opts.id, body)
1230
1595
  } catch (err) {
@@ -1251,8 +1616,14 @@ export class LockDevice extends GenericDevice {
1251
1616
  type: 'DoorLock',
1252
1617
  clusterId: MATTER_CLUSTER_IDS.DoorLock,
1253
1618
  attributes: {
1254
- lockState: { read: async () => { const s = await this.getState(); return !!(s && s.locked) }, write: async (v: any) => this.setState({ locked: !!v }) },
1255
- [MATTER_ATTRIBUTE_IDS.DoorLock.LockState]: { read: async () => { const s = await this.getState(); return !!(s && s.locked) }, write: async (v: any) => this.setState({ locked: !!v }) },
1619
+ lockState: {
1620
+ read: async () => { const s = await this.getState(); return !!(s && s.locked) },
1621
+ write: async (v: any) => this.setState({ locked: !!v }),
1622
+ },
1623
+ [MATTER_ATTRIBUTE_IDS.DoorLock.LockState]: {
1624
+ read: async () => { const s = await this.getState(); return !!(s && s.locked) },
1625
+ write: async (v: any) => this.setState({ locked: !!v }),
1626
+ },
1256
1627
  },
1257
1628
  },
1258
1629
  {
@@ -1260,10 +1631,19 @@ export class LockDevice extends GenericDevice {
1260
1631
  type: 'DoorLockUserManagement',
1261
1632
  clusterId: 0x0301,
1262
1633
  attributes: {
1263
- addUser: { write: async (v: any) => this.setState({ action: 'addUser', user: v?.user, pin: v?.pin }) },
1264
- removeUser: { write: async (v: any) => this.setState({ action: 'removeUser', user: v?.user }) },
1265
- listUsers: { read: async () => { const r = await this.setState({ action: 'listUsers' }); return r }, write: undefined },
1266
- unlockWithPin: { write: async (v: any) => this.setState({ action: 'unlockWithPin', pin: v?.pin }) },
1634
+ addUser: {
1635
+ write: async (v: any) => this.setState({ action: 'addUser', user: v?.user, pin: v?.pin }),
1636
+ },
1637
+ removeUser: {
1638
+ write: async (v: any) => this.setState({ action: 'removeUser', user: v?.user }),
1639
+ },
1640
+ listUsers: {
1641
+ read: async () => { const r = await this.setState({ action: 'listUsers' }); return r },
1642
+ write: undefined,
1643
+ },
1644
+ unlockWithPin: {
1645
+ write: async (v: any) => this.setState({ action: 'unlockWithPin', pin: v?.pin }),
1646
+ },
1267
1647
  },
1268
1648
  },
1269
1649
  ],
@@ -1281,7 +1661,9 @@ export class HumidifierDevice extends GenericDevice {
1281
1661
  Active: {
1282
1662
  get: async () => {
1283
1663
  const s = await this.getState()
1284
- return s && s.on ? 1 : 0
1664
+ return s && s.on
1665
+ ? 1
1666
+ : 0
1285
1667
  },
1286
1668
  set: async (v: any) => {
1287
1669
  await this.setState({ on: v === 1 })
@@ -1290,7 +1672,9 @@ export class HumidifierDevice extends GenericDevice {
1290
1672
  CurrentHumidifierDehumidifierState: {
1291
1673
  get: async () => {
1292
1674
  const s = await this.getState()
1293
- return s && s.on ? 2 : 0 // 0 = Inactive, 2 = Humidifying
1675
+ return s && s.on
1676
+ ? 2
1677
+ : 0 // 0 = Inactive, 2 = Humidifying
1294
1678
  },
1295
1679
  },
1296
1680
  TargetHumidifierDehumidifierState: {
@@ -1302,16 +1686,22 @@ export class HumidifierDevice extends GenericDevice {
1302
1686
  CurrentRelativeHumidity: {
1303
1687
  get: async () => {
1304
1688
  const s = await this.getState()
1305
- return typeof s.humidity === 'number' ? s.humidity : 0
1689
+ return typeof s.humidity === 'number'
1690
+ ? s.humidity
1691
+ : 0
1306
1692
  },
1307
1693
  },
1308
1694
  RelativeHumidityHumidifierThreshold: {
1309
1695
  get: async () => {
1310
1696
  const s = await this.getState()
1311
- return typeof s.targetHumidity === 'number' ? s.targetHumidity : 50
1697
+ return typeof s.targetHumidity === 'number'
1698
+ ? s.targetHumidity
1699
+ : 50
1312
1700
  },
1313
1701
  set: async (v: any) => {
1314
- await this.setState({ humidity: Number(v) })
1702
+ await this.setState({
1703
+ humidity: Number(v),
1704
+ })
1315
1705
  },
1316
1706
  },
1317
1707
  },
@@ -1338,7 +1728,11 @@ export class HumidifierDevice extends GenericDevice {
1338
1728
  }
1339
1729
 
1340
1730
  if (typeof change.dry === 'boolean') {
1341
- const body = { command: 'setDry', parameter: change.dry ? 'on' : 'off', commandType: 'command' }
1731
+ const body = {
1732
+ command: 'setDry',
1733
+ parameter: change.dry ? 'on' : 'off',
1734
+ commandType: 'command',
1735
+ }
1342
1736
  try {
1343
1737
  return await this.client.setDeviceState(this.opts.id, body)
1344
1738
  } catch (err) {
@@ -1367,16 +1761,36 @@ export class HumidifierMatterDevice extends HumidifierDevice {
1367
1761
  type: 'RelativeHumiditySensor',
1368
1762
  clusterId: MATTER_CLUSTER_IDS.RelativeHumidityMeasurement,
1369
1763
  attributes: {
1370
- currentRelativeHumidity: { read: async () => { const s = await this.getState(); return typeof s.humidity === 'number' ? s.humidity : 0 }, write: undefined },
1371
- [MATTER_ATTRIBUTE_IDS.RelativeHumidityMeasurement.MeasuredValue]: { read: async () => { const s = await this.getState(); return typeof s.humidity === 'number' ? s.humidity : 0 }, write: undefined },
1764
+ currentRelativeHumidity: {
1765
+ read: async () => {
1766
+ const s = await this.getState(); return typeof s.humidity === 'number'
1767
+ ? s.humidity
1768
+ : 0
1769
+ },
1770
+ write: undefined,
1771
+ },
1772
+ [MATTER_ATTRIBUTE_IDS.RelativeHumidityMeasurement.MeasuredValue]: {
1773
+ read: async () => {
1774
+ const s = await this.getState(); return typeof s.humidity === 'number'
1775
+ ? s.humidity
1776
+ : 0
1777
+ },
1778
+ write: undefined,
1779
+ },
1372
1780
  },
1373
1781
  },
1374
1782
  {
1375
1783
  type: 'OnOff',
1376
1784
  clusterId: MATTER_CLUSTER_IDS.OnOff,
1377
1785
  attributes: {
1378
- onOff: { read: async () => { const s = await this.getState(); return !!s?.on }, write: async (v: any) => this.setState({ on: !!v }) },
1379
- [MATTER_ATTRIBUTE_IDS.OnOff.OnOff]: { read: async () => { const s = await this.getState(); return !!s?.on }, write: async (v: any) => this.setState({ on: !!v }) },
1786
+ onOff: {
1787
+ read: async () => { const s = await this.getState(); return !!s?.on },
1788
+ write: async (v: any) => this.setState({ on: !!v }),
1789
+ },
1790
+ [MATTER_ATTRIBUTE_IDS.OnOff.OnOff]: {
1791
+ read: async () => { const s = await this.getState(); return !!s?.on },
1792
+ write: async (v: any) => this.setState({ on: !!v }),
1793
+ },
1380
1794
  },
1381
1795
  },
1382
1796
  ],
@@ -1394,7 +1808,9 @@ export class TemperatureSensorDevice extends GenericDevice {
1394
1808
  CurrentTemperature: {
1395
1809
  get: async () => {
1396
1810
  const s = await this.getState()
1397
- return typeof s.temperature === 'number' ? s.temperature : 0
1811
+ return typeof s.temperature === 'number'
1812
+ ? s.temperature
1813
+ : 0
1398
1814
  },
1399
1815
  },
1400
1816
  },
@@ -1426,7 +1842,9 @@ export class PlugDevice extends GenericDevice {
1426
1842
  OutletInUse: {
1427
1843
  get: async () => {
1428
1844
  const s = await this.getState()
1429
- return s && s.inUse ? 1 : 0
1845
+ return s && s.inUse
1846
+ ? 1
1847
+ : 0
1430
1848
  },
1431
1849
  },
1432
1850
  },
@@ -1459,7 +1877,9 @@ export class MeterDevice extends GenericDevice {
1459
1877
  /** The ppm at which HomeKit reports carbon dioxide as detected. */
1460
1878
  protected co2AbnormalThreshold(): number {
1461
1879
  const value = Number((this.cfg as any)?.co2AbnormalThreshold)
1462
- return Number.isFinite(value) && value > 0 ? value : CO2_ABNORMAL_PPM_DEFAULT
1880
+ return Number.isFinite(value) && value > 0
1881
+ ? value
1882
+ : CO2_ABNORMAL_PPM_DEFAULT
1463
1883
  }
1464
1884
 
1465
1885
  createHAPAccessory(api: any) {
@@ -1470,7 +1890,9 @@ export class MeterDevice extends GenericDevice {
1470
1890
  CurrentTemperature: {
1471
1891
  get: async () => {
1472
1892
  const s = await this.getState()
1473
- return typeof s.temperature === 'number' ? s.temperature : 0
1893
+ return typeof s.temperature === 'number'
1894
+ ? s.temperature
1895
+ : 0
1474
1896
  },
1475
1897
  },
1476
1898
  },
@@ -1481,7 +1903,9 @@ export class MeterDevice extends GenericDevice {
1481
1903
  CurrentRelativeHumidity: {
1482
1904
  get: async () => {
1483
1905
  const s = await this.getState()
1484
- return typeof s.humidity === 'number' ? s.humidity : 0
1906
+ return typeof s.humidity === 'number'
1907
+ ? s.humidity
1908
+ : 0
1485
1909
  },
1486
1910
  },
1487
1911
  },
@@ -1491,7 +1915,9 @@ export class MeterDevice extends GenericDevice {
1491
1915
  if (this.reportsCO2()) {
1492
1916
  const co2 = async (): Promise<number> => {
1493
1917
  const s = await this.getState()
1494
- return typeof s.co2 === 'number' ? s.co2 : 0
1918
+ return typeof s.co2 === 'number'
1919
+ ? s.co2
1920
+ : 0
1495
1921
  }
1496
1922
  services.push({
1497
1923
  type: 'CarbonDioxideSensor',
@@ -1500,7 +1926,9 @@ export class MeterDevice extends GenericDevice {
1500
1926
  get: co2,
1501
1927
  },
1502
1928
  CarbonDioxideDetected: {
1503
- get: async () => ((await co2()) >= this.co2AbnormalThreshold() ? 1 : 0),
1929
+ get: async () => ((await co2()) >= this.co2AbnormalThreshold()
1930
+ ? 1
1931
+ : 0),
1504
1932
  },
1505
1933
  },
1506
1934
  })
@@ -1523,7 +1951,9 @@ export class MeterDevice extends GenericDevice {
1523
1951
  get: battery,
1524
1952
  },
1525
1953
  StatusLowBattery: {
1526
- get: async () => ((await battery()) < 20 ? 1 : 0),
1954
+ get: async () => ((await battery()) < 20
1955
+ ? 1
1956
+ : 0),
1527
1957
  },
1528
1958
  ChargingState: {
1529
1959
  get: async () => 2,
@@ -1567,7 +1997,13 @@ export class WaterDetectorDevice extends GenericDevice {
1567
1997
  return status === 1
1568
1998
  }
1569
1999
  if (typeof status === 'string') {
1570
- return ['1', 'leak', 'leaked', 'detected', 'water_leak_detected'].includes(status.toLowerCase())
2000
+ return [
2001
+ '1',
2002
+ 'leak',
2003
+ 'leaked',
2004
+ 'detected',
2005
+ 'water_leak_detected',
2006
+ ].includes(status.toLowerCase())
1571
2007
  }
1572
2008
  return undefined
1573
2009
  }
@@ -1711,7 +2147,9 @@ export class WaterDetectorDevice extends GenericDevice {
1711
2147
  StatusLowBattery: {
1712
2148
  get: () => {
1713
2149
  const b = this.getBatteryFast(api)
1714
- return b < 20 ? 1 : 0
2150
+ return b < 20
2151
+ ? 1
2152
+ : 0
1715
2153
  },
1716
2154
  },
1717
2155
  ChargingState: {