From 2ff5ea0262268ef58a4bbd2d4e4c3f1d9dd9fa3a Mon Sep 17 00:00:00 2001 From: pajowu Date: Wed, 3 May 2017 10:36:35 +0300 Subject: [PATCH] Calculate battery percentage from energy instead of average of batteries (#563) * Calculate battery percentage from energy instead of average of batteries Average only works if all batteries have the same max energy level. If setups with different sizes the smaller ones influence the percentage overproportianly strong * check for battery type before calculating percentage, use old way if needed * translate charge into energy to always calculate correct percentage --- i3pystatus/battery.py | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/i3pystatus/battery.py b/i3pystatus/battery.py index c45ec76..f1fd8ce 100644 --- a/i3pystatus/battery.py +++ b/i3pystatus/battery.py @@ -78,6 +78,9 @@ class BatteryCharge(Battery): def wh_remaining(self): return self.battery_info['CHARGE_NOW'] * self.battery_info['VOLTAGE_NOW'] + def wh_total(self): + return self.battery_info['CHARGE_FULL'] * self.battery_info['VOLTAGE_NOW'] + def wh_depleted(self): return (self.battery_info['CHARGE_FULL'] - self.battery_info['CHARGE_NOW']) * self.battery_info['VOLTAGE_NOW'] @@ -103,6 +106,9 @@ class BatteryEnergy(Battery): def wh_remaining(self): return self.battery_info['ENERGY_NOW'] + def wh_total(self): + return self.battery_info['ENERGY_FULL'] + def wh_depleted(self): return self.battery_info['ENERGY_FULL'] - self.battery_info['ENERGY_NOW'] @@ -220,10 +226,9 @@ class BatteryChecker(IntervalModule): paths = [] def percentage(self, batteries, design=False): - total = 0 - for battery in batteries: - total += battery.percentage(design) - return total / len(batteries) + total_now = [battery.wh_remaining() for battery in batteries] + total_full = [battery.wh_total() for battery in batteries] + return sum(total_now) / sum(total_full) * 100 def consumption(self, batteries): consumption = 0