From 1e0228a3c732e09b7e4bf6b402251c3dc7c76cb8 Mon Sep 17 00:00:00 2001 From: Mook Date: Sat, 28 Nov 2015 12:07:01 -0800 Subject: [PATCH] [Battery - Linux] Look at battery status instead of AC online-ness The names of AC adapters are straight from ACPI and are sometimes called "ADPx" instead of "ACx". To avoid confusion, look at the battery status instead. Check for a power supply with a capacity instead of names that start with "BAT" for the same reason. Fixes #1498 --- powerline/segments/common/bat.py | 35 +++++++++++++++----------------- 1 file changed, 16 insertions(+), 19 deletions(-) diff --git a/powerline/segments/common/bat.py b/powerline/segments/common/bat.py index 0411fcd3..ac426a05 100644 --- a/powerline/segments/common/bat.py +++ b/powerline/segments/common/bat.py @@ -62,27 +62,24 @@ def _fetch_battery_info(pl): pl.debug('Not using DBUS+UPower as no batteries were found') if os.path.isdir('/sys/class/power_supply'): - online_path_verified = None - linux_supplier_fmt = '/sys/class/power_supply/{0}/capacity' - linux_ac_fmt = '/sys/class/power_supply/{0}/online' + linux_capacity_fmt = '/sys/class/power_supply/{0}/capacity' + linux_status_fmt = '/sys/class/power_supply/{0}/status' for linux_supplier in os.listdir('/sys/class/power_supply'): - cap_path = linux_supplier_fmt.format(linux_supplier) - online_path = linux_ac_fmt.format(linux_supplier) - if linux_supplier.startswith('AC') and os.path.exists(online_path): - pl.debug('Using /sys/class/power_supply with AC {0}', online_path) - online_path_verified = online_path - elif linux_supplier.startswith('BAT') and os.path.exists(cap_path): - pl.debug('Using /sys/class/power_supply with battery {0}', linux_supplier) - - def _get_battery_status(pl): + cap_path = linux_capacity_fmt.format(linux_supplier) + status_path = linux_status_fmt.format(linux_supplier) + if not os.path.exists(cap_path): + continue + pl.debug('Using /sys/class/power_supply with battery {0}', linux_supplier) + def _get_battery_status(pl): + with open(cap_path, 'r') as f: + _capacity = int(float(f.readline().split()[0])) + try: + with open(status_path, 'r') as f: + _ac_powered = (f.readline().strip() != 'Discharging') + except IOError: _ac_powered = None - with open(cap_path, 'r') as f: - _capacity = int(float(f.readline().split()[0])) - if online_path_verified: - with open(online_path_verified, 'r') as f: - _ac_powered = int(f.readline()) == 1 - return _capacity, _ac_powered - return _get_battery_status + return _capacity, _ac_powered + return _get_battery_status pl.debug('Not using /sys/class/power_supply as no batteries were found') else: pl.debug('Not using /sys/class/power_supply: no directory')