From c72363092d5cc4642b9fa31ce266b97588054514 Mon Sep 17 00:00:00 2001 From: Matthias Pronk Date: Fri, 22 Feb 2013 13:56:37 +0100 Subject: [PATCH] battery checker added --- i3pystatus/__main__.py.dist | 6 +++++ i3pystatus/batterychecker.py | 46 ++++++++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+) create mode 100644 i3pystatus/batterychecker.py diff --git a/i3pystatus/__main__.py.dist b/i3pystatus/__main__.py.dist index 8561d0f..9c262c0 100755 --- a/i3pystatus/__main__.py.dist +++ b/i3pystatus/__main__.py.dist @@ -64,5 +64,11 @@ status.register_module(nm) tb = thunderbirdnewmail.ThunderbirdMailChecker() status.register_module(tb) + +# the battery status checker module +battery = batterychecker.BatteryChecker() +status.register(battery) + + # start the handler status.run() diff --git a/i3pystatus/batterychecker.py b/i3pystatus/batterychecker.py new file mode 100644 index 0000000..0f927f8 --- /dev/null +++ b/i3pystatus/batterychecker.py @@ -0,0 +1,46 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +from i3pystatus import IntervalModule + +class BatteryChecker(IntervalModule): + """ + This class uses the /proc/acpi/battery interface to check for the + battery status + """ + + def __init__(self, battery_ident="BAT0"): + self.battery_ident = battery_ident + self.base_path = "/sys/class/power_supply/%s" % self.battery_ident + + def run(self): + urgent = False + color = "#ffffff" + status = open('%s/status' % self.base_path, 'r').readline().strip() + energy_now = float(open('%s/energy_now' % self.base_path, 'r').readline()) + + if status == 'Full': + full_text = 'fully charged' + elif status == 'Charging': + energy_full = float(open('%s/energy_full' % self.base_path, 'r').readline()) + energy_percentage = (energy_now / energy_full) * 100 + full_text = '%.2f%% charged' + elif status == 'Discharging': + power_now = float(open('%s/power_now' % self.base_path, 'r').readline()) + remaining_time_secs = (energy_now / power_now) * 3600 + hours, remainder = divmod(remaining_time_secs, 3600) + minutes, seconds = divmod(remainder, 60) + full_text = "%ih %im %is remaining" % (hours, minutes, seconds) + if remaining_time_secs < (15*60): + urgent = True + color = "#ff0000" + else: + full_text = 'n/a' + + self.output = { + "full_text": full_text, + "name": "pybattery", + "instance": self.battery_ident, + "urgent": urgent, + "color": color + }