iobroker.zigbee 1.6.6 → 1.6.8

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/lib/exposes.js CHANGED
@@ -1,789 +1,791 @@
1
- 'use strict';
2
-
3
- const zigbeeHerdsmanConverters = require('zigbee-herdsman-converters');
4
- const statesDefs = require(__dirname + '/states.js').states;
5
- const rgb = require(__dirname + '/rgb.js');
6
- const utils = require(__dirname + '/utils.js');
7
- const colors = require(__dirname + '/colors.js');
8
- const ea = zigbeeHerdsmanConverters.exposes.access;
9
-
10
- function genState(expose, role, name, desc) {
11
- let state;
12
- const readable = (expose.access & ea.STATE) > 0;
13
- const writable = (expose.access & ea.SET) > 0;
14
- const stname = (name || expose.property);
15
- if (typeof stname !== 'string') return;
16
- const stateId = stname.replace(/\*/g, '');
17
- const stateName = (desc || expose.description || expose.name);
18
- const propName = expose.property;
19
- switch (expose.type) {
20
- case 'binary':
21
- state = {
22
- id: stateId,
23
- prop: propName,
24
- name: stateName,
25
- icon: undefined,
26
- role: role || 'state',
27
- write: writable,
28
- read: true,
29
- type: 'boolean',
30
- };
31
- if (readable) {
32
- state.getter = payload => (payload[propName] === (expose.value_on || 'ON'));
33
- }
34
- else {
35
- state.getter = payload => ( undefined);
36
- }
37
- if (writable) {
38
- state.setter = (value) => (value) ? (expose.value_on || 'ON') : ((expose.value_off != undefined) ? expose.value_off : 'OFF');
39
- state.setattr = expose.name;
40
- }
41
- if (expose.endpoint) {
42
- state.epname = expose.endpoint;
43
- }
44
- break;
45
-
46
- case 'numeric':
47
- state = {
48
- id: stateId,
49
- prop: propName,
50
- name: stateName,
51
- icon: undefined,
52
- role: role || 'state',
53
- write: writable,
54
- read: true,
55
- type: 'number',
56
- min: expose.value_min || 0,
57
- max: expose.value_max,
58
- unit: expose.unit,
59
- };
60
- if (expose.endpoint) {
61
- state.epname = expose.endpoint;
62
- }
63
- break;
64
-
65
- case 'enum':
66
- state = {
67
- id: stateId,
68
- prop: propName,
69
- name: stateName,
70
- icon: undefined,
71
- role: role || 'state',
72
- write: writable,
73
- read: true,
74
- type: 'string',
75
- states: expose.values.map((item) => `${item}:${item}`).join(';'),
76
- };
77
- if (expose.endpoint) {
78
- state.epname = expose.endpoint;
79
- state.setattr = expose.name;
80
- }
81
- break;
82
-
83
- case 'text':
84
- state = {
85
- id: stateId,
86
- prop: propName,
87
- name: stateName,
88
- icon: undefined,
89
- role: role || 'state',
90
- write: writable,
91
- read: true,
92
- type: 'string',
93
- };
94
- if (expose.endpoint) {
95
- state.epname = expose.endpoint;
96
- }
97
- break;
98
-
99
- default:
100
- break;
101
- }
102
-
103
- return state;
104
- }
105
-
106
-
107
-
108
- function createFromExposes(model, def) {
109
- const states = [];
110
- // make the different (set and get) part of state is updatable if different exposes is used for get and set
111
- // as example:
112
- // ...
113
- // exposes.binary('some_option', ea.STATE, true, false).withDescription('Some Option'),
114
- // exposes.composite('options', 'options')
115
- // .withDescription('Some composite Options')
116
- // .withFeature(exposes.binary('some_option', ea.SET, true, false).withDescription('Some Option'))
117
- //in this case one state - `some_option` has two different exposes for set an get, we have to combine it ...
118
- //
119
- function pushToStates(state, access) {
120
- if (state === undefined) {
121
- return 0;
122
- }
123
- state.readable = (access & ea.STATE) > 0;
124
- state.writable = (access & ea.SET) > 0;
125
- const stateExists = states.findIndex( (element, index, array) => (element.id === state.id ));
126
- if (stateExists < 0 ) {
127
- state.write = state.writable;
128
- if (! state.writable) {
129
- if ( state.hasOwnProperty('setter') ) {
130
- delete state.setter;
131
- }
132
- if ( state.hasOwnProperty('setattr') ) {
133
- delete state.setattr;
134
- }
135
- }
136
- if (! state.readable) {
137
- if (state.hasOwnProperty('getter') ) {
138
- //to awid some worning on unprocessed data
139
- state.getter = payload => ( undefined );
140
- }
141
- }
142
- return states.push(state);
143
- }
144
- else {
145
- if ( (state.readable) && (! states[stateExists].readable ) ) {
146
- states[stateExists].read = state.read;
147
- // as state is readable, it can't be button or event
148
- if (states[stateExists].role === 'button') {
149
- states[stateExists].role = state.role;
150
- }
151
- if (states[stateExists].hasOwnProperty('isEvent')) {
152
- delete states[stateExists].isEvent;
153
- }
154
- // we have to use the getter from "new" state
155
- if ( state.hasOwnProperty('getter') ) {
156
- states[stateExists].getter = state.getter;
157
- }
158
- // trying to remove the `prop` property, as main key for get and set,
159
- // as it can be different in new and old states, and leave only:
160
- // setattr for old and id for new
161
- if (( state.hasOwnProperty('prop') ) && (state.prop === state.id)) {
162
- if ( states[stateExists].hasOwnProperty('prop') ) {
163
- if (states[stateExists].prop !== states[stateExists].id) {
164
- if (! states[stateExists].hasOwnProperty('setattr')) {
165
- states[stateExists].setattr = states[stateExists].prop;
166
- }
167
- }
168
- delete states[stateExists].prop;
169
- }
170
- }
171
- else if ( state.hasOwnProperty('prop') ) {
172
- states[stateExists].prop = state.prop;
173
- }
174
- states[stateExists].readable = true;
175
- }
176
- if ( (state.writable) && (! states[stateExists].writable ) ) {
177
- states[stateExists].write = state.writable;
178
- // use new state `setter`
179
- if ( state.hasOwnProperty('setter') ) {
180
- states[stateExists].setter = state.setter;
181
- }
182
- // use new state `setterOpt`
183
- if ( state.hasOwnProperty('setterOpt') ) {
184
- states[stateExists].setterOpt = state.setterOpt;
185
- }
186
- // use new state `inOptions`
187
- if ( state.hasOwnProperty('inOptions') ) {
188
- states[stateExists].inOptions = state.inOptions;
189
- }
190
- // as we have new state, responsible for set, we have to use new `isOption`
191
- // or remove it
192
- if (((! state.hasOwnProperty('isOption') ) || (state.isOptions === false))
193
- && (states[stateExists].hasOwnProperty('isOption'))) {
194
- delete states[stateExists].isOption;
195
- }
196
- else {
197
- states[stateExists].isOption = state.isOption;
198
- }
199
- // use new `setattr` or `prop` as `setattr`
200
- if ( state.hasOwnProperty('setattr') ) {
201
- states[stateExists].setattr = state.setattr;
202
- }
203
- else if ( state.hasOwnProperty('prop') ) {
204
- states[stateExists].setattr = state.prop;
205
- }
206
- // remove `prop` equal to if, due to prop is uses as key in set and get
207
- if ( states[stateExists].prop === states[stateExists].id) {
208
- delete states[stateExists].prop;
209
- }
210
- if ( state.hasOwnProperty('epname') ) {
211
- states[stateExists].epname = state.epname;
212
- }
213
- states[stateExists].writable = true;
214
- }
215
- return states.length;
216
- }
217
- }
218
- const icon = utils.getDeviceIcon(def);
219
- for (const expose of def.exposes) {
220
- let state;
221
-
222
- switch (expose.type) {
223
- case 'light':
224
- for (const prop of expose.features) {
225
- switch (prop.name) {
226
- case 'state': {
227
- const stateNameS = expose.endpoint ? `state_${expose.endpoint}` : 'state';
228
- pushToStates({
229
- id: stateNameS,
230
- name: `Switch state ${expose.endpoint ? expose.endpoint : ''}`.trim(),
231
- icon: undefined,
232
- role: 'switch',
233
- write: true,
234
- read: true,
235
- type: 'boolean',
236
- getter: (payload) => (payload[stateNameS] === (prop.value_on || 'ON')),
237
- setter: (value) => (value) ? prop.value_on || 'ON' : ((prop.value_off != undefined) ? prop.value_off : 'OFF'),
238
- epname: expose.endpoint,
239
- setattr: 'state',
240
- }, prop.access);
241
- break;
242
- }
243
- case 'brightness': {
244
- const stateNameB = expose.endpoint ? `brightness_${expose.endpoint}` : 'brightness';
245
- pushToStates({
246
- id: stateNameB,
247
- name: `Brightness ${expose.endpoint ? expose.endpoint : ''}`.trim(),
248
- icon: undefined,
249
- role: 'level.dimmer',
250
- write: true,
251
- read: true,
252
- type: 'number',
253
- min: 0, // ignore expose.value_min
254
- max: 100, // ignore expose.value_max
255
- inOptions: true,
256
- getter: payload => {
257
- return utils.bulbLevelToAdapterLevel(payload[stateNameB]);
258
- },
259
- setter: (value) => {
260
- return utils.adapterLevelToBulbLevel(value);
261
- },
262
- setterOpt: (value, options) => {
263
- const hasTransitionTime = options && options.hasOwnProperty('transition_time');
264
- const transitionTime = hasTransitionTime ? options.transition_time : 0;
265
- const preparedOptions = {...options, transition: transitionTime};
266
- preparedOptions.brightness = utils.adapterLevelToBulbLevel(value);
267
- return preparedOptions;
268
- },
269
- readResponse: (resp) => {
270
- const respObj = resp[0];
271
- if (respObj.status === 0 && respObj.attrData != undefined) {
272
- return utils.bulbLevelToAdapterLevel(respObj.attrData);
273
- }
274
- },
275
- epname: expose.endpoint,
276
- setattr: 'brightness',
277
- }, prop.access);
278
- pushToStates(statesDefs.brightness_move, prop.access);
279
- break;
280
- }
281
- case 'color_temp': {
282
- const stateNameT = expose.endpoint ? `colortemp_${expose.endpoint}` : 'colortemp';
283
- pushToStates({
284
- id: stateNameT,
285
- prop: expose.endpoint ? `color_temp_${expose.endpoint}` : 'color_temp',
286
- name: `Color temperature ${expose.endpoint ? expose.endpoint : ''}`.trim(),
287
- icon: undefined,
288
- role: 'level.color.temperature',
289
- write: true,
290
- read: true,
291
- type: 'number',
292
- min: expose.value_min,
293
- max: expose.value_max,
294
- setter: (value) => {
295
- return utils.toMired(value);
296
- },
297
- setterOpt: (value, options) => {
298
- const hasTransitionTime = options && options.hasOwnProperty('transition_time');
299
- const transitionTime = hasTransitionTime ? options.transition_time : 0;
300
- return {...options, transition: transitionTime};
301
- },
302
- epname: expose.endpoint,
303
- }, prop.access);
304
- pushToStates(statesDefs.colortemp_move, prop.access);
305
- break;
306
- }
307
- case 'color_xy': {
308
- const stateNameC = expose.endpoint ? `color_${expose.endpoint}` : 'color';
309
- pushToStates({
310
- id: stateNameC,
311
- prop: expose.endpoint ? `color_${expose.endpoint}` : 'color',
312
- name: `Color ${expose.endpoint ? expose.endpoint : ''}`.trim(),
313
- icon: undefined,
314
- role: 'level.color.rgb',
315
- write: true,
316
- read: true,
317
- type: 'string',
318
- setter: (value) => {
319
- // convert RGB to XY for set
320
- /*
321
- const result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(value);
322
- let xy = [0, 0];
323
- if (result) {
324
- const r = parseInt(result[1], 16),
325
- g = parseInt(result[2], 16),
326
- b = parseInt(result[3], 16);
327
- xy = rgb.rgb_to_cie(r, g, b);
328
- }
329
- return {
330
- x: xy[0],
331
- y: xy[1]
332
- };
333
- */
334
- let xy = [0, 0];
335
- const rgbcolor = colors.ParseColor(value);
336
-
337
- xy = rgb.rgb_to_cie(rgbcolor.r, rgbcolor.g, rgbcolor.b);
338
- return {
339
- x: xy[0],
340
- y: xy[1]
341
- };
342
-
343
- },
344
- setterOpt: (value, options) => {
345
- const hasTransitionTime = options && options.hasOwnProperty('transition_time');
346
- const transitionTime = hasTransitionTime ? options.transition_time : 0;
347
- return {...options, transition: transitionTime};
348
- },
349
- getter: payload => {
350
- if (payload.color && payload.color.hasOwnProperty('x') && payload.color.hasOwnProperty('y')) {
351
- const colorval = rgb.cie_to_rgb(payload.color.x, payload.color.y);
352
- return '#' + utils.decimalToHex(colorval[0]) + utils.decimalToHex(colorval[1]) + utils.decimalToHex(colorval[2]);
353
- } else {
354
- return undefined;
355
- }
356
- },
357
- epname: expose.endpoint,
358
- setattr: 'color',
359
- }, prop.access);
360
- break;
361
- }
362
- case 'color_hs': {
363
- const stateNameH = expose.endpoint ? `color_${expose.endpoint}` : 'color';
364
- pushToStates({
365
- id: stateNameH,
366
- prop: expose.endpoint ? `color_${expose.endpoint}` : 'color',
367
- name: `Color ${expose.endpoint ? expose.endpoint : ''}`.trim(),
368
- icon: undefined,
369
- role: 'level.color.rgb',
370
- write: true,
371
- read: true,
372
- type: 'string',
373
- setter: (value) => {
374
- const _rgb = colors.ParseColor(value);
375
- const hsv = rgb.rgbToHSV(_rgb.r, _rgb.g, _rgb.b, true);
376
- return {
377
- hue: Math.min(Math.max(hsv.h,1),359),
378
- saturation: hsv.s,
379
- // brightness: Math.floor(hsv.v * 2.55),
380
-
381
- };
382
- },
383
- setterOpt: (value, options) => {
384
- const hasTransitionTime = options && options.hasOwnProperty('transition_time');
385
- const transitionTime = hasTransitionTime ? options.transition_time : 0;
386
- return {...options, transition: transitionTime};
387
- },
388
- epname: expose.endpoint,
389
- setattr: 'color',
390
- }, prop.access);
391
- pushToStates({
392
- id: expose.endpoint ? `hue_${expose.endpoint}` : 'hue',
393
- prop: expose.endpoint ? `color_${expose.endpoint}` : 'color',
394
- name: `Hue ${expose.endpoint ? expose.endpoint : ''}`.trim(),
395
- icon: undefined,
396
- role: 'level.color.hue',
397
- write: true,
398
- read: false,
399
- type: 'number',
400
- min: 0,
401
- max: 360,
402
- inOptions: true,
403
- setter: (value, options) => {
404
- return {
405
- hue: value,
406
- saturation: options.saturation,
407
- };
408
- },
409
- setterOpt: (value, options) => {
410
- const hasTransitionTime = options && options.hasOwnProperty('transition_time');
411
- const transitionTime = hasTransitionTime ? options.transition_time : 0;
412
- const hasHueCalibrationTable = options && options.hasOwnProperty('hue_calibration');
413
- if (hasHueCalibrationTable)
414
- try {
415
- return {...options, transition: transitionTime, hue_correction: JSON.parse(options.hue_calibration)};
416
- }
417
- catch {
418
- const hue_correction_table = [];
419
- options.hue_calibration.split(',').forEach(element => {
420
- const match = /([0-9]+):([0-9]+)/.exec(element);
421
- if (match && match.length ==3)
422
- hue_correction_table.push({ in: Number(match[1]), out: Number(match[2])});
423
- });
424
- if (hue_correction_table.length > 0)
425
- return {...options, transition: transitionTime, hue_correction: hue_correction_table};
426
- }
427
- return {...options, transition: transitionTime};
428
- },
429
-
430
- }, prop.access);
431
- pushToStates({
432
- id: expose.endpoint ? `saturation_${expose.endpoint}` : 'saturation',
433
- prop: expose.endpoint ? `color_${expose.endpoint}` : 'color',
434
- name: `Saturation ${expose.endpoint ? expose.endpoint : ''}`.trim(),
435
- icon: undefined,
436
- role: 'level.color.saturation',
437
- write: true,
438
- read: false,
439
- type: 'number',
440
- min: 0,
441
- max: 100,
442
- inOptions: true,
443
- setter: (value, options) => {
444
- return {
445
- hue: options.hue,
446
- saturation: value,
447
- };
448
- },
449
- setterOpt: (value, options) => {
450
- const hasTransitionTime = options && options.hasOwnProperty('transition_time');
451
- const transitionTime = hasTransitionTime ? options.transition_time : 0;
452
- const hasHueCalibrationTable = options && options.hasOwnProperty('hue_calibration');
453
- if (hasHueCalibrationTable)
454
- try {
455
- return {...options, transition: transitionTime, hue_correction: JSON.parse(options.hue_calibration)};
456
- }
457
- catch {
458
- const hue_correction_table = [];
459
- options.hue_calibration.split(',').forEach(element => {
460
- const match = /([0-9]+):([0-9]+)/.exec(element);
461
- if (match && match.length ==3)
462
- hue_correction_table.push({ in: Number(match[1]), out: Number(match[2])});
463
- });
464
- if (hue_correction_table.length > 0)
465
- return {...options, transition: transitionTime, hue_correction: hue_correction_table};
466
- }
467
- return {...options, transition: transitionTime};
468
- },
469
-
470
- }, prop.access);
471
- pushToStates(statesDefs.hue_move, prop.access);
472
- pushToStates(statesDefs.saturation_move, prop.access);
473
- pushToStates({
474
- id: 'hue_calibration',
475
- prop: 'color',
476
- name: 'Hue color calibration table',
477
- icon: undefined,
478
- role: 'table',
479
- write: true,
480
- read: false,
481
- type: 'string',
482
- inOptions: true,
483
- setter: (value, options) => {
484
- return {
485
- hue: options.hue,
486
- saturation: options.saturation,
487
- };
488
- },
489
- setterOpt: (value, options) => {
490
- const hasTransitionTime = options && options.hasOwnProperty('transition_time');
491
- const transitionTime = hasTransitionTime ? options.transition_time : 0;
492
- const hasHueCalibrationTable = options && options.hasOwnProperty('hue_calibration');
493
- if (hasHueCalibrationTable)
494
- try {
495
- return {...options, transition: transitionTime, hue_correction: JSON.parse(options.hue_calibration)};
496
- }
497
- catch {
498
- const hue_correction_table = [];
499
- options.hue_calibration.split(',').forEach(element => {
500
- const match = /([0-9]+):([0-9]+)/.exec(element);
501
- if (match && match.length ==3)
502
- hue_correction_table.push({ in: Number(match[1]), out: Number(match[2])});
503
- });
504
- if (hue_correction_table.length > 0)
505
- return {...options, transition: transitionTime, hue_correction: hue_correction_table};
506
- }
507
- return {...options, transition: transitionTime};
508
- },
509
-
510
- }, prop.access);
511
- break;
512
- }
513
- default:
514
- pushToStates(genState(prop), prop.access);
515
- break;
516
- }
517
- }
518
- pushToStates(statesDefs.transition_time, ea.STATE_SET);
519
- break;
520
-
521
- case 'switch':
522
- for (const prop of expose.features) {
523
- switch (prop.name) {
524
- case 'state':
525
- pushToStates(genState(prop, 'switch'), prop.access);
526
- break;
527
- default:
528
- pushToStates(genState(prop), prop.access);
529
- break;
530
- }
531
- }
532
- break;
533
-
534
- case 'numeric':
535
- if (expose.endpoint) {
536
- state = genState(expose);
537
- } else {
538
- switch (expose.name) {
539
- case 'linkquality':
540
- state = undefined;
541
- break;
542
-
543
- case 'battery':
544
- state = statesDefs.battery;
545
- break;
546
-
547
- case 'voltage':
548
- state = statesDefs.plug_voltage;
549
- break;
550
-
551
- case 'temperature':
552
- state = statesDefs.temperature;
553
- break;
554
-
555
- case 'humidity':
556
- state = statesDefs.humidity;
557
- break;
558
-
559
- case 'pressure':
560
- state = statesDefs.pressure;
561
- break;
562
-
563
- case 'illuminance':
564
- state = statesDefs.illuminance_raw;
565
- break;
566
-
567
- case 'illuminance_lux':
568
- state = statesDefs.illuminance;
569
- break;
570
-
571
- case 'power':
572
- state = statesDefs.load_power;
573
- break;
574
-
575
- default:
576
- state = genState(expose);
577
- break;
578
- }
579
- }
580
- if (state) pushToStates(state, expose.access);
581
- break;
582
-
583
- case 'enum':
584
- switch (expose.name) {
585
- case 'action': {
586
- if (!Array.isArray(expose.values)) break;
587
- const hasHold = expose.values.find((actionName) => actionName.includes('hold'));
588
- const hasRelease = expose.values.find((actionName) => actionName.includes('release'));
589
- for (const actionName of expose.values) {
590
- // is release state ? - skip
591
- if (hasHold && hasRelease && actionName.includes('release')) continue;
592
- // is hold state ?
593
- if (hasHold && hasRelease && actionName.includes('hold')) {
594
- const releaseActionName = actionName.replace('hold', 'release');
595
- state = {
596
- id: actionName.replace(/\*/g, ''),
597
- prop: 'action',
598
- name: actionName,
599
- icon: undefined,
600
- role: 'button',
601
- write: false,
602
- read: true,
603
- type: 'boolean',
604
- getter: payload => (payload.action === actionName) ? true : (payload.action === releaseActionName) ? false : undefined,
605
- };
606
- } else {
607
- state = {
608
- id: actionName.replace(/\*/g, ''),
609
- prop: 'action',
610
- name: actionName,
611
- icon: undefined,
612
- role: 'button',
613
- write: false,
614
- read: true,
615
- type: 'boolean',
616
- getter: payload => (payload.action === actionName) ? true : undefined,
617
- isEvent: true,
618
- };
619
- }
620
- pushToStates(state, expose.access);
621
- }
622
- state = null;
623
- break;
624
- }
625
- default:
626
- state = genState(expose);
627
- break;
628
- }
629
- if (state) pushToStates(state, expose.access);
630
- break;
631
-
632
- case 'binary':
633
- if (expose.endpoint) {
634
- state = genState(expose);
635
- } else {
636
- switch (expose.name) {
637
- case 'contact':
638
- state = statesDefs.contact;
639
- pushToStates(statesDefs.opened, ea.STATE);
640
- break;
641
-
642
- case 'battery_low':
643
- state = statesDefs.heiman_batt_low;
644
- break;
645
-
646
- case 'tamper':
647
- state = statesDefs.tamper;
648
- break;
649
-
650
- case 'water_leak':
651
- state = statesDefs.water_detected;
652
- break;
653
-
654
- case 'lock':
655
- state = statesDefs.child_lock;
656
- break;
657
-
658
- case 'occupancy':
659
- state = statesDefs.occupancy;
660
- break;
661
-
662
- default:
663
- state = genState(expose);
664
- break;
665
- }
666
- }
667
- if (state) pushToStates(state, expose.access);
668
- break;
669
-
670
- case 'text':
671
- state = genState(expose);
672
- pushToStates(state, expose.access);
673
- break;
674
-
675
- case 'lock':
676
- case 'fan':
677
- case 'cover':
678
- for (const prop of expose.features) {
679
- switch (prop.name) {
680
- case 'state':
681
- pushToStates(genState(prop, 'switch'), prop.access);
682
- break;
683
- default:
684
- pushToStates(genState(prop), prop.access);
685
- break;
686
- }
687
- }
688
- break;
689
-
690
- case 'climate':
691
- for (const prop of expose.features) {
692
- switch (prop.name) {
693
- case 'away_mode':
694
- pushToStates(statesDefs.climate_away_mode, prop.access);
695
- break;
696
- case 'system_mode':
697
- pushToStates(statesDefs.climate_system_mode, prop.access);
698
- break;
699
- case 'running_mode':
700
- pushToStates(statesDefs.climate_running_mode, prop.access);
701
- break;
702
- default:
703
- pushToStates(genState(prop), prop.access);
704
- break;
705
- }
706
- }
707
- break;
708
-
709
- case 'composite':
710
- for (const prop of expose.features) {
711
- const st = genState(prop);
712
- st.prop = expose.property;
713
- st.inOptions = true;
714
- // I'm not fully sure, as it really needed, but
715
- st.setterOpt = (value, options) => {
716
- const result = {};
717
- options[prop.property] = value;
718
- result[expose.property] = options;
719
- return result;
720
- };
721
- // if we have a composite expose, the value have to be an object {expose.property : {prop.property: value}}
722
- if (prop.access & ea.SET) {
723
- st.setter = (value, options) => {
724
- const result = {};
725
- options[prop.property] = value;
726
- result[expose.property] = options;
727
- return result;
728
- };
729
- st.setattr = expose.property;
730
- };
731
- // if we have a composite expose, the payload will be an object {expose.property : {prop.property: value}}
732
- if (prop.access & ea.STATE) {
733
- st.getter = payload => {
734
- if ( (payload.hasOwnProperty(expose.property)) && (payload[expose.property] !== null) && payload[expose.property].hasOwnProperty(prop.property)) {
735
- return !isNaN(payload[expose.property][prop.property]) ? payload[expose.property][prop.property] : undefined
736
- }
737
- else {
738
- return undefined;
739
- }
740
- };
741
- }
742
- else {
743
- st.getter = payload => { return undefined };
744
- }
745
- ;
746
- pushToStates(st, prop.access);
747
- }
748
- break;
749
- default:
750
- console.log(`Unhandled expose type ${expose.type} for device ${model}`);
751
- }
752
- }
753
- const newDev = {
754
- models: [model],
755
- icon: icon,
756
- states: states,
757
- exposed: true,
758
- };
759
- // make the function code printable in log
760
- //console.log(`Created mapping for device ${model}: ${JSON.stringify(newDev, function(key, value) {
761
- // if (typeof value === 'function') {return value.toString() } else { return value } }, ' ')}`);
762
- return newDev;
763
- }
764
-
765
- function applyExposes(mappedDevices, byModel, allExcludesObj) {
766
- // for exlude search
767
- const allExcludesStr = JSON.stringify(allExcludesObj);
768
- // create or update device from exposes
769
- for (const deviceDef of zigbeeHerdsmanConverters.definitions) {
770
- const strippedModel = (deviceDef.model) ? deviceDef.model.replace(/\0.*$/g, '').trim() : '';
771
- // check if device is mapped
772
- const existsMap = byModel.get(strippedModel);
773
-
774
- if ((deviceDef.hasOwnProperty('exposes') && (!existsMap || !existsMap.hasOwnProperty('states'))) || allExcludesStr.indexOf(strippedModel) > 0) {
775
- const newDevice = createFromExposes(strippedModel, deviceDef);
776
- if (!existsMap) {
777
- mappedDevices.push(newDevice);
778
- byModel.set(strippedModel, newDevice);
779
- } else {
780
- existsMap.states = newDevice.states;
781
- existsMap.exposed = true;
782
- }
783
- }
784
- }
785
- }
786
-
787
- module.exports = {
788
- applyExposes: applyExposes,
789
- };
1
+ 'use strict';
2
+
3
+ const zigbeeHerdsmanConverters = require('zigbee-herdsman-converters');
4
+ const statesDefs = require(__dirname + '/states.js').states;
5
+ const rgb = require(__dirname + '/rgb.js');
6
+ const utils = require(__dirname + '/utils.js');
7
+ const colors = require(__dirname + '/colors.js');
8
+ const ea = zigbeeHerdsmanConverters.exposes.access;
9
+
10
+ function genState(expose, role, name, desc) {
11
+ let state;
12
+ const readable = (expose.access & ea.STATE) > 0;
13
+ const writable = (expose.access & ea.SET) > 0;
14
+ const stname = (name || expose.property);
15
+ if (typeof stname !== 'string') return;
16
+ const stateId = stname.replace(/\*/g, '');
17
+ const stateName = (desc || expose.description || expose.name);
18
+ const propName = expose.property;
19
+ switch (expose.type) {
20
+ case 'binary':
21
+ state = {
22
+ id: stateId,
23
+ prop: propName,
24
+ name: stateName,
25
+ icon: undefined,
26
+ role: role || 'state',
27
+ write: writable,
28
+ read: true,
29
+ type: 'boolean',
30
+ };
31
+ if (readable) {
32
+ state.getter = payload => (payload[propName] === (expose.value_on || 'ON'));
33
+ }
34
+ else {
35
+ state.getter = payload => ( undefined);
36
+ }
37
+ if (writable) {
38
+ state.setter = (value) => (value) ? (expose.value_on || 'ON') : ((expose.value_off != undefined) ? expose.value_off : 'OFF');
39
+ state.setattr = expose.name;
40
+ }
41
+ if (expose.endpoint) {
42
+ state.epname = expose.endpoint;
43
+ }
44
+ break;
45
+
46
+ case 'numeric':
47
+ state = {
48
+ id: stateId,
49
+ prop: propName,
50
+ name: stateName,
51
+ icon: undefined,
52
+ role: role || 'state',
53
+ write: writable,
54
+ read: true,
55
+ type: 'number',
56
+ min: expose.value_min || 0,
57
+ max: expose.value_max,
58
+ unit: expose.unit,
59
+ };
60
+ if (expose.endpoint) {
61
+ state.epname = expose.endpoint;
62
+ }
63
+ break;
64
+
65
+ case 'enum':
66
+ state = {
67
+ id: stateId,
68
+ prop: propName,
69
+ name: stateName,
70
+ icon: undefined,
71
+ role: role || 'state',
72
+ write: writable,
73
+ read: true,
74
+ type: 'string',
75
+ states: expose.values.map((item) => `${item}:${item}`).join(';'),
76
+ };
77
+ if (expose.endpoint) {
78
+ state.epname = expose.endpoint;
79
+ state.setattr = expose.name;
80
+ }
81
+ break;
82
+
83
+ case 'text':
84
+ state = {
85
+ id: stateId,
86
+ prop: propName,
87
+ name: stateName,
88
+ icon: undefined,
89
+ role: role || 'state',
90
+ write: writable,
91
+ read: true,
92
+ type: 'string',
93
+ };
94
+ if (expose.endpoint) {
95
+ state.epname = expose.endpoint;
96
+ }
97
+ break;
98
+
99
+ default:
100
+ break;
101
+ }
102
+
103
+ return state;
104
+ }
105
+
106
+
107
+
108
+ function createFromExposes(model, def) {
109
+ const states = [];
110
+ // make the different (set and get) part of state is updatable if different exposes is used for get and set
111
+ // as example:
112
+ // ...
113
+ // exposes.binary('some_option', ea.STATE, true, false).withDescription('Some Option'),
114
+ // exposes.composite('options', 'options')
115
+ // .withDescription('Some composite Options')
116
+ // .withFeature(exposes.binary('some_option', ea.SET, true, false).withDescription('Some Option'))
117
+ //in this case one state - `some_option` has two different exposes for set an get, we have to combine it ...
118
+ //
119
+
120
+ function pushToStates(state, access) {
121
+ if (state === undefined) {
122
+ return 0;
123
+ }
124
+ if (access === undefined) access = ea.ALL;
125
+ state.readable = (access & ea.STATE) > 0;
126
+ state.writable = (access & ea.SET) > 0;
127
+ const stateExists = states.findIndex( (element, index, array) => (element.id === state.id ));
128
+ if (stateExists < 0 ) {
129
+ state.write = state.writable;
130
+ if (! state.writable) {
131
+ if ( state.hasOwnProperty('setter') ) {
132
+ delete state.setter;
133
+ }
134
+ if ( state.hasOwnProperty('setattr') ) {
135
+ delete state.setattr;
136
+ }
137
+ }
138
+ if (! state.readable) {
139
+ if (state.hasOwnProperty('getter') ) {
140
+ //to awid some worning on unprocessed data
141
+ state.getter = payload => ( undefined );
142
+ }
143
+ }
144
+ return states.push(state);
145
+ }
146
+ else {
147
+ if ( (state.readable) && (! states[stateExists].readable ) ) {
148
+ states[stateExists].read = state.read;
149
+ // as state is readable, it can't be button or event
150
+ if (states[stateExists].role === 'button') {
151
+ states[stateExists].role = state.role;
152
+ }
153
+ if (states[stateExists].hasOwnProperty('isEvent')) {
154
+ delete states[stateExists].isEvent;
155
+ }
156
+ // we have to use the getter from "new" state
157
+ if ( state.hasOwnProperty('getter') ) {
158
+ states[stateExists].getter = state.getter;
159
+ }
160
+ // trying to remove the `prop` property, as main key for get and set,
161
+ // as it can be different in new and old states, and leave only:
162
+ // setattr for old and id for new
163
+ if (( state.hasOwnProperty('prop') ) && (state.prop === state.id)) {
164
+ if ( states[stateExists].hasOwnProperty('prop') ) {
165
+ if (states[stateExists].prop !== states[stateExists].id) {
166
+ if (! states[stateExists].hasOwnProperty('setattr')) {
167
+ states[stateExists].setattr = states[stateExists].prop;
168
+ }
169
+ }
170
+ delete states[stateExists].prop;
171
+ }
172
+ }
173
+ else if ( state.hasOwnProperty('prop') ) {
174
+ states[stateExists].prop = state.prop;
175
+ }
176
+ states[stateExists].readable = true;
177
+ }
178
+ if ( (state.writable) && (! states[stateExists].writable ) ) {
179
+ states[stateExists].write = state.writable;
180
+ // use new state `setter`
181
+ if ( state.hasOwnProperty('setter') ) {
182
+ states[stateExists].setter = state.setter;
183
+ }
184
+ // use new state `setterOpt`
185
+ if ( state.hasOwnProperty('setterOpt') ) {
186
+ states[stateExists].setterOpt = state.setterOpt;
187
+ }
188
+ // use new state `inOptions`
189
+ if ( state.hasOwnProperty('inOptions') ) {
190
+ states[stateExists].inOptions = state.inOptions;
191
+ }
192
+ // as we have new state, responsible for set, we have to use new `isOption`
193
+ // or remove it
194
+ if (((! state.hasOwnProperty('isOption') ) || (state.isOptions === false))
195
+ && (states[stateExists].hasOwnProperty('isOption'))) {
196
+ delete states[stateExists].isOption;
197
+ }
198
+ else {
199
+ states[stateExists].isOption = state.isOption;
200
+ }
201
+ // use new `setattr` or `prop` as `setattr`
202
+ if ( state.hasOwnProperty('setattr') ) {
203
+ states[stateExists].setattr = state.setattr;
204
+ }
205
+ else if ( state.hasOwnProperty('prop') ) {
206
+ states[stateExists].setattr = state.prop;
207
+ }
208
+ // remove `prop` equal to if, due to prop is uses as key in set and get
209
+ if ( states[stateExists].prop === states[stateExists].id) {
210
+ delete states[stateExists].prop;
211
+ }
212
+ if ( state.hasOwnProperty('epname') ) {
213
+ states[stateExists].epname = state.epname;
214
+ }
215
+ states[stateExists].writable = true;
216
+ }
217
+ return states.length;
218
+ }
219
+ }
220
+ const icon = utils.getDeviceIcon(def);
221
+ for (const expose of def.exposes) {
222
+ let state;
223
+
224
+ switch (expose.type) {
225
+ case 'light':
226
+ for (const prop of expose.features) {
227
+ switch (prop.name) {
228
+ case 'state': {
229
+ const stateNameS = expose.endpoint ? `state_${expose.endpoint}` : 'state';
230
+ pushToStates({
231
+ id: stateNameS,
232
+ name: `Switch state ${expose.endpoint ? expose.endpoint : ''}`.trim(),
233
+ icon: undefined,
234
+ role: 'switch',
235
+ write: true,
236
+ read: true,
237
+ type: 'boolean',
238
+ getter: (payload) => (payload[stateNameS] === (prop.value_on || 'ON')),
239
+ setter: (value) => (value) ? prop.value_on || 'ON' : ((prop.value_off != undefined) ? prop.value_off : 'OFF'),
240
+ epname: expose.endpoint,
241
+ setattr: 'state',
242
+ }, prop.access);
243
+ break;
244
+ }
245
+ case 'brightness': {
246
+ const stateNameB = expose.endpoint ? `brightness_${expose.endpoint}` : 'brightness';
247
+ pushToStates({
248
+ id: stateNameB,
249
+ name: `Brightness ${expose.endpoint ? expose.endpoint : ''}`.trim(),
250
+ icon: undefined,
251
+ role: 'level.dimmer',
252
+ write: true,
253
+ read: true,
254
+ type: 'number',
255
+ min: 0, // ignore expose.value_min
256
+ max: 100, // ignore expose.value_max
257
+ inOptions: true,
258
+ getter: payload => {
259
+ return utils.bulbLevelToAdapterLevel(payload[stateNameB]);
260
+ },
261
+ setter: (value) => {
262
+ return utils.adapterLevelToBulbLevel(value);
263
+ },
264
+ setterOpt: (value, options) => {
265
+ const hasTransitionTime = options && options.hasOwnProperty('transition_time');
266
+ const transitionTime = hasTransitionTime ? options.transition_time : 0;
267
+ const preparedOptions = {...options, transition: transitionTime};
268
+ preparedOptions.brightness = utils.adapterLevelToBulbLevel(value);
269
+ return preparedOptions;
270
+ },
271
+ readResponse: (resp) => {
272
+ const respObj = resp[0];
273
+ if (respObj.status === 0 && respObj.attrData != undefined) {
274
+ return utils.bulbLevelToAdapterLevel(respObj.attrData);
275
+ }
276
+ },
277
+ epname: expose.endpoint,
278
+ setattr: 'brightness',
279
+ }, prop.access);
280
+ pushToStates(statesDefs.brightness_move, prop.access);
281
+ break;
282
+ }
283
+ case 'color_temp': {
284
+ const stateNameT = expose.endpoint ? `colortemp_${expose.endpoint}` : 'colortemp';
285
+ pushToStates({
286
+ id: stateNameT,
287
+ prop: expose.endpoint ? `color_temp_${expose.endpoint}` : 'color_temp',
288
+ name: `Color temperature ${expose.endpoint ? expose.endpoint : ''}`.trim(),
289
+ icon: undefined,
290
+ role: 'level.color.temperature',
291
+ write: true,
292
+ read: true,
293
+ type: 'number',
294
+ min: expose.value_min,
295
+ max: expose.value_max,
296
+ setter: (value) => {
297
+ return utils.toMired(value);
298
+ },
299
+ setterOpt: (value, options) => {
300
+ const hasTransitionTime = options && options.hasOwnProperty('transition_time');
301
+ const transitionTime = hasTransitionTime ? options.transition_time : 0;
302
+ return {...options, transition: transitionTime};
303
+ },
304
+ epname: expose.endpoint,
305
+ }, prop.access);
306
+ pushToStates(statesDefs.colortemp_move, prop.access);
307
+ break;
308
+ }
309
+ case 'color_xy': {
310
+ const stateNameC = expose.endpoint ? `color_${expose.endpoint}` : 'color';
311
+ pushToStates({
312
+ id: stateNameC,
313
+ prop: expose.endpoint ? `color_${expose.endpoint}` : 'color',
314
+ name: `Color ${expose.endpoint ? expose.endpoint : ''}`.trim(),
315
+ icon: undefined,
316
+ role: 'level.color.rgb',
317
+ write: true,
318
+ read: true,
319
+ type: 'string',
320
+ setter: (value) => {
321
+ // convert RGB to XY for set
322
+ /*
323
+ const result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(value);
324
+ let xy = [0, 0];
325
+ if (result) {
326
+ const r = parseInt(result[1], 16),
327
+ g = parseInt(result[2], 16),
328
+ b = parseInt(result[3], 16);
329
+ xy = rgb.rgb_to_cie(r, g, b);
330
+ }
331
+ return {
332
+ x: xy[0],
333
+ y: xy[1]
334
+ };
335
+ */
336
+ let xy = [0, 0];
337
+ const rgbcolor = colors.ParseColor(value);
338
+
339
+ xy = rgb.rgb_to_cie(rgbcolor.r, rgbcolor.g, rgbcolor.b);
340
+ return {
341
+ x: xy[0],
342
+ y: xy[1]
343
+ };
344
+
345
+ },
346
+ setterOpt: (value, options) => {
347
+ const hasTransitionTime = options && options.hasOwnProperty('transition_time');
348
+ const transitionTime = hasTransitionTime ? options.transition_time : 0;
349
+ return {...options, transition: transitionTime};
350
+ },
351
+ getter: payload => {
352
+ if (payload.color && payload.color.hasOwnProperty('x') && payload.color.hasOwnProperty('y')) {
353
+ const colorval = rgb.cie_to_rgb(payload.color.x, payload.color.y);
354
+ return '#' + utils.decimalToHex(colorval[0]) + utils.decimalToHex(colorval[1]) + utils.decimalToHex(colorval[2]);
355
+ } else {
356
+ return undefined;
357
+ }
358
+ },
359
+ epname: expose.endpoint,
360
+ setattr: 'color',
361
+ }, prop.access);
362
+ break;
363
+ }
364
+ case 'color_hs': {
365
+ const stateNameH = expose.endpoint ? `color_${expose.endpoint}` : 'color';
366
+ pushToStates({
367
+ id: stateNameH,
368
+ prop: expose.endpoint ? `color_${expose.endpoint}` : 'color',
369
+ name: `Color ${expose.endpoint ? expose.endpoint : ''}`.trim(),
370
+ icon: undefined,
371
+ role: 'level.color.rgb',
372
+ write: true,
373
+ read: true,
374
+ type: 'string',
375
+ setter: (value) => {
376
+ const _rgb = colors.ParseColor(value);
377
+ const hsv = rgb.rgbToHSV(_rgb.r, _rgb.g, _rgb.b, true);
378
+ return {
379
+ hue: Math.min(Math.max(hsv.h,1),359),
380
+ saturation: hsv.s,
381
+ // brightness: Math.floor(hsv.v * 2.55),
382
+
383
+ };
384
+ },
385
+ setterOpt: (value, options) => {
386
+ const hasTransitionTime = options && options.hasOwnProperty('transition_time');
387
+ const transitionTime = hasTransitionTime ? options.transition_time : 0;
388
+ return {...options, transition: transitionTime};
389
+ },
390
+ epname: expose.endpoint,
391
+ setattr: 'color',
392
+ }, prop.access);
393
+ pushToStates({
394
+ id: expose.endpoint ? `hue_${expose.endpoint}` : 'hue',
395
+ prop: expose.endpoint ? `color_${expose.endpoint}` : 'color',
396
+ name: `Hue ${expose.endpoint ? expose.endpoint : ''}`.trim(),
397
+ icon: undefined,
398
+ role: 'level.color.hue',
399
+ write: true,
400
+ read: false,
401
+ type: 'number',
402
+ min: 0,
403
+ max: 360,
404
+ inOptions: true,
405
+ setter: (value, options) => {
406
+ return {
407
+ hue: value,
408
+ saturation: options.saturation,
409
+ };
410
+ },
411
+ setterOpt: (value, options) => {
412
+ const hasTransitionTime = options && options.hasOwnProperty('transition_time');
413
+ const transitionTime = hasTransitionTime ? options.transition_time : 0;
414
+ const hasHueCalibrationTable = options && options.hasOwnProperty('hue_calibration');
415
+ if (hasHueCalibrationTable)
416
+ try {
417
+ return {...options, transition: transitionTime, hue_correction: JSON.parse(options.hue_calibration)};
418
+ }
419
+ catch {
420
+ const hue_correction_table = [];
421
+ options.hue_calibration.split(',').forEach(element => {
422
+ const match = /([0-9]+):([0-9]+)/.exec(element);
423
+ if (match && match.length ==3)
424
+ hue_correction_table.push({ in: Number(match[1]), out: Number(match[2])});
425
+ });
426
+ if (hue_correction_table.length > 0)
427
+ return {...options, transition: transitionTime, hue_correction: hue_correction_table};
428
+ }
429
+ return {...options, transition: transitionTime};
430
+ },
431
+
432
+ }, prop.access);
433
+ pushToStates({
434
+ id: expose.endpoint ? `saturation_${expose.endpoint}` : 'saturation',
435
+ prop: expose.endpoint ? `color_${expose.endpoint}` : 'color',
436
+ name: `Saturation ${expose.endpoint ? expose.endpoint : ''}`.trim(),
437
+ icon: undefined,
438
+ role: 'level.color.saturation',
439
+ write: true,
440
+ read: false,
441
+ type: 'number',
442
+ min: 0,
443
+ max: 100,
444
+ inOptions: true,
445
+ setter: (value, options) => {
446
+ return {
447
+ hue: options.hue,
448
+ saturation: value,
449
+ };
450
+ },
451
+ setterOpt: (value, options) => {
452
+ const hasTransitionTime = options && options.hasOwnProperty('transition_time');
453
+ const transitionTime = hasTransitionTime ? options.transition_time : 0;
454
+ const hasHueCalibrationTable = options && options.hasOwnProperty('hue_calibration');
455
+ if (hasHueCalibrationTable)
456
+ try {
457
+ return {...options, transition: transitionTime, hue_correction: JSON.parse(options.hue_calibration)};
458
+ }
459
+ catch {
460
+ const hue_correction_table = [];
461
+ options.hue_calibration.split(',').forEach(element => {
462
+ const match = /([0-9]+):([0-9]+)/.exec(element);
463
+ if (match && match.length ==3)
464
+ hue_correction_table.push({ in: Number(match[1]), out: Number(match[2])});
465
+ });
466
+ if (hue_correction_table.length > 0)
467
+ return {...options, transition: transitionTime, hue_correction: hue_correction_table};
468
+ }
469
+ return {...options, transition: transitionTime};
470
+ },
471
+
472
+ }, prop.access);
473
+ pushToStates(statesDefs.hue_move, prop.access);
474
+ pushToStates(statesDefs.saturation_move, prop.access);
475
+ pushToStates({
476
+ id: 'hue_calibration',
477
+ prop: 'color',
478
+ name: 'Hue color calibration table',
479
+ icon: undefined,
480
+ role: 'table',
481
+ write: true,
482
+ read: false,
483
+ type: 'string',
484
+ inOptions: true,
485
+ setter: (value, options) => {
486
+ return {
487
+ hue: options.hue,
488
+ saturation: options.saturation,
489
+ };
490
+ },
491
+ setterOpt: (value, options) => {
492
+ const hasTransitionTime = options && options.hasOwnProperty('transition_time');
493
+ const transitionTime = hasTransitionTime ? options.transition_time : 0;
494
+ const hasHueCalibrationTable = options && options.hasOwnProperty('hue_calibration');
495
+ if (hasHueCalibrationTable)
496
+ try {
497
+ return {...options, transition: transitionTime, hue_correction: JSON.parse(options.hue_calibration)};
498
+ }
499
+ catch {
500
+ const hue_correction_table = [];
501
+ options.hue_calibration.split(',').forEach(element => {
502
+ const match = /([0-9]+):([0-9]+)/.exec(element);
503
+ if (match && match.length ==3)
504
+ hue_correction_table.push({ in: Number(match[1]), out: Number(match[2])});
505
+ });
506
+ if (hue_correction_table.length > 0)
507
+ return {...options, transition: transitionTime, hue_correction: hue_correction_table};
508
+ }
509
+ return {...options, transition: transitionTime};
510
+ },
511
+
512
+ }, prop.access);
513
+ break;
514
+ }
515
+ default:
516
+ pushToStates(genState(prop), prop.access);
517
+ break;
518
+ }
519
+ }
520
+ pushToStates(statesDefs.transition_time, ea.STATE_SET);
521
+ break;
522
+
523
+ case 'switch':
524
+ for (const prop of expose.features) {
525
+ switch (prop.name) {
526
+ case 'state':
527
+ pushToStates(genState(prop, 'switch'), prop.access);
528
+ break;
529
+ default:
530
+ pushToStates(genState(prop), prop.access);
531
+ break;
532
+ }
533
+ }
534
+ break;
535
+
536
+ case 'numeric':
537
+ if (expose.endpoint) {
538
+ state = genState(expose);
539
+ } else {
540
+ switch (expose.name) {
541
+ case 'linkquality':
542
+ state = undefined;
543
+ break;
544
+
545
+ case 'battery':
546
+ state = statesDefs.battery;
547
+ break;
548
+
549
+ case 'voltage':
550
+ state = statesDefs.plug_voltage;
551
+ break;
552
+
553
+ case 'temperature':
554
+ state = statesDefs.temperature;
555
+ break;
556
+
557
+ case 'humidity':
558
+ state = statesDefs.humidity;
559
+ break;
560
+
561
+ case 'pressure':
562
+ state = statesDefs.pressure;
563
+ break;
564
+
565
+ case 'illuminance':
566
+ state = statesDefs.illuminance_raw;
567
+ break;
568
+
569
+ case 'illuminance_lux':
570
+ state = statesDefs.illuminance;
571
+ break;
572
+
573
+ case 'power':
574
+ state = statesDefs.load_power;
575
+ break;
576
+
577
+ default:
578
+ state = genState(expose);
579
+ break;
580
+ }
581
+ }
582
+ if (state) pushToStates(state, expose.access);
583
+ break;
584
+
585
+ case 'enum':
586
+ switch (expose.name) {
587
+ case 'action': {
588
+ if (!Array.isArray(expose.values)) break;
589
+ const hasHold = expose.values.find((actionName) => actionName.includes('hold'));
590
+ const hasRelease = expose.values.find((actionName) => actionName.includes('release'));
591
+ for (const actionName of expose.values) {
592
+ // is release state ? - skip
593
+ if (hasHold && hasRelease && actionName.includes('release')) continue;
594
+ // is hold state ?
595
+ if (hasHold && hasRelease && actionName.includes('hold')) {
596
+ const releaseActionName = actionName.replace('hold', 'release');
597
+ state = {
598
+ id: actionName.replace(/\*/g, ''),
599
+ prop: 'action',
600
+ name: actionName,
601
+ icon: undefined,
602
+ role: 'button',
603
+ write: false,
604
+ read: true,
605
+ type: 'boolean',
606
+ getter: payload => (payload.action === actionName) ? true : (payload.action === releaseActionName) ? false : undefined,
607
+ };
608
+ } else {
609
+ state = {
610
+ id: actionName.replace(/\*/g, ''),
611
+ prop: 'action',
612
+ name: actionName,
613
+ icon: undefined,
614
+ role: 'button',
615
+ write: false,
616
+ read: true,
617
+ type: 'boolean',
618
+ getter: payload => (payload.action === actionName) ? true : undefined,
619
+ isEvent: true,
620
+ };
621
+ }
622
+ pushToStates(state, expose.access);
623
+ }
624
+ state = null;
625
+ break;
626
+ }
627
+ default:
628
+ state = genState(expose);
629
+ break;
630
+ }
631
+ if (state) pushToStates(state, expose.access);
632
+ break;
633
+
634
+ case 'binary':
635
+ if (expose.endpoint) {
636
+ state = genState(expose);
637
+ } else {
638
+ switch (expose.name) {
639
+ case 'contact':
640
+ state = statesDefs.contact;
641
+ pushToStates(statesDefs.opened, ea.STATE);
642
+ break;
643
+
644
+ case 'battery_low':
645
+ state = statesDefs.heiman_batt_low;
646
+ break;
647
+
648
+ case 'tamper':
649
+ state = statesDefs.tamper;
650
+ break;
651
+
652
+ case 'water_leak':
653
+ state = statesDefs.water_detected;
654
+ break;
655
+
656
+ case 'lock':
657
+ state = statesDefs.child_lock;
658
+ break;
659
+
660
+ case 'occupancy':
661
+ state = statesDefs.occupancy;
662
+ break;
663
+
664
+ default:
665
+ state = genState(expose);
666
+ break;
667
+ }
668
+ }
669
+ if (state) pushToStates(state, expose.access);
670
+ break;
671
+
672
+ case 'text':
673
+ state = genState(expose);
674
+ pushToStates(state, expose.access);
675
+ break;
676
+
677
+ case 'lock':
678
+ case 'fan':
679
+ case 'cover':
680
+ for (const prop of expose.features) {
681
+ switch (prop.name) {
682
+ case 'state':
683
+ pushToStates(genState(prop, 'switch'), prop.access);
684
+ break;
685
+ default:
686
+ pushToStates(genState(prop), prop.access);
687
+ break;
688
+ }
689
+ }
690
+ break;
691
+
692
+ case 'climate':
693
+ for (const prop of expose.features) {
694
+ switch (prop.name) {
695
+ case 'away_mode':
696
+ pushToStates(statesDefs.climate_away_mode, prop.access);
697
+ break;
698
+ case 'system_mode':
699
+ pushToStates(statesDefs.climate_system_mode, prop.access);
700
+ break;
701
+ case 'running_mode':
702
+ pushToStates(statesDefs.climate_running_mode, prop.access);
703
+ break;
704
+ default:
705
+ pushToStates(genState(prop), prop.access);
706
+ break;
707
+ }
708
+ }
709
+ break;
710
+
711
+ case 'composite':
712
+ for (const prop of expose.features) {
713
+ const st = genState(prop);
714
+ st.prop = expose.property;
715
+ st.inOptions = true;
716
+ // I'm not fully sure, as it really needed, but
717
+ st.setterOpt = (value, options) => {
718
+ const result = {};
719
+ options[prop.property] = value;
720
+ result[expose.property] = options;
721
+ return result;
722
+ };
723
+ // if we have a composite expose, the value have to be an object {expose.property : {prop.property: value}}
724
+ if (prop.access & ea.SET) {
725
+ st.setter = (value, options) => {
726
+ const result = {};
727
+ options[prop.property] = value;
728
+ result[expose.property] = options;
729
+ return result;
730
+ };
731
+ st.setattr = expose.property;
732
+ };
733
+ // if we have a composite expose, the payload will be an object {expose.property : {prop.property: value}}
734
+ if (prop.access & ea.STATE) {
735
+ st.getter = payload => {
736
+ if ( (payload.hasOwnProperty(expose.property)) && (payload[expose.property] !== null) && payload[expose.property].hasOwnProperty(prop.property)) {
737
+ return !isNaN(payload[expose.property][prop.property]) ? payload[expose.property][prop.property] : undefined
738
+ }
739
+ else {
740
+ return undefined;
741
+ }
742
+ };
743
+ }
744
+ else {
745
+ st.getter = payload => { return undefined };
746
+ }
747
+ ;
748
+ pushToStates(st, prop.access);
749
+ }
750
+ break;
751
+ default:
752
+ console.log(`Unhandled expose type ${expose.type} for device ${model}`);
753
+ }
754
+ }
755
+ const newDev = {
756
+ models: [model],
757
+ icon: icon,
758
+ states: states,
759
+ exposed: true,
760
+ };
761
+ // make the function code printable in log
762
+ //console.log(`Created mapping for device ${model}: ${JSON.stringify(newDev, function(key, value) {
763
+ // if (typeof value === 'function') {return value.toString() } else { return value } }, ' ')}`);
764
+ return newDev;
765
+ }
766
+
767
+ function applyExposes(mappedDevices, byModel, allExcludesObj) {
768
+ // for exlude search
769
+ const allExcludesStr = JSON.stringify(allExcludesObj);
770
+ // create or update device from exposes
771
+ for (const deviceDef of zigbeeHerdsmanConverters.definitions) {
772
+ const strippedModel = (deviceDef.model) ? deviceDef.model.replace(/\0.*$/g, '').trim() : '';
773
+ // check if device is mapped
774
+ const existsMap = byModel.get(strippedModel);
775
+
776
+ if ((deviceDef.hasOwnProperty('exposes') && (!existsMap || !existsMap.hasOwnProperty('states'))) || allExcludesStr.indexOf(strippedModel) > 0) {
777
+ const newDevice = createFromExposes(strippedModel, deviceDef);
778
+ if (!existsMap) {
779
+ mappedDevices.push(newDevice);
780
+ byModel.set(strippedModel, newDevice);
781
+ } else {
782
+ existsMap.states = newDevice.states;
783
+ existsMap.exposed = true;
784
+ }
785
+ }
786
+ }
787
+ }
788
+
789
+ module.exports = {
790
+ applyExposes: applyExposes,
791
+ };