Allowed for specifying the scheme for powercfg

This commit is contained in:
Daniel Hobley 2016-10-18 09:49:07 +02:00
parent ee9eb7c413
commit a133a2466e
3 changed files with 124 additions and 38 deletions

View File

@ -41,6 +41,9 @@ def _get_powercfg_minute_values(scheme, guid, subguid, safe_name):
'''
Returns the AC/DC values in an array for a guid and subguid for a the given scheme
'''
if scheme is None:
scheme = _get_current_scheme()
if __grains__['osrelease'] == '7':
cmd = "powercfg /q {0} {1}".format(scheme, guid)
else:
@ -60,18 +63,21 @@ def _get_powercfg_minute_values(scheme, guid, subguid, safe_name):
return {"ac": int(raw_settings[0], 0) / 60, "dc": int(raw_settings[1], 0) / 60}
def _set_powercfg_value(setting, power, value):
def _set_powercfg_value(scheme, sub_group, setting_guid, power, value):
'''
Sets the value of a setting with a given power (ac/dc) to
the current scheme
the given scheme
'''
cmd = "powercfg /x {0}-{1} {2}".format(setting, power, value)
if scheme is None:
scheme = _get_current_scheme()
cmd = "powercfg /set{0}valueindex {1} {2} {3} {4}".format(power, scheme, sub_group, setting_guid, value)
return __salt__['cmd.run'](cmd, python_shell=False)
def set_monitor_timeout(timeout, power="ac"):
def set_monitor_timeout(timeout, power="ac", scheme=None):
'''
Set the monitor timeout in minutes for the current power scheme
Set the monitor timeout in minutes for the given power scheme
CLI Example:
@ -85,26 +91,32 @@ def set_monitor_timeout(timeout, power="ac"):
power
Should we set the value for AC or DC (battery)? Valid options ac,dc.
scheme
The scheme to use, leave as None to use the current.
'''
return _set_powercfg_value("monitor-timeout", power, timeout)
return _set_powercfg_value(scheme, "SUB_VIDEO", "VIDEOIDLE", power, timeout)
def get_monitor_timeout():
def get_monitor_timeout(scheme=None):
'''
Get the current monitor timeout of the current scheme
Get the current monitor timeout of the given scheme
CLI Example:
.. code-block:: bash
salt '*' powercfg.get_monitor_timeout
scheme
The scheme to use, leave as None to use the current.
'''
return _get_powercfg_minute_values(_get_current_scheme(), "SUB_VIDEO", "VIDEOIDLE", "Turn off display after")
return _get_powercfg_minute_values(scheme, "SUB_VIDEO", "VIDEOIDLE", "Turn off display after")
def set_disk_timeout(timeout, power="ac"):
def set_disk_timeout(timeout, power="ac", scheme=None):
'''
Set the disk timeout in minutes for the current power scheme
Set the disk timeout in minutes for the given power scheme
CLI Example:
@ -118,26 +130,32 @@ def set_disk_timeout(timeout, power="ac"):
power
Should we set the value for AC or DC (battery)? Valid options ac,dc.
scheme
The scheme to use, leave as None to use the current.
'''
return _set_powercfg_value("disk-timeout", power, timeout)
return _set_powercfg_value(scheme, "SUB_DISK", "DISKIDLE", power, timeout)
def get_disk_timeout():
def get_disk_timeout(scheme=None):
'''
Get the current disk timeout of the current scheme
Get the current disk timeout of the given scheme
CLI Example:
.. code-block:: bash
salt '*' powercfg.get_disk_timeout
scheme
The scheme to use, leave as None to use the current.
'''
return _get_powercfg_minute_values(_get_current_scheme(), "SUB_DISK", "DISKIDLE", "Turn off hard disk after")
return _get_powercfg_minute_values(scheme, "SUB_DISK", "DISKIDLE", "Turn off hard disk after")
def set_standby_timeout(timeout, power="ac"):
def set_standby_timeout(timeout, power="ac", scheme=None):
'''
Set the standby timeout in minutes for the current power scheme
Set the standby timeout in minutes for the given power scheme
CLI Example:
@ -151,26 +169,32 @@ def set_standby_timeout(timeout, power="ac"):
power
Should we set the value for AC or DC (battery)? Valid options ac,dc.
scheme
The scheme to use, leave as None to use the current.
'''
return _set_powercfg_value("standby-timeout", power, timeout)
return _set_powercfg_value(scheme, "SUB_SLEEP", "STANDBYIDLE", power, timeout)
def get_standby_timeout():
def get_standby_timeout(scheme=None):
'''
Get the current standby timeout of the current scheme
Get the current standby timeout of the given scheme
CLI Example:
.. code-block:: bash
salt '*' powercfg.get_standby_timeout
scheme
The scheme to use, leave as None to use the current.
'''
return _get_powercfg_minute_values(_get_current_scheme(), "SUB_SLEEP", "STANDBYIDLE", "Sleep after")
return _get_powercfg_minute_values(scheme, "SUB_SLEEP", "STANDBYIDLE", "Sleep after")
def set_hibernate_timeout(timeout, power="ac"):
def set_hibernate_timeout(timeout, power="ac", scheme=None):
'''
Set the hibernate timeout in minutes for the current power scheme
Set the hibernate timeout in minutes for the given power scheme
CLI Example:
@ -184,18 +208,23 @@ def set_hibernate_timeout(timeout, power="ac"):
power
Should we set the value for AC or DC (battery)? Valid options ac,dc.
scheme
The scheme to use, leave as None to use the current.
'''
return _set_powercfg_value("hibernate-timeout", power, timeout)
return _set_powercfg_value(scheme, "SUB_SLEEP", "HIBERNATEIDLE", power, timeout)
def get_hibernate_timeout():
def get_hibernate_timeout(scheme=None):
'''
Get the current hibernate timeout of the current scheme
Get the current hibernate timeout of the given scheme
CLI Example:
.. code-block:: bash
salt '*' powercfg.get_hibernate_timeout
scheme
The scheme to use, leave as None to use the current.
'''
return _get_powercfg_minute_values(_get_current_scheme(), "SUB_SLEEP", "HIBERNATEIDLE", "Hibernate after")
return _get_powercfg_minute_values(scheme, "SUB_SLEEP", "HIBERNATEIDLE", "Hibernate after")

