Power info

Submitted by ormiret on Sat, 11/08/2007 - 01:47.

I've stopped using a panel, and the thing that I missed most about it was the info on the state of my laptops battery, so I wrote a script to display this information using libnotify:

#!/usr/bin/python

import pynotify, math

batt_info = open('/proc/acpi/battery/BAT0/info').readlines()
batt_state = open('/proc/acpi/battery/BAT0/state').readlines()



full = int(batt_info[2].split()[3])
low = int(batt_info[5].split()[3])

current = int(batt_state[4].split()[2])
rate = int(batt_state[3].split()[2])

state = batt_state[2].split()[2]

if state == "charging":
    remaining = full - current
    time = remaining/float(rate)
    hours = int(math.floor(time))
    mins = int(math.floor((time-hours)*60))
    if pynotify.init("power-info"):
        pynotify.Notification("Charging",
                              repr(hours)+":"+repr(mins).zfill(2)+" till fully charged.").show()

if state == "discharging":
    remaining = current - low
    time = remaining/float(rate)
    hours = int(math.floor(time))
    mins = int(math.floor((time-hours)*60))
    if pynotify.init("power-info"):
        pynotify.Notification("On battery",
                              repr(hours)+":"+repr(mins).zfill(2)+" remaining.").show()

if state == "charged":
    if pynotify.init("power-info"):
        pynotify.Notification("Fully Charged").show()

( categories: )