45 lines
1.6 KiB
Bash
45 lines
1.6 KiB
Bash
#!/usr/bin/env bash
|
|
|
|
# Background loop to monitor battery status and trigger dunstify on low power.
|
|
while true; do
|
|
if [ ! -d /sys/class/power_supply/BAT0 ]; then
|
|
sleep 60
|
|
continue
|
|
fi
|
|
|
|
STATUS=$(cat /sys/class/power_supply/BAT0/status 2>/dev/null)
|
|
# Using the native kernel capacity file instead of relying on ibam
|
|
CAPACITY=$(cat /sys/class/power_supply/BAT0/capacity 2>/dev/null)
|
|
|
|
if [ -z "$CAPACITY" ]; then
|
|
sleep 60
|
|
continue
|
|
fi
|
|
|
|
# Trigger notification at 5% or below
|
|
if [ "$STATUS" == "Discharging" ] && [ "$CAPACITY" -le 5 ]; then
|
|
if [ ! -f "/run/user/${UID}/battery_low" ]; then
|
|
logger "Low power! Triggering notification...."
|
|
dunstify --urgency=critical \
|
|
--icon=/usr/share/icons/breeze-dark/emblems/16/emblem-warning.svg \
|
|
"LOW BATTERY!" \
|
|
"Battery level is at ${CAPACITY}%, plug power quick!"
|
|
touch "/run/user/${UID}/battery_low"
|
|
fi
|
|
|
|
# Clear the notification if we plug back in and rise above 5%
|
|
elif [ "$STATUS" == "Charging" ] && [ "$CAPACITY" -gt 5 ]; then
|
|
if [ -f "/run/user/${UID}/battery_low" ]; then
|
|
logger "Power reconnected and above 5 percent, clearing notification state."
|
|
rm -f "/run/user/${UID}/battery_low"
|
|
|
|
# Find and close the specific dunst notification by ID
|
|
for id in $(dunstctl history | jq '.data[0][] | select(.summary.data == "LOW BATTERY!") | .id.data'); do
|
|
dunstify --close=${id}
|
|
done
|
|
fi
|
|
fi
|
|
|
|
sleep 60
|
|
done
|