2020-03-06 20:34:30 +01:00
|
|
|
# pylint: disable=C0111,R0903
|
|
|
|
|
2020-04-19 13:42:16 +02:00
|
|
|
"""Displays battery status, remaining percentage and charging information.
|
2020-03-06 20:34:30 +01:00
|
|
|
|
|
|
|
Parameters:
|
2020-04-19 14:21:50 +02:00
|
|
|
* battery.device : Comma-separated list of battery devices to read information from (defaults to auto for auto-detection)
|
|
|
|
* battery.warning : Warning threshold in % of remaining charge (defaults to 20)
|
|
|
|
* battery.critical : Critical threshold in % of remaining charge (defaults to 10)
|
|
|
|
* battery.showdevice : If set to 'true', add the device name to the widget (defaults to False)
|
|
|
|
* battery.decorate : If set to 'false', hides additional icons (charging, etc.) (defaults to True)
|
2020-03-06 20:34:30 +01:00
|
|
|
* battery.showpowerconsumption: If set to 'true', show current power consumption (defaults to False)
|
2020-04-19 14:21:50 +02:00
|
|
|
* battery.compact-devices : If set to 'true', compacts multiple batteries into a single entry (default to False)
|
2020-05-08 20:58:35 +02:00
|
|
|
|
|
|
|
(partially) contributed by `martindoublem <https://github.com/martindoublem>`_ - many thanks!
|
2020-04-19 13:42:16 +02:00
|
|
|
"""
|
2020-03-06 20:34:30 +01:00
|
|
|
|
|
|
|
import os
|
|
|
|
import glob
|
2020-03-06 20:57:32 +01:00
|
|
|
import logging
|
2020-05-03 11:15:52 +02:00
|
|
|
|
2020-03-07 13:56:05 +01:00
|
|
|
log = logging.getLogger(__name__)
|
|
|
|
|
2020-03-06 20:57:32 +01:00
|
|
|
try:
|
|
|
|
import power
|
|
|
|
except ImportError:
|
2020-03-07 13:56:05 +01:00
|
|
|
log.warning('unable to import module "power": Time estimates will not be available')
|
2020-03-06 20:34:30 +01:00
|
|
|
|
2020-03-06 20:37:46 +01:00
|
|
|
import core.module
|
|
|
|
import core.input
|
2020-03-06 20:34:30 +01:00
|
|
|
|
2020-03-06 20:37:46 +01:00
|
|
|
import util.format
|
2020-03-06 20:34:30 +01:00
|
|
|
|
2020-05-03 11:15:52 +02:00
|
|
|
|
2020-04-19 14:07:15 +02:00
|
|
|
class BatteryManager(object):
|
|
|
|
def remaining(self):
|
|
|
|
try:
|
|
|
|
estimate = power.PowerManagement().get_time_remaining_estimate()
|
|
|
|
# do not show remaining if on AC
|
|
|
|
if estimate == power.common.TIME_REMAINING_UNLIMITED:
|
|
|
|
return None
|
2020-05-03 11:15:52 +02:00
|
|
|
return estimate * 60 # return value in seconds
|
2020-04-19 14:07:15 +02:00
|
|
|
except Exception as e:
|
|
|
|
return -1
|
|
|
|
return -1
|
|
|
|
|
|
|
|
def read(self, battery, component, default=None):
|
2020-05-03 11:15:52 +02:00
|
|
|
path = "/sys/class/power_supply/{}".format(battery)
|
2020-04-19 14:07:15 +02:00
|
|
|
if not os.path.exists(path):
|
|
|
|
return default
|
|
|
|
try:
|
2020-05-03 11:15:52 +02:00
|
|
|
with open("{}/{}".format(path, component)) as f:
|
2020-04-19 14:07:15 +02:00
|
|
|
return f.read().strip()
|
|
|
|
except IOError:
|
2020-05-03 11:15:52 +02:00
|
|
|
return "n/a"
|
2020-04-19 14:07:15 +02:00
|
|
|
return default
|
|
|
|
|
|
|
|
def capacity(self, battery):
|
2020-05-03 11:15:52 +02:00
|
|
|
capacity = self.read(battery, "capacity", 100)
|
|
|
|
if capacity != "n/a":
|
2020-04-19 14:07:15 +02:00
|
|
|
capacity = int(capacity)
|
|
|
|
|
|
|
|
return capacity if capacity < 100 else 100
|
|
|
|
|
2020-04-19 14:21:50 +02:00
|
|
|
def capacity_all(self, batteries):
|
|
|
|
now = 0
|
|
|
|
full = 0
|
|
|
|
for battery in batteries:
|
|
|
|
try:
|
2020-05-03 11:15:52 +02:00
|
|
|
with open(
|
|
|
|
"/sys/class/power_supply/{}/energy_full".format(battery)
|
|
|
|
) as f:
|
2020-04-19 14:21:50 +02:00
|
|
|
full += int(f.read())
|
2020-05-03 11:15:52 +02:00
|
|
|
with open("/sys/class/power_supply/{}/energy_now".format(battery)) as f:
|
2020-04-19 14:21:50 +02:00
|
|
|
now += int(f.read())
|
|
|
|
except IOError:
|
2020-05-03 11:15:52 +02:00
|
|
|
return "n/a"
|
|
|
|
return int(float(now) / float(full) * 100.0)
|
2020-04-19 14:21:50 +02:00
|
|
|
|
2020-04-19 14:07:15 +02:00
|
|
|
def isac(self, battery):
|
2020-05-03 11:15:52 +02:00
|
|
|
path = "/sys/class/power_supply/{}".format(battery)
|
2020-04-19 14:07:15 +02:00
|
|
|
return not os.path.exists(path)
|
|
|
|
|
2020-04-19 14:21:50 +02:00
|
|
|
def isac_any(self, batteries):
|
|
|
|
for battery in batteries:
|
2020-05-03 11:15:52 +02:00
|
|
|
if self.isac(battery):
|
|
|
|
return True
|
2020-04-19 14:21:50 +02:00
|
|
|
return False
|
|
|
|
|
2020-04-19 14:07:15 +02:00
|
|
|
def consumption(self, battery):
|
2020-05-03 11:15:52 +02:00
|
|
|
consumption = self.read(battery, "power_now", "n/a")
|
|
|
|
if consumption == "n/a":
|
|
|
|
return "n/a"
|
|
|
|
return "{}W".format(int(consumption) / 1000000)
|
2020-04-19 14:07:15 +02:00
|
|
|
|
|
|
|
def charge(self, battery):
|
2020-05-03 11:15:52 +02:00
|
|
|
return self.read(battery, "status", "n/a")
|
2020-03-06 20:57:32 +01:00
|
|
|
|
2020-04-19 14:21:50 +02:00
|
|
|
def charge_any(self, batteries):
|
|
|
|
for battery in batteries:
|
2020-05-03 11:15:52 +02:00
|
|
|
if self.charge(battery) == "Discharging":
|
|
|
|
return "Discharging"
|
|
|
|
return "Charged"
|
|
|
|
|
2020-04-19 14:21:50 +02:00
|
|
|
|
2020-03-06 20:37:46 +01:00
|
|
|
class Module(core.module.Module):
|
2020-04-26 16:39:24 +02:00
|
|
|
def __init__(self, config, theme):
|
2020-05-09 10:57:44 +02:00
|
|
|
super().__init__(config, theme, [])
|
2020-03-06 20:37:46 +01:00
|
|
|
|
2020-04-19 14:07:15 +02:00
|
|
|
self.__manager = BatteryManager()
|
|
|
|
|
2020-05-03 11:15:52 +02:00
|
|
|
self._batteries = util.format.aslist(self.parameter("device", "auto"))
|
|
|
|
if self._batteries[0] == "auto":
|
|
|
|
self._batteries = [
|
|
|
|
os.path.basename(battery)
|
|
|
|
for battery in glob.glob("/sys/class/power_supply/BAT*")
|
|
|
|
]
|
|
|
|
core.input.register(
|
|
|
|
self, button=core.input.LEFT_MOUSE, cmd="gnome-power-statistics"
|
|
|
|
)
|
|
|
|
|
2020-05-28 08:17:35 +02:00
|
|
|
if len(self._batteries) == 0:
|
|
|
|
widget = self.add_widget(full_text=self.ac, name="ac")
|
|
|
|
widget.set("ac", True)
|
|
|
|
widget.set("capacity", 100)
|
2020-04-19 14:21:50 +02:00
|
|
|
else:
|
2020-05-28 08:17:35 +02:00
|
|
|
if util.format.asbool(self.parameter("compact-devices", False)):
|
|
|
|
widget = self.add_widget(full_text=self.capacity, name="all-batteries")
|
|
|
|
else:
|
|
|
|
for battery in self._batteries:
|
|
|
|
log.debug("adding new widget for {}".format(battery))
|
|
|
|
widget = self.add_widget(full_text=self.capacity, name=battery)
|
|
|
|
|
2023-11-08 22:46:58 +01:00
|
|
|
try:
|
|
|
|
with open("/sys/class/power_supply/{}/model_name".format(battery)) as f:
|
|
|
|
widget.set("pen", ("Pen" in f.read().strip()))
|
|
|
|
except Exception:
|
|
|
|
pass
|
|
|
|
|
|
|
|
if util.format.asbool(self.parameter("decorate", True)) == False:
|
|
|
|
for widget in self.widgets():
|
2020-05-03 11:15:52 +02:00
|
|
|
widget.set("theme.exclude", "suffix")
|
2020-03-06 20:34:30 +01:00
|
|
|
|
2022-06-22 10:12:10 +02:00
|
|
|
def hidden(self):
|
|
|
|
return len(self._batteries) == 0
|
|
|
|
|
2020-05-28 08:17:35 +02:00
|
|
|
def ac(self, widget):
|
|
|
|
return "ac"
|
|
|
|
|
2020-03-06 20:34:30 +01:00
|
|
|
def capacity(self, widget):
|
2020-05-03 11:15:52 +02:00
|
|
|
if widget.name == "all-batteries":
|
2020-04-19 14:21:50 +02:00
|
|
|
capacity = self.__manager.capacity_all(self._batteries)
|
|
|
|
else:
|
2020-04-30 12:42:34 +02:00
|
|
|
capacity = self.__manager.capacity(widget.name)
|
2020-05-03 11:15:52 +02:00
|
|
|
widget.set("capacity", capacity)
|
|
|
|
widget.set("ac", self.__manager.isac_any(self._batteries))
|
2020-03-06 20:34:30 +01:00
|
|
|
|
|
|
|
# Read power conumption
|
2020-05-03 11:15:52 +02:00
|
|
|
if util.format.asbool(self.parameter("showpowerconsumption", False)):
|
|
|
|
output = "{}% ({})".format(
|
|
|
|
capacity, self.__manager.consumption(widget.name)
|
|
|
|
)
|
2023-11-08 22:46:58 +01:00
|
|
|
elif capacity < 100:
|
2020-05-03 11:15:52 +02:00
|
|
|
output = "{}%".format(capacity)
|
2023-11-08 22:46:58 +01:00
|
|
|
else:
|
|
|
|
output = ""
|
2020-03-06 20:34:30 +01:00
|
|
|
|
2020-05-03 11:15:52 +02:00
|
|
|
if (
|
|
|
|
util.format.asbool(self.parameter("showremaining", True))
|
|
|
|
and self.__manager.charge(widget.name) == "Discharging"
|
|
|
|
):
|
2020-04-19 14:07:15 +02:00
|
|
|
remaining = self.__manager.remaining()
|
|
|
|
if remaining >= 0:
|
2020-05-03 11:15:52 +02:00
|
|
|
output = "{} {}".format(
|
|
|
|
output, util.format.duration(remaining, compact=True, unit=True)
|
|
|
|
)
|
2020-03-06 20:34:30 +01:00
|
|
|
|
2023-11-08 22:46:58 +01:00
|
|
|
# if bumblebee.util.asbool(self.parameter("rate", True)):
|
|
|
|
# try:
|
|
|
|
# with open("{}/power_now".format(widget.name)) as f:
|
|
|
|
# rate = (float(f.read())/1000000)
|
|
|
|
# if rate > 0:
|
|
|
|
# output = "{} {:.2f}w".format(output, rate)
|
|
|
|
# except Exception:
|
|
|
|
# pass
|
|
|
|
|
|
|
|
|
2020-05-03 11:15:52 +02:00
|
|
|
if util.format.asbool(self.parameter("showdevice", False)):
|
|
|
|
output = "{} ({})".format(output, widget.name)
|
2020-03-06 20:34:30 +01:00
|
|
|
|
|
|
|
return output
|
2020-05-03 11:15:52 +02:00
|
|
|
|
2020-03-06 20:34:30 +01:00
|
|
|
def state(self, widget):
|
|
|
|
state = []
|
2020-05-03 11:15:52 +02:00
|
|
|
capacity = widget.get("capacity")
|
2020-03-06 20:34:30 +01:00
|
|
|
|
2023-11-08 22:46:58 +01:00
|
|
|
if widget.get("pen"):
|
|
|
|
state.append("PEN")
|
|
|
|
|
2020-03-06 20:34:30 +01:00
|
|
|
if capacity < 0:
|
2020-05-03 11:15:52 +02:00
|
|
|
log.debug("battery state: {}".format(state))
|
|
|
|
return ["critical", "unknown"]
|
2020-03-06 20:34:30 +01:00
|
|
|
|
2020-05-03 11:15:52 +02:00
|
|
|
if widget.get("ac"):
|
|
|
|
state.append("AC")
|
2020-03-06 20:34:30 +01:00
|
|
|
else:
|
2020-05-03 11:15:52 +02:00
|
|
|
if widget.name == "all-batteries":
|
2020-04-19 14:21:50 +02:00
|
|
|
charge = self.__manager.charge_any(self._batteries)
|
|
|
|
else:
|
2020-04-30 12:42:34 +02:00
|
|
|
charge = self.__manager.charge(widget.name)
|
2023-11-08 22:46:58 +01:00
|
|
|
if charge in ["Discharging", "Unknown"]:
|
2020-05-03 11:15:52 +02:00
|
|
|
state.append(
|
|
|
|
"discharging-{}".format(
|
2023-11-08 22:46:58 +01:00
|
|
|
min([5, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100], key=lambda i: abs(i - capacity))
|
2020-05-03 11:15:52 +02:00
|
|
|
)
|
|
|
|
)
|
2020-03-06 20:34:30 +01:00
|
|
|
else:
|
|
|
|
if capacity > 95:
|
2020-05-03 11:15:52 +02:00
|
|
|
state.append("charged")
|
2020-03-06 20:34:30 +01:00
|
|
|
else:
|
2020-05-03 11:15:52 +02:00
|
|
|
state.append("charging")
|
2022-09-16 21:36:06 +02:00
|
|
|
|
|
|
|
if (
|
|
|
|
capacity < int(self.parameter("critical", 10))
|
|
|
|
and self.__manager.charge_any(self._batteries) == "Discharging"
|
|
|
|
):
|
|
|
|
state.append("critical")
|
|
|
|
elif (
|
|
|
|
capacity < int(self.parameter("warning", 20))
|
|
|
|
and self.__manager.charge_any(self._batteries) == "Discharging"
|
|
|
|
):
|
|
|
|
state.append("warning")
|
|
|
|
|
2020-03-06 20:34:30 +01:00
|
|
|
return state
|
|
|
|
|
2020-05-03 11:15:52 +02:00
|
|
|
|
2020-03-06 20:34:30 +01:00
|
|
|
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4
|