dotfiles/bin/battery-daemon

52 lines
1.6 KiB
Bash
Executable File

#!/bin/bash
for pid in $(pidof -x battery); do
if [ "$pid" != $$ ]; then
kill -9 "$pid"
fi
done
notify_icons="/home/q/.config/dunst/icons"
# notify when below this percentage
warning_level=20
# how often to check battery status, in seconds
check_interval=120
is_first_start=true
while true; do
path_to_battery=$(upower -e | grep BAT)
battery_level=$(upower -i "$path_to_battery" | grep -E "percentage" | sed 's/[^0-9]//g')
discharging=$(upower -i "$path_to_battery" | grep -E "state" | grep -c "discharging")
time_to_empty=$(upower -i "$path_to_battery" | grep -E "time to empty" | sed 's/^[[:space:]]*time to empty:[[:space:]]*//')
# check if battery is low and discharging
if [ "$battery_level" -lt "$warning_level" ] && [ "$discharging" -eq 1 ] && [ "$is_first_start" = false ];
then
dunstify -a "Battery" \
"Low battery: ${battery_level}%" \
"Battery is low ($time_to_empty left)" \
-r 100 \
-i "$notify_icons/battery-low.png"
mpv "/home/q/.config/alarm/low-battery-sound.mp3" > /dev/null 2>&1
# check_interval=$check_interval/2+$check_interval
check_interval=$(python -c "print(round($check_interval*1.5))")
fi
if [ "$battery_level" -gt 80 ] && [ "$discharging" -eq 0 ] && ["$is_first_start" = false]; then
dunstify -a "Battery" \
"Battery is charged: ${battery_level}%" \
-r 100 \
-i "$notify_icons/battery-charged.png"
check_interval=300
fi
sleep ${check_interval}s
if [ "$is_first_start" = true ]; then
is_first_start=false
echo $is_first_start
fi
done