@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
@@ -5,7 +5,9 @@ import { MATTER_ATTRIBUTE_IDS, MATTER_CLUSTER_IDS } from '../utils.js';
5
5
  import { DeviceBase } from './deviceBase.js';
6
6
  function validateBLEResponseLength(buf, expected, context = '', log) {
7
7
  if (!buf || typeof buf.length !== 'number' || buf.length !== expected) {
8
- log.warn(`[BLE] Invalid response length${context ? ` for ${context}` : ''}: expected ${expected}, got ${buf?.length}`);
8
+ log.warn(`[BLE] Invalid response length${context
9
+ ? ` for ${context}`
10
+ : ''}: expected ${expected}, got ${buf?.length}`);
9
11
  return false;
10
12
  }
11
13
  return true;
@@ -176,7 +178,10 @@ export class GenericDevice extends DeviceBase {
176
178
  // at its fallback for the whole window after one transient error.
177
179
  return { id: this.opts.id, type: this.opts.type, unreadable: true };
178
180
  }
179
- this.stateCache = { at: Date.now(), value };
181
+ this.stateCache = {
182
+ at: Date.now(),
183
+ value,
184
+ };
180
185
  return value;
181
186
  })
182
187
  .finally(() => {
@@ -193,7 +198,12 @@ export class GenericDevice extends DeviceBase {
193
198
  if (raw && (raw instanceof Buffer || Array.isArray(raw) || raw instanceof Uint8Array)) {
194
199
  // Default to 12, override per device if needed
195
200
  if (!validateBLEResponseLength(raw, 12, this.opts.type, this.log)) {
196
- return { id: this.opts.id, type: this.opts.type, error: 'invalid_ble_response_length', raw };
201
+ return {
202
+ id: this.opts.id,
203
+ type: this.opts.type,
204
+ error: 'invalid_ble_response_length',
205
+ raw,
206
+ };
197
207
  }
198
208
  }
199
209
  // Normalize common response shapes
@@ -215,7 +225,9 @@ export class GenericDevice extends DeviceBase {
215
225
  }
216
226
  }
217
227
  catch (e) {
218
- this.log?.debug?.(`getState: could not read status for ${this.opts.id}:`, e instanceof Error ? e.message : e);
228
+ this.log?.debug?.(`getState: could not read status for ${this.opts.id}:`, e instanceof Error
229
+ ? e.message
230
+ : e);
219
231
  }
220
232
  }
221
233
  catch (e) {
@@ -338,8 +350,14 @@ export class GenericDevice extends DeviceBase {
338
350
  type: 'OnOff',
339
351
  clusterId: MATTER_CLUSTER_IDS.OnOff,
340
352
  attributes: {
341
- onOff: { read: async () => !!(await this.getState()).on || (await this.getState()).power === 'on' || (await this.getState()).state === 'on', write: async (v) => this.setState({ on: !!v }) },
342
- [MATTER_ATTRIBUTE_IDS.OnOff.OnOff]: { read: async () => !!(await this.getState()).on || (await this.getState()).power === 'on' || (await this.getState()).state === 'on', write: async (v) => this.setState({ on: !!v }) },
353
+ onOff: {
354
+ read: async () => !!(await this.getState()).on || (await this.getState()).power === 'on' || (await this.getState()).state === 'on',
355
+ write: async (v) => this.setState({ on: !!v }),
356
+ },
357
+ [MATTER_ATTRIBUTE_IDS.OnOff.OnOff]: {
358
+ read: async () => !!(await this.getState()).on || (await this.getState()).power === 'on' || (await this.getState()).state === 'on',
359
+ write: async (v) => this.setState({ on: !!v }),
360
+ },
343
361
  },
344
362
  });
345
363
  }
@@ -349,8 +367,18 @@ export class GenericDevice extends DeviceBase {
349
367
  type: 'LevelControl',
350
368
  clusterId: MATTER_CLUSTER_IDS.LevelControl,
351
369
  attributes: {
352
- currentLevel: { read: async () => (await this.getState()).brightness ?? 100, write: async (v) => this.setState({ brightness: Number(v) }) },
353
- [MATTER_ATTRIBUTE_IDS.LevelControl.CurrentLevel]: { read: async () => (await this.getState()).brightness ?? 100, write: async (v) => this.setState({ brightness: Number(v) }) },
370
+ currentLevel: {
371
+ read: async () => (await this.getState()).brightness ?? 100,
372
+ write: async (v) => this.setState({
373
+ brightness: Number(v),
374
+ }),
375
+ },
376
+ [MATTER_ATTRIBUTE_IDS.LevelControl.CurrentLevel]: {
377
+ read: async () => (await this.getState()).brightness ?? 100,
378
+ write: async (v) => this.setState({
379
+ brightness: Number(v),
380
+ }),
381
+ },
354
382
  },
355
383
  });
356
384
  }
@@ -360,15 +388,51 @@ export class GenericDevice extends DeviceBase {
360
388
  type: 'ColorControl',
361
389
  clusterId: MATTER_CLUSTER_IDS.ColorControl,
362
390
  attributes: {
363
- colorMode: { read: async () => 0 },
364
- colorHue: { read: async () => (await this.getState()).hue ?? 0, write: async (v) => this.setState({ hue: Number(v) }) },
365
- [MATTER_ATTRIBUTE_IDS.ColorControl.CurrentHue]: { read: async () => (await this.getState()).hue ?? 0, write: async (v) => this.setState({ hue: Number(v) }) },
366
- colorSaturation: { read: async () => (await this.getState()).saturation ?? 0, write: async (v) => this.setState({ saturation: Number(v) }) },
367
- [MATTER_ATTRIBUTE_IDS.ColorControl.CurrentSaturation]: { read: async () => (await this.getState()).saturation ?? 0, write: async (v) => this.setState({ saturation: Number(v) }) },
391
+ colorMode: {
392
+ read: async () => 0,
393
+ },
394
+ colorHue: {
395
+ read: async () => (await this.getState()).hue ?? 0,
396
+ write: async (v) => this.setState({
397
+ hue: Number(v),
398
+ }),
399
+ },
400
+ [MATTER_ATTRIBUTE_IDS.ColorControl.CurrentHue]: {
401
+ read: async () => (await this.getState()).hue ?? 0,
402
+ write: async (v) => this.setState({
403
+ hue: Number(v),
404
+ }),
405
+ },
406
+ colorSaturation: {
407
+ read: async () => (await this.getState()).saturation ?? 0,
408
+ write: async (v) => this.setState({
409
+ saturation: Number(v),
410
+ }),
411
+ },
412
+ [MATTER_ATTRIBUTE_IDS.ColorControl.CurrentSaturation]: {
413
+ read: async () => (await this.getState()).saturation ?? 0,
414
+ write: async (v) => this.setState({
415
+ saturation: Number(v),
416
+ }),
417
+ },
368
418
  ...(typeof state.colorTemperature === 'number' || typeof state.kelvin === 'number'
369
419
  ? {
370
- colorTemperature: { read: async () => typeof (await this.getState()).colorTemperature === 'number' ? (await this.getState()).colorTemperature : Math.round(1000000 / ((await this.getState()).kelvin ?? 2500)), write: async (v) => this.setState({ colorTemperature: Number(v) }) },
371
- [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) => this.setState({ colorTemperature: Number(v) }) },
420
+ colorTemperature: {
421
+ read: async () => typeof (await this.getState()).colorTemperature === 'number'
422
+ ? (await this.getState()).colorTemperature
423
+ : Math.round(1000000 / ((await this.getState()).kelvin ?? 2500)),
424
+ write: async (v) => this.setState({
425
+ colorTemperature: Number(v),
426
+ }),
427
+ },
428
+ [MATTER_ATTRIBUTE_IDS.ColorControl.ColorTemperatureMireds]: {
429
+ read: async () => typeof (await this.getState()).colorTemperature === 'number'
430
+ ? (await this.getState()).colorTemperature
431
+ : Math.round(1000000 / ((await this.getState()).kelvin ?? 2500)),
432
+ write: async (v) => this.setState({
433
+ colorTemperature: Number(v),
434
+ }),
435
+ },
372
436
  }
373
437
  : {}),
374
438
  },
