-
Notifications
You must be signed in to change notification settings - Fork 13
/
ExposesDeviceGenerator.js
1576 lines (1334 loc) · 69.7 KB
/
ExposesDeviceGenerator.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* ExposesDeviceGenerator.js - Generate devices using the Zigbee2MQTT
* Exposes API: https://www.zigbee2mqtt.io/information/exposes
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.*
*/
class ExposesDeviceGenerator {
constructor(adapter, config) {
this.adapter = adapter;
this.config = config;
// Zigbee2MQTT access bit to read a property via the published state of the object
this.ACCESS_BIT_STATE = 1;
// Zigbee2MQTT access bit to set a property via /SET
this.ACCESS_BIT_SET = 2;
// Zigbee2MQTT access bit to read a property via /GET
this.ACCESS_BIT_GET = 4;
// The Zigbee2MQTT access mask for which we are going to generate Actions instead of Properties
this.ACCESS_MASK_ACTION = this.ACCESS_BIT_SET;
}
/**
* Generate a WebThings device definition based on the provided
* Zigbee2MQTT device definition, i.e. the Exposes API as described in
* https://www.zigbee2mqtt.io/information/exposes
*/
generateDevice(info, zigbee_id, model_id) {
try{
if(info == null){
console.log("Error, no exposes data (info was null)");
return;
}
if (!info.supported || !info.definition || !info.definition.exposes) {
console.log("Error, missing exposes data, cannot generate device: ", info);
return;
}
var device = new Object();
device['@type'] = [];
device.zigbee_id = zigbee_id;
device.model_id = model_id;
device.properties = new Object();
device.actions = new Object();
device.id = "z2m-" + zigbee_id;
if(this.config.debug){
console.log(" ");
console.log(" ");
console.log(".");
console.log(".");
console.debug('Device', info.friendly_name, 'exposes', JSON.stringify(info.definition.exposes));
}
device.name = info.definition.description;
//console.log(device);
var property_names_list = this.pre_parse_device(info.definition.exposes,[]);
if(this.config.debug){
console.log(" ");
console.log("Exposes: generateDevice: property_names_list = ", property_names_list);
}
device = this.parse_device(info.definition.exposes, device, property_names_list);
//console.log("= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = ");
//console.log("device is now:");
//console.log(device);
if(this.config.debug){
console.log(".");
console.log("final device: ", device);
console.log(" ");
console.log(" ");
console.log(" ");
console.log(" ");
}
}
catch (error){
console.error("Error in generateDevice: " + error);
console.debug('Device', info.friendly_name, 'exposes', JSON.stringify(info.definition.exposes));
}
return device;
}
// Finds all the potential property names first, so that later manipulating logic can use that foresight. Returns a list of property names.
pre_parse_device(exposes_info, property_names_list){
try{
for (var k in exposes_info)
{
if (typeof exposes_info[k] == "object" && exposes_info[k] !== null){
if( typeof exposes_info[k]["access"] != "undefined" ){
if(typeof exposes_info[k]['name'] != "undefined"){
if( exposes_info[k]['name'] != "x" && exposes_info[k]['name'] != "y"){ // Skip the color fragments
if( !(exposes_info[k]['name'] in property_names_list) ){
property_names_list.push(exposes_info[k]['name'].toLowerCase()); // TODO: check if toLowerCase is a good idea. So far Z2M exposes names are all lowercaser already.
}
}
}
}
property_names_list = this.pre_parse_device(exposes_info[k], property_names_list);
}
}
}
catch (error){
console.log("Error in device pre-parsing: " + error)
}
return property_names_list;
}
// note: this method is recursive, it calls itself internally to parse deeper complex structures
parse_device(exposes_info, device, property_names_list){
if(typeof device == 'undefined'){
console.log("ERROR: ExposesGenerator: device was undefined in parse_device");
}
if(typeof device['@type'] == 'undefined'){
console.log("ERROR: ExposesGenerator: device @TYPE was undefined in parse_device");
}
if(this.config.debug){
console.log(" ");
console.log("+ + + + + + + + + + + + + parse_device + + + + + + + + + + + + + + + + + + + + + + + +");
console.log("property_names_list: ", property_names_list);
console.log("exposes_info : ", exposes_info);
console.log("device : ", device);
}
//console.log(device);
try{
if(typeof exposes_info['features'] != "undefined"){
console.log("features spotted");
}
else{
console.log("Error, parse_device: device features are undefined?");
}
if(typeof exposes_info['type'] != "undefined"){
console.log("type also spotted. Type = " + exposes_info['type']);
if(exposes_info['type'] == "composite"){
if(this.config.debug){
console.log("it's a composite");
console.log("exposes_info[property]: ", exposes_info['property']);
}
if(typeof exposes_info['property'] != "undefined"){
if(this.config.debug){
console.log("___color: exposes_info[property]: ", exposes_info['property']);
}
if(exposes_info['property'] == "color" || exposes_info['property'] == "color_xy"){
if(this.config.debug){
console.log("composite color property spotted. Will create extra color property");
}
device['@type'].push('ColorControl');
const composite_expose = {
'access':7,
'name':'color',
'property':'color',
'description': exposes_info['description'],
'type':'text'
}
//console.log(composite_expose);
device = this.parse_property(composite_expose, device, property_names_list);
}
}
}
else if(exposes_info['type'] == "light"){
if(this.config.debug){
console.log("it's a lamp");
}
device['@type'].push('Light');
}
else if(exposes_info['type'] == "switch"){
if(this.config.debug){
console.log("it's a switch");
}
/*
if(device.model_id == 'ZB-SW01'){
console.log("turning ZB-SW01 into a lock instead"); // not reliable, multiple devices have that id...
device['@type'].push('Lock');
}
else{
device['@type'].push('OnOffSwitch');
}
*/
}
/*
// Locks require a read-only enum for the state which shows one of: ["locked", "unlocked", "unknown", "jammed"]
// And it requires two action buttons to lock and unlock. This is a bit of a pain to implement.
// Also, actions don't work with Voco voice control yet.
else if(exposes_info['type'] == "lock"){
if(this.config.debug){
console.log("it's a lock");
}
//if(property_names_list.indexOf('Child lock') == -1){
device['@type'].push('Lock');
//}
}
*/
else if(exposes_info['type'] == "climate"){
if(this.config.debug){
console.log("it's a thermostat");
}
device['@type'].push('Thermostat');
}
else{
if(this.config.debug){
console.log("Warning, exposes_info type fell through");
}
}
}
else{
console.log("Error, parse_device: device has features, but type is undefined?");
}
}
catch (error){
console.log("Error in first part of parse_device: " + error);
}
try{
for (var k in exposes_info){
//console.log("__________");
//console.log("k = ", k, exposes_info[k]);
if (typeof exposes_info[k] == "object" && exposes_info[k] !== null){
// If we spot an access property at this level, then we are at the level of useful property data.
if( typeof exposes_info[k]["access"] != "undefined" ){
//console.log("access data spotted at current level");
if(typeof exposes_info[k]['name'] != "undefined"){
if( exposes_info[k]['name'] != "x" && exposes_info[k]['name'] != "y" && exposes_info[k]['name'] != "Action group" && exposes_info[k]['name'] != "Action rate"){ // Skip the color fragments
device = this.parse_property(exposes_info[k], device, property_names_list);
}
else{
if(this.config.debug){
console.log("skipping an exposed property: " + exposes_info[k]['name'] );
}
}
}
else{
//console.log("Weird, at useful property level, but name of property was not defined");
}
}
//else if(k != "values"){
else { //if(typeof exposes_info[k]["access"] == "object"){
//console.log(".. diving deeper .. typeof device: " + typeof device);
device = this.parse_device(exposes_info[k], device, property_names_list);
}
} // end of object check
else{
//console.log("WEIRD in exposes generator: exposes_info[k] was not object?:", exposes_info[k]);
//console.log("..");
}
} // end of for loop
}
catch (error){
console.log("Error in second part of parse_device: " + error);
}
return device;
}
// ?
flip_value_on_and_off(expose){
return expose;
}
parse_property(expose, device, property_names_list){
if(this.config.debug){
console.log("+ (parse_property)");
console.log("expose: ", expose);
console.log("+");
}
//console.log("parse_property: is device undefined? " + typeof device);
try{
//
// Making some changes based on the specific device we're dealing with.
//
//console.log("parse_prop: ************* device model: ", device.model_id);
if(this.adapter.reverse_list_contact.includes(device.model_id) && expose.property == 'contact'){
if(this.config.debug){
console.log("- spotted a boolean propery that should be reversed");
}
expose['reversed'] = true;
}
}
catch(e){
console.log("Error while doing device-specific adjustments: ", e);
}
/*
if(typeof expose['name'] != "undefined"){
if( expose['name'] == 'Action group' || expose['name'] == 'Action rate'){
console.log("");
return device;
}
}
*/
try{
if(typeof expose['type'] != "undefined"){
// Decide between float and integer
if(expose['type'] == "numeric"){
/*
if(typeof expose["value_max"] != "undefined"){
if(expose["value_max"] != 255 && expose["value_max"] != 254 && expose["value_max"] != 100){
expose.type = "float";
}
}
*/
if(typeof expose["value_step"] != "undefined"){
if( expose["value_step"] != 1){
expose.type = "float";
}
}
if(typeof expose["name"] != "undefined"){
if( expose["name"] == "local_temperature"){
expose.type = "float";
}
}
}
const device_id = device.id.split("/").pop();
const wt_id_base = device_id + "-";
const wt_id = wt_id_base + expose.property;
if(this.config.debug){
console.log("device_id: ", device_id);
console.log("wt_id: ", wt_id);
console.log("expose.type: ", expose.type);
}
try{
switch (expose.type) {
// Generic type binary
case 'binary':
if (expose.access === this.ACCESS_MASK_ACTION) {
device.actions[wt_id] = this.binaryPropertyToBooleanAction(expose);
} else {
/*
var turn_into_lock_property = false;
// find binary expose that should be converted to enum lock property
if(typeof expose.value_on != 'undefined' && typeof expose.value_off != 'undefined'){
if( (expose.value_off.toLowerCase() == 'unlock' && expose.value_on.toLowerCase() == 'lock') || (expose.value_off.toLowerCase() == 'lock' && expose.value_on.toLowerCase() == 'unlock'){
turn_into_lock_property = true;
//expose.type: 'enum',
//expose.values: [ 'lock', 'unlock']
}
}
if(turn_into_lock_property == false){
device.properties[wt_id] = this.binaryPropertyToBooleanProperty(expose);
}
else{
device.properties[wt_id] = this.binaryPropertyToLockProperty(expose);
}
*/
device.properties[wt_id] = this.binaryPropertyToBooleanProperty(expose);
}
break;
// Generic type numeric - integer variant
case 'numeric':
if (expose.access === this.ACCESS_MASK_ACTION) {
device.actions[wt_id] = this.numericPropertyToIntegerAction(expose);
} else {
device.properties[wt_id] = this.numericPropertyToIntegerProperty(expose);
}
break;
case 'float':
if (expose.access === this.ACCESS_MASK_ACTION) {
device.actions[wt_id] = this.numericPropertyToFloatAction(expose);
} else {
device.properties[wt_id] = this.numericPropertyToFloatProperty(expose);
}
break;
// Generic type enum
case 'enum':
if (expose.access === this.ACCESS_MASK_ACTION) {
device.actions[expose.property] = this.enumPropertyToStringAction(expose);
} else {
if(device.model_id == 'STARKVIND Air purifier'){
device.properties[wt_id] = this.STARKVINDenumPropertyToStringProperty(expose);
}else{
let found_heat_cool_property = false;
try{
// find HeatingCoolingProperty
if(expose.access == 1){
if(expose.values.length < 4){
let thermostat_heat_found = false;
let thermostat_cool_found = false;
let thermostat_off_found = false;
for(let v = 0; v < expose.values.length; v++){
if(expose.values[v].toLowerCase() == 'heat' || expose.values[v].toLowerCase() == 'heating'){
thermostat_heat_found = true;
}
else if(expose.values[v].toLowerCase() == 'cool' || expose.values[v].toLowerCase() == 'cooling'){
thermostat_heat_found = true;
}
else if(expose.values[v].toLowerCase() == 'off' || expose.values[v].toLowerCase() == 'idle'){
thermostat_off_found = true;
}
}
if( thermostat_off_found && (thermostat_heat_found || thermostat_cool_found) ){
found_heat_cool_property = true;
}
}
}
}catch(e){
console.log("Error finding thermostat cooling-heating property");
}
if(found_heat_cool_property){
device.properties[wt_id] = this.enumPropertyToHeatCoolProperty(expose);
device.properties[wt_id]['@type'] = 'HeatingCoolingProperty';
}
else{
device.properties[wt_id] = this.enumPropertyToStringProperty(expose);
}
}
}
break;
// Generic type text
case 'text':
if (expose.access === this.ACCESS_MASK_ACTION) {
device.actions[wt_id] = this.textPropertyToStringAction(expose);
} else {
device.properties[wt_id] = this.textPropertyToStringProperty(expose);
}
break;
// Composite is complicated. Could be a color for exmaple, but also something else..
case 'composite':
if( expose["name"].indexOf("color") !== -1){
if (expose.access === this.ACCESS_MASK_ACTION) {
device.actions[wt_id] = this.textPropertyToStringAction(expose);
} else {
device.properties[wt_id] = this.textPropertyToStringProperty(expose);
}
break;
}
else{
// Room to handle any other complex composite properties in the future
if(this.config.debug){
console.log("\nWARNING, COMPOSITE ELEMENT FELL THROUGH\n");
}
break;
}
}
}
catch(e){
console.log("error in parse_property while generating property options: ", e);
}
try{
// Add initial value if available (artificially added for properties derived from actions, using the value from devices_overview)
// TODO wait, no, this doesn't set the initial value. the set_value on the eventual property still has to be called.
if(typeof expose['value'] != "undefined"){
if(this.config.debug){
console.log("there was value in the expose data, adding it to property: " + expose['value']);
}
if(typeof device.properties[wt_id] != 'undefined'){
device.properties[wt_id]['value'] = expose['value'];
}
}
if(typeof expose['property'] != "undefined"){
//console.log("expose['property']: " + expose['property']);
if(typeof device.properties[wt_id] != 'undefined'){
device.properties[wt_id]['property'] = expose['property'];
}
}
if(typeof device.properties[wt_id] != 'undefined'){
if(device.properties[wt_id]['title'] == "Color temp"){
device.properties[wt_id]['title'] = "Color temperature";
}
}
// Add unit if available
if(typeof expose['unit'] != "undefined" && device.properties[wt_id] != 'undefined'){
device.properties[wt_id]['unit'] = expose['unit'];
if( expose['unit'] == 'mV' || expose['unit'] == 'lqi'){
//console.log("spotted lqi");
//device.properties[wt_id]['unit'] = 'volt';
device.properties[wt_id]['multipleOf'] = 1;
//device.properties[wt_id]['value'] = expose['value'] / 1000;
}
else if( expose['unit'] == '°C' ){
device.properties[wt_id]['unit'] = 'degree celsius';
device.properties[wt_id]['multipleOf'] = 0.1;
}
else if( expose['unit'] == 'V' ){
device.properties[wt_id]['unit'] = 'volt';
device.properties[wt_id]['multipleOf'] = 0.1;
}
else if( expose['unit'] == 'A' ){
device.properties[wt_id]['unit'] = 'ampere';
device.properties[wt_id]['multipleOf'] = 0.01;
}
else if( expose['unit'] == '%' ){
device.properties[wt_id]['unit'] = 'percent';
device.properties[wt_id]['multipleOf'] = 1;
}
else if( expose['unit'] == 'µg/m³' ){
device.properties[wt_id]['minimum'] = 0;
//device.properties[wt_id]['maximum'] = 65535;
}
}
if( expose['name'] == 'humidity' && device.properties[wt_id] != 'undefined'){
device.properties[wt_id]['unit'] = 'percent';
device.properties[wt_id]['multipleOf'] = 0.1;
}
}
catch(e){
console.log("error in parse_property while improving units: ", e);
}
// add capabilities information
if(typeof expose['name'] != "undefined" && device.properties[wt_id] != 'undefined'){
//console.log("expose.name = " + expose.property);
try{
if(typeof device['@type'] == 'undefined'){
console.error("ERROR, device[@type] was undefined. Creating empty @type array now.");
device['@type'] = [];
}
else{
console.log("device @type exists on expose.name: ", expose.name);
console.log('device[@type] : ', device['@type']);
}
if(typeof device.properties[wt_id] != 'undefined'){
if( expose.name.endsWith("state") ){
//console.log("expose.name ends with state, so adding onOfProperty @type");
//console.log("device['@type'] = ", device['@type']);
//console.log( device['@type'].indexOf('Lock') );
if(typeof device['@type'] != 'undefined'){
if(device['@type'].indexOf("Lock") > -1){
//console.log("IS LOCK");
if(expose.access == 1){ // indicates read-only?
device.properties[wt_id]['@type'] = 'LockedProperty'; // read-only? Not according to the spec.
}else{
//console.log("IS ACTIONABLE LOCK");
if(expose.type == 'binary'){
device.properties[wt_id]['@type'] = 'OnOffProperty'; // should be read-only. But it might work?
if(device['@type'].indexOf("EnergyMonitor") > -1 && device['@type'].indexOf("SmartPlug") == -1){
device['@type'].unshift('SmartPlug');
}
/*
if(device['@type'].indexOf("SmartPlug") > -1 && device['@type'].indexOf("Light") == -1){
device['@type'].push('Light');
}
*/
if(device['@type'].indexOf("OnOffSwitch") == -1){
device['@type'].push('OnOffSwitch');
}
}
/*
// Experiment to add actions to a lock, as the spec prefers. However, actions are (currently) not compatible with Voco voice control, so I abandoned this for now. Also had trouble receiving the action.
try{
// Add actions for the lock
var lock_action = new Object();
lock_action.title = 'Lock now';
lock_action.description = 'Lock the locking mechanism';
//lock_action.input = this.binaryPropertyToBooleanProperty(binary);
lock_action.input = 'ON';
lock_action['@type'] = 'LockAction';
//lock_action.expose = expose.name;
device.actions[expose.name + '-lock'] = lock_action;
var unlock_action = new Object();
unlock_action.title = 'Unlock now';
unlock_action.description = 'Unlock the locking mechanism';
unlock_action.input = 'OFF';
//unlock_action.input = this.binaryPropertyToBooleanProperty(binary);
unlock_action['@type'] = 'UnlockAction';
//unlock_action.expose = expose.name;
device.actions[expose.name + '-unlock'] = unlock_action;
}
catch(e){
console.log(e);
}
*/
//console.log("device.actions:", device.actions);
/*
this.add_action('lock', {
'@type': 'LockAction',
'title': 'Lock',
'description': 'Lock the locking mechanism',
})
this.add_action('unlock', {
'@type': 'UnlockAction',
'title': 'Unlock',
'description': 'Unlock the locking mechanism',
})
*/
}
}
else{
//console.log("IS NOT LOCK");
if(expose.access == 1){
if(expose.type == 'binary'){
device.properties[wt_id]['@type'] = 'PushedProperty';
if(device['@type'].indexOf("PushButton") == -1){
device['@type'].push('PushButton');
}
}
}else{
if(expose.type == 'binary'){
device.properties[wt_id]['@type'] = 'OnOffProperty';
if(device['@type'].indexOf("EnergyMonitor") > -1 && device['@type'].indexOf("SmartPlug") == -1){
device['@type'].unshift('SmartPlug');
}
/*
if(device['@type'].indexOf("Light") == -1){ // plugs may pretend to be lights
device['@type'].push('Light');
}
*/
if(device['@type'].indexOf("OnOffSwitch") == -1){
device['@type'].push('OnOffSwitch');
}
}
}
}
}
}
else if(expose.name.endsWith("brightness") ){
device.properties[wt_id]['@type'] = 'BrightnessProperty';
}
else if(expose.name == "position" && expose.access == 7 && expose.value_max != null){
device.properties[wt_id]['@type'] = 'LevelProperty';
if(device['@type'].indexOf("MultiLevelSwitch") == -1){
device['@type'].push('MultiLevelSwitch');
}
}
else if(expose.name == "cube_side" || expose.name == "angle" || expose.name == "illuminance_lux"){
device.properties[wt_id]['@type'] = 'LevelProperty';
if(device['@type'].indexOf("MultiLevelSensor") == -1){
device['@type'].push('MultiLevelSensor');
}
}
else if(expose.name == "color"){
console.log('___color: wt_id: ', wt_id);
console.log('___color: device: ', device);
console.log('___color: device.properties[wt_id]: ', device.properties[wt_id]);
device.properties[wt_id]['@type'] = 'ColorProperty';
if(device['@type'].indexOf("ColorControl") == -1){ // && device['@type'].length == 0
device['@type'].unshift('ColorControl');
}
}
/*
// Causes the icon to not be shown / addng it does not seem to offer any benefits.
else if(expose.name == "color_temp"){
device.properties[wt_id]['@type'] = 'ColorTemperatureProperty';
if(device['@type'].indexOf("Light") == -1){ // && device['@type'].length == 0
device['@type'].push('Light');
}
}
*/
else if(expose.name == "color_xy" || expose.name == "color_hs"){
console.log('___color_XY: wt_id: ', wt_id);
console.log('___color_XY: device: ', device);
console.log('___color_XY: device.properties[wt_id]: ', device.properties[wt_id]);
device.properties[wt_id]['@type'] = 'ColorProperty';
if(device['@type'].indexOf("ColorControl") == -1){
device['@type'].unshift('ColorControl');
}
}
else if(expose.name == "occupied_heating_setpoint" || expose.name == "occupied_cooling_setpoint" || expose.name == "comfort_temperature" || expose.name == "eco_temperature" || expose.name == "current_heating_setpoint"){
device.properties[wt_id]['@type'] = 'TargetTemperatureProperty';
if(device['@type'].indexOf("Thermostat") == -1){
device['@type'].unshift('Thermostat');
}
}
else if(expose.name == "local_temperature" || expose.name == "temperature" || expose.name == "cpu_temperature"){
device.properties[wt_id]['@type'] = 'TemperatureProperty';
if(device['@type'].indexOf("TemperatureSensor") == -1){
device['@type'].push('TemperatureSensor');
}
}
else if(expose.name == "humidity" || expose.name == "soil_moisture"){
device.properties[wt_id]['@type'] = 'HumidityProperty';
if(device['@type'].indexOf("HumiditySensor") == -1){
device['@type'].push('HumiditySensor');
}
}
else if(expose.name == "pressure"){
device.properties[wt_id]['@type'] = 'BarometricPressureProperty';
if(device['@type'].indexOf("BarometricPressureSensor") == -1){
device['@type'].push('BarometricPressureSensor');
}
}
else if(expose.name == "presence" || expose.name == "occupancy" || expose.name == "vibration"){
device.properties[wt_id]['@type'] = 'MotionProperty';
if(device['@type'].indexOf("MotionSensor") == -1){
device['@type'].push('MotionSensor');
//device['@type'] = ['MotionSensor'];
}
}
else if( expose.name == "contact"){
device.properties[wt_id]['@type'] = 'OpenProperty';
if(device['@type'].indexOf("DoorSensor") == -1){ // && device['@type'].length == 0){
device['@type'].push('DoorSensor');
}
}
else if(expose.name == "alarm" || expose.name == "sos" || expose.name == "carbon_monoxide" || expose.name == "gas"){
device.properties[wt_id]['@type'] = 'AlarmProperty';
if(device['@type'].indexOf("Alarm") == -1){
device['@type'].push('Alarm');
}
}
/*
else if(expose.name == "lock"){
device.properties[wt_id]['@type'] = 'LockedProperty';
if(device['@type'].indexOf("Lock") == -1){
device['@type'].push('Lock');
}
}
*/
else if(expose.name == "smoke"){
device.properties[wt_id]['@type'] = 'SmokeProperty';
if(device['@type'].indexOf("SmokeSensor") == -1){
device['@type'].push('SmokeSensor');
}
}
else if(expose.name == "switch"){
device.properties[wt_id]['@type'] = 'BooleanProperty';
if(device['@type'].indexOf("BinarySensor") == -1){
device['@type'].push('BinarySensor');
}
}
else if(expose.name == "water_leak"){
device.properties[wt_id]['@type'] = 'LeakProperty';
if(device['@type'].indexOf("LeakSensor") == -1){
device['@type'].push('LeakSensor');
}
}
else if(expose.name == "power"){
device.properties[wt_id]['@type'] = 'InstantaneousPowerProperty';
if(device['@type'].indexOf("OnOffSwitch") > -1 && device['@type'].indexOf("SmartPlug") == -1){
device['@type'].unshift('SmartPlug');
}
//if(device['@type'].indexOf("SmartPlug") > -1 && device['@type'].indexOf("Light") == -1){
// device['@type'].push('Light');
//}
if(device['@type'].indexOf("EnergyMonitor") == -1){
device['@type'].push('EnergyMonitor');
}
}
else if(expose.name == "voltage"){
device.properties[wt_id]['@type'] = 'VoltageProperty';
//if(device['@type'].indexOf("EnergyMonitor") == -1){
// device['@type'].push('EnergyMonitor');
//}
}
else if(expose.name == "current"){
device.properties[wt_id]['@type'] = 'CurrentProperty';
if(device['@type'].indexOf("EnergyMonitor") == -1){
device['@type'].push('EnergyMonitor');
}
}
else if(expose.name == "co2" || expose.name == "eco2" || expose.name == "voc"){
device.properties[wt_id]['@type'] = 'ConcentrationProperty';
if(device['@type'].indexOf("AirQualitySensor") == -1){
device['@type'].push('AirQualitySensor');
}
}
else if(expose.name == "pm10" || expose.name == "pm25" || expose.name == "hcho"){
device.properties[wt_id]['@type'] = 'DensityProperty';
if(device['@type'].indexOf("AirQualitySensor") == -1){
device['@type'].push('AirQualitySensor');
}
}
}
}
catch(e){
console.log("error in parse_device while trying to add capabilities: ", e);
}
}
// Capability upgrade based on the value_on property. // TODO: superfluous? When did this happen?
/*
// Should implement a proper lock parser
if(typeof expose['value_on'] != "undefined"){
if(this.config.debug){
console.log("value_on was defined: " + expose['value_on']);
}
if( (expose['value_on'] == "LOCK" && expose['value_off'] == "UNLOCK") || (expose['value_on'] == "UNLOCK" && expose['value_off'] == "LOCK") ){
device.properties[wt_id]['@type'] = 'LockedProperty';
}
}
*/
//if( expose.name == "child lock" ){
/*
if( expose.name == "contact" ){
console.log(">");
console.log("->");
console.log("--> contact");
if(typeof device.properties[wt_id] != 'undefined'){
console.log("--->", device.properties[wt_id]);
if(typeof device.properties[wt_id]['value_off'] != 'undefined' && typeof device.properties[wt_id]['value_on'] != 'undefined'){
console.log("----> FLIPPING?");
//const old_off = device.properties[wt_id]['value_off'];
//device.properties[wt_id]['value_off'] = device.properties[wt_id]['value_on'];
//device.properties[wt_id]['value_on'] = old_off;
}
}
}
*/
//
// ADD EXTRA GENERATED PROPERTIES BASED ON READ-ONLY ACTIONS
//
// Check if an extra boolean property could be generated from the enum values.
if(expose['type'] == "enum" && expose.access == 1){ // For now, only read-only enum properties (access type 1) with 'on' and 'off' in them will get an extra OnOffProperty
//var already_added_extra_state = false; // to avoid adding both an extra toggle and an extra power_state property
//console.log("about to add fake properties");
var add_push_button = false;
var add_brightness = false;
try{
// Has brightness up, but not brightness down? This seems too indicate a push button, at least for IKEA.
if(expose.values.indexOf('brightness_move_up') !== -1 && expose.values.indexOf('brightness_move_down') === -1){
if(property_names_list.indexOf('pushed') == -1){
//console.log('Adding push button');
add_push_button = true;
if(this.config.debug){
console.log("exposesDeviceGenerator spotted a brightness_stop in an action property enum list, and no pre-existing brightness property");
}
var fake_exposes_info = {'access': 1,
'name':'pushed',
'property':'pushed',
'description': 'momentary push button (generated from actions data)',
'type': 'binary',
'value_off': 'on',
'value_on': 'off'
};
// This should always be false:
fake_exposes_info['value'] = this.adapter.get_persistent_value(device.zigbee_id,'pushed',false,true,false); // zigbee_id, property_name, value, read-only, percentage
property_names_list.push('pushed');
device = this.parse_property(fake_exposes_info, device, property_names_list);
// add capability if there isn't one already
if( device['@type'].indexOf('PushButton') == -1){ // In this case it's unfortunate that the WebThings Gateway can only handle one capability per type.
device['@type'].push('PushButton');
}
// TODO: instead of 'pushed' also use a more complex wt_id here
device.properties[wt_id_base + 'pushed']['@type'] = 'PushedProperty';
device.properties[wt_id_base + 'pushed'].origin = "exposes-generated-from-action";
}
}
// Add additional brightness property if it doesn't exist yet. This may be a bad idea, as Z2M also handles this with the virtual brightness feature.
else if( (expose.values.indexOf('brightness_step_up') !== -1 || expose.values.indexOf('brightness_move_up') !== -1) && (expose.values.indexOf('brightness_step_down') !== -1 || expose.values.indexOf('brightness_move_down') !== -1) && expose.values.indexOf('brightness_stop') !== -1){
if(property_names_list.indexOf('brightness') == -1){
//console.log("adding brightness property generated from actions"); // doesn't Z2M already handle this automatically?
add_brightness == true;
if(this.config.debug){
console.log("exposesDeviceGenerator spotted a brightness_stop in an action property enum list, and no pre-existing brightness property");
}
var fake_exposes_info = {'access': 1,
'name':'brightness',
'property':'brightness',
'description': 'read-only brightness property (generated from actions data)',
'type': 'numeric',
'value_max': 100,
'value_min': 0,
'value_step':1
};
const initial_value = this.adapter.get_persistent_value(device.zigbee_id,'brightness',0,true,true); // zigbee_id, property_name, value, read-only, percentage
if(initial_value != null){
fake_exposes_info['value'] = initial_value;
}
property_names_list.push('brightness');
device = this.parse_property(fake_exposes_info, device, property_names_list);
// add capability if there isn't one already
if( device['@type'].indexOf('MultiLevelSensor') == -1){
device['@type'].push('MultiLevelSensor');
device.properties[wt_id_base + 'brightness']['@type'] = 'LevelProperty';
}
// TODO: instead of 'brightness' also use a more complex wt_id here
device.properties[wt_id_base + 'brightness'].origin = "exposes-generated-from-action";
}
else{
console.log("GENERATING FROM ACTIONS: BRIGHTNESS PROPERTY ALREADY EXISTED");
}
}
// Add additional toggle button if toggle action is spotted,
// OR if "on" and "off" as spotted, and those aren't already used for a pushed property
if ( expose.values.indexOf('toggle') !== -1 || (expose.values.indexOf('on') !== -1 && expose.values.indexOf('off') !== -1 && add_push_button == false) ){
if (property_names_list.indexOf('toggle') == -1 && property_names_list.indexOf('state') == -1 ){ // make sure these properties don't exist officially already.