mirror of
https://gitlab.freedesktop.org/upower/upower.git
synced 2025-12-26 12:00:06 +01:00
linux: Fix batteries which report capacity, but not energy/charge
Many Android devices only export charge_full and capacity, but not charge_now or energy_now. In that case, directly read the percentage (from the capacity attribute) and calculate current energy from that. Thanks to Seth Forshee for the original patch! https://bugs.freedesktop.org/show_bug.cgi?id=68337
This commit is contained in:
parent
aa1656f6ed
commit
07b95b8e27
2 changed files with 36 additions and 8 deletions
|
|
@ -408,6 +408,36 @@ class Tests(unittest.TestCase):
|
|||
self.assertEqual(self.get_dbus_dev_property(bat0_up, 'Percentage'), 40.0)
|
||||
self.stop_daemon()
|
||||
|
||||
def test_battery_capacity_and_charge(self):
|
||||
'''battery which reports capacity and charge_full'''
|
||||
|
||||
bat0 = self.testbed.add_device('power_supply', 'BAT0', None,
|
||||
['type', 'Battery',
|
||||
'present', '1',
|
||||
'status', 'Discharging',
|
||||
'charge_full', '10500000',
|
||||
'charge_full_design', '11000000',
|
||||
'capacity', '40',
|
||||
'voltage_now', '12000000'], [])
|
||||
|
||||
self.start_daemon()
|
||||
devs = self.proxy.EnumerateDevices()
|
||||
self.assertEqual(len(devs), 1)
|
||||
bat0_up = devs[0]
|
||||
|
||||
self.assertEqual(self.get_dbus_dev_property(bat0_up, 'Percentage'), 40.0)
|
||||
self.assertEqual(self.get_dbus_dev_property(bat0_up, 'IsPresent'), True)
|
||||
self.assertEqual(self.get_dbus_dev_property(bat0_up, 'State'), UP_DEVICE_STATE_DISCHARGING)
|
||||
self.assertEqual(self.get_dbus_dev_property(bat0_up, 'Energy'), 50.4)
|
||||
self.assertEqual(self.get_dbus_dev_property(bat0_up, 'EnergyFull'), 126.0)
|
||||
self.assertEqual(self.get_dbus_dev_property(bat0_up, 'EnergyFullDesign'), 132.0)
|
||||
self.assertEqual(self.get_dbus_dev_property(bat0_up, 'Voltage'), 12.0)
|
||||
self.assertEqual(self.get_dbus_dev_property(bat0_up, 'NativePath'), bat0)
|
||||
|
||||
self.assertEqual(self.get_dbus_property('OnBattery'), True)
|
||||
#self.assertEqual(self.get_dbus_property('OnLowBattery'), False)
|
||||
self.stop_daemon()
|
||||
|
||||
def test_ups_ac(self):
|
||||
'''UPS properties with and without AC'''
|
||||
|
||||
|
|
|
|||
|
|
@ -693,7 +693,12 @@ up_device_supply_refresh_battery (UpDeviceSupply *supply)
|
|||
energy_rate = up_device_supply_calculate_rate (supply, energy);
|
||||
|
||||
/* get a precise percentage */
|
||||
if (energy_full > 0.0f) {
|
||||
if (sysfs_file_exists (native_path, "capacity")) {
|
||||
percentage = sysfs_get_double (native_path, "capacity");
|
||||
/* for devices which provide capacity, but not {energy,charge}_now */
|
||||
if (energy < 0.1f && energy_full > 0.0f)
|
||||
energy = energy_full * percentage / 100;
|
||||
} else if (energy_full > 0.0f) {
|
||||
percentage = 100.0 * energy / energy_full;
|
||||
if (percentage < 0.0f)
|
||||
percentage = 0.0f;
|
||||
|
|
@ -701,13 +706,6 @@ up_device_supply_refresh_battery (UpDeviceSupply *supply)
|
|||
percentage = 100.0f;
|
||||
}
|
||||
|
||||
/* device is a peripheral and not providing power to the computer */
|
||||
if (energy < 0.01f &&
|
||||
energy_rate < 0.01f &&
|
||||
energy_full < 0.01f) {
|
||||
percentage = sysfs_get_double (native_path, "capacity");
|
||||
}
|
||||
|
||||
/* the battery isn't charging or discharging, it's just
|
||||
* sitting there half full doing nothing: try to guess a state */
|
||||
if (state == UP_DEVICE_STATE_UNKNOWN) {
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue