-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Expand file tree
/
Copy pathinovelli.test.ts
More file actions
3053 lines (2838 loc) · 133 KB
/
Copy pathinovelli.test.ts
File metadata and controls
3053 lines (2838 loc) · 133 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import {beforeAll, describe, expect, it, vi} from "vitest";
import {definitions as inovelliDeviceDefinitions} from "../src/devices/inovelli";
import {findByDevice} from "../src/index";
import type {Definition, Expose, Fz, KeyValue, KeyValueAny, Tz, Zh} from "../src/lib/types";
import {mockDevice} from "./utils";
/** EP2 raw scene buffer: `data[4]` must be `0x00` for scene parsing; `data[5]` / `data[6]` index `buttonLookup` / `clickLookup` in `src/lib/inovelli.ts`. */
function rawInovelliEp2Scene(data4: number, buttonLookupKey: number, clickLookupKey: number): number[] {
return [0, 0, 0, 0, data4, buttonLookupKey, clickLookupKey];
}
function processFromZigbeeMessage(
definition: Definition,
cluster: string,
type: string,
data: KeyValue | number[],
endpointID: number,
// Required for converters that touch globalStore; the store discriminates entities by constructor name and rejects plain mocks.
device?: Zh.Device,
) {
const converters = definition.fromZigbee.filter((c) => {
const typeMatch = Array.isArray(c.type) ? c.type.includes(type) : c.type === type;
return c.cluster === cluster && typeMatch;
});
// biome-ignore lint/suspicious/noExplicitAny: test mock
const endpoint = device ? device.getEndpoint(endpointID) : ({ID: endpointID} as any);
let payload: KeyValue = {};
for (const converter of converters) {
// biome-ignore lint/suspicious/noExplicitAny: test mock
const msg: Fz.Message<any, any, any> = {
data,
endpoint,
device: null,
meta: null,
groupID: 0,
// biome-ignore lint/suspicious/noExplicitAny: test mock
type: type as any,
// biome-ignore lint/suspicious/noExplicitAny: test mock
cluster: cluster as any,
linkquality: 0,
};
const converted = converter.convert(definition, msg, () => {}, {}, {state: {}, device: null, deviceExposesChanged: () => {}});
if (converted) {
payload = {...payload, ...converted};
}
}
return payload;
}
function findTzConverter(definition: Definition, key: string): Tz.Converter {
const converter = definition.toZigbee.find((c) => c.key.includes(key));
expect(converter, `toZigbee converter for key "${key}" not found`).toBeDefined();
return converter as Tz.Converter;
}
function buildMeta(device: ReturnType<typeof mockDevice>, overrides?: Partial<Tz.Meta>): Tz.Meta {
return {
state: {},
device,
message: {} as KeyValueAny,
mapped: {} as Definition,
options: {},
endpoint_name: undefined,
...overrides,
} as Tz.Meta;
}
async function setupVZM30(softwareBuildID?: string) {
const device = mockDevice({
modelID: "VZM30-SN",
endpoints: [{ID: 1, inputClusters: ["genOnOff", "genLevelCtrl"]}, {ID: 2}, {ID: 3}, {ID: 4}],
softwareBuildID,
});
const definition = await findByDevice(device);
return {device, definition};
}
async function setupVZM31(softwareBuildID?: string) {
const device = mockDevice({
modelID: "VZM31-SN",
endpoints: [{ID: 1, inputClusters: ["genOnOff", "genLevelCtrl"]}, {ID: 2}, {ID: 3}],
softwareBuildID,
});
const definition = await findByDevice(device);
return {device, definition};
}
async function setupVZM32(softwareBuildID?: string) {
const device = mockDevice({
modelID: "VZM32-SN",
endpoints: [{ID: 1, inputClusters: ["genOnOff", "genLevelCtrl"]}, {ID: 2}, {ID: 3}],
softwareBuildID,
});
const definition = await findByDevice(device);
return {device, definition};
}
async function setupVZM35(softwareBuildID?: string) {
const device = mockDevice({
modelID: "VZM35-SN",
endpoints: [
{ID: 1, inputClusters: ["genOnOff", "genLevelCtrl"]},
{ID: 2, inputClusters: []},
],
softwareBuildID,
});
const definition = await findByDevice(device);
return {device, definition};
}
async function setupVZM36(softwareBuildID?: string) {
const device = mockDevice({
modelID: "VZM36",
endpoints: [
{ID: 1, inputClusters: ["genOnOff", "genLevelCtrl"]},
{ID: 2, inputClusters: ["genOnOff", "genLevelCtrl"]},
],
softwareBuildID,
});
const definition = await findByDevice(device);
return {device, definition};
}
type MockConfiguredDevice = ReturnType<typeof mockDevice>;
function patchDeviceForConfigure(device: MockConfiguredDevice) {
vi.spyOn(device, "save").mockImplementation(() => {});
const defaults: Record<string, number> = {
acPowerDivisor: 10,
acPowerMultiplier: 1,
divisor: 100,
multiplier: 1,
};
for (const ep of device.endpoints) {
vi.spyOn(ep, "save").mockImplementation(() => {});
vi.spyOn(ep, "read").mockImplementation((cluster: string, attrs: string[]) => {
const result: Record<string, number> = {};
for (const attr of attrs) {
result[attr] = defaults[attr] ?? 0;
}
try {
ep.saveClusterAttributeKeyValue(cluster, result);
} catch {
// Custom clusters (e.g. manuSpecificInovelli) may not be registered in Zcl
}
return Promise.resolve(result);
});
}
}
function collectReadAttributes(device: MockConfiguredDevice): string[] {
const allReadKeys: string[] = [];
for (const ep of device.endpoints) {
for (const call of (ep.read as ReturnType<typeof vi.fn>).mock.calls) {
allReadKeys.push(...(call[1] as string[]));
}
}
return allReadKeys;
}
/** Normalize a fromZigbee converter (or a bare fingerprint) to {cluster, sorted type}. */
function fzFingerprint(converter: {cluster: string | number; type: string | string[]}) {
return {
cluster: converter.cluster,
type: Array.isArray(converter.type) ? [...converter.type].sort() : converter.type,
};
}
type ReportingItemExpectation = {attribute: string; min: number; max: number; change: number | null | "NaN"};
type ReportingCallExpectation = {cluster: string; items: ReportingItemExpectation[]};
/** Expected `endpoint.command(cluster, command, payload, ...)` call during `configure()`. */
type CommandCallExpectation = {cluster: string; command: string; payload: Record<string, unknown>};
interface IntegrationAssertion {
model: string;
device: MockConfiguredDevice;
meta: Record<string, unknown> | undefined;
fromZigbeeFingerprint: {cluster: string; type: string | string[]}[];
toZigbeeKeysContain: string[];
toZigbeeKeysOmit?: string[];
exposeFingerprints: string[];
bind: Record<number, string[]>;
readCount: Record<number, number>;
readClusters?: Record<number, string[]>;
writeCount: Record<number, number>;
configureReporting: Record<number, ReportingCallExpectation[]>;
/** Optional: assert that specific `endpoint.command(...)` calls were made during `configure()` (e.g. mmWave query_areas). */
commands?: Record<number, CommandCallExpectation[]>;
}
/** Assert a full Inovelli device definition against its configure-time side effects. */
async function assertInovelliIntegration(e: IntegrationAssertion): Promise<Definition> {
patchDeviceForConfigure(e.device);
const definition = await findByDevice(e.device);
expect(definition.model).toBe(e.model);
expect(definition.ota).toBe(true);
expect(definition.meta).toEqual(e.meta);
expect(definition.fromZigbee.map(fzFingerprint)).toStrictEqual(e.fromZigbeeFingerprint.map(fzFingerprint));
const allTzKeys = definition.toZigbee.flatMap((c) => c.key);
for (const key of e.toZigbeeKeysContain) {
expect(allTzKeys, `toZigbee should contain "${key}"`).toContain(key);
}
for (const key of e.toZigbeeKeysOmit ?? []) {
expect(allTzKeys, `toZigbee should not contain "${key}"`).not.toContain(key);
}
const exposes = resolveExposes(definition, e.device);
const actualFingerprints = exposes
.map((ex) => ex.property ?? `${ex.type}${ex.endpoint ? `_${ex.endpoint}` : ""}(${ex.features?.map((f) => f.name).join(",")})`)
.sort();
expect(actualFingerprints).toStrictEqual([...e.exposeFingerprints].sort());
await definition.configure(e.device, e.device.getEndpoint(1), definition);
for (const ep of e.device.endpoints) {
const bindCalls = (ep.bind as ReturnType<typeof vi.fn>).mock.calls.map((c) => c[0]);
expect(bindCalls, `bind(EP${ep.ID})`).toStrictEqual(e.bind[ep.ID] ?? []);
const readCalls = (ep.read as ReturnType<typeof vi.fn>).mock.calls;
expect(readCalls.length, `read count (EP${ep.ID})`).toBe(e.readCount[ep.ID] ?? 0);
if (e.readClusters?.[ep.ID]) {
const clustersRead = Array.from(new Set(readCalls.map((c) => c[0] as string))).sort();
expect(clustersRead, `read clusters (EP${ep.ID})`).toStrictEqual([...e.readClusters[ep.ID]].sort());
}
const writeCalls = (ep.write as ReturnType<typeof vi.fn>).mock.calls;
expect(writeCalls.length, `write count (EP${ep.ID})`).toBe(e.writeCount[ep.ID] ?? 0);
const reportingCalls = (ep.configureReporting as ReturnType<typeof vi.fn>).mock.calls;
const expectedReporting = e.configureReporting[ep.ID] ?? [];
expect(reportingCalls.length, `configureReporting count (EP${ep.ID})`).toBe(expectedReporting.length);
reportingCalls.forEach((call, callIdx) => {
const want = expectedReporting[callIdx];
const items = call[1] as ReportingItem[];
expect(call[0], `configureReporting[${callIdx}] cluster (EP${ep.ID})`).toBe(want.cluster);
expect(items.length, `configureReporting[${callIdx}] item count (EP${ep.ID}, cluster=${want.cluster})`).toBe(want.items.length);
items.forEach((item, itemIdx) => {
const wantItem = want.items[itemIdx];
const label = `configureReporting[${callIdx}].items[${itemIdx}] (EP${ep.ID}, cluster=${want.cluster})`;
expect(item.attribute, `${label} attribute`).toBe(wantItem.attribute);
expect(item.minimumReportInterval, `${label} min`).toBe(wantItem.min);
expect(item.maximumReportInterval, `${label} max`).toBe(wantItem.max);
if (wantItem.change === "NaN") {
expect(item.reportableChange, `${label} change`).toBeNaN();
} else {
expect(item.reportableChange, `${label} change`).toBe(wantItem.change);
}
});
});
for (const want of e.commands?.[ep.ID] ?? []) {
const match = (ep.command as ReturnType<typeof vi.fn>).mock.calls.find((call) => call[0] === want.cluster && call[1] === want.command);
expect(match, `command ${want.cluster}.${want.command} (EP${ep.ID})`).toBeDefined();
expect(match?.[2]).toStrictEqual(want.payload);
}
}
return definition;
}
type ReportingItem = {attribute: string; minimumReportInterval: number; maximumReportInterval: number; reportableChange: number | null | typeof NaN};
async function runInovelliConfigure(device: MockConfiguredDevice): Promise<string[]> {
const definition = await findByDevice(device);
patchDeviceForConfigure(device);
await definition.configure(device, device.getEndpoint(1), definition);
return collectReadAttributes(device);
}
describe("Inovelli toZigbee converters", () => {
describe("inovelli_parameters (write + get)", () => {
it("convertSet for an enum attribute should write mapped numeric value", async () => {
const {device, definition} = await setupVZM31();
const converter = findTzConverter(definition, "switchType");
const endpoint = device.getEndpoint(1);
const meta = buildMeta(device, {mapped: definition, message: {switchType: "3-Way Dumb Switch"}});
const result = await converter.convertSet(endpoint, "switchType", "3-Way Dumb Switch", meta);
expect(endpoint.write).toHaveBeenCalledWith(
"manuSpecificInovelli",
{22: {value: 1, type: expect.any(Number)}},
{manufacturerCode: 0x122f},
);
expect(result).toStrictEqual({state: {switchType: "3-Way Dumb Switch"}});
});
it("convertSet for a numeric attribute should write raw numeric value", async () => {
const {device, definition} = await setupVZM31();
const converter = findTzConverter(definition, "dimmingSpeedUpRemote");
const endpoint = device.getEndpoint(1);
const meta = buildMeta(device, {mapped: definition, message: {dimmingSpeedUpRemote: 50}});
const result = await converter.convertSet(endpoint, "dimmingSpeedUpRemote", 50, meta);
expect(endpoint.write).toHaveBeenCalledWith(
"manuSpecificInovelli",
{1: {value: 50, type: expect.any(Number)}},
{manufacturerCode: 0x122f},
);
expect(result).toStrictEqual({state: {dimmingSpeedUpRemote: 50}});
});
it("convertGet should read from the cluster with manufacturer code", async () => {
const {device, definition} = await setupVZM31();
const converter = findTzConverter(definition, "dimmingSpeedUpRemote");
const endpoint = device.getEndpoint(1);
const meta = buildMeta(device, {mapped: definition});
await converter.convertGet(endpoint, "dimmingSpeedUpRemote", meta);
expect(endpoint.read).toHaveBeenCalledWith("manuSpecificInovelli", ["dimmingSpeedUpRemote"], {manufacturerCode: 0x122f});
});
it("convertSet should return undefined for unknown key", async () => {
const {device, definition} = await setupVZM31();
const converter = findTzConverter(definition, "dimmingSpeedUpRemote");
const endpoint = device.getEndpoint(1);
const meta = buildMeta(device, {mapped: definition, message: {nonExistentKey: 42}});
const result = await converter.convertSet(endpoint, "nonExistentKey", 42, meta);
expect(result).toBeUndefined();
});
// Regression test for https://github.com/Koenkk/zigbee-herdsman-converters/issues/11698
it("convertSet should write all matching keys from meta.message in one batched payload", async () => {
const {device, definition} = await setupVZM31();
const converter = findTzConverter(definition, "defaultLevelLocal");
const endpoint = device.getEndpoint(1);
const meta = buildMeta(device, {
mapped: definition,
message: {defaultLevelLocal: 100, defaultLevelRemote: 100},
});
const result = await converter.convertSet(endpoint, "defaultLevelLocal", 100, meta);
expect(endpoint.write).toHaveBeenCalledTimes(1);
expect(endpoint.write).toHaveBeenCalledWith(
"manuSpecificInovelli",
{
13: {value: 100, type: expect.any(Number)},
14: {value: 100, type: expect.any(Number)},
},
{manufacturerCode: 0x122f},
);
expect(result).toStrictEqual({state: {defaultLevelLocal: 100, defaultLevelRemote: 100}});
});
it("convertSet should batch keys regardless of the order they appear in meta.message", async () => {
const {device, definition} = await setupVZM31();
const converter = findTzConverter(definition, "defaultLevelLocal");
const endpoint = device.getEndpoint(1);
const meta = buildMeta(device, {
mapped: definition,
message: {defaultLevelRemote: 50, defaultLevelLocal: 50},
});
const result = await converter.convertSet(endpoint, "defaultLevelRemote", 50, meta);
expect(endpoint.write).toHaveBeenCalledTimes(1);
expect(endpoint.write).toHaveBeenCalledWith(
"manuSpecificInovelli",
{
13: {value: 50, type: expect.any(Number)},
14: {value: 50, type: expect.any(Number)},
},
{manufacturerCode: 0x122f},
);
expect(result).toStrictEqual({state: {defaultLevelRemote: 50, defaultLevelLocal: 50}});
});
it("convertSet should ignore unknown and read-only keys when batching", async () => {
const {device, definition} = await setupVZM31();
const converter = findTzConverter(definition, "defaultLevelLocal");
const endpoint = device.getEndpoint(1);
const meta = buildMeta(device, {
mapped: definition,
message: {
defaultLevelLocal: 75,
nonExistentKey: 42,
// internalTemperature is a read-only attribute on the same cluster
internalTemperature: 99,
},
});
const result = await converter.convertSet(endpoint, "defaultLevelLocal", 75, meta);
expect(endpoint.write).toHaveBeenCalledTimes(1);
expect(endpoint.write).toHaveBeenCalledWith(
"manuSpecificInovelli",
{13: {value: 75, type: expect.any(Number)}},
{manufacturerCode: 0x122f},
);
expect(result).toStrictEqual({state: {defaultLevelLocal: 75}});
});
it("convertSet should map enum values when batched alongside numeric values", async () => {
const {device, definition} = await setupVZM31();
const converter = findTzConverter(definition, "switchType");
const endpoint = device.getEndpoint(1);
const meta = buildMeta(device, {
mapped: definition,
message: {switchType: "3-Way Dumb Switch", dimmingSpeedUpRemote: 50},
});
const result = await converter.convertSet(endpoint, "switchType", "3-Way Dumb Switch", meta);
expect(endpoint.write).toHaveBeenCalledTimes(1);
expect(endpoint.write).toHaveBeenCalledWith(
"manuSpecificInovelli",
{
1: {value: 50, type: expect.any(Number)},
22: {value: 1, type: expect.any(Number)},
},
{manufacturerCode: 0x122f},
);
expect(result).toStrictEqual({state: {switchType: "3-Way Dumb Switch", dimmingSpeedUpRemote: 50}});
});
});
describe("VZM36 endpoint resolution", () => {
it("convertSet with suffixed key should write to the correct endpoint", async () => {
const {device, definition} = await setupVZM36();
const converter = findTzConverter(definition, "dimmingSpeedUpRemote_2");
const ep1 = device.getEndpoint(1);
const ep2 = device.getEndpoint(2);
// biome-ignore lint/style/useNamingConvention: matches device attribute key format
const meta = buildMeta(device, {mapped: definition, message: {dimmingSpeedUpRemote_2: 25}});
await converter.convertSet(ep1, "dimmingSpeedUpRemote_2", 25, meta);
expect(ep2.write).toHaveBeenCalledWith("manuSpecificInovelli", {1: {value: 25, type: expect.any(Number)}}, {manufacturerCode: 0x122f});
expect(ep1.write).not.toHaveBeenCalled();
});
it("convertSet should bucket combined _1 / _2 keys to their respective endpoints", async () => {
const {device, definition} = await setupVZM36();
const converter = findTzConverter(definition, "defaultLevelRemote_1");
const ep1 = device.getEndpoint(1);
const ep2 = device.getEndpoint(2);
const meta = buildMeta(device, {
mapped: definition,
message: {
// biome-ignore lint/style/useNamingConvention: matches device attribute key format
defaultLevelRemote_1: 80,
// biome-ignore lint/style/useNamingConvention: matches device attribute key format
defaultLevelRemote_2: 40,
},
});
const result = await converter.convertSet(ep1, "defaultLevelRemote_1", 80, meta);
expect(ep1.write).toHaveBeenCalledTimes(1);
expect(ep1.write).toHaveBeenCalledWith("manuSpecificInovelli", {14: {value: 80, type: expect.any(Number)}}, {manufacturerCode: 0x122f});
expect(ep2.write).toHaveBeenCalledTimes(1);
expect(ep2.write).toHaveBeenCalledWith("manuSpecificInovelli", {14: {value: 40, type: expect.any(Number)}}, {manufacturerCode: 0x122f});
expect(result).toStrictEqual({
state: {
// biome-ignore lint/style/useNamingConvention: matches device attribute key format
defaultLevelRemote_1: 80,
// biome-ignore lint/style/useNamingConvention: matches device attribute key format
defaultLevelRemote_2: 40,
},
});
});
it("convertGet with suffixed key should read from the correct endpoint", async () => {
const {device, definition} = await setupVZM36();
const converter = findTzConverter(definition, "dimmingSpeedUpRemote_2");
const ep1 = device.getEndpoint(1);
const ep2 = device.getEndpoint(2);
const meta = buildMeta(device, {mapped: definition});
await converter.convertGet(ep1, "dimmingSpeedUpRemote_2", meta);
expect(ep2.read).toHaveBeenCalledWith("manuSpecificInovelli", ["dimmingSpeedUpRemote"], {manufacturerCode: 0x122f});
expect(ep1.read).not.toHaveBeenCalled();
});
});
describe("inovelli_parameters_readOnly", () => {
it("convertGet should read from cluster with manufacturer code", async () => {
const {device, definition} = await setupVZM31();
const converter = findTzConverter(definition, "internalTemperature");
const endpoint = device.getEndpoint(1);
const meta = buildMeta(device, {mapped: definition});
await converter.convertGet(endpoint, "internalTemperature", meta);
expect(endpoint.read).toHaveBeenCalledWith("manuSpecificInovelli", ["internalTemperature"], {manufacturerCode: 0x122f});
});
it("converter should not have convertSet", async () => {
const {definition} = await setupVZM31();
const converter = findTzConverter(definition, "internalTemperature");
expect(converter.convertSet).toBeUndefined();
});
});
describe("LED effect commands", () => {
it("inovelli_led_effect should send ledEffect command with correct params", async () => {
const {device, definition} = await setupVZM31();
const converter = findTzConverter(definition, "led_effect");
const endpoint = device.getEndpoint(1);
const values = {effect: "chase", color: 100, level: 80, duration: 30};
const meta = buildMeta(device, {mapped: definition, message: {led_effect: values}});
const result = await converter.convertSet(endpoint, "led_effect", values, meta);
expect(endpoint.command).toHaveBeenCalledWith(
"manuSpecificInovelli",
"ledEffect",
{effect: 5, color: 100, level: 80, duration: 30},
{disableResponse: true, disableDefaultResponse: true},
);
expect(result).toStrictEqual({state: {led_effect: values}});
});
it("inovelli_led_effect should clamp values", async () => {
const {device, definition} = await setupVZM31();
const converter = findTzConverter(definition, "led_effect");
const endpoint = device.getEndpoint(1);
const values = {effect: "solid", color: 300, level: 200, duration: 999};
const meta = buildMeta(device, {mapped: definition, message: {led_effect: values}});
await converter.convertSet(endpoint, "led_effect", values, meta);
expect(endpoint.command).toHaveBeenCalledWith(
"manuSpecificInovelli",
"ledEffect",
{effect: 1, color: 255, level: 100, duration: 255},
{disableResponse: true, disableDefaultResponse: true},
);
});
it("inovelli_individual_led_effect should convert 1-based to 0-based LED number", async () => {
const {device, definition} = await setupVZM31();
const converter = findTzConverter(definition, "individual_led_effect");
const endpoint = device.getEndpoint(1);
const values = {led: "1", effect: "solid", color: 50, level: 50, duration: 10};
const meta = buildMeta(device, {mapped: definition, message: {individual_led_effect: values}});
const result = await converter.convertSet(endpoint, "individual_led_effect", values, meta);
expect(endpoint.command).toHaveBeenCalledWith(
"manuSpecificInovelli",
"individualLedEffect",
{led: 0, effect: 1, color: 50, level: 50, duration: 10},
{disableResponse: true, disableDefaultResponse: true},
);
expect(result).toStrictEqual({state: {individual_led_effect: values}});
});
it("inovelli_individual_led_effect LED 7 -> sent as 6", async () => {
const {device, definition} = await setupVZM31();
const converter = findTzConverter(definition, "individual_led_effect");
const endpoint = device.getEndpoint(1);
const values = {led: "7", effect: "pulse", color: 200, level: 100, duration: 255};
const meta = buildMeta(device, {mapped: definition, message: {individual_led_effect: values}});
await converter.convertSet(endpoint, "individual_led_effect", values, meta);
expect(endpoint.command).toHaveBeenCalledWith(
"manuSpecificInovelli",
"individualLedEffect",
{led: 6, effect: 4, color: 200, level: 100, duration: 255},
{disableResponse: true, disableDefaultResponse: true},
);
});
it("inovelli_individual_led_effect should clamp values", async () => {
const {device, definition} = await setupVZM31();
const converter = findTzConverter(definition, "individual_led_effect");
const endpoint = device.getEndpoint(1);
const values = {led: "99", effect: "off", color: 500, level: 300, duration: 999};
const meta = buildMeta(device, {mapped: definition, message: {individual_led_effect: values}});
await converter.convertSet(endpoint, "individual_led_effect", values, meta);
expect(endpoint.command).toHaveBeenCalledWith(
"manuSpecificInovelli",
"individualLedEffect",
{led: 6, effect: 0, color: 255, level: 100, duration: 255},
{disableResponse: true, disableDefaultResponse: true},
);
});
});
describe("energy_reset", () => {
it("should send energyReset command with empty payload", async () => {
const {device, definition} = await setupVZM31();
const converter = findTzConverter(definition, "energy_reset");
const endpoint = device.getEndpoint(1);
const meta = buildMeta(device, {mapped: definition, message: {energy_reset: ""}});
await converter.convertSet(endpoint, "energy_reset", "", meta);
expect(endpoint.command).toHaveBeenCalledWith(
"manuSpecificInovelli",
"energyReset",
{},
{disableResponse: true, disableDefaultResponse: true},
);
});
});
describe("light_onoff_brightness_inovelli", () => {
it("off with no transition -> delegates to on_off path", async () => {
const {device, definition} = await setupVZM31();
const converter = findTzConverter(definition, "state");
const endpoint = device.getEndpoint(1);
const meta = buildMeta(device, {mapped: definition, message: {state: "OFF"}, state: {state: "ON"}});
await converter.convertSet(endpoint, "state", "OFF", meta);
expect(endpoint.command).toHaveBeenCalledWith("genOnOff", "off", {}, expect.any(Object));
});
it("toggle with no transition -> uses on_off path", async () => {
const {device, definition} = await setupVZM31();
const converter = findTzConverter(definition, "state");
const endpoint = device.getEndpoint(1);
const meta = buildMeta(device, {mapped: definition, message: {state: "toggle"}, state: {state: "ON"}});
const result = await converter.convertSet(endpoint, "state", "toggle", meta);
expect(endpoint.command).toHaveBeenCalledWith("genOnOff", "toggle", {}, expect.any(Object));
expect(result).toStrictEqual({state: {state: "OFF"}});
});
it("on with brightness + no transition -> uses 0xffff transtime", async () => {
const {device, definition} = await setupVZM31();
const converter = findTzConverter(definition, "state");
const endpoint = device.getEndpoint(1);
const meta = buildMeta(device, {mapped: definition, message: {state: "ON", brightness: 128}, state: {}});
await converter.convertSet(endpoint, "state", "ON", meta);
expect(endpoint.command).toHaveBeenCalledWith(
"genLevelCtrl",
"moveToLevelWithOnOff",
expect.objectContaining({level: 128, transtime: 0xffff}),
expect.any(Object),
);
});
it("on with explicit transition -> uses that transition value", async () => {
const {device, definition} = await setupVZM31();
const converter = findTzConverter(definition, "state");
const endpoint = device.getEndpoint(1);
const meta = buildMeta(device, {mapped: definition, message: {state: "ON", brightness: 200, transition: 2}, state: {}});
await converter.convertSet(endpoint, "state", "ON", meta);
expect(endpoint.command).toHaveBeenCalledWith(
"genLevelCtrl",
"moveToLevelWithOnOff",
expect.objectContaining({level: 200, transtime: 20}),
expect.any(Object),
);
});
it("on with no brightness and no transition -> uses on_off path", async () => {
const {device, definition} = await setupVZM31();
const converter = findTzConverter(definition, "state");
const endpoint = device.getEndpoint(1);
const meta = buildMeta(device, {mapped: definition, message: {state: "ON"}, state: {}});
await converter.convertSet(endpoint, "state", "ON", meta);
expect(endpoint.command).toHaveBeenCalledWith("genOnOff", expect.any(String), expect.any(Object), expect.any(Object));
});
});
describe("fan_mode toZigbee (VZM35-SN)", () => {
// `low`/`medium`/`high` map to the same levels that the fromZigbee path parses back; `transtime=0xffff`
// signals "no transition" to the firmware. `state: "ON"` is implicitly added to the returned state.
it.each([
{fan_mode: "low", level: 2},
{fan_mode: "medium", level: 86},
{fan_mode: "high", level: 170},
])("sends moveToLevelWithOnOff level=$level for fan_mode=$fan_mode", async ({fan_mode, level}) => {
const {device, definition} = await setupVZM35();
const converter = findTzConverter(definition, "fan_mode");
const ep1 = device.getEndpoint(1);
const meta = buildMeta(device, {mapped: definition, message: {fan_mode}, state: {}});
const result = await converter.convertSet(ep1, "fan_mode", fan_mode, meta);
expect(ep1.command).toHaveBeenCalledWith(
"genLevelCtrl",
"moveToLevelWithOnOff",
{level, transtime: 0xffff, optionsMask: 0, optionsOverride: 0},
expect.any(Object),
);
expect(result).toStrictEqual({state: {fan_mode, state: "ON"}});
});
it("convertGet should read currentLevel from the correct endpoint", async () => {
const {device, definition} = await setupVZM35();
const converter = findTzConverter(definition, "fan_mode");
const ep1 = device.getEndpoint(1);
const meta = buildMeta(device, {mapped: definition});
await converter.convertGet(ep1, "fan_mode", meta);
expect(ep1.read).toHaveBeenCalledWith("genLevelCtrl", ["currentLevel"]);
});
});
describe("fan_mode toZigbee (VZM36 EP2)", () => {
it("should return fan_state ON when endpointId is 2", async () => {
const {device, definition} = await setupVZM36();
const converter = findTzConverter(definition, "fan_mode");
const ep1 = device.getEndpoint(1);
const ep2 = device.getEndpoint(2);
const meta = buildMeta(device, {mapped: definition, message: {fan_mode: "low"}, state: {}});
const result = await converter.convertSet(ep1, "fan_mode", "low", meta);
expect(ep2.command).toHaveBeenCalledWith(
"genLevelCtrl",
"moveToLevelWithOnOff",
{level: 2, transtime: 0xffff, optionsMask: 0, optionsOverride: 0},
expect.any(Object),
);
expect(result).toStrictEqual({state: {fan_mode: "low", fan_state: "ON"}});
});
});
describe("fan_state toZigbee (VZM35-SN)", () => {
it("should delegate to on_off and remap state to fan_state", async () => {
const {device, definition} = await setupVZM35();
const converter = findTzConverter(definition, "fan_state");
const ep1 = device.getEndpoint(1);
const meta = buildMeta(device, {mapped: definition, message: {fan_state: "ON"}, state: {fan_state: "OFF"}});
const result = await converter.convertSet(ep1, "fan_state", "ON", meta);
expect(ep1.command).toHaveBeenCalledWith("genOnOff", "on", {}, expect.any(Object));
expect(result).toStrictEqual({state: {fan_state: "ON"}});
});
it("should handle OFF state", async () => {
const {device, definition} = await setupVZM35();
const converter = findTzConverter(definition, "fan_state");
const ep1 = device.getEndpoint(1);
const meta = buildMeta(device, {mapped: definition, message: {fan_state: "OFF"}, state: {fan_state: "ON"}});
const result = await converter.convertSet(ep1, "fan_state", "OFF", meta);
expect(ep1.command).toHaveBeenCalledWith("genOnOff", "off", {}, expect.any(Object));
expect(result).toStrictEqual({state: {fan_state: "OFF"}});
});
it("convertGet should read onOff", async () => {
const {device, definition} = await setupVZM35();
const converter = findTzConverter(definition, "fan_state");
const ep1 = device.getEndpoint(1);
const meta = buildMeta(device, {mapped: definition});
await converter.convertGet(ep1, "fan_state", meta);
expect(ep1.read).toHaveBeenCalledWith("genOnOff", ["onOff"]);
});
});
describe("breezeMode toZigbee (VZM35-SN)", () => {
it("should encode full 5-speed config into packed integer", async () => {
const {device, definition} = await setupVZM35();
const converter = findTzConverter(definition, "breezeMode");
const ep1 = device.getEndpoint(1);
const value = {
speed1: "low",
time1: 10,
speed2: "medium",
time2: 15,
speed3: "high",
time3: 20,
speed4: "low",
time4: 5,
speed5: "medium",
time5: 10,
};
const meta = buildMeta(device, {mapped: definition, message: {breezeMode: value}});
const result = await converter.convertSet(ep1, "breezeMode", value, meta);
expect(result).toStrictEqual({state: {breezeMode: value}});
const expectedValue = 1 + 8 + 128 + 768 + 12288 + 65536 + 262144 + 1048576 + 33554432 + 134217728;
expect(ep1.write).toHaveBeenCalledWith("manuSpecificInovelli", {breezeMode: expectedValue.toString()}, {manufacturerCode: 0x122f});
});
it("should terminate early when speed2 is off", async () => {
const {device, definition} = await setupVZM35();
const converter = findTzConverter(definition, "breezeMode");
const ep1 = device.getEndpoint(1);
const value = {
speed1: "high",
time1: 5,
speed2: "off",
time2: 0,
speed3: "low",
time3: 10,
speed4: "medium",
time4: 15,
speed5: "high",
time5: 20,
};
const meta = buildMeta(device, {mapped: definition, message: {breezeMode: value}});
await converter.convertSet(ep1, "breezeMode", value, meta);
const expectedValue = 3 + 4;
expect(ep1.write).toHaveBeenCalledWith("manuSpecificInovelli", {breezeMode: expectedValue.toString()}, {manufacturerCode: 0x122f});
});
});
describe("mmWave toZigbee (VZM32-SN)", () => {
// mmwave_control_commands maps a string command to a numeric controlID that the firmware accepts.
it.each([
{controlID: "reset_mmwave_module", id: 0},
{controlID: "set_interference", id: 1},
{controlID: "query_areas", id: 2},
])("mmwave_control_commands maps $controlID to controlID $id", async ({controlID, id}) => {
const {device, definition} = await setupVZM32();
const converter = findTzConverter(definition, "mmwave_control_commands");
const endpoint = device.getEndpoint(1);
const values = {controlID};
const meta = buildMeta(device, {mapped: definition, message: {mmwave_control_commands: values}});
const result = await converter.convertSet(endpoint, "mmwave_control_commands", values, meta);
expect(endpoint.command).toHaveBeenCalledWith(
"manuSpecificInovelliMMWave",
"mmWaveControl",
{controlID: id},
{disableResponse: true, disableDefaultResponse: true},
);
expect(result).toStrictEqual({state: {mmwave_control_commands: values}});
});
it("mmwave_detection_areas should send setDetectionArea for each area", async () => {
const {device, definition} = await setupVZM32();
const converter = findTzConverter(definition, "mmwave_detection_areas");
const endpoint = device.getEndpoint(1);
const values = {
area1: {width_min: -100, width_max: 100, depth_min: 0, depth_max: 500, height_min: -50, height_max: 200},
area2: {width_min: -200, width_max: 200, depth_min: 50, depth_max: 600, height_min: 0, height_max: 300},
};
const meta = buildMeta(device, {mapped: definition, message: {mmwave_detection_areas: values}});
const result = await converter.convertSet(endpoint, "mmwave_detection_areas", values, meta);
expect(endpoint.command).toHaveBeenCalledWith(
"manuSpecificInovelliMMWave",
"setDetectionArea",
{areaId: 0, xMin: -100, xMax: 100, yMin: 0, yMax: 500, zMin: -50, zMax: 200},
{disableResponse: true, disableDefaultResponse: true},
);
expect(endpoint.command).toHaveBeenCalledWith(
"manuSpecificInovelliMMWave",
"setDetectionArea",
{areaId: 1, xMin: -200, xMax: 200, yMin: 50, yMax: 600, zMin: 0, zMax: 300},
{disableResponse: true, disableDefaultResponse: true},
);
expect(endpoint.command).toHaveBeenCalledWith(
"manuSpecificInovelliMMWave",
"mmWaveControl",
{controlID: 2},
{disableResponse: true, disableDefaultResponse: true},
);
expect(result).toStrictEqual({
state: {mmwave_detection_areas: {area1: values.area1, area2: values.area2}},
});
});
it("mmwave_interference_areas should send setInterferenceArea", async () => {
const {device, definition} = await setupVZM32();
const converter = findTzConverter(definition, "mmwave_interference_areas");
const endpoint = device.getEndpoint(1);
const values = {
area3: {width_min: -300, width_max: 300, depth_min: 100, depth_max: 700, height_min: 10, height_max: 400},
};
const meta = buildMeta(device, {mapped: definition, message: {mmwave_interference_areas: values}});
await converter.convertSet(endpoint, "mmwave_interference_areas", values, meta);
expect(endpoint.command).toHaveBeenCalledWith(
"manuSpecificInovelliMMWave",
"setInterferenceArea",
{areaId: 2, xMin: -300, xMax: 300, yMin: 100, yMax: 700, zMin: 10, zMax: 400},
{disableResponse: true, disableDefaultResponse: true},
);
});
it("mmwave_stay_areas should send setStayArea", async () => {
const {device, definition} = await setupVZM32();
const converter = findTzConverter(definition, "mmwave_stay_areas");
const endpoint = device.getEndpoint(1);
const values = {
area4: {width_min: 0, width_max: 150, depth_min: 0, depth_max: 250, height_min: 0, height_max: 100},
};
const meta = buildMeta(device, {mapped: definition, message: {mmwave_stay_areas: values}});
await converter.convertSet(endpoint, "mmwave_stay_areas", values, meta);
expect(endpoint.command).toHaveBeenCalledWith(
"manuSpecificInovelliMMWave",
"setStayArea",
{areaId: 3, xMin: 0, xMax: 150, yMin: 0, yMax: 250, zMin: 0, zMax: 100},
{disableResponse: true, disableDefaultResponse: true},
);
});
});
});
describe("Inovelli VZM36 endpoint routing", () => {
let definition: Definition;
beforeAll(async () => {
({definition} = await setupVZM36());
expect(definition.model).toBe("VZM36");
});
describe("genOnOff from endpoint 2 (fan)", () => {
it("should set fan_state without affecting light state", () => {
const payload = processFromZigbeeMessage(definition, "genOnOff", "attributeReport", {onOff: 1}, 2);
expect(payload).toStrictEqual({fan_state: "ON"});
});
it("should not leak raw onOff data", () => {
const payload = processFromZigbeeMessage(definition, "genOnOff", "attributeReport", {onOff: 0}, 2);
expect(payload).not.toHaveProperty("onOff");
expect(payload).toStrictEqual({fan_state: "OFF"});
});
});
describe("genOnOff from endpoint 1 (light)", () => {
it("should set light state without affecting fan_state", () => {
const payload = processFromZigbeeMessage(definition, "genOnOff", "attributeReport", {onOff: 1}, 1);
expect(payload).toStrictEqual({state: "ON"});
});
it("should not leak raw onOff data", () => {
const payload = processFromZigbeeMessage(definition, "genOnOff", "attributeReport", {onOff: 0}, 1);
expect(payload).not.toHaveProperty("onOff");
expect(payload).toStrictEqual({state: "OFF"});
});
});
describe("genLevelCtrl from endpoint 2 (fan)", () => {
it("should set fan_mode without affecting light brightness", () => {
const payload = processFromZigbeeMessage(definition, "genLevelCtrl", "attributeReport", {currentLevel: 33}, 2);
expect(payload).not.toHaveProperty("brightness");
expect(payload).not.toHaveProperty("currentLevel");
expect(payload).toHaveProperty("fan_mode");
});
});
describe("genLevelCtrl from endpoint 1 (light)", () => {
it("should set brightness without affecting fan_mode", () => {
const payload = processFromZigbeeMessage(definition, "genLevelCtrl", "attributeReport", {currentLevel: 200}, 1);
expect(payload).not.toHaveProperty("fan_mode");
expect(payload).not.toHaveProperty("currentLevel");
expect(payload).toStrictEqual({brightness: 200});
});
});
});
function resolveExposes(definition: Definition, device: ReturnType<typeof mockDevice>): Expose[] {
if (typeof definition.exposes === "function") {
return definition.exposes(device, {});
}
return definition.exposes as Expose[];
}
function findExpose(exposes: Expose[], name: string): Expose | undefined {
return exposes.find((exp) => exp.name === name);
}
function assertExpose(exposes: Expose[], name: string): Expose {
const expose = exposes.find((exp) => exp.name === name);
expect(expose).toBeDefined();
return expose as Expose;
}
function getEnumValues(expose: Expose): (string | number)[] {
expect(expose.type).toBe("enum");
return (expose as {values: (string | number)[]} & Expose).values;
}
describe("Inovelli baseline exposes", () => {
it("VZM30-SN should expose all expected attributes", async () => {