-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinstall_zigbee2mqtt.sh
executable file
·90 lines (73 loc) · 3.21 KB
/
install_zigbee2mqtt.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
#!/usr/bin/bash
set -euo pipefail
# See https://www.zigbee2mqtt.io/guide/installation/01_linux.html#installing
Z2M_INSTALL_PATH="$1"
Z2M2W_RUN_PATH="$2"
Z2M_RUN_PATH="$Z2M2W_RUN_PATH/zigbee2mqtt"
SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
echo "systemctl status zigbee2mqtt.service" > "$Z2M2W_RUN_PATH/zigbee2mqtt_active.sh"
echo "sudo journalctl --follow --unit zigbee2mqtt" > "$Z2M2W_RUN_PATH/zigbee2mqtt_logs.sh"
echo "sudo systemctl restart zigbee2mqtt.service" > "$Z2M2W_RUN_PATH/zigbee2mqtt_restart.sh"
chmod +x "$Z2M2W_RUN_PATH/zigbee2mqtt_active.sh"
chmod +x "$Z2M2W_RUN_PATH/zigbee2mqtt_logs.sh"
chmod +x "$Z2M2W_RUN_PATH/zigbee2mqtt_restart.sh"
systemctl is-active --quiet zigbee2mqtt.service && echo "Zigbee2Mqtt is running!" && exit 0
sudo apt-get install --assume-yes nodejs git make g++ gcc > /dev/null
# With set -e, this will terminate the script if node or npm are not present
node --version > /dev/null
npm --version > /dev/null
mkdir -p "$Z2M_RUN_PATH"
if [[ -d "$Z2M_INSTALL_PATH" ]]; then
echo "Not clonning Zigbee2Mqtt: already exists at $Z2M_INSTALL_PATH. Remove it if you want to reinstall"
else
mkdir "$Z2M_INSTALL_PATH"
pushd "$Z2M_INSTALL_PATH"
git clone --depth 1 https://github.com/Koenkk/zigbee2mqtt.git .
npm ci
npm run build
popd
fi
ZIGBEE_ADAPTER_PATH=/dev/ttyUSB0
if [ ! -e "$ZIGBEE_ADAPTER_PATH" ]; then
echo "Can't find Zigbee adapter at $ZIGBEE_ADAPTER_PATH, trying alternate path"
ZIGBEE_ADAPTER_PATH=/dev/ttyACM0
fi
if [ ! -e "$ZIGBEE_ADAPTER_PATH" ]; then
echo "Can't find Zigbee adapter at $ZIGBEE_ADAPTER_PATH and out of guesses"
exit 1
fi
echo "Assuming Zigbee adapter lives at $ZIGBEE_ADAPTER_PATH"
echo "If this is wrong, correct it in $Z2M_RUN_PATH/configuration.yaml"
if ! test -w "$ZIGBEE_ADAPTER_PATH"; then
ADAPTER_GROUP=$( ls -lha /dev/ttyACM0 | awk '{print $4}' )
echo -e "\033[0;31m"
echo "Can't write to $ZIGBEE_ADAPTER_PATH, will try to fix permissions by adding $(whoami) to group $ADAPTER_GROUP"
echo "This may or may not fix the permissions, but there is no way to know without logging out and in to refresh groups"
echo -e "\033[0m"
sudo usermod -a -G "$ADAPTER_GROUP" "$(whoami)"
fi
# Configure, install as service
cat "$SCRIPT_DIR/zigbee2mqtt.conf.template" | \
sed "s|#ZIGBEE_ADAPTER_PATH#|$ZIGBEE_ADAPTER_PATH|g" | \
sed "s|#Z2M_RUN_PATH#|$Z2M_RUN_PATH|g" | \
sed "s|#Z2M_INSTALL_PATH#|$Z2M_INSTALL_PATH|g" | \
sed "s|#RUN_USER#|$(whoami)|g" | \
tee >/dev/null "$Z2M_RUN_PATH/configuration.yaml"
cat "$SCRIPT_DIR/zigbee2mqtt.service.template" | \
sed "s|#ZIGBEE_ADAPTER_PATH#|$ZIGBEE_ADAPTER_PATH|g" | \
sed "s|#Z2M_RUN_PATH#|$Z2M_RUN_PATH|g" | \
sed "s|#Z2M_INSTALL_PATH#|$Z2M_INSTALL_PATH|g" | \
sed "s|#RUN_USER#|$(whoami)|g" | \
sudo tee >/dev/null ./zigbee2mqtt.service
sudo mv ./zigbee2mqtt.service /etc/systemd/system/zigbee2mqtt.service
sudo systemctl stop zigbee2mqtt.service | true > /dev/null
sudo systemctl daemon-reload
sudo systemctl enable zigbee2mqtt.service
sudo systemctl start zigbee2mqtt.service
if ! systemctl is-active --quiet zigbee2mqtt.service ; then
echo -e "\033[0;31m"
echo "Failed to install zigbee2mqtt"
echo -e "\033[0m"
sudo journalctl --unit zigbee2mqtt.service
exit 1
fi