[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
This commit is contained in:
Mook 2015-11-28 12:07:01 -08:00
parent eaa772f195
commit 1e0228a3c7
1 changed files with 16 additions and 19 deletions

View File

@ -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')