-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbatterynotify
executable file
·35 lines (27 loc) · 1.63 KB
/
batterynotify
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
#!/bin/sh
# Taken and modified from here: https://github.com/ericmurphyxyz/dotfiles/blob/master/.local/bin/batterynotify
# Send a notification if the laptop battery is either low or is fully charged.
export DISPLAY=:0
export DBUS_SESSION_BUS_ADDRESS="unix:path=/run/user/1000/bus"
# Battery percentage at which to notify
WARNING_LEVEL=15
BATTERY_DISCHARGING=$(acpi -b | grep "Battery 0" | grep -c "Discharging")
BATTERY_LEVEL=$(acpi -b | grep "Battery 0" | awk '{print $4}' | awk -F "%" '{print $1}')
# Use two files to store whether we've shown a notification or not (to prevent multiple notifications)
EMPTY_FILE=/tmp/batteryempty
FULL_FILE=/tmp/batteryfull
# Reset notifications if the computer is charging/discharging
if [ "$BATTERY_DISCHARGING" -eq 1 ] && [ -f $FULL_FILE ]; then
rm $FULL_FILE
elif [ "$BATTERY_DISCHARGING" -eq 0 ] && [ -f $EMPTY_FILE ]; then
rm $EMPTY_FILE
fi
# If the battery is charging and is full (and has not shown notification yet)
if [ "$BATTERY_LEVEL" -eq 100 ] && [ "$BATTERY_DISCHARGING" -eq 0 ] && [ ! -f $FULL_FILE ]; then
notify-send "Battery Charged: 100%" "Battery is fully charged." -u low -t 0 -i "/home/windward/.config/dunst/icons/full-battery.png" -r 9991
touch $FULL_FILE
# If the battery is low and is not charging (and has not shown notification yet)
elif [ "$BATTERY_LEVEL" -le $WARNING_LEVEL ] && [ "$BATTERY_DISCHARGING" -eq 1 ] && [ ! -f $EMPTY_FILE ]; then
notify-send "Low Battery" "${BATTERY_LEVEL}% of battery remaining. Please charge your computer right now before all data is lost." -u critical -i "/home/windward/.config/dunst/icons/alert-battery.png" -r 9991
touch $EMPTY_FILE
fi