diff --git a/power/common.py b/power/common.py index 4874987..21a0f1b 100644 --- a/power/common.py +++ b/power/common.py @@ -133,6 +133,12 @@ def remove_observer(self, observer): """ self._weak_observers.remove(weakref.ref(observer)) + @abstractmethod + def get_remaining_percentage(self): + """ + Returns the remaining percentage of power of your battery or batteries + """ + def remove_all_observers(self): """ Removes all registered observers. diff --git a/power/linux.py b/power/linux.py index 7159f91..ef44e1a 100644 --- a/power/linux.py +++ b/power/linux.py @@ -19,7 +19,7 @@ class PowerManagement(common.PowerManagementBase): @staticmethod - def power_source_type(supply_path): + def power_source_type(supply_path=POWER_SUPPLY_PATH): """ @param supply_path: Path to power supply @return: One of common.POWER_TYPE_* @@ -37,7 +37,7 @@ def power_source_type(supply_path): raise RuntimeError("Type of {path} ({type}) is not supported".format(path=supply_path, type=type)) @staticmethod - def is_ac_online(supply_path): + def is_ac_online(supply_path=POWER_SUPPLY_PATH): """ @param supply_path: Path to power supply @return: True if ac is online. Otherwise False @@ -46,7 +46,7 @@ def is_ac_online(supply_path): return online_file.readline().strip() == '1' @staticmethod - def is_battery_present(supply_path): + def is_battery_present(supply_path=POWER_SUPPLY_PATH): """ @param supply_path: Path to power supply @return: True if battery is present. Otherwise False @@ -55,7 +55,7 @@ def is_battery_present(supply_path): return present_file.readline().strip() == '1' @staticmethod - def is_battery_discharging(supply_path): + def is_battery_discharging(supply_path=POWER_SUPPLY_PATH): """ @param supply_path: Path to power supply @return: True if ac is online. Otherwise False @@ -64,7 +64,7 @@ def is_battery_discharging(supply_path): return status_file.readline().strip() == 'Discharging' @staticmethod - def get_battery_state(supply_path): + def get_battery_state(supply_path=POWER_SUPPLY_PATH): """ @param supply_path: Path to power supply @return: Tuple (energy_full, energy_now, power_now) @@ -179,6 +179,22 @@ def get_time_remaining_estimate(self): else: return common.TIME_REMAINING_UNKNOWN + + def get_remaining_percentage(self): + all_energy_full = [] + all_energy_current = [] + for supply in os.listdir(POWER_SUPPLY_PATH): + supply_path = os.path.join(POWER_SUPPLY_PATH, supply) + try: + supp_type = self.power_source_type(supply_path) + if supp_type == common.POWER_TYPE_BATTERY: + energy_full, energy_now, power_now = self.get_battery_state(supply_path) + all_energy_full.append(energy_full) + all_energy_current.append(energy_now) + except (RuntimeError, IOError) as e: + warnings.warn("Unable to read properties of {path}: {error}".format(path=supply_path, error=str(e))) + return (sum(all_energy_current) / sum(all_energy_full)) + def add_observer(self, observer): warnings.warn("Current system does not support observing.") pass diff --git a/power/tests.py b/power/tests.py index 5f00131..f5e2175 100644 --- a/power/tests.py +++ b/power/tests.py @@ -26,6 +26,12 @@ def testGetProvidingPowerSource(self): self.assertIsInstance(type, int) self.assertIn(type, [power.POWER_TYPE_AC, power.POWER_TYPE_BATTERY, power.POWER_TYPE_UPS]) + def testGetRemainingPercentage(self): + rem_percentage = power.PowerManagement().get_remaining_percentage() + self.assertIsNotNone(rem_percentage) + self.assertIsInstance(rem_percentage, float) + self.assertTrue(rem_percentage >= 0 and rem_percentage <= 100) + class TestObserver(power.PowerManagementObserver): def on_power_sources_change(self, power_management):