Today, I decided to help someone out and, as is often the case with coding, I got carried away.
I set out to help fix up a shell script but, instead, I decided to “do it right” and produced a simple Python script which…
- Uses a combination of
GLib.timeout_add_secondsand UPower events to react to power supply changes immediately while still putting a limit on how often battery percentage changes should be able to cause wakeups.
- Uses GStreamer and notify2 to provide audio and visual low-battery warnings without the weight and delay of calling subprocesses.
- Is compact and easy to customize because it’s written in Python
By design, it doesn’t really have much of a UI (just a persistent notification popup and alert sound that come and go in response to changes in power state), but here’s what the popup looks like when using xfce4-notifyd:
It does require the 1.0 UPower API for its “Display Device” abstraction, so an 0.99.x-series version of UPower or newer is required, but for people on a new enough Debian/Ubuntu/Mint release (Vivid, in Ubuntu’s case), the dependencies can be installed as follows:
sudo apt-get install python-notify2 python-gi gir1.2-upowerglib-1.0 gir1.2-gstreamer-1.0 gir1.2-gst-plugins-base-1.0
It hasn’t been tested under Python 3.x but it looks like it should work perfectly if you just replace
python-notify2 python-gi with
python3-notify2 python3-gi in the dependencies and edit the shebang line at the top of the file.
I only wish I’d realized what I was doing sooner so I could start tracking its evolution properly in git. As-is, all I could do was translate the snapshots from Dropbox’s revision history.
As usual for small, single-file things, here’s the direct download link followed by a GitHub Gist embed.