-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathha_control
executable file
·170 lines (155 loc) · 3.93 KB
/
ha_control
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
#!/bin/bash
# credit: adapted from Qmail 'qmailctl' script
# assumes Home Assistant is using 'systemd' setup per https://home-assistant.io/docs/autostart/systemd/
##### configuration #####
sc=/bin/systemctl
jc=/bin/journalctl
ha_user=homeassistant
ha_home=/home/homeassistant
ozw_log=$ha_home/.homeassistant/OZW_Log.txt
##### configuration #####
##### functions #####
function ha_status {
$sc status $ha
}
function ha_stop {
echo Stopping home assistant
$sc stop $ha
}
function ha_start {
echo Starting home assistant
$sc start $ha
}
function ha_restart {
echo Restarting home assistant
ha_stop
ha_start
}
function ha_disable {
echo Disabling automatic start at boot
$sc disable $ha
}
function ha_enable {
echo Enabling automatic start at boot
$sc enable $ha
}
function ha_logs {
echo Viewing latest log entries:
$jc -n 25 -u $ha
}
function ha_errors {
echo View latest errors only in log entries:
$jc -u $ha | grep -i error
}
function ha_backup {
echo Back up configuration
# must make a copy before tar otherwise HA may change file during the process
cp -R $ha_home/.homeassistant $ha_home/config_temp
timestamp=`date '+%Y%m%d_%H%M_%S'`
tar -zcf $ha_home/ha_config_$timestamp.tar.gz $ha_home/config_temp
rm -R $ha_home/config_temp
}
function ha_upgrade {
# https://www.home-assistant.io/docs/installation/raspberry-pi/
echo Upgrade to latest Home Assistant
ha_stop
echo su to user $ha_user for upgrades
sudo -u $ha_user ./ha_upgrade
echo Done as user $ha_user
ha_start
echo Upgrade process complete
}
function update_ssl {
echo update SSL certs from Lets Encrypt
/usr/bin/certbot renew --quiet --no-self-upgrade --standalone --preferred-challenges http-01
echo if certbot failed, likely need to temporarily open router firewall for port 80
cp /etc/letsencrypt/live/mroczek689154.duckdns.org/privkey.pem /home/homeassistant/ssl/privkey.pem
cp /etc/letsencrypt/live/mroczek689154.duckdns.org/fullchain.pem /home/homeassistant/ssl/fullchain.pem
echo give Home Assistant permissions
chown $ha_user:$ha_user /home/homeassistant/ssl/*.pem
echo ssl certificate update is complete
}
function check_systemd {
##### check that systemd is on the system #####
systemd_expected=systemd
systemd_result=`ps -p 1 -o comm=`
if [ "$systemd_expected" != "$systemd_result" ]; then
echo System does not appear to be running "systemd"
echo See https://home-assistant.io/docs/autostart/systemd/ for setup instructions
exit 1
fi
}
function check_root {
##### check if running as root #####
# credit: https://askubuntu.com/questions/15853/how-can-a-script-check-if-its-being-run-as-root
if [[ $EUID -ne 0 ]]; then
echo "This script must be run as root"
exit 1
fi
}
##### end functions #####
######################
##### MAIN EVENT #####
######################
check_systemd
check_root
##### respond to the requested function #####
case "$1" in
status)
ha_status
;;
start)
ha_start
;;
stop)
ha_stop
;;
restart)
ha_restart
;;
disable)
ha_disable
;;
enable)
ha_enable
;;
logs)
ha_logs
;;
errors)
ha_errors
;;
backup)
ha_backup
;;
upgrade)
ha_upgrade
;;
ssl)
update_ssl
;;
help)
cat <<HELP
This script simplifies control of Home Assistant when running under "systemd".
Commands:
status - displays status of Home Assistant daemon
start - start Home Assistant daemon
stop - stop Home Assistant daemon
restart - restart the Home Assistant daemon
enable - enables automatic start at boot
disable - disables automatic start at boot
logs - view latest log entries
errors - view only errors in logs
backup - back up all configuration files in a tar.gz file
upgrade - upgrade to the latest release
ozw - save Open Z-wave log file with timestamp
ssl - update the SSL certificate
HELP
;;
*)
echo Command not recognized. Usage: $0 help
exit 1
;;
esac
exit 0