View File

@ -41,7 +41,7 @@ def _check_or_set(check_func, set_func, value, power):
return False
def set_timeout(name, value, power="ac"):
def set_timeout(name, value, power="ac", scheme=None):
'''
Set the sleep timeouts of specific items such as disk, monitor.
@ -67,6 +67,9 @@ def set_timeout(name, value, power="ac"):
power
Should we set the value for AC or DC (battery)? Valid options ac,dc.
scheme
The scheme to use, leave as None to use the current.
'''
ret = {'name': name,
'result': True,
@ -85,12 +88,12 @@ def set_timeout(name, value, power="ac"):
check_func = __salt__["powercfg.get_{0}_timeout".format(name)]
set_func = __salt__["powercfg.set_{0}_timeout".format(name)]
values = check_func()
values = check_func(scheme=scheme)
if values[power] == value:
comment.append("{0} {1} is already set with the value {2}.".format(name, power, value))
else:
ret['changes'] = {name: {power: value}}
set_func(value, power)
set_func(value, power, scheme=scheme)
ret['comment'] = ' '.join(comment)
return ret

View File

@ -43,37 +43,61 @@ class PowerCfgTestCase(TestCase):
'''
Test to make sure we can set the monitor timeout value
'''
mock = MagicMock(return_value="")
mock = MagicMock()
mock.side_effect = ["Power Scheme GUID: 381b4222-f694-41f0-9685-ff5bb260df2e (Balanced)", self.query_ouput]
with patch.dict(powercfg.__salt__, {'cmd.run': mock}):
powercfg.set_monitor_timeout(0, "dc")
mock.assert_called_once_with('powercfg /x monitor-timeout-dc 0', python_shell=False)
calls = [
call('powercfg /getactivescheme', python_shell=False),
call('powercfg /setdcvalueindex 381b4222-f694-41f0-9685-ff5bb260df2e SUB_VIDEO VIDEOIDLE 0', python_shell=False)
]
mock.assert_has_calls(calls)
def test_set_disk_timeout(self):
'''
Test to make sure we can set the disk timeout value
'''
mock = MagicMock(return_value="")
mock = MagicMock()
mock.side_effect = ["Power Scheme GUID: 381b4222-f694-41f0-9685-ff5bb260df2e (Balanced)", self.query_ouput]
with patch.dict(powercfg.__salt__, {'cmd.run': mock}):
powercfg.set_disk_timeout(0, "dc")
mock.assert_called_once_with('powercfg /x disk-timeout-dc 0', python_shell=False)
calls = [
call('powercfg /getactivescheme', python_shell=False),
call('powercfg /setdcvalueindex 381b4222-f694-41f0-9685-ff5bb260df2e SUB_DISK DISKIDLE 0', python_shell=False)
]
mock.assert_has_calls(calls)
def test_set_standby_timeout(self):
'''
Test to make sure we can set the standby timeout value
'''
mock = MagicMock(return_value="")
mock = MagicMock()
mock.side_effect = ["Power Scheme GUID: 381b4222-f694-41f0-9685-ff5bb260df2e (Balanced)", self.query_ouput]
with patch.dict(powercfg.__salt__, {'cmd.run': mock}):
powercfg.set_standby_timeout(0, "dc")
mock.assert_called_once_with('powercfg /x standby-timeout-dc 0', python_shell=False)
calls = [
call('powercfg /getactivescheme', python_shell=False),
call('powercfg /setdcvalueindex 381b4222-f694-41f0-9685-ff5bb260df2e SUB_SLEEP STANDBYIDLE 0', python_shell=False)
]
mock.assert_has_calls(calls)
def test_set_hibernate_timeout(self):
'''
Test to make sure we can set the hibernate timeout value
'''
mock = MagicMock(return_value="")
mock = MagicMock()
mock.side_effect = ["Power Scheme GUID: 381b4222-f694-41f0-9685-ff5bb260df2e (Balanced)", self.query_ouput]
with patch.dict(powercfg.__salt__, {'cmd.run': mock}):
powercfg.set_hibernate_timeout(0, "dc")
mock.assert_called_once_with('powercfg /x hibernate-timeout-dc 0', python_shell=False)
calls = [
call('powercfg /getactivescheme', python_shell=False),
call('powercfg /setdcvalueindex 381b4222-f694-41f0-9685-ff5bb260df2e SUB_SLEEP HIBERNATEIDLE 0', python_shell=False)
]
mock.assert_has_calls(calls)
def test_get_monitor_timeout(self):
'''
@ -161,6 +185,36 @@ class PowerCfgTestCase(TestCase):
self.assertEqual({'ac': 30, 'dc': 15}, ret)
def test_set_hibernate_timeout_scheme(self):
'''
Test to make sure we can set the hibernate timeout value
'''
mock = MagicMock()
mock.side_effect = [self.query_ouput]
with patch.dict(powercfg.__salt__, {'cmd.run': mock}):
powercfg.set_hibernate_timeout(0, "dc", scheme="SCHEME_MIN")
calls = [
call('powercfg /setdcvalueindex SCHEME_MIN SUB_SLEEP HIBERNATEIDLE 0', python_shell=False)
]
mock.assert_has_calls(calls)
def test_get_hibernate_timeout_scheme(self):
'''
Test to make sure we can get the hibernate timeout value with a specified scheme
'''
mock = MagicMock()
mock.side_effect = [self.query_ouput]
with patch.dict(powercfg.__salt__, {'cmd.run': mock}):
ret = powercfg.get_hibernate_timeout(scheme="SCHEME_MIN")
calls = [
call('powercfg /q SCHEME_MIN SUB_SLEEP HIBERNATEIDLE', python_shell=False)
]
mock.assert_has_calls(calls)
self.assertEqual({'ac': 30, 'dc': 15}, ret)
if __name__ == '__main__':
from integration import run_tests
run_tests(PowerCfgTestCase, needs_daemon=False)