Skip to content

Instantly share code, notes, and snippets.

@highgain86j
Last active June 21, 2018 14:17
Show Gist options
  • Save highgain86j/29812a3bf20d7cf79c9cc02229d537fc to your computer and use it in GitHub Desktop.
Save highgain86j/29812a3bf20d7cf79c9cc02229d537fc to your computer and use it in GitHub Desktop.
homebridge_install.bash
#!/bin/bash
RUN_SETUP=false
NIC=wlan0
GPIO=(
"name momentary/toggle"
"disabled"
"Dinner-Bell momentary"
"disabled"
"disabled"
"disabled"
"disabled"
"disabled"
"disabled"
"disabled"
"disabled"
"disabled"
"disabled"
"disabled"
"disabled"
"disabled"
"disabled"
"disabled"
"disabled"
"disabled"
"disabled"
"disabled"
"disabled"
"disabled"
"disabled"
"disabled"
"disabled"
"disabled"
)
function install_homebridge(){
local confdir="~/.homebridge"
sudo apt-get update
sudo apt-get -y install libavahi-compat-libdnssd-dev npm
sudo apt-get install jq -y
sudo npm cache clean -f
sudo npm install -g n
sudo n latest
sudo npm install -g --unsafe-perm homebridge
sudo npm install -g --unsafe-perm homebridge-cmdswitch2
if [ ! -e ${confdir} ]; then
mkdir ${confdir}
fi
}
function generate_configuration(){
local mac_nic="`ifconfig ${NIC} | grep ether | awk '{print $2}'`"
local device_name
local count
local arr=()
local on_cmd
local off_cmd
local sw_type
local print_comma=false
mac_nic="${mac_nic^^}"
echo -e "\
{\n\
\"bridge\": {\n\
\"name\": \"Homebridge\",\n\
\"username\": \"${mac_nic}\",\n\
\"port\": 51826,\n\
\"pin\": \"031-45-154\"\n\
},\n\
\"description\": \"homebridge\",\n\
\"platforms\": [\n\
{\n\
\"platform\": \"cmdSwitch2\",\n\
\"name\": \"CMD Switch\",\n\
\"switches\": ["
for (( count=1 ; count<${#GPIO[@]}; count++ )) ; do
gpio -g mode ${count} out
gpio -g write ${count} 0
if [ "${GPIO[${count}]}" != "disabled" ]; then
if ${print_comma}; then
echo -e ","
fi
print_comma=true
arr=(${GPIO[${count}]})
device_name=${arr[0]}
sw_type=${arr[1]}
if [ "${sw_type}" == "momentary" ]; then
on_cmd="gpio -g write ${count} 1; sleep 0.5; gpio -g write ${count} 0"
fi
if [ "${sw_type}" == "toggle" ]; then
on_cmd="gpio -g write ${count} 1"
fi
echo -e "\
{\n\
\"name\" : \"${device_name}\",\n\
\"polling\": true,\n\
\"on_cmd\": \"${on_cmd}\",\n\
\"off_cmd\": \"gpio -g write ${count} 0\",\n\
\"state_cmd\": \"gpio -g read ${count} | grep -v 0\"\n\
}"
fi
done
echo -e "\
]\n\
}\n\
]"
echo -e "}"
# | sudo tee ~/.homebridge/config.json
#sudo nano ~/.homebridge/config.json
}
function generate_systemd_entry(){
echo -e "\
[Unit]\n\
Description=run homebridge\n\
After=syslog.target network-online.target\n\
\n\
[Service]\n\
Type=simple\n\
User=pi\n\
ExecStart=/usr/local/bin/homebridge\n\
Restart=on-failure\n\
RestartSec=10\n\
KillMode=process\n\
\n\
[Install]\n\
WantedBy=multi-user.target\n\
" | sudo tee /etc/systemd/system/homebridge.service
sudo nano /etc/systemd/system/homebridge.service
sudo systemctl daemon-reload
sudo systemctl start homebridge
sudo systemctl status homebridge
sudo systemctl enable homebridge
}
generate_configuration
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment