-
Notifications
You must be signed in to change notification settings - Fork 1
/
RPI4-UPSplus-HA-Prep.sh
391 lines (363 loc) · 12.9 KB
/
RPI4-UPSplus-HA-Prep.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
#!/bin/bash
############################################################
## Dedicated Script to Community GeekPi and HomeAssistant ##
## RaspberryPi + RaspbianOS + UPSplus + HA + MQTT ##
## by: hellresistor 2021-05 V0.5 Alpha ##
############################################################
# shellcheck disable=SC1091
####################
## CHANGE IT ZONE ##
MYMQTTUSER="homeassistant"
MYMQTTPASS="Str0ngP455w0rd"
RPIBUILD="raspberrypi4"
###############
## Variables ##
MYIP=$(ifconfig eth0 | awk '{ print $2}' | grep -E -o "([0-9]{1,3}[\.]){3}[0-9]{1,3}")
MYDEPPACKAGES=(sudo git curl network-manager software-properties-common apt-transport-https apparmor-utils ca-certificates dbus jq python3 python3-pip i2c-tools mosquitto mosquitto-clients dnsmasq hostapd)
MYPYTHONDEP=(RPi.GPIO smbus smbus2 pi-ina219 paho-mqtt requests)
source /etc/os-release
#####################
## Basic functions ##
endy=$'\e[0m'
greeny=$'\e[92m'
bluey=$'\e[94m'
redy=$'\e[91m'
yellowy=$'\e[93m'
function ok { echo -e "${greeny}[OK] $* ${endy}"; }
function error { echo -e "${redy}[ERROR] $* ${endy}"; exit 1; }
function warn { echo -e "${yellowy}[WARN] $* ${endy}"; }
function info { echo -e "${bluey}[INFO] $* ${endy}"; }
###################
## Verify system ##
info "Checking Compatible System ..."
if [[ "$USER" == "pi" ]] ; then
error "Please use < sudo -i > command to log as root"
elif [[ "$USER" == "root" ]]; then
ok "Root User Logged"
else
error "$USER User NOT VALID. Use root"
fi
case "${ID,,}" in
raspbian) ok "Raspbian System Detected !!" ;;
*) error "Operating System NOT VALID" ;;
esac
case "$(uname -m)" in
aarch64|armv7l) ok "ARCH $(uname -m) Detected !!" ;;
*) error "$(uname -m) Architeture system NOT VALID" ;;
esac
#############################
## Installing dependencies ##
info "Updating System ..."
apt-get -y update > /dev/null 2>&1 && apt-get -y upgrade > /dev/null 2>&1
info "Installing Dependencies ..."
for i in "${MYDEPPACKAGES[@]}" ; do
if command -v "$i" > /dev/null 2>&1; then
info "Package $i is already Installed ..."
else
if sudo apt-get -y install "$i" > /dev/null 2>&1; then
ok "Package $i installed !!"
else
error "Unable install $i package ..."
fi
fi
done
info "Installing python3 Dependencies ..."
for f in "${MYPYTHONDEP[@]}" ; do
info "Installing $f package ..."
pip3 install "$f" > /dev/null 2>&1
done
############################
## Configuring the System ##
info "Configuring System ..."
if sudo systemctl disable ModemManager > /dev/null 2>&1 ; then
ok "ModemManager Disabled !!"
else
warn "Some problem on ModemManager service ..."
fi
if sudo systemctl stop ModemManager > /dev/null 2>&1 ; then
ok "ModemManager Stopped !!"
else
warn "Some problem on ModemManager service ..."
fi
##mount | grep securityfs
##mount securityfs -t securityfs /sys/kernel/security
if sed -i 's/.*/& lsm=apparmor/' /boot/cmdline.txt ; then
ok "apparmor added into cmdline.txt file Succefully !!"
else
error "Some problem adding apparmor config ..."
fi
#########################################
## Installing HomeAssistant Supervised ##
cd ~ || error "Cannot find directory"
info "Installing Docker ..."
if curl -fsSL get.docker.com | sh > /dev/null 2>&1 ; then
ok "Docker Installed Succefully !! "
else
error "A DOCKER BIG PROBLEM !!!!"
fi
info "Installing HomeAssistant Supervised ..."
if curl -sL "https://raw.githubusercontent.com/Kanga-Who/home-assistant/master/supervised-installer.sh" | bash -s -- -m "$RPIBUILD"; then
echo
info "PLEASE, Wait here like 30minutes, and Finish the HomeAssistant Setup on WebPage"
warn "If unable open http://$MYIP:8123 , just wait more and keep trying !!!"
info "When you ENTER ON HomeAssistant DASHBOARD, and Upload your configuration and than Back here and ..."
read -n 1 -r -s -p $'Press enter to continue...\n'
ok "HomeAssistant Supervised Installed Succefully !! "
else
error "A HOMEASSISTANT INSTALLATION BIG PROBLEM !!!!"
fi
##############################
## Configure GeekPi UPSPlus ##
info "Configuring GeekPi UPSplus ..."
if sed -i '/^dtparam=i2c_arm=on/a dtoverlay=i2c-rtc,ds1307' /boot/config.txt ; then
ok "i2c added into /boot/config.txt file Succefully !!"
else
error "i2c NOT added into /boot/config.txt file ..."
fi
#cd ~ || error "Cannot find directory"
#if curl -Lso- https://git.io/JLygb | bash > /dev/null 2>&1 ; then
# ok "UPSplus Installed Succefully !!"
#else
# error "A BIG PROBLEM with UPSplus scrypt ..."
#fi
################################################
## Installing and config Mosquitto MQTTBroker ##
info "Installing and configuring Mosquitto MQTTBroker Server ..."
if cat >> /etc/mosquitto/conf.d/mosquitto.conf <<EOTF
allow_anonymous false
password_file /etc/mosquitto/conf.d/pwfile
port 1883
EOTF
then
ok "Mosquito MQTT Broker Server Configured Succefully !!"
else
error "Mosquito MQTT Broker Server NOT Configured ..."
fi
if echo -e "${MYMQTTPASS}\n${MYMQTTPASS}\n" | sudo mosquitto_passwd -c /etc/mosquitto/conf.d/pwfile $MYMQTTUSER > /dev/null 2>&1
then
ok "Created $MYMQTTUSER User for Mosquitto Succefully !!"
else
error "Some problem creating user $MYMQTTUSER to Mosquitto ..."
fi
if sudo service mosquitto restart > /dev/null 2>&1
then
ok "Mosquito MQTT Broker Restarted Succefully !!"
else
error "Some problem with Mosquito MQTT Broker Service ..."
fi
info "Add Mosquitto configuration into homeassistant configuration.yaml file..."
if cat >> /usr/share/hassio/homeassistant/configuration.yaml <<EOTF
# Local MQTT server
mqtt:
discovery: true
discovery_prefix: homeassistant
broker: $MYIP
port: 1883
client_id: home-assistant-1
keepalive: 60
username: $MYMQTTUSER
password: $MYMQTTPASS
EOTF
then
ok "Mosquito MQTT Broker added to homeassistant configuration.yaml file Succefully !!"
else
error "Some problem adding Mosquito MQTT Broker into HomeAssistant configuration.yaml file ..."
fi
########################################################
## Configuring MQTTBroker to UPSplus On HomeAssistant ##
info "Configuring MQTTBroker to UPSplus On HomeAssistant ..."
cd /home/pi || error "Cannot find directory"
mkdir scripts logs | warn "Unable create Scripts logs directories"
git clone https://github.com/frtz13/UPSPlus_mqtt.git > /dev/null 2>&1
cp UPSPlus_mqtt/fanShutDownUps.py scripts/fanShutDownUps.py > /dev/null 2>&1
cp UPSPlus_mqtt/fanShutDownUps.ini scripts/fanShutDownUps.ini > /dev/null 2>&1
cp UPSPlus_mqtt/launcher.sh scripts/launcher.sh > /dev/null 2>&1
sed -i "s/BROKER=.*/BROKER=$MYIP/" scripts/fanShutDownUps.ini > /dev/null 2>&1 || error "Unable to set BROKER into scripts/fanShutDownUps.ini"
sed -i "s/USERNAME = .*/USERNAME = $MYMQTTUSER/" scripts/fanShutDownUps.ini > /dev/null 2>&1 || error "Unable to set USERNAME into scripts/fanShutDownUps.ini"
sed -i "s/PASSWORD = .*/PASSWORD = $MYMQTTPASS/" scripts/fanShutDownUps.ini > /dev/null 2>&1 || error "Unable to set PASSWORD into scripts/fanShutDownUps.ini"
sudo chown -R pi scripts && sudo chown -R pi logs
if python3 /home/pi/UPSPlus_mqtt/fanShutDownUps.py --notimerbias &
then
ok "Mosquitto MQTT Broker installed and Runnnig Succefully !! "
else
error "A @frtz13 python script BIG PROBLEM !!!!"
fi
############################################
## Adding sensors configuration.yaml file ##
info "Adding sensors configuration.yaml file ..."
if cat >> /usr/share/hassio/homeassistant/configuration.yaml <<EOTF
sensor:
- platform: mqtt
name: CPU fan speed
unit_of_measurement: "%"
state_topic: "home/rpi/fanspeed"
availability:
- topic: "home/rpi/LWT"
payload_available: "online"
payload_not_available: "offline"
- platform: mqtt
name: "UPS Battery Voltage"
device_class: voltage
state_topic: "home/rpi/ups"
value_template: '{{ value_json["BatteryVoltage_V"] }}'
unit_of_measurement: "V"
availability:
- topic: "home/rpi/LWT"
payload_available: "online"
payload_not_available: "offline"
- platform: mqtt
name: "UPS Battery current"
device_class: current
state_topic: "home/rpi/ups"
value_template: '{{ value_json["BatteryCurrent_mA"] }}'
unit_of_measurement: "mA"
availability:
- topic: "home/rpi/LWT"
payload_available: "online"
payload_not_available: "offline"
- platform: mqtt
name: "UPS average Battery current"
device_class: current
state_topic: "home/rpi/ups"
value_template: '{{ value_json["BatteryCurrent_avg_mA"] }}'
unit_of_measurement: "mA"
availability:
- topic: "home/rpi/LWT"
payload_available: "online"
payload_not_available: "offline"
- platform: mqtt
name: "UPS average Battery power"
device_class: power
state_topic: "home/rpi/ups"
value_template: '{{ value_json["BatteryPower_avg_mW"] }}'
unit_of_measurement: "mW"
availability:
- topic: "home/rpi/LWT"
payload_available: "online"
payload_not_available: "offline"
- platform: mqtt
name: "UPS Battery temperature"
device_class: temperature
state_topic: "home/rpi/ups"
value_template: '{{ value_json["BatteryTemperature_degC"] }}'
unit_of_measurement: "°C"
availability:
- topic: "home/rpi/LWT"
payload_available: "online"
payload_not_available: "offline"
- platform: mqtt
name: "UPS Output Voltage"
device_class: voltage
state_topic: "home/rpi/ups"
value_template: '{{ value_json["OutputVoltage_V"] }}'
unit_of_measurement: "V"
availability:
- topic: "home/rpi/LWT"
payload_available: "online"
payload_not_available: "offline"
- platform: mqtt
name: "UPS minimum Output Voltage"
device_class: voltage
state_topic: "home/rpi/ups"
value_template: '{{ value_json["OutputVoltage_mini_V"] }}'
unit_of_measurement: "V"
availability:
- topic: "home/rpi/LWT"
payload_available: "online"
payload_not_available: "offline"
- platform: mqtt
name: "UPS Output current"
device_class: current
state_topic: "home/rpi/ups"
value_template: '{{ value_json["OutputCurrent_mA"] }}'
unit_of_measurement: "mA"
availability:
- topic: "home/rpi/LWT"
payload_available: "online"
payload_not_available: "offline"
- platform: mqtt
name: "UPS average output current"
device_class: current
state_topic: "home/rpi/ups"
value_template: '{{ value_json["OutputCurrent_avg_mA"] }}'
unit_of_measurement: "mA"
availability:
- topic: "home/rpi/LWT"
payload_available: "online"
payload_not_available: "offline"
- platform: mqtt
name: "UPS peak output current"
device_class: current
state_topic: "home/rpi/ups"
value_template: '{{ value_json["OutputCurrent_peak_mA"] }}'
unit_of_measurement: "mA"
availability:
- topic: "home/rpi/LWT"
payload_available: "online"
payload_not_available: "offline"
- platform: mqtt
name: "UPS average output power"
device_class: power
state_topic: "home/rpi/ups"
value_template: '{{ value_json["OutputPower_avg_mW"] }}'
unit_of_measurement: "mW"
availability:
- topic: "home/rpi/LWT"
payload_available: "online"
payload_not_available: "offline"
- platform: mqtt
name: "UPS Battery Remaining Capacity"
device_class: energy
state_topic: "home/rpi/ups"
value_template: '{{ value_json["BatteryRemainingCapacity_percent"] }}'
unit_of_measurement: "%"
availability:
- topic: "home/rpi/LWT"
payload_available: "online"
payload_not_available: "offline"
binary_sensor:
- platform: mqtt
name: "UPS on Battery"
state_topic: "home/rpi/ups"
value_template: '{{ value_json["OnBattery"] }}'
payload_on: "True"
payload_off: "False"
availability:
- topic: "home/rpi/LWT"
payload_available: "online"
payload_not_available: "offline"
- platform: mqtt
name: "UPS Battery Charging"
state_topic: "home/rpi/ups"
value_template: '{{ value_json["BatteryCharging"] }}'
payload_on: "True"
payload_off: "False"
availability:
- topic: "home/rpi/LWT"
payload_available: "online"
payload_not_available: "offline"
EOTF
then
ok "Added UPSplus sensors on HomeAssistant configuration.yaml file Succefully !!!"
else
error "Some problem adding UPSplus sensors into HomeAssistant configuration.yaml file ..."
fi
#######################
## Configure Crontab ##
info "Configuring Crontab jobs ..."
crontab -l > mycron
#echo "@reboot sh /$USER/scripts/launcher.sh >/$USER/logs/cronlog 2>&1" >> mycron
echo "@reboot sh /home/pi/scripts/launcher.sh >/home/pi/logs/cronlog 2>&1" >> mycron
echo "0 5 * * 5 sudo apt -y update && sudo apt -y upgrade && sudo apt -y autoremove" >> mycron
if sudo crontab mycron ; then
ok "Set Crontab jobs Succefully !! "
else
error "Some problem adding the Crontab jobs ..."
fi
sudo rm mycron > /dev/null 2>&1
#############
## The END ##
ok "Instalation and Configuration of Raspberry + UPSplus + HomeAssistant COMPLETED !!!"
apt-get -y autoremove > /dev/null 2>&1
info "A reboot command should be executed on this System :) "
exit 0