dotfiles/bin/battery-daemon

52 lines
1.6 KiB
Plaintext
Raw Permalink Normal View History

2023-12-31 04:41:02 -05:00
#!/bin/bash
2023-07-11 10:06:43 -04:00
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
2023-12-31 04:41:02 -05:00
warning_level=20
2023-07-11 10:06:43 -04:00
# how often to check battery status, in seconds
2023-12-31 04:41:02 -05:00
check_interval=120
is_first_start=true
2023-07-11 10:06:43 -04:00
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")
2024-01-02 10:45:32 -05:00
time_to_empty=$(upower -i "$path_to_battery" | grep -E "time to empty" | sed 's/^[[:space:]]*time to empty:[[:space:]]*//')
2023-07-11 10:06:43 -04:00
# check if battery is low and discharging
2023-12-31 04:41:02 -05:00
if [ "$battery_level" -lt "$warning_level" ] && [ "$discharging" -eq 1 ] && [ "$is_first_start" = false ];
2023-07-11 10:06:43 -04:00
then
dunstify -a "Battery" \
"Low battery: ${battery_level}%" \
2024-01-02 10:45:32 -05:00
"Battery is low ($time_to_empty left)" \
2023-07-11 10:06:43 -04:00
-r 100 \
-i "$notify_icons/battery-low.png"
2023-08-14 19:28:08 -04:00
mpv "/home/q/.config/alarm/low-battery-sound.mp3" > /dev/null 2>&1
2023-12-31 04:41:02 -05:00
# check_interval=$check_interval/2+$check_interval
check_interval=$(python -c "print(round($check_interval*1.5))")
2023-08-14 19:28:08 -04:00
fi
2023-12-31 04:41:02 -05:00
if [ "$battery_level" -gt 80 ] && [ "$discharging" -eq 0 ] && ["$is_first_start" = false]; then
2023-08-14 19:28:08 -04:00
dunstify -a "Battery" \
"Battery is charged: ${battery_level}%" \
-r 100 \
-i "$notify_icons/battery-charged.png"
2023-12-31 04:41:02 -05:00
check_interval=300
2023-07-11 10:06:43 -04:00
fi
sleep ${check_interval}s
2023-12-31 04:41:02 -05:00
if [ "$is_first_start" = true ]; then
is_first_start=false
echo $is_first_start
fi
2023-07-11 10:06:43 -04:00
done