From 7531a055b2b1935124bc44dc1e7925e4e0e122e8 Mon Sep 17 00:00:00 2001 From: Pig Monkey Date: Thu, 28 Jan 2016 20:27:55 -0800 Subject: [PATCH] warn on low battery Addresses part of #18 --- roles/laptop/files/lowbatt.sh | 15 +++++++++++++++ roles/laptop/files/udev.rules | 1 + roles/laptop/tasks/main.yml | 5 ++++- 3 files changed, 20 insertions(+), 1 deletion(-) create mode 100755 roles/laptop/files/lowbatt.sh diff --git a/roles/laptop/files/lowbatt.sh b/roles/laptop/files/lowbatt.sh new file mode 100755 index 0000000..7aa4143 --- /dev/null +++ b/roles/laptop/files/lowbatt.sh @@ -0,0 +1,15 @@ +#!/bin/sh + +capacity='low' + +# Try to get an actual percentage. +if [ -f /sys/class/power_supply/BAT0/capacity ]; then + capacity="at `cat /sys/class/power_supply/BAT0/capacity`%" +fi + +# Build the message body. +message="Battery is $capacity. We need more power, Scotty!" + +# Make some noise. +notify-send --urgency=critical "Low Battery" "$message" +wall "Battery is low. $message" diff --git a/roles/laptop/files/udev.rules b/roles/laptop/files/udev.rules index e5d6aa6..9e181ad 100644 --- a/roles/laptop/files/udev.rules +++ b/roles/laptop/files/udev.rules @@ -1 +1,2 @@ +SUBSYSTEM=="power_supply", ATTR{status}=="Discharging", ATTR{capacity}=="[5-10]", RUN+="/usr/local/bin/lowbatt.sh" SUBSYSTEM=="power_supply", ATTR{status}=="Discharging", ATTR{capacity}=="[0-5]", RUN+="/usr/bin/systemctl suspend" diff --git a/roles/laptop/tasks/main.yml b/roles/laptop/tasks/main.yml index c088537..c269869 100644 --- a/roles/laptop/tasks/main.yml +++ b/roles/laptop/tasks/main.yml @@ -5,5 +5,8 @@ - name: Copy logind configuration file copy: src=logind.conf dest=/etc/systemd/logind.conf -- name: Push udev rule to suspend on low battery +- name: Push low battery warning script + copy: src=lowbatt.sh dest=/usr/local/bin/lowbatt mode=0755 + +- name: Push low battery udev rules copy: src=udev.rules dest=/etc/udev/rules.d/99-lowbat.rules