@@ -380,7 +444,9 @@ export class GenericDevice extends DeviceBase {
380
444
  type: 'TemperatureMeasurement',
381
445
  // No clusterId, not present in MATTER_CLUSTER_IDS
382
446
  attributes: {
383
- measuredValue: { read: async () => (await this.getState()).temperature ?? 0 },
447
+ measuredValue: {
448
+ read: async () => (await this.getState()).temperature ?? 0,
449
+ },
384
450
  },
385
451
  });
386
452
  }
@@ -390,7 +456,9 @@ export class GenericDevice extends DeviceBase {
390
456
  type: 'RelativeHumidityMeasurement',
391
457
  clusterId: MATTER_CLUSTER_IDS.RelativeHumidityMeasurement,
392
458
  attributes: {
393
- measuredValue: { read: async () => (await this.getState()).humidity ?? 0 },
459
+ measuredValue: {
460
+ read: async () => (await this.getState()).humidity ?? 0,
461
+ },
394
462
  },
395
463
  });
396
464
  }
@@ -399,7 +467,9 @@ export class GenericDevice extends DeviceBase {
399
467
  clusters.push({
400
468
  type: 'AirQuality',
401
469
  attributes: {
402
- CO2: { read: async () => (await this.getState()).CO2 ?? 0 },
470
+ CO2: {
471
+ read: async () => (await this.getState()).CO2 ?? 0,
472
+ },
403
473
  },
404
474
  });
405
475
  }
@@ -409,7 +479,12 @@ export class GenericDevice extends DeviceBase {
409
479
  type: 'DoorLock',
410
480
  clusterId: MATTER_CLUSTER_IDS.DoorLock,
411
481
  attributes: {
412
- lockState: { read: async () => (await this.getState()).lockState ?? (await this.getState()).locked ? 1 : 0, write: async (v) => this.setState({ locked: !!v }) },
482
+ lockState: {
483
+ read: async () => (await this.getState()).lockState ?? (await this.getState()).locked
484
+ ? 1
485
+ : 0,
486
+ write: async (v) => this.setState({ locked: !!v }),
487
+ },
413
488
  },
414
489
  });
415
490
  }
@@ -419,7 +494,11 @@ export class GenericDevice extends DeviceBase {
419
494
  type: 'OccupancySensing',
420
495
  // No clusterId, not present in MATTER_CLUSTER_IDS
421
496
  attributes: {
422
- occupancy: { read: async () => (await this.getState()).moveDetected === true || (await this.getState()).motion === true ? 1 : 0 },
497
+ occupancy: {
498
+ read: async () => (await this.getState()).moveDetected === true || (await this.getState()).motion === true
499
+ ? 1
500
+ : 0,
501
+ },
423
502
  },
424
503
  });
425
504
  }
@@ -429,7 +508,11 @@ export class GenericDevice extends DeviceBase {
429
508
  type: 'BooleanState',
430
509
  // No clusterId, not present in MATTER_CLUSTER_IDS
431
510
  attributes: {
432
- stateValue: { read: async () => (await this.getState()).openState === 'open' || (await this.getState()).open === true ? 1 : 0 },
511
+ stateValue: {
512
+ read: async () => (await this.getState()).openState === 'open' || (await this.getState()).open === true
513
+ ? 1
514
+ : 0,
515
+ },
433
516
  },
434
517
  });
435
518
  }
@@ -438,7 +521,11 @@ export class GenericDevice extends DeviceBase {
438
521
  clusters.push({
439
522
  type: 'LeakSensor',
440
523
  attributes: {
441
- leakDetected: { read: async () => (await this.getState()).leak === true || (await this.getState()).status === 1 ? 1 : 0 },
524
+ leakDetected: {
525
+ read: async () => (await this.getState()).leak === true || (await this.getState()).status === 1
526
+ ? 1
527
+ : 0,
528
+ },
442
529
  },
443
530
  });
444
531
  }
@@ -448,9 +535,15 @@ export class GenericDevice extends DeviceBase {
448
535
  type: 'ElectricalMeasurement',
449
536
  // No clusterId, not present in MATTER_CLUSTER_IDS
450
537
  attributes: {
451
- voltage: { read: async () => (await this.getState()).voltage ?? 0 },
452
- power: { read: async () => (await this.getState()).power ?? 0 },
453
- electricCurrent: { read: async () => (await this.getState()).electricCurrent ?? 0 },
538
+ voltage: {
539
+ read: async () => (await this.getState()).voltage ?? 0,
540
+ },
541
+ power: {
542
+ read: async () => (await this.getState()).power ?? 0,
543
+ },
544
+ electricCurrent: {
545
+ read: async () => (await this.getState()).electricCurrent ?? 0,
546
+ },
454
547
  },
455
548
  });
456
549
  }
@@ -460,7 +553,12 @@ export class GenericDevice extends DeviceBase {
460
553
  type: 'FanControl',
461
554
  clusterId: MATTER_CLUSTER_IDS.FanControl,
462
555
  attributes: {
463
- speedCurrent: { read: async () => (await this.getState()).speed ?? (await this.getState()).fanSpeed ?? 0, write: async (v) => this.setState({ speed: Number(v) }) },
556
+ speedCurrent: {
557
+ read: async () => (await this.getState()).speed ?? (await this.getState()).fanSpeed ?? 0,
558
+ write: async (v) => this.setState({
559
+ speed: Number(v),
560
+ }),
561
+ },
464
562
  },
465
563
  });
466
564
  }
@@ -469,7 +567,9 @@ export class GenericDevice extends DeviceBase {
469
567
  clusters.push({
470
568
  type: 'RobotVacuumCleaner',
471
569
  attributes: {
472
- workingStatus: { read: async () => (await this.getState()).workingStatus ?? 'StandBy' },
570
+ workingStatus: {
571
+ read: async () => (await this.getState()).workingStatus ?? 'StandBy',
572
+ },
473
573
  },
474
574
  });
475
575
  }
