summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorBenjamin Berg <bberg@redhat.com>2022-06-15 15:53:17 +0200
committerBenjamin Berg <bberg@redhat.com>2022-06-21 11:16:27 +0200
commit8ec7362e93a8ac942d77b9766b4103d6ce55da36 (patch)
tree87ad44d96ccc1ead7d457818f69ec3f1ce4103c9 /src
parent14f06728b58957f530c48aad63ab14dab131ee9c (diff)
battery: Add full/empty guessing based on capacity
Assuming we have some estimation for the current battery capacity (i.e. percentage), we can infer a FULL/EMPTY state. Do so if the battery state is unknown. Related: #196
Diffstat (limited to 'src')
-rwxr-xr-xsrc/linux/integration-test.py34
-rw-r--r--src/up-device-battery.c18
2 files changed, 48 insertions, 4 deletions
diff --git a/src/linux/integration-test.py b/src/linux/integration-test.py
index 2ad5845..ea275e7 100755
--- a/src/linux/integration-test.py
+++ b/src/linux/integration-test.py
@@ -653,6 +653,40 @@ class Tests(dbusmock.DBusTestCase):
self.assertEqual(self.get_dbus_display_property('State'), UP_DEVICE_STATE_PENDING_CHARGE)
self.stop_daemon()
+ def test_empty_guessing(self):
+ '''One empty batter not reporting a state'''
+
+ self.testbed.add_device('power_supply', 'BAT0', None,
+ ['type', 'Battery',
+ 'present', '1',
+ 'status', 'Unknown',
+ 'charge_full', '10500000',
+ 'charge_full_design', '11000000',
+ 'capacity', '0',
+ 'voltage_now', '12000000'], [])
+
+ self.start_daemon()
+ self.assertDevs({ 'battery_BAT0': { 'State' : UP_DEVICE_STATE_EMPTY } })
+ self.assertEqual(self.get_dbus_display_property('State'), UP_DEVICE_STATE_EMPTY)
+ self.stop_daemon()
+
+ def test_full_guessing(self):
+ '''One full batter not reporting a state'''
+
+ self.testbed.add_device('power_supply', 'BAT0', None,
+ ['type', 'Battery',
+ 'present', '1',
+ 'status', 'Unknown',
+ 'charge_full', '10500000',
+ 'charge_full_design', '11000000',
+ 'capacity', '99',
+ 'voltage_now', '12000000'], [])
+
+ self.start_daemon()
+ self.assertDevs({ 'battery_BAT0': { 'State' : UP_DEVICE_STATE_FULLY_CHARGED } })
+ self.assertEqual(self.get_dbus_display_property('State'), UP_DEVICE_STATE_FULLY_CHARGED)
+ self.stop_daemon()
+
def test_display_state_aggregation(self):
bat0 = self.testbed.add_device('power_supply', 'BAT0', None,
['type', 'Battery',
diff --git a/src/up-device-battery.c b/src/up-device-battery.c
index 544dedd..c3dc667 100644
--- a/src/up-device-battery.c
+++ b/src/up-device-battery.c
@@ -323,10 +323,6 @@ up_device_battery_report (UpDeviceBattery *self,
if (values->percentage <= 0)
values->percentage = values->energy.cur / priv->energy_full * 100;
- /* QUIRK: Some devices keep reporting PENDING_CHARGE even when full */
- if (values->state == UP_DEVICE_STATE_PENDING_CHARGE && values->percentage >= UP_FULLY_CHARGED_THRESHOLD)
- values->state = UP_DEVICE_STATE_FULLY_CHARGED;
-
/* NOTE: We used to do more for the UNKNOWN state. However, some of the
* logic relies on only one battery device to be present. Plus, it
* requires knowing the AC state.
@@ -343,6 +339,20 @@ up_device_battery_report (UpDeviceBattery *self,
/* Do estimations */
up_device_battery_estimate (self, &values->state);
+ /* QUIRK: Do a FULL/EMPTY guess if the state is still unknown
+ * Maybe limit to when we have good estimates
+ * (would require rate/current information) */
+ if (values->state == UP_DEVICE_STATE_UNKNOWN) {
+ if (values->percentage >= UP_FULLY_CHARGED_THRESHOLD)
+ values->state = UP_DEVICE_STATE_FULLY_CHARGED;
+ else if (values->percentage < 1.0)
+ values->state = UP_DEVICE_STATE_EMPTY;
+ }
+
+ /* QUIRK: Some devices keep reporting PENDING_CHARGE even when full */
+ if (values->state == UP_DEVICE_STATE_PENDING_CHARGE && values->percentage >= UP_FULLY_CHARGED_THRESHOLD)
+ values->state = UP_DEVICE_STATE_FULLY_CHARGED;
+
/* Set the main properties (setting "update-time" last) */
g_object_set (self,
"energy", values->energy.cur,