This repository has been archived by the owner on Jan 12, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathConfigCreator.sh
executable file
·1837 lines (1703 loc) · 70.9 KB
/
ConfigCreator.sh
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
#!/bin/bash
#
# This script is to generate a complete Cmd4 configuration file needed for the cmd4-advantageair plugin
# This script can handle up to 3 independent AdvantageAir (AA) systems
#
# This script can be invoked in two ways:
# 1. from homebridge customUI
# a. click "SETTING" on cmd4-advantageair plugin and
# b. at the bottom of the SETTING page, define your AdvantageAir Device(s), then clikc SAVE
# c. click "SETTING" again and
# d. check the checkbox if you want the fan to be setup as fanSwitch
# e. click "CONFIG CREATOR" button
#
# 2. from a terminal
# a. find out where the bash script "ConfigCreator.sh" is installed (please see plugin wiki for details)
# b. run the bash script ConfigCreator.sh
# c. Enter the name and IP address of your AdvantageAir system(s) - up to 3 systems can be processed
# d. you can choose whether you want the fan to be setup as fanSwitch or not
# e. you might need to enter the path to AdvAir.sh if it is not found by the script.
# f. you might also need to enter the path to the Homebridge config.json file if it is not found by the script.
#
# Once the Cmd4 configuration file is generated and copied to Homebridge config.json and if you know
# what you are doing you can do some edits on the Cmd4 configuration file in Cmd4 Config Editor
# Click SAVE when you are done.
#
# NOTE: If you need to 'flip' the GarageDoorOpener, you have to add that in yourself.
#
UIversion="customUI"
AAIP="$1"
AAname="$2"
AAdebug="$3"
AAIP2="$4"
AAname2="$5"
AAdebug2="$6"
AAIP3="$7"
AAname3="$8"
AAdebug3="$9"
fanSetup="${10}"
zoneSetup="${11}"
timerSetup="${12}"
ADVAIR_SH_PATH="${13}"
# define the possible names for cmd4 platform
cmd4Platform=""
cmd4Platform1="\"platform\": \"Cmd4\""
cmd4Platform2="\"platform\": \"homebridge-cmd4\""
# define some other variables
name=""
hasAircons=false
hasLights=false
hasThings=false
# define some file variables
homebridgeConfigJson="" # homebridge config.json
configJson="config.json.copy" # a working copy of homebridge config.json
cmd4ConfigJson="cmd4Config.json" # Homebridge-Cmd4 config.json
cmd4ConfigJsonAA="cmd4Config_AA.json"
cmd4ConfigConstantsAA="cmd4Config.json.AAconstants"
cmd4ConfigQueueTypesAA="cmd4Config.json.AAqueueTypes"
cmd4ConfigAccessoriesAA="cmd4Config.json.AAaccessories"
cmd4ConfigJsonAAwithNonAA="${cmd4ConfigJsonAA}.withNonAA"
cmd4ConfigNonAA="cmd4Config.json.nonAA"
cmd4ConfigConstantsNonAA="cmd4Config.json.nonAAconstants"
cmd4ConfigQueueTypesNonAA="cmd4Config.json.nonAAqueueTypes"
cmd4ConfigAccessoriesNonAA="cmd4Config.json.nonAAaccessories"
cmd4ConfigMiscKeys="cmd4Config.json.miscKeys"
configJsonNew="${configJson}.new" # new homebridge config.json
# fun color stuff
BOLD=$(tput bold)
TRED=$(tput setaf 1)
TGRN=$(tput setaf 2)
TYEL=$(tput setaf 3)
TPUR=$(tput setaf 5)
TLBL=$(tput setaf 6)
TNRM=$(tput sgr0)
function cmd4Header()
{
local debugCmd4="false"
if [ "${debug}" = "true" ]; then
debugCmd4="true"
fi
{ echo "{"
echo " \"platform\": \"Cmd4\","
echo " \"name\": \"Cmd4\","
echo " \"debug\": ${debugCmd4},"
echo " \"outputConstants\": false,"
echo " \"statusMsg\": true,"
echo " \"timeout\": 60000,"
echo " \"stateChangeResponseTime\": 0,"
} > "$1"
}
function cmd4ConstantsHeader()
{
{ echo " \"constants\": ["
} > "$1"
}
function cmd4Constants()
{
local debugA=""
if [ "${debug}" = "true" ]; then
debugA="-debug"
fi
{ echo " {"
echo " \"key\": \"${ip}\","
echo " \"value\": \"${IPA}${debugA}\""
echo " },"
} >> "$1"
}
function cmd4QueueTypesHeader()
{
{ echo " \"queueTypes\": ["
} > "$1"
}
function cmd4QueueTypes()
{
{ echo " {"
echo " \"queue\": \"${queue}\","
echo " \"queueType\": \"WoRm2\""
echo " },"
} >> "$1"
}
function cmd4AccessoriesHeader()
{
{ echo " \"accessories\": ["
} > "$1"
}
function cmd4ConstantsQueueTypesAccessoriesMiscFooter()
{
cp "$1" "$1.temp"
sed '$ d' "$1.temp" > "$1"
rm "$1.temp"
{ echo " }"
echo " ],"
} >> "$1"
}
function cmd4LightbulbNoDimmer()
{
local name="$2"
local id="$3"
id=${id//\"/}
{ echo " {"
echo " \"type\": \"Lightbulb\","
echo " \"displayName\": \"${name}\","
echo " \"on\": \"FALSE\","
echo " \"name\": \"${name}\","
echo " \"manufacturer\": \"Advantage Air Australia\","
echo " \"model\": \"${sysType}\","
echo " \"serialNumber\": \"${tspModel}\","
echo " \"queue\": \"$queue\","
echo " \"polling\": ["
echo " {"
echo " \"characteristic\": \"on\""
echo " }"
echo " ],"
echo " \"state_cmd\": \"'${ADVAIR_SH_PATH}'\","
#echo " \"state_cmd_suffix\": \"'light:$name' ${ip}\""
echo " \"state_cmd_suffix\": \"ligID:$id ${ip}\""
echo " },"
} >> "$1"
}
function cmd4LightbulbWithDimmer()
{
local name="$2"
local id="$3"
id=${id//\"/}
{ echo " {"
echo " \"type\": \"Lightbulb\","
echo " \"displayName\": \"${name}\","
echo " \"on\": \"FALSE\","
echo " \"brightness\": 80,"
echo " \"name\": \"${name}\","
echo " \"manufacturer\": \"Advantage Air Australia\","
echo " \"model\": \"${sysType}\","
echo " \"serialNumber\": \"${tspModel}\","
echo " \"queue\": \"$queue\","
echo " \"polling\": ["
echo " {"
echo " \"characteristic\": \"on\""
echo " },"
echo " {"
echo " \"characteristic\": \"brightness\""
echo " }"
echo " ],"
echo " \"state_cmd\": \"'${ADVAIR_SH_PATH}'\","
#echo " \"state_cmd_suffix\": \"'light:${name}' ${ip}\""
echo " \"state_cmd_suffix\": \"ligID:$id ${ip}\""
echo " },"
} >> "$1"
}
function cmd4GarageDoorOpener()
{
local name="$2"
local id="$3"
id=${id//\"/}
{ echo " {"
echo " \"type\": \"GarageDoorOpener\","
echo " \"displayName\": \"${name}\","
echo " \"obstructionDetected\": \"FALSE\","
echo " \"currentDoorState\": 1,"
echo " \"targetDoorState\": 1,"
echo " \"name\": \"${name}\","
echo " \"manufacturer\": \"Advantage Air Australia\","
echo " \"model\": \"${sysType}\","
echo " \"serialNumber\": \"${tspModel}\","
echo " \"queue\": \"$queue\","
echo " \"polling\": ["
echo " {"
echo " \"characteristic\": \"currentDoorState\""
echo " },"
echo " {"
echo " \"characteristic\": \"targetDoorState\""
echo " }"
echo " ],"
echo " \"state_cmd\": \"'${ADVAIR_SH_PATH}'\","
#echo " \"state_cmd_suffix\": \"'thing:${name}' ${ip}\""
echo " \"state_cmd_suffix\": \"thiID:$id ${ip}\""
echo " },"
} >> "$1"
}
function cmd4ZoneLightbulb()
{
local name="$2"
local ac_l=" ${ac}"
if [ "${ac_l}" = " ac1" ]; then ac_l=""; fi
{ echo " {"
echo " \"type\": \"Lightbulb\","
echo " \"displayName\": \"${name}\","
echo " \"on\": \"FALSE\","
echo " \"brightness\": 50,"
echo " \"name\": \"${name}\","
echo " \"manufacturer\": \"Advantage Air Australia\","
echo " \"model\": \"${sysType}\","
echo " \"serialNumber\": \"${tspModel}\","
echo " \"queue\": \"$queue\","
echo " \"polling\": ["
echo " {"
echo " \"characteristic\": \"on\""
echo " },"
echo " {"
echo " \"characteristic\": \"brightness\""
echo " }"
echo " ],"
echo " \"state_cmd\": \"'${ADVAIR_SH_PATH}'\","
echo " \"state_cmd_suffix\": \"${zoneStr} ${ip}${ac_l}\""
echo " },"
} >> "$1"
}
function cmd4ZoneLightbulb2()
{
local name="$2"
local ac_l=" ${ac}"
if [ "${ac_l}" = " ac1" ]; then ac_l=""; fi
{ echo " {"
echo " \"type\": \"Lightbulb\","
echo " \"displayName\": \"${name}\","
echo " \"on\": \"FALSE\","
echo " \"brightness\": 50,"
echo " \"name\": \"${name}\","
echo " \"manufacturer\": \"Advantage Air Australia\","
echo " \"model\": \"${sysType}\","
echo " \"serialNumber\": \"${tspModel}\","
echo " \"queue\": \"$queue\","
echo " \"polling\": ["
echo " {"
echo " \"characteristic\": \"on\""
echo " },"
echo " {"
echo " \"characteristic\": \"brightness\""
echo " }"
echo " ],"
echo " \"state_cmd\": \"'${ADVAIR_SH_PATH}'\","
echo " \"state_cmd_suffix\": \"${zoneStr} ${ip}${ac_l}\","
} >> "$1"
}
function cmd4TimerLightbulb()
{
local name="$2"
local suffix="$3"
local ac_l=" ${ac}"
if [ "${ac_l}" = " ac1" ]; then ac_l=""; fi
{ echo " {"
echo " \"type\": \"Lightbulb\","
echo " \"displayName\": \"${name}\","
echo " \"on\": \"FALSE\","
echo " \"brightness\": 0,"
echo " \"name\": \"${name}\","
echo " \"manufacturer\": \"Advantage Air Australia\","
echo " \"model\": \"${sysType}\","
echo " \"serialNumber\": \"${tspModel}\","
echo " \"queue\": \"$queue\","
echo " \"polling\": ["
echo " {"
echo " \"characteristic\": \"on\""
echo " },"
echo " {"
echo " \"characteristic\": \"brightness\""
echo " }"
echo " ],"
echo " \"state_cmd\": \"'${ADVAIR_SH_PATH}'\","
echo " \"state_cmd_suffix\": \"${suffix} ${ip}${ac_l}\""
echo " },"
} >> "$1"
}
function cmd4Thermostat()
{
local airconName="$2"
local ac_l=" ${ac}"
if [ "${ac_l}" = " ac1" ]; then ac_l=""; fi
{ echo " {"
echo " \"type\": \"Thermostat\","
echo " \"displayName\": \"${airconName}\","
echo " \"currentHeatingCoolingState\": \"OFF\","
echo " \"targetHeatingCoolingState\": \"OFF\","
echo " \"currentTemperature\": 24,"
echo " \"targetTemperature\": 24,"
echo " \"temperatureDisplayUnits\": \"CELSIUS\","
echo " \"name\": \"${airconName}\","
echo " \"manufacturer\": \"Advantage Air Australia\","
echo " \"model\": \"${sysType}\","
echo " \"serialNumber\": \"${tspModel}\","
echo " \"queue\": \"$queue\","
echo " \"polling\": ["
echo " {"
echo " \"characteristic\": \"currentHeatingCoolingState\""
echo " },"
echo " {"
echo " \"characteristic\": \"targetHeatingCoolingState\""
echo " },"
echo " {"
echo " \"characteristic\": \"currentTemperature\""
echo " },"
echo " {"
echo " \"characteristic\": \"targetTemperature\""
echo " }"
echo " ],"
echo " \"state_cmd\": \"'${ADVAIR_SH_PATH}'\","
echo " \"state_cmd_suffix\": \"${ip}${ac_l}\","
} >> "$1"
}
function cmd4ZoneFanv2()
{
local name="$2"
local ac_l=" ${ac}"
if [ "${ac_l}" = " ac1" ]; then ac_l=""; fi
{ echo " {"
echo " \"type\": \"Fanv2\","
echo " \"displayName\": \"${name}\","
echo " \"active\": 0,"
echo " \"rotationSpeed\": 100,"
echo " \"rotationDirection\": 1,"
echo " \"name\": \"${name}\","
echo " \"manufacturer\": \"Advantage Air Australia\","
echo " \"model\": \"${sysType}\","
echo " \"serialNumber\": \"${tspModel}\","
echo " \"queue\": \"$queue\","
echo " \"polling\": ["
echo " {"
echo " \"characteristic\": \"active\""
echo " },"
echo " {"
echo " \"characteristic\": \"rotationSpeed\""
echo " },"
echo " {"
echo " \"characteristic\": \"rotationDirection\""
echo " }"
echo " ],"
echo " \"state_cmd\": \"'${ADVAIR_SH_PATH}'\","
echo " \"state_cmd_suffix\": \"${zoneStr} ${ip}${ac_l}\","
} >> "$1"
}
function cmd4ZoneFanv2noRotationDirection()
{
local name="$2"
local ac_l=" ${ac}"
if [ "${ac_l}" = " ac1" ]; then ac_l=""; fi
{ echo " {"
echo " \"type\": \"Fanv2\","
echo " \"displayName\": \"${name}\","
echo " \"active\": 0,"
echo " \"rotationSpeed\": 100,"
echo " \"name\": \"${name}\","
echo " \"manufacturer\": \"Advantage Air Australia\","
echo " \"model\": \"${sysType}\","
echo " \"serialNumber\": \"${tspModel}\","
echo " \"queue\": \"$queue\","
echo " \"polling\": ["
echo " {"
echo " \"characteristic\": \"active\""
echo " },"
echo " {"
echo " \"characteristic\": \"rotationSpeed\""
echo " }"
echo " ],"
echo " \"state_cmd\": \"'${ADVAIR_SH_PATH}'\","
echo " \"state_cmd_suffix\": \"${zoneStr} ${ip}${ac_l}\","
} >> "$1"
}
function cmd4ZoneFan()
{
local name="$2"
local ac_l=" ${ac}"
if [ "${ac_l}" = " ac1" ]; then ac_l=""; fi
{ echo " {"
echo " \"type\": \"Fan\","
echo " \"displayName\": \"${name}\","
echo " \"on\": \"FALSE\","
echo " \"rotationSpeed\": 100,"
echo " \"name\": \"${name}\","
echo " \"manufacturer\": \"Advantage Air Australia\","
echo " \"model\": \"${sysType}\","
echo " \"serialNumber\": \"${tspModel}\","
echo " \"queue\": \"$queue\","
echo " \"polling\": ["
echo " {"
echo " \"characteristic\": \"on\""
echo " },"
echo " {"
echo " \"characteristic\": \"rotationSpeed\""
echo " }"
echo " ],"
echo " \"state_cmd\": \"'${ADVAIR_SH_PATH}'\","
echo " \"state_cmd_suffix\": \"${zoneStr} ${ip}${ac_l}\""
echo " },"
} >> "$1"
}
function cmd4Fan()
{
local fanName="$2"
local ac_l=" ${ac}"
if [ "${ac_l}" = " ac1" ]; then ac_l=""; fi
{ echo " {"
echo " \"type\": \"Fan\","
echo " \"displayName\": \"${fanName}\","
echo " \"on\": \"FALSE\","
echo " \"rotationSpeed\": 100,"
echo " \"name\": \"${fanName}\","
echo " \"manufacturer\": \"Advantage Air Australia\","
echo " \"model\": \"${sysType}\","
echo " \"serialNumber\": \"${tspModel}\","
echo " \"queue\": \"$queue\","
echo " \"polling\": ["
echo " {"
echo " \"characteristic\": \"on\""
echo " },"
echo " {"
echo " \"characteristic\": \"rotationSpeed\""
echo " }"
echo " ],"
echo " \"state_cmd\": \"'${ADVAIR_SH_PATH}'\","
echo " \"state_cmd_suffix\": \"${ip}${ac_l}\""
echo " },"
} >> "$1"
}
function cmd4FanSwitch()
{
local fanName="$2"
local ac_l=" ${ac}"
if [ "${ac_l}" = " ac1" ]; then ac_l=""; fi
{ echo " {"
echo " \"type\": \"Switch\","
echo " \"displayName\": \"${fanName}\","
echo " \"on\": \"FALSE\","
echo " \"name\": \"${fanName}\","
echo " \"manufacturer\": \"Advantage Air Australia\","
echo " \"model\": \"${sysType}\","
echo " \"serialNumber\": \"${tspModel}\","
echo " \"queue\": \"$queue\","
echo " \"polling\": ["
echo " {"
echo " \"characteristic\": \"on\""
echo " }"
echo " ],"
echo " \"state_cmd\": \"'${ADVAIR_SH_PATH}'\","
echo " \"state_cmd_suffix\": \"${ip}${ac_l}\","
} >> "$1"
}
function cmd4FanLinkTypes()
{
local fanSpeedName="$2"
local ac_l=" ${ac}"
if [ "${ac_l}" = " ac1" ]; then ac_l=""; fi
{ echo " \"linkedTypes\": ["
echo " {"
echo " \"type\": \"Fan\","
echo " \"displayName\": \"${fanSpeedName}\","
echo " \"on\": \"TRUE\","
echo " \"rotationSpeed\": 100,"
echo " \"name\": \"${fanSpeedName}\","
echo " \"manufacturer\": \"Advantage Air Australia\","
echo " \"model\": \"${sysType}\","
echo " \"serialNumber\": \"${tspModel}\","
echo " \"queue\": \"$queue\","
echo " \"polling\": ["
echo " {"
echo " \"characteristic\": \"on\""
echo " },"
echo " {"
echo " \"characteristic\": \"rotationSpeed\""
echo " }"
echo " ],"
echo " \"state_cmd\": \"'${ADVAIR_SH_PATH}'\","
echo " \"state_cmd_suffix\": \"${ip} fanSpeed${ac_l}\""
echo " }"
echo " ]"
echo " },"
} >> "$1"
}
function cmd4myZoneSwitch()
{
local myZoneName="$2"
local ac_l=" ${ac}"
if [ "${ac_l}" = " ac1" ]; then ac_l=""; fi
{ echo " {"
echo " \"type\": \"Switch\","
echo " \"displayName\": \"${myZoneName}\","
echo " \"on\": \"FALSE\","
echo " \"name\": \"${myZoneName}\","
echo " \"manufacturer\": \"Advantage Air Australia\","
echo " \"model\": \"${sysType}\","
echo " \"serialNumber\": \"${tspModel}\","
echo " \"queue\": \"$queue\","
echo " \"polling\": ["
echo " {"
echo " \"characteristic\": \"on\""
echo " }"
echo " ],"
echo " \"state_cmd\": \"'${ADVAIR_SH_PATH}'\","
echo " \"state_cmd_suffix\": \"myZone=${zone} ${ip}${ac_l}\""
echo " },"
} >> "$1"
}
function cmd4ZoneTempSensor()
{
local name="$2"
local ac_l=" ${ac}"
if [ "${ac_l}" = " ac1" ]; then ac_l=""; fi
{ echo " {"
echo " \"type\": \"TemperatureSensor\","
echo " \"subType\": \"TempSensor${b}\","
echo " \"displayName\": \"${name}\","
echo " \"currentTemperature\": 25,"
echo " \"statusLowBattery\": \"BATTERY_LEVEL_LOW\","
echo " \"name\": \"${name}\","
echo " \"manufacturer\": \"Advantage Air Australia\","
echo " \"model\": \"${sysType}\","
echo " \"serialNumber\": \"${tspModel}\","
echo " \"queue\": \"$queue\","
echo " \"polling\": ["
echo " {"
echo " \"characteristic\": \"currentTemperature\""
echo " },"
echo " {"
echo " \"characteristic\": \"statusLowBattery\""
echo " }"
echo " ],"
echo " \"state_cmd\": \"'${ADVAIR_SH_PATH}'\","
echo " \"state_cmd_suffix\": \"${zoneStr} ${ip}${ac_l}\""
echo " },"
} >> "$1"
}
function cmd4ZoneLinkedTypesTempSensor()
{
local name="$2"
local ac_l=" ${ac}"
if [ "${ac_l}" = " ac1" ]; then ac_l=""; fi
{ echo " \"linkedTypes\": ["
echo " {"
echo " \"type\": \"TemperatureSensor\","
echo " \"displayName\": \"${name}\","
echo " \"currentTemperature\": 25,"
echo " \"statusLowBattery\": \"BATTERY_LEVEL_LOW\","
echo " \"name\": \"${name}\","
echo " \"manufacturer\": \"Advantage Air Australia\","
echo " \"model\": \"${sysType}\","
echo " \"serialNumber\": \"${tspModel}\","
echo " \"queue\": \"$queue\","
echo " \"polling\": ["
echo " {"
echo " \"characteristic\": \"currentTemperature\""
echo " },"
echo " {"
echo " \"characteristic\": \"statusLowBattery\""
echo " }"
echo " ],"
echo " \"state_cmd\": \"'${ADVAIR_SH_PATH}'\","
echo " \"state_cmd_suffix\": \"${zoneStr} ${ip}${ac_l}\""
echo " }"
echo " ]"
echo " },"
} >> "$1"
}
function cmd4ZoneSwitch()
{
local name="$2"
local ac_l=" ${ac}"
if [ "${ac_l}" = " ac1" ]; then ac_l=""; fi
{ echo " {"
echo " \"type\": \"Switch\","
echo " \"displayName\": \"${name}\","
echo " \"on\": \"FALSE\","
echo " \"name\": \"${name}\","
echo " \"manufacturer\": \"Advantage Air Australia\","
echo " \"model\": \"${sysType}\","
echo " \"serialNumber\": \"${tspModel}\","
echo " \"queue\": \"$queue\","
echo " \"polling\": true,"
echo " \"state_cmd\": \"'${ADVAIR_SH_PATH}'\","
echo " \"state_cmd_suffix\": \"${zoneStr} ${ip}${ac_l}\""
echo " },"
} >> "$1"
}
function cmd4ZoneSwitch2()
{
local name="$2"
local ac_l=" ${ac}"
if [ "${ac_l}" = " ac1" ]; then ac_l=""; fi
{ echo " {"
echo " \"type\": \"Switch\","
echo " \"displayName\": \"${name}\","
echo " \"on\": \"FALSE\","
echo " \"name\": \"${name}\","
echo " \"manufacturer\": \"Advantage Air Australia\","
echo " \"model\": \"${sysType}\","
echo " \"serialNumber\": \"${tspModel}\","
echo " \"queue\": \"$queue\","
echo " \"polling\": true,"
echo " \"state_cmd\": \"'${ADVAIR_SH_PATH}'\","
echo " \"state_cmd_suffix\": \"${zoneStr} ${ip}${ac_l}\","
} >> "$1"
}
function cmd4Footer()
{
lastLine=$(tail -n 1 "$1")
squareBracket=$(echo "${lastLine}"|grep "]")
cp "$1" "$1.temp"
sed '$ d' "$1.temp" > "$1"
rm "$1.temp"
#
if [ -n "${squareBracket}" ]; then
{ echo " ]"
echo "}"
} >> "$1"
else
{ echo " }"
echo "}"
} >> "$1"
fi
}
function readHomebridgeConfigJson()
{
case $UIversion in
customUI )
DIR=$(pwd)
homebridgeConfigJson="${DIR}/config.json"
if [ -f "${homebridgeConfigJson}" ]; then
# expand the json just in case it is in compact form
jq --indent 4 '.' "${homebridgeConfigJson}" > "${configJson}"
checkForPlatformCmd4InHomebridgeConfigJson
if [ -z "${validFile}" ]; then
echo "ERROR: no Cmd4 Config found in \"${homebridgeConfigJson}\"! Please ensure that Homebridge-Cmd4 plugin is installed"
exit 1
fi
else
echo "ERROR: no Homebridge config.json found in \"${DIR}\"! Please copy \"${cmd4ConfigJsonAA}\" to cmd4 JASON Config manually."
cleanUp
exit 1
fi
;;
nonUI )
INPUT=""
homebridgeConfigJson=""
getHomebridgeConfigJsonPath
if [ "${fullPath}" != "" ]; then homebridgeConfigJson="${fullPath}"; fi
# if no config.json file found, ask user to input the full path
if [ -z "${homebridgeConfigJson}" ]; then
homebridgeConfigJson=""
echo ""
echo "${TPUR}WARNING: No Homebridge config.json file located by the script!${TNRM}"
echo ""
until [ -n "${INPUT}" ]; do
echo "${TYEL}Please enter the full path of your Homebridge config.json file,"
echo "otherwise just hit enter to abort copying \"${cmd4ConfigJsonAA}\" to Homebridge config.json."
echo "The config.json path should be in the form of /*/*/*/config.json ${TNRM}"
read -r -p "${BOLD}> ${TNRM}" INPUT
if [ -z "${INPUT}" ]; then
echo "${TPUR}WARNING: No Homebridge config.json file specified"
echo " Copying of ${cmd4ConfigJsonAA} to Homebridge config.json was aborted"
echo ""
echo "${TLBL}${BOLD}INFO: Please copy/paste the ${cmd4ConfigJsonAA} into Cmd4 JASON Config Editor manually${TNRM}"
cleanUp
exit 1
elif expr "${INPUT}" : '[./a-zA-Z0-9]*/config.json$' >/dev/null; then
if [ -f "${INPUT}" ]; then
homebridgeConfigJson="${INPUT}"
break
else
echo ""
echo "${TPUR}WARNING: No such file exits!${TNRM}"
echo ""
INPUT=""
fi
else
echo ""
echo "${TPUR}WARNING: Wrong format for file path for Homebridge config.json!${TNRM}"
echo ""
INPUT=""
fi
done
fi
if [ -f "${homebridgeConfigJson}" ]; then
if [ -z "${INPUT}" ]; then
echo ""
echo "${TLBL}INFO: The Homebridge config.json found: ${homebridgeConfigJson}${TNRM}"
echo ""
else
echo ""
echo "${TLBL}INFO: The Homebridge config.json specified: ${homebridgeConfigJson}${TNRM}"
echo ""
fi
# expand the json just in case it is in compact form
jq --indent 4 '.' "${homebridgeConfigJson}" > "${configJson}"
checkForPlatformCmd4InHomebridgeConfigJson
if [ -z "${validFile}" ]; then
echo ""
echo "${TRED}ERROR: no Cmd4 Config found in \"${homebridgeConfigJson}\"! Please ensure that Homebridge-Cmd4 plugin is installed${TNRM}"
echo "${TLBL}INFO: ${cmd4ConfigJsonAA} was created but not copied to Homebridge-Cmd4 JASON Config Editor!"
echo " Please copy/paste the ${cmd4ConfigJsonAA} into Cmd4 JASON Config Editor manually${TNRM}"
cleanUp
exit 1
fi
fi
;;
esac
}
function extractCmd4ConfigFromConfigJson()
{
noOfPlatforms=$(( $( jq ".platforms|keys" "${configJson}" | wc -w) - 2 ))
cmd4PlatformName=$(echo "${cmd4Platform}"|cut -d'"' -f4)
for ((i=0; i<noOfPlatforms; i++)); do
plaftorm=$( jq ".platforms[${i}].platform" "${configJson}" )
if [ "${plaftorm}" = "\"${cmd4PlatformName}\"" ]; then
jq --indent 4 ".platforms[${i}]" "${configJson}" > "${cmd4ConfigJson}"
jq --indent 4 "del(.platforms[${i}])" "${configJson}" > "${configJson}.Cmd4less"
break
fi
done
}
function extractCmd4ConfigNonAAandAccessoriesNonAA()
{
AAaccessories=""
count=0
presenceOfAccessories=$(jq ".accessories" "${cmd4ConfigJson}")
if [ "${presenceOfAccessories}" != "null" ]; then
noOfAccessories=$(( $( jq ".accessories|keys" "${cmd4ConfigJson}" | wc -w) - 2 ))
for (( i=0; i<noOfAccessories; i++ )); do
cmd4StateCmd=$( jq ".accessories[${i}].state_cmd" "${cmd4ConfigJson}" | grep -n "homebridge-cmd4-advantageair" )
# save the ${i} n a string for use to delete the AA accessories from ${cmd4ConfigJson}
if [ "${cmd4StateCmd}" != "" ]; then
if [ "${AAaccessories}" = "" ]; then
AAaccessories="${i}"
else
AAaccessories="${AAaccessories},${i}"
fi
else # create the non-AA accessories
count=$(( count + 1 ))
if [ "${count}" -eq 1 ]; then
jq --indent 4 ".accessories[${i}]" "${cmd4ConfigJson}" > "${cmd4ConfigAccessoriesNonAA}"
else
sed '$d' "${cmd4ConfigAccessoriesNonAA}" > "${cmd4ConfigAccessoriesNonAA}.tmp"
mv "${cmd4ConfigAccessoriesNonAA}.tmp" "${cmd4ConfigAccessoriesNonAA}"
echo "}," >> "${cmd4ConfigAccessoriesNonAA}"
jq --indent 4 ".accessories[${i}]" "${cmd4ConfigJson}" >> "${cmd4ConfigAccessoriesNonAA}"
fi
fi
done
fi
# delete the AA accessories to create ${cmd4ConfigNonAA} for use later
if [ "${AAaccessories}" = "" ]; then
cp "${cmd4ConfigJson}" "${cmd4ConfigNonAA}"
else
jq --indent 4 "del(.accessories[${AAaccessories}])" "${cmd4ConfigJson}" > "${cmd4ConfigNonAA}"
fi
# check that there are non-AA accessories, if not, remove the file
if [ -f "${cmd4ConfigAccessoriesNonAA}" ]; then
validFile=$(head -n 1 "${cmd4ConfigAccessoriesNonAA}")
if [ "${validFile}" = "" ]; then rm "${cmd4ConfigAccessoriesNonAA}"; fi
fi
}
function extractNonAAconstants()
{
count=0
noOfConstans=$(( $( jq ".constants|keys" "${cmd4ConfigNonAA}" | wc -w) - 2 ))
for ((i=0; i<noOfConstans; i++)); do
key=$( jq ".constants[${i}].key" "${cmd4ConfigNonAA}" )
key=${key//\"/}
keyUsed=$(grep -n "${key}" "${cmd4ConfigAccessoriesNonAA}"|grep -v 'key'|head -n 1|cut -d":" -f1)
if [ -n "${keyUsed}" ]; then
count=$(( count + 1 ))
if [ "${count}" -eq 1 ]; then
jq --indent 4 ".constants[${i}]" "${cmd4ConfigNonAA}" > "${cmd4ConfigConstantsNonAA}"
else
sed '$d' "${cmd4ConfigConstantsNonAA}" > "${cmd4ConfigConstantsNonAA}.tmp"
mv "${cmd4ConfigConstantsNonAA}.tmp" "${cmd4ConfigConstantsNonAA}"
echo "}," >> "${cmd4ConfigConstantsNonAA}"
jq --indent 4 ".constants[${i}]" "${cmd4ConfigNonAA}" >> "${cmd4ConfigConstantsNonAA}"
fi
fi
done
if [ -f "${cmd4ConfigConstantsNonAA}" ]; then
validFile=$(head -n 1 "${cmd4ConfigConstantsNonAA}")
if [ "${validFile}" = "" ]; then rm "${cmd4ConfigConstantsNonAA}"; fi
fi
}
function extractNonAAqueueTypes()
{
count=0
noOfQueues=$(( $( jq ".queueTypes|keys" "${cmd4ConfigNonAA}" | wc -w) - 2 ))
for ((i=0; i<noOfQueues; i++)); do
queue=$( jq ".queueTypes[${i}].queue" "${cmd4ConfigNonAA}" )
queueUsed=$(grep -n "${queue}" "${cmd4ConfigAccessoriesNonAA}"|head -n 1)
if [ -n "${queueUsed}" ]; then
count=$(( count + 1 ))
if [ "${count}" -eq 1 ]; then
jq --indent 4 ".queueTypes[${i}]" "${cmd4ConfigNonAA}" > "${cmd4ConfigQueueTypesNonAA}"
else
sed '$d' "${cmd4ConfigQueueTypesNonAA}" > "${cmd4ConfigQueueTypesNonAA}.tmp"
mv "${cmd4ConfigQueueTypesNonAA}.tmp" "${cmd4ConfigQueueTypesNonAA}"
echo "}," >> "${cmd4ConfigQueueTypesNonAA}"
jq --indent 4 ".queueTypes[${i}]" "${cmd4ConfigNonAA}" >> "${cmd4ConfigQueueTypesNonAA}"
fi
fi
done
if [ -f "${cmd4ConfigQueueTypesNonAA}" ]; then
validFile=$(head -n 1 "${cmd4ConfigQueueTypesNonAA}")
if [ "${validFile}" = "" ]; then rm "${cmd4ConfigQueueTypesNonAA}"; fi
fi
}
function extractCmd4MiscKeys()
{
# Extract any misc Cmd4 Keys used for non-AA accessories
count=0
keys=$( jq ".|keys" "${cmd4ConfigNonAA}" )
noOfKeys=$(( $(echo "${keys}" | wc -w) - 2 ))
for ((i=0; i<noOfKeys; i++)); do
key=$( echo "${keys}" | jq ".[${i}]" )
key=${key//\"/}
if [[ "${key}" != "platform" && "${key}" != "name" && "${key}" != "debug" && "${key}" != "outputConstants" && "${key}" != "statusMsg" && "${key}" != "timeout" && "${key}" != "stateChangeResponseTime" && "${key}" != "constants" && "${key}" != "queueTypes" && "${key}" != "accessories" ]]; then
count=$(( count + 1 ))
miscKey=$( echo "${keys}" | jq ".[${i}]" )
if [ "${count}" -eq 1 ]; then echo "{" >> "${cmd4ConfigMiscKeys}"; fi
if [ "${count}" -gt 1 ]; then echo "," >> "${cmd4ConfigMiscKeys}"; fi
echo "${miscKey}:" >> "${cmd4ConfigMiscKeys}"
jq --indent 4 ".${miscKey}" "${cmd4ConfigNonAA}" >> "${cmd4ConfigMiscKeys}"
fi
done
if [ -f "${cmd4ConfigMiscKeys}" ]; then
validFile=$(head -n 1 "${cmd4ConfigMiscKeys}")
if [ -z "${validFile}" ]; then
rm -f "${cmd4ConfigMiscKeys}"
else
# reformat it to proper json and then remove the "{" and "}" at the begining and the end of the file
echo "}" >> "${cmd4ConfigMiscKeys}"
jq --indent 4 '.' "${cmd4ConfigMiscKeys}" | sed '1d;$d' > "${cmd4ConfigMiscKeys}".tmp
mv "${cmd4ConfigMiscKeys}".tmp "${cmd4ConfigMiscKeys}"
fi
fi
}
function extractNonAAaccessoriesrConstantsQueueTypesMisc()
{
# extract non-AA config and non-AA accessories from ${cmd4ConfigJson}
extractCmd4ConfigNonAAandAccessoriesNonAA
# extract non-AA constants and non-AA queueTypes
if [ -f "${cmd4ConfigAccessoriesNonAA}" ]; then
extractNonAAconstants
extractNonAAqueueTypes
fi
# extract some misc. keys existing in Cmd4
extractCmd4MiscKeys
}
function assembleCmd4ConfigJson()
{
cmd4Header "${cmd4ConfigJsonAA}"
cat "${cmd4ConfigConstantsAA}" >> "${cmd4ConfigJsonAA}"
cmd4ConstantsQueueTypesAccessoriesMiscFooter "${cmd4ConfigJsonAA}"
cat "${cmd4ConfigQueueTypesAA}" >> "${cmd4ConfigJsonAA}"
cmd4ConstantsQueueTypesAccessoriesMiscFooter "${cmd4ConfigJsonAA}"
cat "${cmd4ConfigAccessoriesAA}" >> "${cmd4ConfigJsonAA}"
cmd4ConstantsQueueTypesAccessoriesMiscFooter "${cmd4ConfigJsonAA}"
cmd4Footer "${cmd4ConfigJsonAA}"
}
function assembleCmd4ConfigJsonAAwithNonAA()
{
cmd4Header "${cmd4ConfigJsonAAwithNonAA}"
cat "${cmd4ConfigConstantsAA}" >> "${cmd4ConfigJsonAAwithNonAA}"
if [ -f "${cmd4ConfigConstantsNonAA}" ]; then cat "${cmd4ConfigConstantsNonAA}" >> "${cmd4ConfigJsonAAwithNonAA}"; fi
cmd4ConstantsQueueTypesAccessoriesMiscFooter "${cmd4ConfigJsonAAwithNonAA}"
cat "${cmd4ConfigQueueTypesAA}" >> "${cmd4ConfigJsonAAwithNonAA}"
if [ -f "${cmd4ConfigQueueTypesNonAA}" ]; then cat "${cmd4ConfigQueueTypesNonAA}" >> "${cmd4ConfigJsonAAwithNonAA}"; fi
cmd4ConstantsQueueTypesAccessoriesMiscFooter "${cmd4ConfigJsonAAwithNonAA}"
cat "${cmd4ConfigAccessoriesAA}" >> "${cmd4ConfigJsonAAwithNonAA}"
if [ -f "${cmd4ConfigAccessoriesNonAA}" ]; then cat "${cmd4ConfigAccessoriesNonAA}" >> "${cmd4ConfigJsonAAwithNonAA}"; fi
cmd4ConstantsQueueTypesAccessoriesMiscFooter "${cmd4ConfigJsonAAwithNonAA}"
if [ -f "${cmd4ConfigMiscKeys}" ]; then cat "${cmd4ConfigMiscKeys}" >> "${cmd4ConfigJsonAAwithNonAA}"; fi
cmd4Footer "${cmd4ConfigJsonAAwithNonAA}"
}
function writeToHomebridgeConfigJson()
{
# Writing the created "${cmd4ConfigJsonAAwithNonAA}" to "${configJson}.Cmd4less" to create "${configJsonNew}"
# before copying to Homebridge config.json
jq --argjson cmd4Config "$(<"${cmd4ConfigJsonAAwithNonAA}")" --indent 4 '.platforms += [$cmd4Config]' "${configJson}.Cmd4less" > "${configJsonNew}"
rc=$?
if [ "${rc}" != "0" ]; then
echo "${TRED}${BOLD}ERROR: Writing of created Cmd4 config to config.json.new failed!${TNRM}"
echo "${TLBL}${BOLD}INFO: Instead you can copy/paste the content of \"${cmd4ConfigJsonAA}\" into Cmd4 JASON Config editor.${TNRM}"
cleanUp
exit 1