@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.
- package/config.schema.json +29 -0
- package/dist/SwitchBotMatterPlatform.js +1 -1
- package/dist/SwitchBotMatterPlatform.js.map +1 -1
- package/dist/deviceFactory.js +1 -1
- package/dist/deviceFactory.js.map +1 -1
- package/dist/devices/genericDevice.d.ts +2 -0
- package/dist/devices/genericDevice.d.ts.map +1 -1
- package/dist/devices/genericDevice.js +573 -123
- package/dist/devices/genericDevice.js.map +1 -1
- package/dist/homebridge-ui/endpoints/discovery.d.ts.map +1 -1
- package/dist/homebridge-ui/endpoints/discovery.js +2 -0
- package/dist/homebridge-ui/endpoints/discovery.js.map +1 -1
- package/dist/homebridge-ui/homebridge-ui/endpoints/discovery.js +2 -0
- package/dist/homebridge-ui/homebridge-ui/endpoints/discovery.js.map +1 -1
- package/dist/homebridge-ui/public/index.html +12 -2
- package/dist/homebridge-ui/public/js/advanced-settings.d.ts +4 -1
- package/dist/homebridge-ui/public/js/advanced-settings.d.ts.map +1 -1
- package/dist/homebridge-ui/public/js/advanced-settings.js +21 -11
- package/dist/homebridge-ui/public/js/advanced-settings.js.map +1 -1
- package/dist/homebridge-ui/public/js/advanced-settings.ts +23 -11
- package/dist/homebridge-ui/public/js/api.d.ts.map +1 -1
- package/dist/homebridge-ui/public/js/api.js +21 -7
- package/dist/homebridge-ui/public/js/api.js.map +1 -1
- package/dist/homebridge-ui/public/js/api.ts +21 -7
- package/dist/homebridge-ui/public/js/app.js +78 -20
- package/dist/homebridge-ui/public/js/app.js.map +2 -2
- package/dist/homebridge-ui/public/js/credentials.d.ts.map +1 -1
- package/dist/homebridge-ui/public/js/credentials.js +9 -3
- package/dist/homebridge-ui/public/js/credentials.js.map +1 -1
- package/dist/homebridge-ui/public/js/credentials.ts +9 -3
- package/dist/homebridge-ui/public/js/devices-delete.d.ts.map +1 -1
- package/dist/homebridge-ui/public/js/devices-delete.js +6 -2
- package/dist/homebridge-ui/public/js/devices-delete.js.map +1 -1
- package/dist/homebridge-ui/public/js/devices-delete.ts +6 -2
- package/dist/homebridge-ui/public/js/devices.d.ts.map +1 -1
- package/dist/homebridge-ui/public/js/devices.js +3 -1
- package/dist/homebridge-ui/public/js/devices.js.map +1 -1
- package/dist/homebridge-ui/public/js/devices.ts +3 -1
- package/dist/homebridge-ui/public/js/discovery.d.ts.map +1 -1
- package/dist/homebridge-ui/public/js/discovery.js +89 -25
- package/dist/homebridge-ui/public/js/discovery.js.map +1 -1
- package/dist/homebridge-ui/public/js/discovery.ts +89 -25
- package/dist/homebridge-ui/public/js/modals.d.ts.map +1 -1
- package/dist/homebridge-ui/public/js/modals.js +15 -5
- package/dist/homebridge-ui/public/js/modals.js.map +1 -1
- package/dist/homebridge-ui/public/js/modals.ts +15 -5
- package/dist/homebridge-ui/public/js/render.d.ts.map +1 -1
- package/dist/homebridge-ui/public/js/render.js +67 -17
- package/dist/homebridge-ui/public/js/render.js.map +1 -1
- package/dist/homebridge-ui/public/js/render.ts +67 -17
- package/dist/homebridge-ui/public/js/toast.d.ts.map +1 -1
- package/dist/homebridge-ui/public/js/toast.js +6 -2
- package/dist/homebridge-ui/public/js/toast.js.map +1 -1
- package/dist/homebridge-ui/public/js/toast.ts +6 -2
- package/dist/homebridge-ui/settings.js.map +1 -1
- package/dist/homebridge-ui/switchbotClient.js +4 -0
- package/dist/homebridge-ui/switchbotClient.js.map +1 -1
- package/dist/settings.d.ts +1 -0
- package/dist/settings.d.ts.map +1 -1
- package/dist/settings.js.map +1 -1
- package/dist/switchbotClient.d.ts +1 -0
- package/dist/switchbotClient.d.ts.map +1 -1
- package/dist/switchbotClient.js +4 -0
- package/dist/switchbotClient.js.map +1 -1
- package/docs/variables/default.html +1 -1
- package/package.json +9 -9
- package/src/SwitchBotMatterPlatform.ts +1 -1
- package/src/deviceFactory.ts +1 -1
- package/src/devices/genericDevice.ts +572 -134
- package/src/homebridge-ui/endpoints/discovery.ts +2 -0
- package/src/homebridge-ui/public/index.html +12 -2
- package/src/homebridge-ui/public/js/advanced-settings.ts +23 -11
- package/src/homebridge-ui/public/js/api.ts +21 -7
- package/src/homebridge-ui/public/js/credentials.ts +9 -3
- package/src/homebridge-ui/public/js/devices-delete.ts +6 -2
- package/src/homebridge-ui/public/js/devices.ts +3 -1
- package/src/homebridge-ui/public/js/discovery.ts +89 -25
- package/src/homebridge-ui/public/js/modals.ts +15 -5
- package/src/homebridge-ui/public/js/render.ts +67 -17
- package/src/homebridge-ui/public/js/toast.ts +6 -2
- package/src/settings.ts +1 -0
- package/src/switchbotClient.ts +5 -0
- package/test/client/node-switchbot-commands.spec.ts +44 -8
- package/test/client/switchbot-client-debounce.spec.ts +24 -3
- package/test/client/switchbot-client-discovery.spec.ts +26 -5
- package/test/client/switchbot-client-keys.spec.ts +30 -7
- package/test/client/switchbot-client-openapi.spec.ts +6 -1
- package/test/client/switchbotClient.spec.ts +30 -6
- package/test/device/bot-hap.spec.ts +131 -22
- package/test/device/curtain-ble-position.spec.ts +46 -0
- package/test/device/deviceBase.spec.ts +6 -1
- package/test/device/deviceFactory-edge.spec.ts +12 -2
- package/test/device/deviceFactory.spec.ts +6 -1
- package/test/device/fan-swing.spec.ts +9 -2
- package/test/device/genericDevice-blepoll.spec.ts +17 -3
- package/test/device/getstate-cache.spec.ts +37 -6
- package/test/device/getstate-returns-status.spec.ts +65 -13
- package/test/device/lock-users.spec.ts +24 -7
- package/test/device/matter-descriptors.spec.ts +6 -1
- package/test/device/matter-device-state.spec.ts +6 -1
- package/test/device/meter-battery.spec.ts +86 -14
- package/test/device/meter-co2.spec.ts +65 -10
- package/test/device/model-identities.spec.ts +6 -1
- package/test/device/water-detector-battery.spec.ts +19 -3
- package/test/device/water-detector-leak.spec.ts +6 -1
- package/test/helpers/matter-harness.ts +21 -4
- package/test/platform/accessory-restore.spec.ts +18 -3
- package/test/platform/hap-cache-restart.spec.ts +66 -13
- package/test/platform/matter-childbridge.spec.ts +16 -3
- package/test/platform/matter-integration.spec.ts +6 -1
- package/test/platform/matter-registration.spec.ts +47 -10
- package/test/platform/platform-edge.spec.ts +13 -3
- package/test/platform/platform.integration.spec.ts +16 -3
- package/test/utils/utils.spec.ts +42 -8
- 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
|
|
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 = {
|
|
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 {
|
|
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
|
|
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: {
|
|
372
|
-
|
|
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: {
|
|
384
|
-
|
|
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: {
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
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: {
|
|
403
|
-
|
|
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: {
|
|
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: {
|
|
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: {
|
|
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: {
|
|
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: {
|
|
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: {
|
|
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: {
|
|
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: {
|
|
492
|
-
|
|
493
|
-
|
|
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: {
|
|
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: {
|
|
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
|
|
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 = {
|
|
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())
|
|
672
|
+
get: async () => (await isOn())
|
|
673
|
+
? active
|
|
674
|
+
: inactive,
|
|
568
675
|
set: async (value: any) => setOn(requestedOn(value)),
|
|
569
|
-
...(pressMode
|
|
676
|
+
...(pressMode
|
|
677
|
+
? {
|
|
678
|
+
refreshAfterSet: [name],
|
|
679
|
+
}
|
|
680
|
+
: {}),
|
|
570
681
|
})
|
|
571
682
|
const position = () => ({
|
|
572
|
-
CurrentPosition: {
|
|
573
|
-
|
|
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: {
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
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'
|
|
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'
|
|
914
|
+
return typeof s.position === 'number'
|
|
915
|
+
? Math.round(s.position * 100)
|
|
916
|
+
: 0
|
|
700
917
|
},
|
|
701
|
-
write: async (v: any) => this.setState({
|
|
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'
|
|
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'
|
|
957
|
+
return typeof s.tilt === 'number'
|
|
958
|
+
? Math.round(s.tilt * 100)
|
|
959
|
+
: 0
|
|
737
960
|
},
|
|
738
|
-
write: async (v: any) => this.setState({
|
|
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'
|
|
1005
|
+
return typeof s.speed === 'number'
|
|
1006
|
+
? s.speed
|
|
1007
|
+
: 0
|
|
781
1008
|
},
|
|
782
1009
|
set: async (v: any) => {
|
|
783
|
-
await this.setState({
|
|
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 = {
|
|
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: {
|
|
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]: {
|
|
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: {
|
|
854
|
-
|
|
855
|
-
|
|
856
|
-
|
|
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'
|
|
1146
|
+
return typeof s.brightness === 'number'
|
|
1147
|
+
? s.brightness
|
|
1148
|
+
: 100
|
|
884
1149
|
},
|
|
885
1150
|
set: async (v: any) => {
|
|
886
|
-
await this.setState({
|
|
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({
|
|
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
|
|
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({
|
|
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({
|
|
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: {
|
|
1033
|
-
|
|
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: {
|
|
1042
|
-
|
|
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: {
|
|
1052
|
-
|
|
1053
|
-
|
|
1054
|
-
|
|
1055
|
-
|
|
1056
|
-
|
|
1057
|
-
|
|
1058
|
-
|
|
1059
|
-
|
|
1060
|
-
|
|
1061
|
-
|
|
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 = {
|
|
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
|
|
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
|
|
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
|
|
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 = {
|
|
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 = {
|
|
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: {
|
|
1255
|
-
|
|
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: {
|
|
1264
|
-
|
|
1265
|
-
|
|
1266
|
-
|
|
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
|
|
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
|
|
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'
|
|
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'
|
|
1697
|
+
return typeof s.targetHumidity === 'number'
|
|
1698
|
+
? s.targetHumidity
|
|
1699
|
+
: 50
|
|
1312
1700
|
},
|
|
1313
1701
|
set: async (v: any) => {
|
|
1314
|
-
await this.setState({
|
|
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 = {
|
|
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: {
|
|
1371
|
-
|
|
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: {
|
|
1379
|
-
|
|
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'
|
|
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
|
|
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
|
|
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'
|
|
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'
|
|
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'
|
|
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()
|
|
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
|
|
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 [
|
|
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
|
|
2150
|
+
return b < 20
|
|
2151
|
+
? 1
|
|
2152
|
+
: 0
|
|
1715
2153
|
},
|
|
1716
2154
|
},
|
|
1717
2155
|
ChargingState: {
|