iobroker.zigbee2mqtt 0.1.0

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