iobroker.zigbee 1.7.6 → 1.8.1

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