Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions power/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
26 changes: 21 additions & 5 deletions power/linux.py
Original file line number Diff line number Diff line change
Expand Up @@ -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_*
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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)
Expand Down Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions power/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down