@@ -506,55 +606,124 @@ export class BotDevice extends GenericDevice {
506
606
  if (pressMode && !on) {
507
607
  return;
508
608
  }
509
- const command = pressMode ? 'press' : on ? 'turnOn' : 'turnOff';
609
+ const command = pressMode
610
+ ? 'press'
611
+ : on ? 'turnOn' : 'turnOff';
510
612
  const result = await this.setState({ command, parameter: 'default', commandType: 'command' });
511
613
  if (result !== true && result?.success !== true) {
512
614
  throw new Error(`SwitchBot Bot ${command} failed: ${result?.reason ?? 'unknown error'}`);
513
615
  }
514
616
  if (!pressMode) {
515
- this.lastCommand = { on, at: Date.now() };
617
+ this.lastCommand = {
618
+ on,
619
+ at: Date.now(),
620
+ };
516
621
  }
517
622
  };
518
623
  const control = (name, active, inactive, requestedOn) => ({
519
- get: async () => (await isOn()) ? active : inactive,
624
+ get: async () => (await isOn())
625
+ ? active
626
+ : inactive,
520
627
  set: async (value) => setOn(requestedOn(value)),
521
- ...(pressMode ? { refreshAfterSet: [name] } : {}),
628
+ ...(pressMode
629
+ ? {
630
+ refreshAfterSet: [name],
631
+ }
632
+ : {}),
522
633
  });
523
634
  const position = () => ({
524
- CurrentPosition: { get: async () => (await isOn()) ? 100 : 0 },
525
- PositionState: { get: async () => 2 },
635
+ CurrentPosition: {
636
+ get: async () => (await isOn())
637
+ ? 100
638
+ : 0,
639
+ },
640
+ PositionState: {
641
+ get: async () => 2,
642
+ },
526
643
  TargetPosition: control('TargetPosition', 100, 0, value => Number(value) > 0),
527
644
  });
528
645
  const active = (value) => Number(value) === 1;
529
646
  const presentations = {
530
- switch: { type: 'Switch', characteristics: { On: control('On', true, false, value => value === true) } },
531
- outlet: { type: 'Outlet', characteristics: { On: control('On', true, false, value => value === true) } },
532
- door: { type: 'Door', characteristics: position() },
533
- window: { type: 'Window', characteristics: position() },
534
- windowcovering: { type: 'WindowCovering', characteristics: position() },
535
- garagedoor: { type: 'GarageDoorOpener', characteristics: {
536
- CurrentDoorState: { get: async () => (await isOn()) ? 0 : 1 },
647
+ switch: {
648
+ type: 'Switch',
649
+ characteristics: {
650
+ On: control('On', true, false, value => value === true),
651
+ },
652
+ },
653
+ outlet: {
654
+ type: 'Outlet',
655
+ characteristics: {
656
+ On: control('On', true, false, value => value === true),
657
+ },
658
+ },
659
+ door: {
660
+ type: 'Door',
661
+ characteristics: position(),
662
+ },
663
+ window: {
664
+ type: 'Window',
665
+ characteristics: position(),
666
+ },
667
+ windowcovering: {
668
+ type: 'WindowCovering',
669
+ characteristics: position(),
670
+ },
671
+ garagedoor: {
672
+ type: 'GarageDoorOpener',
673
+ characteristics: {
674
+ CurrentDoorState: {
675
+ get: async () => (await isOn())
676
+ ? 0
677
+ : 1,
678
+ },
537
679
  TargetDoorState: control('TargetDoorState', 0, 1, value => Number(value) === 0),
538
- ObstructionDetected: { get: async () => false },
539
- } },
540
- lock: { type: 'LockMechanism', characteristics: {
541
- LockCurrentState: { get: async () => (await isOn()) ? 0 : 1 },
680
+ ObstructionDetected: {
681
+ get: async () => false,
682
+ },
683
+ },
684
+ },
685
+ lock: {
686
+ type: 'LockMechanism',
687
+ characteristics: {
688
+ LockCurrentState: {
689
+ get: async () => (await isOn())
690
+ ? 0
691
+ : 1,
692
+ },
542
693
  LockTargetState: control('LockTargetState', 0, 1, value => Number(value) === 0),
543
- } },
544
- faucet: { type: 'Faucet', characteristics: { Active: control('Active', 1, 0, active) } },
545
- fan: { type: 'Fanv2', characteristics: { Active: control('Active', 1, 0, active) } },
546
- stateful: { type: 'StatefulProgrammableSwitch', characteristics: {
694
+ },
695
+ },
696
+ faucet: {
697
+ type: 'Faucet',
698
+ characteristics: {
699
+ Active: control('Active', 1, 0, active),
700
+ },
701
+ },
702
+ fan: {
703
+ type: 'Fanv2',
704
+ characteristics: {
705
+ Active: control('Active', 1, 0, active),
706
+ },
707
+ },
708
+ stateful: {
709
+ type: 'StatefulProgrammableSwitch',
710
+ characteristics: {
547
711
  ProgrammableSwitchEvent: {},
548
712
  ProgrammableSwitchOutputState: {
549
713
  ...control('ProgrammableSwitchOutputState', 1, 0, active),
550
- ...(pressMode ? { refreshAfterSet: ['ProgrammableSwitchOutputState'] } : {}),
714
+ ...(pressMode
715
+ ? {
716
+ refreshAfterSet: ['ProgrammableSwitchOutputState'],
717
+ }
718
+ : {}),
551
719
  afterSet: (value, service, hap) => {
552
720
  if (!pressMode || active(value)) {
553
721
  service.getCharacteristic(hap.Characteristic.ProgrammableSwitchEvent).sendEventNotification(0);
554
722
  }
555
723
  },
556
724
  },
557
- } },
725
+ },
726
+ },
558
727
  };
