Skip to content

Commit

Permalink
Added a method to check battery status.
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 487569334
  • Loading branch information
hanszolo authored and copybara-github committed Nov 10, 2022
1 parent 43253c8 commit 04f3923
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 1 deletion.
19 changes: 19 additions & 0 deletions gwinpy/wmi/hw_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,25 @@ def IsVirtualMachine(self):
logging.debug('No virtual hardware detected.')
return False

def IsOnBattery(self):
"""Detect whether the local machine appears to be on battery.
Returns:
true if on battery; else false
"""
query = 'SELECT BatteryStatus FROM Win32_Battery'
unplugged_statuses = [1, 13, 14, 15, 16, 17] # on battery or battery saver
results = self.wmi.Query(query)
if results:
for result in results:
logging.debug('Found Win32_Battery with BatteryStatus: %s',
result.BatteryStatus)
if result.BatteryStatus in unplugged_statuses:
return True
else:
logging.debug('No Win32_Battery detected.')
return False

def LenovoSystemModel(self):
"""Get the Lenovo-specific common model name (instead of the model number).
Expand Down
16 changes: 16 additions & 0 deletions gwinpy/wmi/hw_info_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,22 @@ def testIsVirtualMachine(self):
model.return_value = 'HP Z620 Workstation'
self.assertFalse(self.hwinfo.IsVirtualMachine())

def testIsOnBattery(self):
self.hwinfo.wmi.Query.return_value = [mock.Mock(BatteryStatus=1)]
self.assertTrue(self.hwinfo.IsOnBattery())
self.hwinfo.wmi.Query.return_value = [
mock.Mock(BatteryStatus=2),
mock.Mock(BatteryStatus=2),
mock.Mock(BatteryStatus=13)
]
self.assertTrue(self.hwinfo.IsOnBattery())
self.hwinfo.wmi.Query.return_value = [mock.Mock(BatteryStatus=2)]
self.assertFalse(self.hwinfo.IsOnBattery())
self.hwinfo.wmi.Query.return_value = []
self.assertFalse(self.hwinfo.IsOnBattery())
self.hwinfo.wmi.Query.return_value = None
self.assertFalse(self.hwinfo.IsOnBattery())

def testLenovoSystemModel(self):
self.hwinfo.wmi.Query.return_value = [mock.Mock(Version='ThinkPad T430s')]
self.assertEqual(self.hwinfo.LenovoSystemModel(), 'ThinkPad T430s')
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

distutils.core.setup(
name='gwinpy',
version='0.3.5',
version='0.3.6',
packages=['gwinpy', 'gwinpy.net', 'gwinpy.registry', 'gwinpy.wmi'],
license='Apache License',
url='https://github.com/google/winops/',
Expand Down

0 comments on commit 04f3923

Please sign in to comment.