-
Notifications
You must be signed in to change notification settings - Fork 0
/
warn-battery
executable file
·43 lines (38 loc) · 1.19 KB
/
warn-battery
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
#!/usr/bin/env bash
# warns me when battery is at 10 percent
# there are other battery modules but I sort
# of wanted to emulate mac's behaviour:
#
# Warn me once, when battery goes under 10%
# and then wait till battery goes above 15%
# before warning again
readonly BATTERY_NAME="${1:?Must provide battery name (e.g. BAT0) as first argument}"
declare BATTERY_PERC
declare WARNED=0 # state machine
set_battery_perc() {
BATTERY_PERC=$(cat /sys/class/power_supply/"$BATTERY_NAME"/capacity) || return 1
}
is_charging() {
local status
status=$(cat /sys/class/power_supply/"$BATTERY_NAME"/status) || return 1
[[ "$status" == 'Charging' ]]
}
# make sure that we can get the battery percentage
set_battery_perc "${BATTERY_NAME}" || exit $?
while true; do
# if already charging, skip check
is_charging && {
WARNED=0
sleep 60 && continue
}
set_battery_perc "${BATTERY_NAME}"
if ((WARNED == 0)); then
# hasn't been warned, check if battery is under 10%
((BATTERY_PERC <= 10)) && notify -u critical 'Battery low!' && WARNED=1
elif ((WARNED == 1)); then
((BATTERY_PERC <= 5)) && notify -u critical 'Battery critical!' && WARNED=2
else # WARNED = 2
((BATTERY_PERC > 15)) && WARNED=0
fi
sleep 60
done