-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConfigCreator.sh
executable file
·1067 lines (983 loc) · 44.3 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 configuration file needed for the bondbridge plugin
# This script can handle up to 3 independent BondBridge (BB) systems
#
# This script can be invoked in two ways:
# 1. from homebridge customUI
#
# 2. from a terminal
#
UIversion="customUI"
BBIP1="${1}"
BBtoken1="${2}"
CFsetupOption1="${3}"
CFtimerSetup1="${4}"
BBdebug1="${5}"
BBIP2="${6}"
BBtoken2="${7}"
CFsetupOption2="${8}"
CFtimerSetup2="${9}"
BBdebug2="${10}"
BBIP3="${11}"
BBtoken3="${12}"
CFsetupOption3="${13}"
CFtimerSetup3="${14}"
BBdebug3="${15}"
BONDBRIDGE_SH_PATH="${16}"
# define the possible names for MyPlace platform
myPlacePlatform=""
myPlacePlatform1="MyPlace"
myPlacePlatform2="homebridge-myplace"
# Initialize some JSON varialbles
myPlaceConstantsBB="{}"
myPlaceQueueTypesBB="{}"
myPlaceAccessoriesBB="{}"
# 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 myPlaceBasicKeys()
{
myPlaceBasicKeys="{}"
myPlaceBasicKeys=$( echo "${myPlaceBasicKeys}" | jq ".title |= \"MyPlace configuration generated by Configuration Creator\"" \
| jq ".debug |= ${debugMyPlace}" \
| jq ".outputConstants |= false" \
| jq ".statusMsg |= true" \
| jq ".timeout |= 60000" \
| jq ".stateChangeResponseTime |= 0" )
}
function myPlaceConstants()
{
local debugA=""
if [ "${debug}" = "true" ]; then
debugA="-debug"
fi
myPlaceConstants="{}"
myPlaceConstants=$( echo "${myPlaceConstants}" | jq ".key |= \"${ip}\"" \
| jq ".value |= \"${IPA}${debugA}\"" )
myPlaceConstantsBB=$( echo "${myPlaceConstantsBB}" | jq ".constants += [${myPlaceConstants}]" )
}
function myPlaceQueueTypes()
{
myPlaceQueueTypes="{}"
myPlaceQueueTypes=$( echo "${myPlaceQueueTypes}" | jq ".queue |= \"${queue}\"" \
| jq ".queueType |= \"WoRm2\"" )
myPlaceQueueTypesBB=$( echo "${myPlaceQueueTypesBB}" | jq ".queueTypes += [${myPlaceQueueTypes}]" )
}
function myPlaceModelQueue()
{
myPlaceModelQueue="{}"
myPlaceModelQueue=$( echo "${myPlaceModelQueue}" | jq ".manufacturer |= \"OLIBRA\"" \
| jq ".model |= \"${model}\"" \
| jq ".serialNumber |= \"${bondid}\"" \
| jq ".queue |= \"${queue}\"" )
}
function myPlaceFan()
{
local name="$1"
local minStep="${2}"
myPlaceFan="{}"
myPlaceFan=$( echo "${myPlaceFan}" | jq ".type |= \"Fan\"" \
| jq ".displayName |= \"${name}\"" \
| jq ".on |= false" \
| jq ".rotationSpeed |= 25" \
| jq ".name |= \"${name}\"" \
| jq ". += ${myPlaceModelQueue}" \
| jq ".polling[0].characteristic |= \"on\"" \
| jq ".polling[1].characteristic |= \"rotationSpeed\"" \
| jq ".props.rotationSpeed.minStep |= ${minStep}" \
| jq ".state_cmd |= \"'${BONDBRIDGE_SH_PATH}'\"" \
| jq ".state_cmd_suffix |= \"fan 'token:${bondToken}' 'device:${device}' ${ip}\"" )
myPlaceAccessoriesBB=$( echo "${myPlaceAccessoriesBB}" | jq ".accessories += [${myPlaceFan}]" )
}
function myPlaceLightbulbNoDimmer()
{
local name="$1"
local accType="${2}"
myPlaceLightbulbNoDimmer="{}"
myPlaceLightbulbNoDimmer=$( echo "${myPlaceLightbulbNoDimmer}" | jq ".type |= \"Lightbulb\"" \
| jq ".displayName |= \"${name}\"" \
| jq ".on |= false" \
| jq ".name |= \"${name}\"" \
| jq ". += ${myPlaceModelQueue}" \
| jq ".polling[0].characteristic |= \"on\"" \
| jq ".state_cmd |= \"'${BONDBRIDGE_SH_PATH}'\"" \
| jq ".state_cmd_suffix |= \"${accType} 'token:${bondToken}' 'device:${device}' ${ip}\"" )
myPlaceAccessoriesBB=$( echo "${myPlaceAccessoriesBB}" | jq ".accessories += [${myPlaceLightbulbNoDimmer}]" )
}
function myPlaceLightbulbWithDimmer()
{
local name="$1"
local accType="${2}"
local minStep="${3}"
myPlaceLightbulbWithDimmer="{}"
myPlaceLightbulbWithDimmer=$( echo "${myPlaceLightbulbWithDimmer}" | jq ".type |= \"Lightbulb\"" \
| jq ".displayName |= \"${name}\"" \
| jq ".on |= false" \
| jq ".brightness |= 80" \
| jq ".name |= \"${name}\"" \
| jq ". += ${myPlaceModelQueue}" \
| jq ".polling[0].characteristic |= \"on\"" \
| jq ".polling[1].characteristic |= \"brightness\"" \
| jq ".props.brightness.minStep |= ${minStep}" \
| jq ".state_cmd |= \"'${BONDBRIDGE_SH_PATH}'\"" \
| jq ".state_cmd_suffix |= \"${accType} 'token:${bondToken}' 'device:${device}' ${ip}\"" )
myPlaceAccessoriesBB=$( echo "${myPlaceAccessoriesBB}" | jq ".accessories += [${myPlaceLightbulbWithDimmer}]" )
}
function myPlaceTimerLightbulb()
{
local name="$1"
local accType="$2"
local deviceType="$3"
myPlaceTimerLightbulb="{}"
myPlaceTimerLightbulb=$( echo "${myPlaceTimerLightbulb}" | jq ".type |= \"Lightbulb\"" \
| jq ".displayName |= \"${name}\"" \
| jq ".on |= false" \
| jq ".brightness |= 0" \
| jq ".name |= \"${name}\"" \
| jq ". += ${myPlaceModelQueue}" \
| jq ".polling[0].characteristic |= \"on\"" \
| jq ".polling[1].characteristic |= \"brightness\"" \
| jq ".props.brightness.minStep |= 1" \
| jq ".state_cmd |= \"'${BONDBRIDGE_SH_PATH}'\"" \
| jq ".state_cmd_suffix |= \"${accType} 'token:${bondToken}' 'device:${timerDevice}' '${deviceType}:${device}' ${ip}\"" )
myPlaceAccessoriesBB=$( echo "${myPlaceAccessoriesBB}" | jq ".accessories += [${myPlaceTimerLightbulb}]" )
}
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
config=$( jq '.' "${homebridgeConfigJson}" )
checkForPlatformMyPlaceInHomebridgeConfigJson
if [ -z "${validFile}" ]; then
echo "ERROR: no MyPlace Config found in \"${homebridgeConfigJson}\"! Please ensure that homebridge-myplace plugin is installed"
exit 1
fi
else
echo "ERROR: no Homebridge config.json found in \"${DIR}\"! Please copy \"${myPlaceConfigBB}\" to MyPlace JASON Config manually."
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 \"${myPlaceConfigBB}\" 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 ${myPlaceConfigBB} to Homebridge config.json was aborted"
echo ""
echo "${TLBL}${BOLD}INFO: Please copy/paste the ${myPlaceConfigBB} into MyPlace JASON Config Editor manually${TNRM}"
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
config=$( jq '.' "${homebridgeConfigJson}" )
checkForPlatformMyPlaceInHomebridgeConfigJson
if [ -z "${validFile}" ]; then
echo ""
echo "${TRED}ERROR: no MyPlace Config found in \"${homebridgeConfigJson}\"! Please ensure that homebridge-myplace plugin is installed${TNRM}"
echo "${TLBL}INFO: ${myPlaceConfigBB} was created but not copied to homebridge-myplace JASON Config Editor!"
echo " Please copy/paste the ${myPlaceConfigBB} into MyPlace JASON Config Editor manually${TNRM}"
exit 1
fi
fi
;;
esac
}
function extractBBmyPlaceConfigFromConfigJson()
{
# Extract MyPlace platform and create a config without MyPlace platforms
noOfPlatforms=$(( $( echo "${config}" | jq ".platforms|keys" | wc -w) - 2 ))
for ((i=0; i<noOfPlatforms; i++)); do
plaftorm=$( echo "${config}" | jq ".platforms[${i}].platform" )
if [ "${plaftorm}" = "\"${myPlacePlatform}\"" ]; then
configMyPlace=$( echo "${config}" | jq ".platforms[${i}]" )
configMyPlaceLess=$( echo "${config}" | jq "del(.platforms[${i}])" )
break
fi
done
# Now extract BB platform and create a config without BB and MyPlace platforms
noOfPlatforms=$(( $( echo "${configMyPlaceLess}" | jq ".platforms|keys" | wc -w) - 2 ))
for ((i=0; i<noOfPlatforms; i++)); do
plaftorm=$( echo "${configMyPlaceLess}" | jq ".platforms[${i}].platform" )
if [ "${plaftorm}" = "\"BondBridge\"" ]; then
configBB=$( echo "${configMyPlaceLess}" | jq ".platforms[${i}]" )
configBBmyPlaceLess=$( echo "${configMyPlaceLess}" | jq "del(.platforms[${i}])" )
break
fi
done
}
function extractMyPlaceConfigNonBBandAccessoriesNonBB()
{
myPlaceConfigNonBB="${configMyPlace}"
presenceOfAccessories=$( echo "${configMyPlace}" | jq ".accessories" )
if [ "${presenceOfAccessories}" != "null" ]; then
noOfAccessories=$(( $( echo "${configMyPlace}" | jq ".accessories|keys" | wc -w) - 2 ))
for (( i=0; i<noOfAccessories; i++ )); do
myPlaceStateCmd=$( echo "${configMyPlace}" | jq ".accessories[${i}].state_cmd" | grep -n "homebridge-bondbridge" )
# create the non-BB myPlaceConfigNonBB
if [ "${myPlaceStateCmd}" != "" ]; then
myPlaceConfigNonBB=$( echo "${myPlaceConfigNonBB}" | jq "del(.accessories[${i}])" )
else # create the non-BB accessories
accessoriesNonBB=$( echo "${configMyPlace}" | jq ".accessories[${i}]" )
myPlaceAccessoriesBBwithNonBB=$( echo "${myPlaceAccessoriesBBwithNonBB}" | jq ".accessories += [${accessoriesNonBB}]" )
fi
done
fi
}
function extractNonBBconstants()
{
noOfConstants=$(( $( echo "${myPlaceConfigNonBB}" | jq ".constants|keys" | wc -w) - 2 ))
for ((i=0; i<noOfConstants; i++)); do
key=$( echo "${myPlaceConfigNonBB}" | jq ".constants[${i}].key" )
key=${key//\"/}
keyUsed=$( echo "${accessoriesNonBB}" | grep -n "${key}" | grep -v 'key' | head -n 1 | cut -d":" -f1 )
if [ -n "${keyUsed}" ]; then
constantsNonBB=$( echo "${myPlaceConfigNonBB}" | jq ".constants[${i}]" )
myPlaceConstantsBBwithNonBB=$( echo "${myPlaceConstantsBBwithNonBB}" | jq ".constants += [${constantsNonBB}]" )
fi
done
}
function extractNonBBqueueTypes()
{
noOfQueues=$(( $( echo "${myPlaceConfigNonBB}" | jq ".queueTypes|keys" | wc -w) - 2 ))
for ((i=0; i<noOfQueues; i++)); do
queue=$( echo "${myPlaceConfigNonBB}" | jq ".queueTypes[${i}].queue" )
queueUsed=$( echo "${accessoriesNonBB}" | grep -n "${queue}" | head -n 1 )
if [ -n "${queueUsed}" ]; then
queueTypesNonBB=$( echo "${myPlaceConfigNonBB}" | jq ".queueTypes[${i}]" )
myPlaceQueueTypesBBwithNonBB=$( echo "${myPlaceQueueTypesBBwithNonBB}" | jq ".queueTypes += [${queueTypesNonBB}]" )
fi
done
}
function extractMyPlaceMiscKeys()
{
# Extract any misc MyPlace Keys used by non-BB accessories
myPlaceMiscKeys="${myPlaceConfigNonBB}"
keys=$( echo "${myPlaceConfigNonBB}" | jq ".|keys" )
noOfKeys=$(( $(echo "${keys}" | wc -w) - 2 ))
for ((i=0; i<noOfKeys; i++)); do
key=$( echo "${keys}" | jq ".[${i}]" )
key=${key//\"/}
if [[ "${key}" = "title" || "${key}" = "name" || "${key}" = "devices" || "${key}" = "platform" || "${key}" = "debug" || "${key}" = "outputConstants" || "${key}" = "statusMsg" || "${key}" = "timeout" || "${key}" = "stateChangeResponseTime" || "${key}" = "constants" || "${key}" = "queueTypes" || "${key}" = "accessories" ]]; then
myPlaceMiscKeys=$( echo "${myPlaceMiscKeys}" | jq "del(.${key})" )
fi
done
}
function extractNonBBaccessoriesConstantsQueueTypesMisc()
{
# this function should only be called after the BB constants, queueTypes and accessories are created
# extract non-BB config from ${configMyPlace} and concatenate non-BB accessories to BB accessories
myPlaceAccessoriesBBwithNonBB="${myPlaceAccessoriesBB}"
extractMyPlaceConfigNonBBandAccessoriesNonBB
# extract and concatenate the non-BB constants, non-BB queueTypes and non-BB related misc. keys if there are non-BB accessories
myPlaceConstantsBBwithNonBB="${myPlaceConstantsBB}"
myPlaceQueueTypesBBwithNonBB="${myPlaceQueueTypesBB}"
myPlaceMiscKeys="{}"
if [ -n "${accessoriesNonBB}" ]; then
extractNonBBconstants
extractNonBBqueueTypes
extractMyPlaceMiscKeys
fi
}
function extractMyPlaceDevices()
{
# Extract MyPlace devices (name and devices) information
myPlaceDevices="{}"
devices=$( echo "${myPlaceConfigNonBB}" | jq '.devices' )
if [ "${devices}" != "null" ]; then
myPlaceDevices=$( echo "${myPlaceDevices}" | jq ".devices += $devices" )
fi
}
function updateConfigBB()
{
# To update the parmaters of BB devices and is only call for nonUI case
# The parameters of BB devices are updated by config.schema for customUI case
# update device1
if [ "${CFtimerSetup1}" = "includeTimers" ]; then timerSetup=true; else timerSetup=false; fi
configBB=$( echo "${configBB}" | jq ".devices[0].ipAddress |= \"${BBIP1}\"" \
| jq ".devices[0].token |= \"${BBtoken1}\"" \
| jq ".devices[0].debug |= ${BBdebug1}" \
| jq ".devices[0].CFsettings.setupOption |= \"${CFsetupOption1}\"" \
| jq ".devices[0].CFsettings.timerSetup |= ${timerSetup}" )
# update/insert/remove device2
if [ "${CFtimerSetup2}" = "includeTimers" ]; then timerSetup=true; else timerSetup=false; fi
if [ -n "${BBIP2}" ]; then
configBB=$( echo "${configBB}" | jq ".devices[1].ipAddress |= \"${BBIP2}\"" \
| jq ".devices[1].token |= \"${BBtoken2}\"" \
| jq ".devices[1].debug |= ${BBdebug2}" \
| jq ".devices[1].CFsettings.setupOption |= \"${CFsetupOption2}\"" \
| jq ".devices[1].CFsettings.timerSetup |= ${timerSetup}" )
else # remove device2
configBB=$( echo "${configBB}" | jq "del(.devices[1])" )
fi
# update/insert device3
if [ "${CFtimerSetup3}" = "includeTimers" ]; then timerSetup=true; else timerSetup=false; fi
if [ -n "${BBIP3}" ]; then
configBB=$( echo "${configBB}" | jq ".devices[2].ipAddress |= \"${BBIP3}\"" \
| jq ".devices[2].token |= \"${BBtoken3}\"" \
| jq ".devices[2].debug |= ${BBdebug3}" \
| jq ".devices[2].CFsettings.setupOption |= \"${CFsetupOption3}\"" \
| jq ".devices[2].CFsettings.timerSetup |= ${timerSetup}" )
else # remove device2
configBB=$( echo "${configBB}" | jq "del(.devices[2])" )
fi
}
function getGlobalNodeModulesPathForFile()
{
file="$1"
fullPath=""
for ((tryIndex = 1; tryIndex <= 8; tryIndex ++)); do
case $tryIndex in
1)
foundPath=$(find /var/lib/hoobs 2>&1|grep -v find|grep -v System|grep -v cache|grep node_modules|grep bondbridge|grep "/${file}$")
fullPath=$(echo "${foundPath}"|head -n 1)
if [ -f "${fullPath}" ]; then
return
else
fullPath=""
fi
;;
2)
foundPath=$(npm root -g)
fullPath="${foundPath}/homebridge-bondbridge/${file}"
if [ -f "${fullPath}" ]; then
return
else
fullPath=""
fi
;;
3)
fullPath="/var/lib/homebridge/node_modules/homebridge-bondbridge/${file}"
if [ -f "${fullPath}" ]; then
return
else
fullPath=""
fi
;;
4)
fullPath="/var/lib/node_modules/homebridge-bondbridge/${file}"
if [ -f "${fullPath}" ]; then
return
else
fullPath=""
fi
;;
5)
fullPath="/usr/local/lib/node_modules/homebridge-bondbridge/${file}"
if [ -f "${fullPath}" ]; then
return
else
fullPath=""
fi
;;
6)
fullPath="/usr/lib/node_modules/homebridge-bondbridge/${file}"
if [ -f "${fullPath}" ]; then
return
else
fullPath=""
fi
;;
7)
fullPath="/opt/homebrew/lib/node_modules/homebridge-bondbridge/${file}"
if [ -f "${fullPath}" ]; then
return
else
fullPath=""
fi
;;
8)
fullPath="/opt/homebridge/lib/node_modules/homebridge-bondbridge/${file}"
if [ -f "${fullPath}" ]; then
return
else
fullPath=""
fi
;;
esac
done
}
function getHomebridgeConfigJsonPath()
{
fullPath=""
# Typicall HOOBS installation has its config.json root path same as the root path of "BondBridge.sh"
# The typical full path to the "BondBridge.sh" script is .../hoobs/<bridge>/node_modules/homebridge-bondbridge/BondBridge.sh
# First, determine whether this is a HOOBS installation
Hoobs=$( echo "$BONDBRIDGE_SH_PATH" | grep "/hoobs/" )
if [ -n "${Hoobs}" ]; then
fullPath="${BONDBRIDGE_SH_PATH%/*/*/*}/config.json"
if [ -f "${fullPath}" ]; then
checkForMyPlacePlatformNameInFile
if [ -z "${myPlacePlatformFound}" ]; then
fullPath=""
fi
return
fi
fi
for ((tryIndex = 1; tryIndex <= 6; tryIndex ++)); do
case $tryIndex in
1)
# Typical RPi, Synology NAS installations have this path to config.json
fullPath="/var/lib/homebridge/config.json"
if [ -f "${fullPath}" ]; then
checkForMyPlacePlatformNameInFile
if [ -n "${myPlacePlatformFound}" ]; then
return
else
fullPath=""
fi
fi
;;
2)
# Typical Mac installation has this path to config.json
fullPath="$HOME/.homebridge/config.json"
if [ -f "${fullPath}" ]; then
checkForMyPlacePlatformNameInFile
if [ -n "${myPlacePlatformFound}" ]; then
return
else
fullPath=""
fi
fi
;;
3)
foundPath=$(find /usr/local/lib 2>&1|grep -v find|grep -v System|grep -v cache|grep -v hassio|grep -v node_modules|grep "/config.json$")
noOfInstances=$(echo "${foundPath}"|wc -l)
for ((i = 1; i <= noOfInstances; i ++)); do
fullPath=$(echo "${foundPath}"|sed -n "${i}"p)
if [ -f "${fullPath}" ]; then
checkForMyPlacePlatformNameInFile
if [ -n "${myPlacePlatformFound}" ]; then
return
else
fullPath=""
fi
fi
done
;;
4)
foundPath=$(find /usr/lib 2>&1|grep -v find|grep -v System|grep -v cache|grep -v hassio|grep -v node_modules|grep "/config.json$")
noOfInstances=$(echo "${foundPath}"|wc -l)
for ((i = 1; i <= noOfInstances; i ++)); do
fullPath=$(echo "${foundPath}"|sed -n "${i}"p)
if [ -f "${fullPath}" ]; then
checkForMyPlacePlatformNameInFile
if [ -n "${myPlacePlatformFound}" ]; then
return
else
fullPath=""
fi
fi
done
;;
5)
foundPath=$(find /var/lib 2>&1|grep -v find|grep -v hoobs|grep -v System|grep -v cache|grep -v hassio|grep -v node_modules|grep "/config.json$")
noOfInstances=$(echo "${foundPath}"|wc -l)
for ((i = 1; i <= noOfInstances; i ++)); do
fullPath=$(echo "${foundPath}"|sed -n "${i}"p)
if [ -f "${fullPath}" ]; then
checkForMyPlacePlatformNameInFile
if [ -n "${myPlacePlatformFound}" ]; then
return
else
fullPath=""
fi
fi
done
;;
6)
foundPath=$(find /opt 2>&1|grep -v find|grep -v hoobs|grep -v System|grep -v cache|grep -v hassio|grep -v node_modules|grep "/config.json$")
noOfInstances=$(echo "${foundPath}"|wc -l)
for ((i = 1; i <= noOfInstances; i ++)); do
fullPath=$(echo "${foundPath}"|sed -n "${i}"p)
if [ -f "${fullPath}" ]; then
checkForMyPlacePlatformNameInFile
if [ -n "${myPlacePlatformFound}" ]; then
return
else
fullPath=""
fi
fi
done
;;
esac
done
}
function checkForPlatformMyPlaceInHomebridgeConfigJson()
{
validFile=""
for ((tryIndex = 1; tryIndex <= 2; tryIndex ++)); do
case $tryIndex in
1)
validFile=$( echo "${config}" | grep "\"${myPlacePlatform1}\"" )
if [ -n "${validFile}" ]; then
myPlacePlatform="${myPlacePlatform1}"
return
fi
;;
2)
validFile=$( echo "${config}" | grep "\"${myPlacePlatform2}\"" )
if [ -n "${validFile}" ]; then
myPlacePlatform="${myPlacePlatform2}"
return
fi
;;
esac
done
}
function checkForMyPlacePlatformNameInFile()
{
myPlacePlatformFound=""
for ((Index = 1; Index <= 2; Index ++)); do
case $Index in
1)
myPlacePlatformFound=$( grep "\"${myPlacePlatform1}\"" "${fullPath}" )
if [ -n "${myPlacePlatformFound}" ]; then
return
fi
;;
2)
myPlacePlatformFound=$( grep -n "\"${myPlacePlatform2}\"" "${fullPath}" )
if [ -n "${myPlacePlatformFound}" ]; then
return
fi
;;
esac
done
}
function assembleMyPlaceConfigBB()
{
myPlaceConfigBB="{}"
myPlaceConfigBB=$( echo "${myPlaceConfigBB}" | jq ".name += \"MyPlace\"" \
| jq ". += ${myPlaceDevices}" \
| jq ". += ${myPlaceBasicKeys}" \
| jq ". += ${myPlaceConstantsBB}" \
| jq ". += ${myPlaceQueueTypesBB}" \
| jq ". += ${myPlaceAccessoriesBB}" \
| jq ".platform += \"MyPlace\"" )
}
function assembleMyPlaceConfigBBwithNonBB()
{
myPlaceConfigBBwithNonBB="{}"
myPlaceConfigBBwithNonBB=$( echo "${myPlaceConfigBBwithNonBB}" | jq ".name += \"MyPlace\"" \
| jq ". += ${myPlaceDevices}" \
| jq ". += ${myPlaceBasicKeys}" \
| jq ". += ${myPlaceConstantsBBwithNonBB}" \
| jq ". += ${myPlaceQueueTypesBBwithNonBB}" \
| jq ". += ${myPlaceAccessoriesBBwithNonBB}" \
| jq ". += ${myPlaceMiscKeys}" \
| jq ".platform += \"MyPlace\"" )
}
function writeToHomebridgeConfigJson()
{
# Writing the "${configBB}" and created "${myPlaceConfigBBwithNonBB}" to "${configBBmyPlaceLess}" to create "${configNew}"
# before copying to Homebridge config.json
configNew=$( echo "${configBBmyPlaceLess}" | jq ".platforms += [$configBB]" \
| jq ".platforms += [$myPlaceConfigBBwithNonBB]" )
rc=$?
if [ "${rc}" != "0" ]; then
echo "${TRED}${BOLD}ERROR: Writing of created MyPlace config to config.json.new failed!${TNRM}"
echo "${TLBL}${BOLD}INFO: Instead you can copy/paste the content of \"${myPlaceConfigBB}\" into MyPlace JASON Config editor.${TNRM}"
exit 1
fi
# Copy the "${configNew}" to Homebridge config.json
fname=$( find "${homebridgeConfigJson}" -user "$USER" );
if [ "${fname}" = "${homebridgeConfigJson}" ]; then
echo "${configNew}" | tee "${homebridgeConfigJson}" > /dev/null
rc=$?
else
echo "${configNew}" | sudo tee "${homebridgeConfigJson}" > /dev/null
rc=$?
fi
}
# main starts here
if [ -z "${BONDBRIDGE_SH_PATH}" ]; then UIversion="nonUI"; fi
case $UIversion in
customUI )
if expr "${BBIP1}" : '[0-9]*\.[0-9]*\.[0-9]*\.[0-9]*$' >/dev/null; then
echo ""
else
echo "WARNING: the specified IP address ${BBIP1} is in wrong format"
exit 1
fi
if [[ -n "${BBIP2}" && "${BBIP2}" != "undefined" ]]; then
if expr "${BBIP2}" : '[0-9]*\.[0-9]*\.[0-9]*\.[0-9]*$' >/dev/null; then
echo ""
else
echo "WARNING: the specified IP address ${BBIP2} is in wrong format"
exit 1
fi
else
BBIP2=""
BBtoken2=""
fi
if [[ -n "${BBIP3}" && "${BBIP3}" != "undefined" ]]; then
if expr "$5" : '[0-9]*\.[0-9]*\.[0-9]*\.[0-9]*$' >/dev/null; then
echo ""
else
echo "WARNING: the specified IP address ${BBIP3} is in wrong format"
exit 1
fi
else
BBIP3=""
BBtoken3=""
fi
;;
nonUI )
BBIP1=""
BBIP2=""
BBIP3=""
until [ -n "${BBIP1}" ]; do
echo "${TYEL}Please enter the IP address and the token of your BondBridge device:"
read -r -p "${TYEL}IP address (xxx.xxx.xxx.xxx): ${TNRM}" INPUT
if expr "${INPUT}" : '[0-9]*\.[0-9]*\.[0-9]*\.[0-9]*$' >/dev/null; then
BBIP1="${INPUT}"
read -r -p "${TYEL}Token of this device (this can be found in Bond Settings of Bond app): ${TNRM}" INPUT
BBtoken1="${INPUT}"
BBdebug1="false"
echo "${TYEL}"
echo "${BOLD}Zone Control Setup Options:${TNRM}"
echo "${TYEL}1. Configure only a Ceiling Fan with SpeedControl"
echo "2. Configure a Ceiling fan with SpeedControl and a Light Switch"
echo "3. Configure a Ceiling Fan with SpeedControl and a Light with Dimmer"
echo "4. Configure only a Light Dimmer"
echo "5. Do not configure this Ceiling Fan"
echo ""
CFsetupOption1=""
until [ -n "${CFsetupOption1}" ]; do
read -r -p "${TYEL}Select Ceiling Fan setup options (1, 2, 3, 4 or 5, default=5):${TNRM} " INPUT
if [ "${INPUT}" = "1" ]; then CFsetupOption1="fan"
elif [ "${INPUT}" = "2" ]; then CFsetupOption1="fanLight"
elif [ "${INPUT}" = "3" ]; then CFsetupOption1="fanLightDimmer"
elif [ "${INPUT}" = "4" ]; then CFsetupOption1="lightDimmer"
elif [[ "${INPUT}" = "5" || "${INPUT}" = "" ]]; then CFsetupOption1="doNotConfigure"
else
echo ""
echo "${TPUR}WARNING: Invalid option selected. Try again!${TNRM}"
echo ""
fi
done
CFtimerSetup1="noTimers"
read -r -p "${TYEL}Include a Light and a Fan timer? (y/n, default=n): ${TNRM}" INPUT
if [[ "${INPUT}" = "y" || "${INPUT}" = "Y" || "${INPUT}" = "true" ]]; then CFtimerSetup1="includeTimers"; fi
read -r -p "${TYEL}Enable debug? (y/n, default=n): ${TNRM}" INPUT
if [[ "${INPUT}" = "y" || "${INPUT}" = "Y" || "${INPUT}" = "true" ]]; then BBdebug1="true"; fi
else
echo ""
echo "${TPUR}WARNING: Wrong format for an IP address! Please enter again!${TNRM}"
echo ""
fi
done
until [ -n "${BBIP2}" ]; do
echo ""
echo "${TYEL}Please enter the IP address and the token of your 2nd BondBridge device if any. Just hit 'enter' if none:"
read -r -p "${TYEL}IP address (xxx.xxx.xxx.xxx): ${TNRM}" INPUT
if [ -z "${INPUT}" ]; then
break
fi
if expr "${INPUT}" : '[0-9]*\.[0-9]*\.[0-9]*\.[0-9]*$' >/dev/null; then
BBIP2="${INPUT}"
read -r -p "${TYEL}Token of this device (this can be found in Bond Settings of Bond app): ${TNRM}" INPUT
BBtoken2="${INPUT}"
BBdebug2="false"
CFsetupOption2=""
until [ -n "${CFsetupOption2}" ]; do
read -r -p "${TYEL}Select Ceiling Fan setup options (1, 2, 3, 4 or 5, default=5):${TNRM} " INPUT
if [ "${INPUT}" = "1" ]; then CFsetupOption2="fan"
elif [ "${INPUT}" = "2" ]; then CFsetupOption2="fanLight"
elif [ "${INPUT}" = "3" ]; then CFsetupOption2="fanLightDimmer"
elif [ "${INPUT}" = "4" ]; then CFsetupOption2="lightDimmer"
elif [[ "${INPUT}" = "5" || "${INPUT}" = "" ]]; then CFsetupOption2="doNotConfigure"
else
echo ""
echo "${TPUR}WARNING: Invalid option selected. Try again!${TNRM}"
echo ""
fi
done
CFtimerSetup2="noTimers"
read -r -p "${TYEL}Include a Light and a Fan timer? (y/n, default=n): ${TNRM}" INPUT
if [[ "${INPUT}" = "y" || "${INPUT}" = "Y" || "${INPUT}" = "true" ]]; then CFtimerSetup2="includeTimers"; fi
read -r -p "${TYEL}Enable debug? (y/n, default=n): ${TNRM}" INPUT
if [[ "${INPUT}" = "y" || "${INPUT}" = "Y" || "${INPUT}" = "true" ]]; then BBdebug2="true"; fi
else
echo ""
echo "${TPUR}WARNING: Wrong format for an IP address! Please enter again!${TNRM}"
echo ""
fi
done
if [ -n "${BBIP2}" ]; then
until [ -n "${BBIP3}" ]; do
echo ""
echo "${TYEL}Please enter the IP address and the token of your 2nd BondBridge device if any. Just hit 'enter' if none:"
read -r -p "${TYEL}IP address (xxx.xxx.xxx.xxx): ${TNRM}" INPUT
if [ -z "${INPUT}" ]; then
break
fi
if expr "${INPUT}" : '[0-9]*\.[0-9]*\.[0-9]*\.[0-9]*$' >/dev/null; then
BBIP3="${INPUT}"
read -r -p "${TYEL}Token of this device (this can be found in Bond Settings of Bond app): ${TNRM}" INPUT
BBtoken3="${INPUT}"
BBdebug3="false"
CFsetupOption3=""
until [ -n "${CFsetupOption3}" ]; do
read -r -p "${TYEL}Select Ceiling Fan setup options (1, 2, 3, 4 or 5, default=5):${TNRM} " INPUT
if [ "${INPUT}" = "1" ]; then CFsetupOption3="fan"
elif [ "${INPUT}" = "2" ]; then CFsetupOption3="fanLight"
elif [ "${INPUT}" = "3" ]; then CFsetupOption3="fanLightDimmer"
elif [ "${INPUT}" = "4" ]; then CFsetupOption3="lightDimmer"
elif [[ "${INPUT}" = "5" || "${INPUT}" = "" ]]; then CFsetupOption3="doNotConfigure"
else
echo ""
echo "${TPUR}WARNING: Invalid option selected. Try again!${TNRM}"
echo ""
fi
done
CFtimerSetup3="noTimers"
read -r -p "${TYEL}Include a Light and a Fan timer? (y/n, default=n): ${TNRM}" INPUT
if [[ "${INPUT}" = "y" || "${INPUT}" = "Y" || "${INPUT}" = "true" ]]; then CFtimerSetup3="includeTimers"; fi
read -r -p "${TYEL}Enable debug? (y/n, default=n): ${TNRM}" INPUT
if [[ "${INPUT}" = "y" || "${INPUT}" = "Y" || "${INPUT}" = "true" ]]; then BBdebug3="true"; fi
else
echo ""
echo "${TNRM}${TPUR}WARNING: Wrong format for an IP address! Please enter again!${TNRM}"
echo ""
fi
done
fi
echo ""
echo "${TLBL}INFO: (1) ip1:${BBIP1}, token1:${BBtoken1}, ${CFsetupOption1}, ${CFtimerSetup1}${TNRM}"
if [ -n "${BBIP2}" ]; then echo "${TLBL}INFO: (2) ip2:${BBIP2}, token2:${BBtoken2}, ${CFsetupOption2}, ${CFtimerSetup2}${TNRM}"; fi
if [ -n "${BBIP3}" ]; then echo "${TLBL}INFO: (3) ip3:${BBIP3}, token3:${BBtoken3}, ${CFsetupOption3}, ${CFtimerSetup3}${TNRM}"; fi
echo ""
# get the full path to BondBridge.sh
BONDBRIDGE_SH_PATH=""
getGlobalNodeModulesPathForFile "BondBridge.sh"
if [ -n "${fullPath}" ]; then
BONDBRIDGE_SH_PATH=${fullPath}
echo "${TLBL}INFO: BondBridge.sh found: ${BONDBRIDGE_SH_PATH}${TNRM}"
fi
if [ -z "${BONDBRIDGE_SH_PATH}" ]; then
BONDBRIDGE_SH_PATH=""
until [ -n "${BONDBRIDGE_SH_PATH}" ]; do
echo ""
echo "${TYEL}Please enter the full path of where the BondBridge.sh is installed in your system"
echo "The file path format should be : /*/*/*/node_modules/homebridge-bondbridge/BondBridge.sh${TNRM}"
read -r -p "${BOLD}> ${TNRM}" INPUT
if expr "${INPUT}" : '/[a-zA-Z0-9/_]*/node_modules/homebridge-bondbridge/BondBridge.sh$' >/dev/null; then
if [ -f "${INPUT}" ]; then
BONDBRIDGE_SH_PATH=${INPUT}
echo ""
echo "${TLBL}INFO: BondBridge.sh specified: ${BONDBRIDGE_SH_PATH}${TNRM}"
break
else
echo ""
echo "${TPUR}WARNING: file ${INPUT} not found${TNRM}"
fi
else
echo ""
echo "${TPUR}WARNING: file ${INPUT} is in wrong format${TNRM}"
fi
done
fi
;;
esac
noOfBondBridges=1
if [ -n "${BBIP2}" ]; then noOfBondBridges=2; fi
if [ -n "${BBIP3}" ]; then noOfBondBridges=3; fi
for ((n=1; n<=noOfBondBridges; n++)); do
if [ "${n}" = "1" ]; then
ip="\${BBIP1}"
IPA="${BBIP1}"
bondToken="${BBtoken1}"
CFsetupOption="${CFsetupOption1}"
CFtimerSetup="${CFtimerSetup1}"
debug="${BBdebug1}"
queue="BBA"
fi
if [ "${n}" = "2" ]; then
ip="\${BBIP2}"
IPA="${BBIP2}"
bondToken="${BBtoken2}"
CFsetupOption="${CFsetupOption2}"
CFtimerSetup="${CFtimerSetup2}"
debug="${BBdebug2}"
queue="BBB"
fi
if [ "${n}" = "3" ]; then
ip="\${BBIP3}"
IPA="${BBIP3}"
bondToken="${BBtoken3}"
CFsetupOption="${CFsetupOption3}"
CFtimerSetup="${CFtimerSetup3}"
debug="${BBdebug3}"
queue="BBC"
fi
if [[ "${n}" = "1" && "${UIversion}" = "nonUI" ]]; then
echo ""
if [ "${noOfBondBridges}" = "1" ]; then echo "${TLBL}${BOLD}INFO: This will take up to 1 minute to process!${TNRM}"; fi
if [ "${noOfBondBridges}" = "2" ]; then echo "${TLBL}${BOLD}INFO: This will take up to 2 minutes to process!${TNRM}"; fi
if [ "${noOfBondBridges}" = "3" ]; then echo "${TLBL}${BOLD}INFO: This will take up to 3 minutes to process!${TNRM}"; fi
fi
if [ "${UIversion}" = "nonUI" ]; then
echo "${TLBL}INFO: Fetching and processing data from your BondBridge device (${bondToken} ${IPA}).... ${TNRM}"
fi
# retrieve the Bond Bridge system info and get the bondid
version=$(curl -s -g -H -i http://"${IPA}"/v2/sys/version)
if [ -z "${version}" ]; then
echo "${TRED}ERROR: BondBridge device is inaccessible or your IP address ${IPA} is invalid!${TNRM}"
exit 1
fi
bondid=$(echo "${version}" | jq ".bondid")
bondid=${bondid//\"/}
if [ -z "${bondid}" ]; then
echo "${TRED}ERROR: jq failed! Please make sure that jq is installed!${TNRM}"
exit 1
fi
model=$(echo "${version}" | jq ".model")
model=${model//\"/}
# Read the existing Homebridge config.json file
readHomebridgeConfigJson
# Extract BB plaform and MyPlace platform
extractBBmyPlaceConfigFromConfigJson
# Create the ${myPlaceBasicKeys}
# First check whether the original MyPlace platform has its "debug=true", if so leave that as "true"
debugPresence=$( echo "${configMyPlace}" | jq ".constants" | grep ":2025-debug" )
debugMyPlace=false
if [ -n "${debugPresence}" ]; then
debugMyPlace=true
else
if [[ "${BBdebug1}" = "true" || "${BBdebug2}" = "true" ||"${BBdebug3}" = "true" ]]; then
debugMyPlace=true
fi
fi
myPlaceBasicKeys
# Create the ${myPlaceConstantsBB} and ${myPlaceQueueTypesBB}
if [ "${CFsetupOption}" != "doNotConfigure" ]; then
myPlaceConstants
myPlaceQueueTypes
fi
# Create the myPlaceModelQueue for this BB system
myPlaceModelQueue
# Create the $myPlaceConfigAccessories
# first retireve the info of all the defined devices
Devices=$(curl -s -g -H "BOND-Token: ${bondToken}" http://"${IPA}"/v2/devices)
devices=$(echo "$Devices" | jq '. | keys')
noOfDevices=$(($(echo "${devices}" | wc -w) - 2))
# Now create the config for the devices
for (( i=0;i<noOfDevices;i++ )); do
device=$(echo "${devices}"|jq ".[$i]")
if expr "${device}" : '^\"[a-f0-9]*\"$' >/dev/null; then
device=${device//\"/}
timerDevice=$(echo "${device}" | rev)
max_speed=$(curl -s -g -H "BOND-Token: ${bondToken}" http://"${IPA}"/v2/devices/"${device}"/properties | jq ".max_speed")
speed_interval=$((100 / max_speed))
name=$(curl -s -g -H "BOND-Token: ${bondToken}" http://"${IPA}"/v2/devices/"${device}" | jq ".name")
name=${name//\"/}
if [ "${CFsetupOption}" != "doNotConfigure" ]; then
if expr "${name}" : '[a-zA-Z0-9 ]*Fan$' >/dev/null; then