upower as fallback battery check

This commit is contained in:
piotr
2021-11-16 03:03:40 +01:00
parent 8031a73bc5
commit f786261f61
2 changed files with 36 additions and 16 deletions

View File

@@ -28,7 +28,8 @@ commands = {
"playerctl": False,
"netifaces": False,
"pybluez": False,
"wlr-randr": False
"wlr-randr": False,
"upower": False
}
icons_path = "" # "icons_light", "icons_dark" or "" (GTK icons)

View File

@@ -280,11 +280,11 @@ def list_outputs(sway=False, tree=None, silent=False):
and transform is not None:
if transform == "normal":
outputs_dict[name] = {'name': name,
'x': x,
'y': y,
'width': w,
'height': h,
'transform': transform}
'x': x,
'y': y,
'width': w,
'height': h,
'transform': transform}
else:
outputs_dict[name] = {'name': name,
'x': x,
@@ -450,19 +450,38 @@ def set_brightness(slider):
def get_battery():
percent, time, charging = 0, "", False
success = False
try:
b = psutil.sensors_battery()
percent = int(round(b.percent, 0))
charging = b.power_plugged
seconds = b.secsleft
if seconds != psutil.POWER_TIME_UNLIMITED and seconds != psutil.POWER_TIME_UNKNOWN:
time = seconds2string(seconds)
else:
time = ""
return percent, time, charging
if b:
percent = int(round(b.percent, 0))
charging = b.power_plugged
seconds = b.secsleft
if seconds != psutil.POWER_TIME_UNLIMITED and seconds != psutil.POWER_TIME_UNKNOWN:
time = seconds2string(seconds)
else:
time = ""
success = True
except:
return 0, "", False
pass
if not success and nwg_panel.common.commands["upower"]:
lines = subprocess.check_output(
"upower -i $(upower -e | grep devices/battery) | grep --color=never -E 'state|to\ full|to\ empty|percentage'",
shell=True).decode("utf-8").strip().splitlines()
for line in lines:
if "state:" in line:
charging = line.split(":")[1].strip() == "charging"
elif "time to" in line:
time = line.split(":")[1].strip()
elif "percentage:" in line:
try:
percent = int(line.split(":")[1].strip()[:-1])
except:
pass
return percent, time, charging
def seconds2string(seconds):