559
728
  return {
560
729
  services: [presentations[display] ?? presentations.switch],
@@ -566,6 +735,8 @@ export class CurtainDevice extends GenericDevice {
566
735
  lastTargetPosition = 0;
567
736
  positionState = 2;
568
737
  preferLocalPositionUntil = 0;
738
+ lastAdvertisement;
739
+ positionBeforeCommand;
569
740
  clampHomeKitPosition(position) {
570
741
  return Math.max(0, Math.min(100, Math.round(Number(position))));
571
742
  }
@@ -576,6 +747,21 @@ export class CurtainDevice extends GenericDevice {
576
747
  return 100 - this.clampHomeKitPosition(homeKitPosition);
577
748
  }
578
749
  async getPositionForHomeKit() {
750
+ const device = this.client?.getCachedDevice?.(this.opts.id);
751
+ if (device?.hasBLE?.()) {
752
+ const advertisement = device.getInfo?.()?.bleServiceData;
753
+ if (advertisement && advertisement !== this.lastAdvertisement) {
754
+ this.lastAdvertisement = advertisement;
755
+ if (typeof advertisement.position === 'number' && Number.isFinite(advertisement.position)) {
756
+ const position = this.toHomeKitPosition(advertisement.position);
757
+ if (this.positionBeforeCommand === undefined || position !== this.positionBeforeCommand) {
758
+ this.lastKnownPosition = position;
759
+ this.lastTargetPosition = position;
760
+ }
761
+ }
762
+ }
763
+ return this.lastKnownPosition;
764
+ }
579
765
  if (Date.now() < this.preferLocalPositionUntil) {
580
766
  return this.lastKnownPosition;
581
767
  }
@@ -583,6 +769,9 @@ export class CurtainDevice extends GenericDevice {
583
769
  this.getState(),
584
770
  new Promise(resolve => setTimeout(resolve, 1000)),
585
771
  ]);
772
+ if (Date.now() < this.preferLocalPositionUntil) {
773
+ return this.lastKnownPosition;
774
+ }
586
775
  if (typeof state?.position === 'number') {
587
776
  this.lastKnownPosition = this.toHomeKitPosition(Number(state.position));
588
777
  this.lastTargetPosition = this.lastKnownPosition;
@@ -597,7 +786,8 @@ export class CurtainDevice extends GenericDevice {
597
786
  characteristics: {
598
787
  CurrentPosition: {
599
788
  get: async () => {
600
- return this.getPositionForHomeKit();
789
+ void this.getPositionForHomeKit().catch(() => { });
790
+ return this.lastKnownPosition;
601
791
  },
602
792
  },
603
793
  PositionState: {
@@ -605,17 +795,42 @@ export class CurtainDevice extends GenericDevice {
605
795
  },
606
796
  TargetPosition: {
607
797
  get: async () => {
608
- await this.getPositionForHomeKit();
798
+ void this.getPositionForHomeKit().catch(() => { });
609
799
  return this.lastTargetPosition;
610
800
  },
611
801
  set: async (v) => {
612
802
  const position = this.clampHomeKitPosition(Number(v));
803
+ const bleDevice = this.client?.getCachedDevice?.(this.opts.id);
804
+ if (bleDevice?.hasBLE?.()) {
805
+ await this.getPositionForHomeKit();
806
+ }
807
+ const previousPosition = this.lastKnownPosition;
613
808
  this.lastTargetPosition = position;
614
- this.positionState = position > this.lastKnownPosition ? 1 : position < this.lastKnownPosition ? 0 : 2;
615
- await this.setState({ position: this.toSwitchBotPosition(position) });
616
- this.lastKnownPosition = position;
617
- this.preferLocalPositionUntil = Date.now() + 30000;
618
- this.positionState = 2;
809
+ this.positionState = position > this.lastKnownPosition
810
+ ? 1
811
+ : position < this.lastKnownPosition
812
+ ? 0
813
+ : 2;
814
+ try {
815
+ const result = await this.setState({
816
+ position: this.toSwitchBotPosition(position),
817
+ });
818
+ if (result === false || result?.success === false) {
819
+ throw new Error(result?.reason ?? 'Curtain position command failed');
820
+ }
821
+ this.lastKnownPosition = position;
822
+ if (bleDevice?.hasBLE?.()) {
823
+ this.positionBeforeCommand = previousPosition;
824
+ }
825
+ this.preferLocalPositionUntil = Date.now() + 30000;
826
+ }
827
+ catch (error) {
828
+ this.lastTargetPosition = this.lastKnownPosition;
829
+ throw error;
830
+ }
831
+ finally {
832
+ this.positionState = 2;
833
+ }
619
834
  },
620
835
  refreshAfterSet: ['CurrentPosition', 'TargetPosition', 'PositionState'],
621
836
  },
@@ -633,16 +848,22 @@ export class CurtainDevice extends GenericDevice {
633
848
  currentPositionLiftPercent100ths: {
634
849
  read: async () => {
635
850
  const s = await this.getState();
636
- return typeof s.position === 'number' ? Math.round(s.position * 100) : 0;
851
+ return typeof s.position === 'number'
852
+ ? Math.round(s.position * 100)
853
+ : 0;
637
854
  },
638
855
  write: undefined,
639
856
  },
640
857
  targetPositionLiftPercent100ths: {
641
858
  read: async () => {
642
859
  const s = await this.getState();
643
- return typeof s.position === 'number' ? Math.round(s.position * 100) : 0;
860
+ return typeof s.position === 'number'
861
+ ? Math.round(s.position * 100)
862
+ : 0;
644
863
  },
645
- write: async (v) => this.setState({ position: Math.round(Number(v) / 100) }),
864
+ write: async (v) => this.setState({
865
+ position: Math.round(Number(v) / 100),
866
+ }),
646
867
  },
647
868
  operationalStatus: {
648
869
  read: async () => state.operationalStatus ?? { global: 0, lift: 0, tilt: 0 },
@@ -670,16 +891,22 @@ export class CurtainDevice extends GenericDevice {
670
891
  attributes.currentPositionTiltPercent100ths = {
671
892
  read: async () => {
672
893
  const s = await this.getState();
673
- return typeof s.tilt === 'number' ? Math.round(s.tilt * 100) : 0;
894
+ return typeof s.tilt === 'number'
895
+ ? Math.round(s.tilt * 100)
896
+ : 0;
674
897
  },
675
898
  write: undefined,
676
899
  };
677
900
  attributes.targetPositionTiltPercent100ths = {
678
901
  read: async () => {
679
902
  const s = await this.getState();
680
- return typeof s.tilt === 'number' ? Math.round(s.tilt * 100) : 0;
903
+ return typeof s.tilt === 'number'
904
+ ? Math.round(s.tilt * 100)
905
+ : 0;
681
906
  },
682
- write: async (v) => this.setState({ tilt: Math.round(Number(v) / 100) }),
907
+ write: async (v) => this.setState({
908
+ tilt: Math.round(Number(v) / 100),
909
+ }),
683
910
  };
684
911
  }
685
912
  const windowCoveringCluster = {
@@ -720,10 +947,14 @@ export class FanDevice extends GenericDevice {
720
947
  RotationSpeed: {
721
948
  get: async () => {
722
949
  const s = await this.getState();
723
- return typeof s.speed === 'number' ? s.speed : 0;
950
+ return typeof s.speed === 'number'
951
+ ? s.speed
952
+ : 0;
724
953
  },
725
954
  set: async (v) => {
726
- await this.setState({ speed: Number(v) });
955
+ await this.setState({
956
+ speed: Number(v),
957
+ });
727
958
  },
728
959
  },
729
960
  },
@@ -737,7 +968,11 @@ export class FanDevice extends GenericDevice {
737
968
  }
738
969
  // Oscillation support
739
970
  if (typeof change.oscillate === 'boolean') {
740
- const body = { command: 'setOscillation', parameter: change.oscillate ? 'on' : 'off', commandType: 'command' };
971
+ const body = {
972
+ command: 'setOscillation',
973
+ parameter: change.oscillate ? 'on' : 'off',
974
+ commandType: 'command',
975
+ };
741
976
  try {
742
977
  return await this.client.setDeviceState(this.opts.id, body);
743
978
  }
@@ -793,9 +1028,15 @@ export class FanDevice extends GenericDevice {
793
1028
  type: 'OnOff',
794
1029
  clusterId: MATTER_CLUSTER_IDS.OnOff,
795
1030
  attributes: {
796
- onOff: { read: async () => { const s = await this.getState(); return !!(s && (s.on === true || s.state === 'on')); }, write: async (v) => this.setState({ on: !!v }) },
1031
+ onOff: {
1032
+ read: async () => { const s = await this.getState(); return !!(s && (s.on === true || s.state === 'on')); },
1033
+ write: async (v) => this.setState({ on: !!v }),
1034
+ },
797
1035
  // numeric attribute id for onOff
798
- [MATTER_ATTRIBUTE_IDS.OnOff.OnOff]: { read: async () => { const s = await this.getState(); return !!(s && (s.on === true || s.state === 'on')); }, write: async (v) => this.setState({ on: !!v }) },
1036
+ [MATTER_ATTRIBUTE_IDS.OnOff.OnOff]: {
1037
+ read: async () => { const s = await this.getState(); return !!(s && (s.on === true || s.state === 'on')); },
1038
+ write: async (v) => this.setState({ on: !!v }),
1039
+ },
799
1040
  },
800
1041
  },
801
1042
  {
@@ -803,10 +1044,36 @@ export class FanDevice extends GenericDevice {
803
1044
  type: 'FanControl',
804
1045
  clusterId: MATTER_CLUSTER_IDS.FanControl,
805
1046
  attributes: {
806
- rotationSpeed: { read: async () => { const s = await this.getState(); return typeof s.speed === 'number' ? s.speed : 0; }, write: async (v) => this.setState({ speed: Number(v) }) },
807
- [MATTER_ATTRIBUTE_IDS.FanControl.SpeedCurrent]: { read: async () => { const s = await this.getState(); return typeof s.speed === 'number' ? s.speed : 0; }, write: async (v) => this.setState({ speed: Number(v) }) },
808
- oscillation: { read: async () => { const s = await this.getState(); return !!s?.oscillating; }, write: async (v) => this.setState({ oscillate: !!v }) },
809
- swingMode: { read: async () => { const s = await this.getState(); return s?.swingMode ?? null; }, write: async (v) => this.setState({ swingMode: v }) },
1047
+ rotationSpeed: {
1048
+ read: async () => {
1049
+ const s = await this.getState();
1050
+ return typeof s.speed === 'number'
1051
+ ? s.speed
1052
+ : 0;
1053
+ },
1054
+ write: async (v) => this.setState({
1055
+ speed: Number(v),
1056
+ }),
1057
+ },
1058
+ [MATTER_ATTRIBUTE_IDS.FanControl.SpeedCurrent]: {
1059
+ read: async () => {
1060
+ const s = await this.getState();
1061
+ return typeof s.speed === 'number'
1062
+ ? s.speed
1063
+ : 0;
1064
+ },
1065
+ write: async (v) => this.setState({
1066
+ speed: Number(v),
1067
+ }),
1068
+ },
1069
+ oscillation: {
1070
+ read: async () => { const s = await this.getState(); return !!s?.oscillating; },
1071
+ write: async (v) => this.setState({ oscillate: !!v }),
1072
+ },
1073
+ swingMode: {
1074
+ read: async () => { const s = await this.getState(); return s?.swingMode ?? null; },
1075
+ write: async (v) => this.setState({ swingMode: v }),
1076
+ },
810
1077
  },
811
1078
  },
812
1079
  ],
@@ -833,10 +1100,14 @@ export class LightDevice extends GenericDevice {
833
1100
  props: { minValue: 0, maxValue: 100, minStep: 1 },
834
1101
  get: async () => {
835
1102
  const s = await this.getState();
836
- return typeof s.brightness === 'number' ? s.brightness : 100;
1103
+ return typeof s.brightness === 'number'
1104
+ ? s.brightness
1105
+ : 100;
837
1106
  },
838
1107
  set: async (v) => {
839
- await this.setState({ brightness: Number(v) });
1108
+ await this.setState({
1109
+ brightness: Number(v),
1110
+ });
840
1111
  },
841
1112
  },
842
1113
  Hue: {
@@ -886,7 +1157,9 @@ export class LightDevice extends GenericDevice {
886
1157
  return 0;
887
1158
  },
888
1159
  set: async (v) => {
889
- await this.setState({ hue: Number(v) });
1160
+ await this.setState({
1161
+ hue: Number(v),
1162
+ });
890
1163
  },
891
1164
  },
892
1165
  Saturation: {
@@ -906,13 +1179,17 @@ export class LightDevice extends GenericDevice {
906
1179
  const mx = Math.max(r, g, b);
907
1180
  const mn = Math.min(r, g, b);
908
1181
  const d = mx - mn;
909
- const sat = mx === 0 ? 0 : Math.round((d / mx) * 100);
1182
+ const sat = mx === 0
1183
+ ? 0
1184
+ : Math.round((d / mx) * 100);
910
1185
  return sat;
911
1186
  }
912
1187
  return 0;
913
1188
  },
914
1189
  set: async (v) => {
915
- await this.setState({ saturation: Number(v) });
1190
+ await this.setState({
1191
+ saturation: Number(v),
1192
+ });
916
1193
  },
917
1194
  },
918
1195
  ColorTemperature: {
@@ -933,7 +1210,9 @@ export class LightDevice extends GenericDevice {
933
1210
  return 400;
934
1211
  },
935
1212
  set: async (v) => {
936
- await this.setState({ colorTemperature: Number(v) });
1213
+ await this.setState({
1214
+ colorTemperature: Number(v),
1215
+ });
937
1216
  },
938
1217
  },
939
1218
  },
@@ -1010,8 +1289,14 @@ export class LightDevice extends GenericDevice {
1010
1289
  type: 'OnOff',
1011
1290
  clusterId: MATTER_CLUSTER_IDS.OnOff,
1012
1291
  attributes: {
1013
- onOff: { read: async () => { const s = await this.getState(); return !!(s && (s.on === true || s.state === 'on' || s.power === 'on')); }, write: async (v) => this.setState({ on: !!v }) },
1014
- [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) => this.setState({ on: !!v }) },
1292
+ onOff: {
1293
+ read: async () => { const s = await this.getState(); return !!(s && (s.on === true || s.state === 'on' || s.power === 'on')); },
1294
+ write: async (v) => this.setState({ on: !!v }),
1295
+ },
1296
+ [MATTER_ATTRIBUTE_IDS.OnOff.OnOff]: {
1297
+ read: async () => { const s = await this.getState(); return !!(s && (s.on === true || s.state === 'on' || s.power === 'on')); },
1298
+ write: async (v) => this.setState({ on: !!v }),
1299
+ },
1015
1300
  },
1016
1301
  },
1017
1302
  {
@@ -1019,8 +1304,28 @@ export class LightDevice extends GenericDevice {
1019
1304
  type: 'LevelControl',
1020
1305
  clusterId: MATTER_CLUSTER_IDS.LevelControl,
1021
1306
  attributes: {
1022
- currentLevel: { read: async () => { const s = await this.getState(); return typeof s.brightness === 'number' ? s.brightness : 100; }, write: async (v) => this.setState({ brightness: Number(v) }) },
1023
- [MATTER_ATTRIBUTE_IDS.LevelControl.CurrentLevel]: { read: async () => { const s = await this.getState(); return typeof s.brightness === 'number' ? s.brightness : 100; }, write: async (v) => this.setState({ brightness: Number(v) }) },
1307
+ currentLevel: {
1308
+ read: async () => {
1309
+ const s = await this.getState();
1310
+ return typeof s.brightness === 'number'
1311
+ ? s.brightness
1312
+ : 100;
1313
+ },
1314
+ write: async (v) => this.setState({
1315
+ brightness: Number(v),
1316
+ }),
1317
+ },
1318
+ [MATTER_ATTRIBUTE_IDS.LevelControl.CurrentLevel]: {
1319
+ read: async () => {
1320
+ const s = await this.getState();
1321
+ return typeof s.brightness === 'number'
1322
+ ? s.brightness
1323
+ : 100;
1324
+ },
1325
+ write: async (v) => this.setState({
1326
+ brightness: Number(v),
1327
+ }),
1328
+ },
1024
1329
  },
1025
1330
  },
1026
1331
  {
@@ -1029,12 +1334,55 @@ export class LightDevice extends GenericDevice {
1029
1334
  clusterId: MATTER_CLUSTER_IDS.ColorControl,
1030
1335
  attributes: {
1031
1336
  // Required colorMode attribute for Matter conformance (0 = currentHueAndSaturation)
1032
- colorMode: { read: async () => 0 },
1033
- colorHue: { read: async () => { const s = await this.getState(); return typeof s.hue === 'number' ? s.hue : 0; }, write: async (v) => this.setState({ hue: Number(v) }) },
1034
- [MATTER_ATTRIBUTE_IDS.ColorControl.CurrentHue]: { read: async () => { const s = await this.getState(); return typeof s.hue === 'number' ? s.hue : 0; }, write: async (v) => this.setState({ hue: Number(v) }) },
1035
- colorSaturation: { read: async () => { const s = await this.getState(); return typeof s.saturation === 'number' ? s.saturation : 0; }, write: async (v) => this.setState({ saturation: Number(v) }) },
1036
- [MATTER_ATTRIBUTE_IDS.ColorControl.CurrentSaturation]: { read: async () => { const s = await this.getState(); return typeof s.saturation === 'number' ? s.saturation : 0; }, write: async (v) => this.setState({ saturation: Number(v) }) },
1037
- colorTemperature: { read: async () => {
1337
+ colorMode: {
1338
+ read: async () => 0,
1339
+ },
1340
+ colorHue: {
1341
+ read: async () => {
1342
+ const s = await this.getState();
1343
+ return typeof s.hue === 'number'
1344
+ ? s.hue
1345
+ : 0;
1346
+ },
1347
+ write: async (v) => this.setState({
1348
+ hue: Number(v),
1349
+ }),
1350
+ },
1351
+ [MATTER_ATTRIBUTE_IDS.ColorControl.CurrentHue]: {
1352
+ read: async () => {
1353
+ const s = await this.getState();
1354
+ return typeof s.hue === 'number'
1355
+ ? s.hue
1356
+ : 0;
1357
+ },
1358
+ write: async (v) => this.setState({
1359
+ hue: Number(v),
1360
+ }),
1361
+ },
1362
+ colorSaturation: {
1363
+ read: async () => {
1364
+ const s = await this.getState();
1365
+ return typeof s.saturation === 'number'
1366
+ ? s.saturation
1367
+ : 0;
1368
+ },
1369
+ write: async (v) => this.setState({
1370
+ saturation: Number(v),
1371
+ }),
1372
+ },
1373
+ [MATTER_ATTRIBUTE_IDS.ColorControl.CurrentSaturation]: {
1374
+ read: async () => {
1375
+ const s = await this.getState();
1376
+ return typeof s.saturation === 'number'
1377
+ ? s.saturation
1378
+ : 0;
1379
+ },
1380
+ write: async (v) => this.setState({
1381
+ saturation: Number(v),
1382
+ }),
1383
+ },
1384
+ colorTemperature: {
1385
+ read: async () => {
1038
1386
  const s = await this.getState();
1039
1387
  if (typeof s.colorTemperature === 'number') {
1040
1388
  return s.colorTemperature;
@@ -1043,8 +1391,13 @@ export class LightDevice extends GenericDevice {
1043
1391
  return Math.round(1000000 / s.kelvin);
1044
1392
  }
1045
1393
  return 400;
1046
- }, write: async (v) => this.setState({ colorTemperature: Number(v) }) },
1047
- [MATTER_ATTRIBUTE_IDS.ColorControl.ColorTemperatureMireds]: { read: async () => {
1394
+ },
1395
+ write: async (v) => this.setState({
1396
+ colorTemperature: Number(v),
1397
+ }),
1398
+ },
1399
+ [MATTER_ATTRIBUTE_IDS.ColorControl.ColorTemperatureMireds]: {
1400
+ read: async () => {
1048
1401
  const s = await this.getState();
1049
1402
  if (typeof s.colorTemperature === 'number') {
1050
1403
  return s.colorTemperature;
@@ -1053,7 +1406,11 @@ export class LightDevice extends GenericDevice {
1053
1406
  return Math.round(1000000 / s.kelvin);
1054
1407
  }
1055
1408
  return 400;
1056
- }, write: async (v) => this.setState({ colorTemperature: Number(v) }) },
1409
+ },
1410
+ write: async (v) => this.setState({
1411
+ colorTemperature: Number(v),
1412
+ }),
1413
+ },
1057
1414
  },
1058
1415
  },
1059
1416
  ],
@@ -1086,7 +1443,11 @@ export class MotionSensorDevice extends GenericDevice {
1086
1443
  }
1087
1444
  // Oscillation support
1088
1445
  if (typeof change.oscillate === 'boolean') {
1089
- const body = { command: 'setOscillation', parameter: change.oscillate ? 'on' : 'off', commandType: 'command' };
1446
+ const body = {
1447
+ command: 'setOscillation',
1448
+ parameter: change.oscillate ? 'on' : 'off',
1449
+ commandType: 'command',
1450
+ };
1090
1451
  try {
1091
1452
  return await this.client.setDeviceState(this.opts.id, body);
1092
1453
  }
@@ -1141,7 +1502,9 @@ export class ContactSensorDevice extends GenericDevice {
1141
1502
  ContactSensorState: {
1142
1503
  get: async () => {
1143
1504
  const s = await this.getState();
1144
- return s && s.open ? 1 : 0;
1505
+ return s && s.open
1506
+ ? 1
1507
+ : 0;
1145
1508
  },
1146
1509
  },
1147
1510
  },
@@ -1166,13 +1529,17 @@ export class LockDevice extends GenericDevice {
1166
1529
  LockCurrentState: {
1167
1530
  get: async () => {
1168
1531
  const s = await this.getState();
1169
- return s && s.locked ? 1 : 0;
1532
+ return s && s.locked
1533
+ ? 1
1534
+ : 0;
1170
1535
  },
1171
1536
  },
1172
1537
  LockTargetState: {
1173
1538
  get: async () => {
1174
1539
  const s = await this.getState();
1175
- return s && s.locked ? 1 : 0;
1540
+ return s && s.locked
1541
+ ? 1
1542
+ : 0;
1176
1543
  },
1177
1544
  set: async (v) => {
1178
1545
  await this.setState({ locked: !!v });
@@ -1199,7 +1566,11 @@ export class LockDevice extends GenericDevice {
1199
1566
  }
1200
1567
  if (action === 'removeUser' && (change.user || change.userId)) {
1201
1568
  const user = change.user ?? change.userId;
1202
- const body = { command: 'removeUserCode', parameter: String(user), commandType: 'command' };
1569
+ const body = {
1570
+ command: 'removeUserCode',
1571
+ parameter: String(user),
1572
+ commandType: 'command',
1573
+ };
1203
1574
  return await this.client.setDeviceState(this.opts.id, body);
1204
1575
  }
1205
1576
  if (action === 'listUsers') {
@@ -1226,7 +1597,11 @@ export class LockDevice extends GenericDevice {
1226
1597
  // Support setting lock PIN/passcode via `pin` or `passcode` (fallback)
1227
1598
  const pin = change?.pin ?? change?.passcode ?? change?.code;
1228
1599
  if (typeof pin === 'string' || typeof pin === 'number') {
1229
- const body = { command: 'setLockPin', parameter: String(pin), commandType: 'command' };
1600
+ const body = {
1601
+ command: 'setLockPin',
1602
+ parameter: String(pin),
1603
+ commandType: 'command',
1604
+ };
1230
1605
  try {
1231
1606
  return await this.client.setDeviceState(this.opts.id, body);
1232
1607
  }
@@ -1255,8 +1630,14 @@ export class LockDevice extends GenericDevice {
1255
1630
  type: 'DoorLock',
1256
1631
  clusterId: MATTER_CLUSTER_IDS.DoorLock,
1257
1632
  attributes: {
1258
- lockState: { read: async () => { const s = await this.getState(); return !!(s && s.locked); }, write: async (v) => this.setState({ locked: !!v }) },
1259
- [MATTER_ATTRIBUTE_IDS.DoorLock.LockState]: { read: async () => { const s = await this.getState(); return !!(s && s.locked); }, write: async (v) => this.setState({ locked: !!v }) },
1633
+ lockState: {
1634
+ read: async () => { const s = await this.getState(); return !!(s && s.locked); },
1635
+ write: async (v) => this.setState({ locked: !!v }),
1636
+ },
1637
+ [MATTER_ATTRIBUTE_IDS.DoorLock.LockState]: {
1638
+ read: async () => { const s = await this.getState(); return !!(s && s.locked); },
1639
+ write: async (v) => this.setState({ locked: !!v }),
1640
+ },
1260
1641
  },
1261
1642
  },
1262
1643
  {
@@ -1264,10 +1645,19 @@ export class LockDevice extends GenericDevice {
1264
1645
  type: 'DoorLockUserManagement',
1265
1646
  clusterId: 0x0301,
1266
1647
  attributes: {
1267
- addUser: { write: async (v) => this.setState({ action: 'addUser', user: v?.user, pin: v?.pin }) },
1268
- removeUser: { write: async (v) => this.setState({ action: 'removeUser', user: v?.user }) },
1269
- listUsers: { read: async () => { const r = await this.setState({ action: 'listUsers' }); return r; }, write: undefined },
1270
- unlockWithPin: { write: async (v) => this.setState({ action: 'unlockWithPin', pin: v?.pin }) },
1648
+ addUser: {
1649
+ write: async (v) => this.setState({ action: 'addUser', user: v?.user, pin: v?.pin }),
1650
+ },
1651
+ removeUser: {
1652
+ write: async (v) => this.setState({ action: 'removeUser', user: v?.user }),
1653
+ },
1654
+ listUsers: {
1655
+ read: async () => { const r = await this.setState({ action: 'listUsers' }); return r; },
1656
+ write: undefined,
1657
+ },
1658
+ unlockWithPin: {
1659
+ write: async (v) => this.setState({ action: 'unlockWithPin', pin: v?.pin }),
1660
+ },
1271
1661
  },
1272
1662
  },
1273
1663
  ],
@@ -1284,7 +1674,9 @@ export class HumidifierDevice extends GenericDevice {
1284
1674
  Active: {
1285
1675
  get: async () => {
1286
1676
  const s = await this.getState();
1287
- return s && s.on ? 1 : 0;
1677
+ return s && s.on
1678
+ ? 1
1679
+ : 0;
1288
1680
  },
1289
1681
  set: async (v) => {
1290
1682
  await this.setState({ on: v === 1 });
@@ -1293,7 +1685,9 @@ export class HumidifierDevice extends GenericDevice {
1293
1685
  CurrentHumidifierDehumidifierState: {
1294
1686
  get: async () => {
1295
1687
  const s = await this.getState();
1296
- return s && s.on ? 2 : 0; // 0 = Inactive, 2 = Humidifying
1688
+ return s && s.on
1689
+ ? 2
1690
+ : 0; // 0 = Inactive, 2 = Humidifying
1297
1691
  },
1298
1692
  },
1299
1693
  TargetHumidifierDehumidifierState: {
@@ -1305,16 +1699,22 @@ export class HumidifierDevice extends GenericDevice {
1305
1699
  CurrentRelativeHumidity: {
1306
1700
  get: async () => {
1307
1701
  const s = await this.getState();
1308
- return typeof s.humidity === 'number' ? s.humidity : 0;
1702
+ return typeof s.humidity === 'number'
1703
+ ? s.humidity
1704
+ : 0;
1309
1705
  },
1310
1706
  },
1311
1707
  RelativeHumidityHumidifierThreshold: {
1312
1708
  get: async () => {
1313
1709
  const s = await this.getState();
1314
- return typeof s.targetHumidity === 'number' ? s.targetHumidity : 50;
1710
+ return typeof s.targetHumidity === 'number'
1711
+ ? s.targetHumidity
1712
+ : 50;
1315
1713
  },
1316
1714
  set: async (v) => {
1317
- await this.setState({ humidity: Number(v) });
1715
+ await this.setState({
1716
+ humidity: Number(v),
1717
+ });
1318
1718
  },
1319
1719
  },
1320
1720
  },
@@ -1344,7 +1744,11 @@ export class HumidifierDevice extends GenericDevice {
1344
1744
  }
1345
1745
  }
1346
1746
  if (typeof change.dry === 'boolean') {
1347
- const body = { command: 'setDry', parameter: change.dry ? 'on' : 'off', commandType: 'command' };
1747
+ const body = {
1748
+ command: 'setDry',
1749
+ parameter: change.dry ? 'on' : 'off',
1750
+ commandType: 'command',
1751
+ };
1348
1752
  try {
1349
1753
  return await this.client.setDeviceState(this.opts.id, body);
1350
1754
  }
@@ -1375,16 +1779,38 @@ export class HumidifierMatterDevice extends HumidifierDevice {
1375
1779
  type: 'RelativeHumiditySensor',
1376
1780
  clusterId: MATTER_CLUSTER_IDS.RelativeHumidityMeasurement,
1377
1781
  attributes: {
1378
- currentRelativeHumidity: { read: async () => { const s = await this.getState(); return typeof s.humidity === 'number' ? s.humidity : 0; }, write: undefined },
1379
- [MATTER_ATTRIBUTE_IDS.RelativeHumidityMeasurement.MeasuredValue]: { read: async () => { const s = await this.getState(); return typeof s.humidity === 'number' ? s.humidity : 0; }, write: undefined },
1782
+ currentRelativeHumidity: {
1783
+ read: async () => {
1784
+ const s = await this.getState();
1785
+ return typeof s.humidity === 'number'
1786
+ ? s.humidity
1787
+ : 0;
1788
+ },
1789
+ write: undefined,
1790
+ },
1791
+ [MATTER_ATTRIBUTE_IDS.RelativeHumidityMeasurement.MeasuredValue]: {
1792
+ read: async () => {
1793
+ const s = await this.getState();
1794
+ return typeof s.humidity === 'number'
1795
+ ? s.humidity
1796
+ : 0;
1797
+ },
1798
+ write: undefined,
1799
+ },
1380
1800
  },
1381
1801
  },
1382
1802
  {
1383
1803
  type: 'OnOff',
1384
1804
  clusterId: MATTER_CLUSTER_IDS.OnOff,
1385
1805
  attributes: {
1386
- onOff: { read: async () => { const s = await this.getState(); return !!s?.on; }, write: async (v) => this.setState({ on: !!v }) },
1387
- [MATTER_ATTRIBUTE_IDS.OnOff.OnOff]: { read: async () => { const s = await this.getState(); return !!s?.on; }, write: async (v) => this.setState({ on: !!v }) },
1806
+ onOff: {
1807
+ read: async () => { const s = await this.getState(); return !!s?.on; },
1808
+ write: async (v) => this.setState({ on: !!v }),
1809
+ },
1810
+ [MATTER_ATTRIBUTE_IDS.OnOff.OnOff]: {
1811
+ read: async () => { const s = await this.getState(); return !!s?.on; },
1812
+ write: async (v) => this.setState({ on: !!v }),
1813
+ },
1388
1814
  },
1389
1815
  },
1390
1816
  ],
@@ -1401,7 +1827,9 @@ export class TemperatureSensorDevice extends GenericDevice {
1401
1827
  CurrentTemperature: {
1402
1828
  get: async () => {
1403
1829
  const s = await this.getState();
1404
- return typeof s.temperature === 'number' ? s.temperature : 0;
1830
+ return typeof s.temperature === 'number'
1831
+ ? s.temperature
1832
+ : 0;
1405
1833
  },
1406
1834
  },
1407
1835
  },
@@ -1434,7 +1862,9 @@ export class PlugDevice extends GenericDevice {
1434
1862
  OutletInUse: {
1435
1863
  get: async () => {
1436
1864
  const s = await this.getState();
1437
- return s && s.inUse ? 1 : 0;
1865
+ return s && s.inUse
1866
+ ? 1
1867
+ : 0;
1438
1868
  },
1439
1869
  },
1440
1870
  },
@@ -1466,7 +1896,9 @@ export class MeterDevice extends GenericDevice {
1466
1896
  /** The ppm at which HomeKit reports carbon dioxide as detected. */
1467
1897
  co2AbnormalThreshold() {
1468
1898
  const value = Number(this.cfg?.co2AbnormalThreshold);
1469
- return Number.isFinite(value) && value > 0 ? value : CO2_ABNORMAL_PPM_DEFAULT;
1899
+ return Number.isFinite(value) && value > 0
1900
+ ? value
1901
+ : CO2_ABNORMAL_PPM_DEFAULT;
1470
1902
  }
1471
1903
  createHAPAccessory(api) {
1472
1904
  const services = [
@@ -1476,7 +1908,9 @@ export class MeterDevice extends GenericDevice {
1476
1908
  CurrentTemperature: {
1477
1909
  get: async () => {
1478
1910
  const s = await this.getState();
1479
- return typeof s.temperature === 'number' ? s.temperature : 0;
1911
+ return typeof s.temperature === 'number'
1912
+ ? s.temperature
1913
+ : 0;
1480
1914
  },
1481
1915
  },
1482
1916
  },
@@ -1487,7 +1921,9 @@ export class MeterDevice extends GenericDevice {
1487
1921
  CurrentRelativeHumidity: {
1488
1922
  get: async () => {
1489
1923
  const s = await this.getState();
1490
- return typeof s.humidity === 'number' ? s.humidity : 0;
1924
+ return typeof s.humidity === 'number'
1925
+ ? s.humidity
1926
+ : 0;
1491
1927
  },
1492
1928
  },
1493
1929
  },
@@ -1496,7 +1932,9 @@ export class MeterDevice extends GenericDevice {
1496
1932
  if (this.reportsCO2()) {
1497
1933
  const co2 = async () => {
1498
1934
  const s = await this.getState();
1499
- return typeof s.co2 === 'number' ? s.co2 : 0;
1935
+ return typeof s.co2 === 'number'
1936
+ ? s.co2
1937
+ : 0;
1500
1938
  };
1501
1939
  services.push({
1502
1940
  type: 'CarbonDioxideSensor',
@@ -1505,7 +1943,9 @@ export class MeterDevice extends GenericDevice {
1505
1943
  get: co2,
1506
1944
  },
1507
1945
  CarbonDioxideDetected: {
1508
- get: async () => ((await co2()) >= this.co2AbnormalThreshold() ? 1 : 0),
1946
+ get: async () => ((await co2()) >= this.co2AbnormalThreshold()
1947
+ ? 1
1948
+ : 0),
1509
1949
  },
1510
1950
  },
1511
1951
  });
@@ -1527,7 +1967,9 @@ export class MeterDevice extends GenericDevice {
1527
1967
  get: battery,
1528
1968
  },
1529
1969
  StatusLowBattery: {
1530
- get: async () => ((await battery()) < 20 ? 1 : 0),
1970
+ get: async () => ((await battery()) < 20
1971
+ ? 1
1972
+ : 0),
1531
1973
  },
1532
1974
  ChargingState: {
1533
1975
  get: async () => 2,
@@ -1566,7 +2008,13 @@ export class WaterDetectorDevice extends GenericDevice {
1566
2008
  return status === 1;
1567
2009
  }
1568
2010
  if (typeof status === 'string') {
1569
- return ['1', 'leak', 'leaked', 'detected', 'water_leak_detected'].includes(status.toLowerCase());
2011
+ return [
2012
+ '1',
2013
+ 'leak',
2014
+ 'leaked',
2015
+ 'detected',
2016
+ 'water_leak_detected',
2017
+ ].includes(status.toLowerCase());
1570
2018
  }
1571
2019
  return undefined;
1572
2020
  }
@@ -1702,7 +2150,9 @@ export class WaterDetectorDevice extends GenericDevice {
1702
2150
  StatusLowBattery: {
1703
2151
  get: () => {
1704
2152
  const b = this.getBatteryFast(api);
1705
- return b < 20 ? 1 : 0;
2153
+ return b < 20
2154
+ ? 1
2155
+ : 0;
1706
2156
  },
1707
2157
  },
1708
2158
  ChargingState: {