battery-notify

battery level notifier
git clone git://git.pyratebeard.net/battery-notify.git
Log | Files | Refs | README

commit 864e0ce5dd3a42862f367207206d902505556ad3
parent 08ceb8f7075e29d3d4e02c0119a0ac1450541af6
Author: pyratebeard <root@pyratebeard.net>
Date:   Thu, 14 Mar 2019 15:01:02 +0000

Merge pull request #1 from venam/master

Battery notifier updated state file and suspension mechanism
Diffstat:
Mcheck-batt | 78+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-------------
1 file changed, 65 insertions(+), 13 deletions(-)

diff --git a/check-batt b/check-batt @@ -1,22 +1,74 @@ #!/usr/bin/env bash -BATTSTATUS=$(cat /sys/class/power_supply/BAT0/status) -BATTPERC=$(cat /sys/class/power_supply/BAT0/capacity) +BAT_NUM='1' +STATUS_FILE=/tmp/check-bat.tmp +BATTPERC=$(cat /sys/class/power_supply/BAT${BAT_NUM}/capacity) +BATTSTATUS=$(cat /sys/class/power_supply/BAT${BAT_NUM}/status) +LAST_STATUS='' +LAST_NOTIF_LEVEL=0 +NOTIF_LEVEL=0 + +if [ -e $STATUS_FILE ] ; then + . $STATUS_FILE +fi + +save_status() { + LAST_STATUS=$BATTSTATUS + echo "LAST_STATUS=${BATTSTATUS}" > $STATUS_FILE + echo "LAST_NOTIF_LEVEL=${NOTIF_LEVEL}" >> $STATUS_FILE +} notify() { - level=$1 - msg=$2 - DISPLAY=:0 /usr/bin/notify-send -u "$level" "POWER" "$msg" + level=$1 + msg=$2 + DISPLAY=:0 /usr/bin/notify-send -t 3500 -u "$level" "POWER" "$msg" } -if [[ $BATTSTATUS != 'Discharging' ]] ; then - exit 0 +# Battery status changed +if [[ "$BATTSTATUS" != "$LAST_STATUS" ]] ; then + notify "normal" "battery is now $BATTSTATUS, Capacity: $BATTPERC" + save_status + exit fi -if [[ $BATTPERC == 50 ]] ; then - notify "low" "battery is at 50%" -elif [[ $BATTPERC == 30 ]] ; then - notify "normal" "battery is at 30%" -elif [[ "$BATTPERC | sed -e :a -e 's/^.\{1\}$/0&/;ta'" < 10 ]] ; then - notify "critical" "battery is less than 10%" +if [ "$BATTSTATUS" = Discharging ] && [ "$BATTPERC" -le 10 ]; then + NOTIF_LEVEL=10 + notify "critical" "battery is less than 10% - Connect charger, going to sleep in 10s" + sleep 10 + BATTSTATUS=$(cat /sys/class/power_supply/BAT${BAT_NUM}/status) + if [ "$BATTSTATUS" = Discharging ]; then + notify "critical" "going to suspend now" + sleep 3 + logger "Critical battery threshold, suspending" + systemctl suspend + else + notify "normal" "battery is now charging, cancelling suspend" + fi + save_status + exit fi + +if [[ $BATTPERC -le 20 ]] ; then + NOTIF_LEVEL=20 + notify "critical" "battery $BATTSTATUS is less than 20%" + save_status +elif [[ $BATTPERC -le 30 ]] ; then + NOTIF_LEVEL=30 + if [ "$NOTIF_LEVEL" != "$LAST_NOTIF_LEVEL" ]; then + notify "normal" "battery $BATTSTATUS is at 30%" + save_status + fi +elif [[ $BATTPERC -le 50 ]] ; then + NOTIF_LEVEL=50 + if [ "$NOTIF_LEVEL" != "$LAST_NOTIF_LEVEL" ] ; then + notify "low" "battery $BATTSTATUS is at 50%" + save_status + fi +elif [[ $BATTPERC -le 60 ]] ; then + NOTIF_LEVEL=60 + if [ "$NOTIF_LEVEL" != "$LAST_NOTIF_LEVEL" ] ; then + notify "normal" "battery $BATTSTATUS is at 60%" + save_status + fi +fi +