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