From a18a8f8156abed7a0e350af44584d21cf3911afc Mon Sep 17 00:00:00 2001 From: 2Shirt <2xShirt@gmail.com> Date: Wed, 27 Oct 2021 18:03:52 -0600 Subject: [PATCH] Set custom power plan name and sleep timeouts --- scripts/wk/cfg/repairs.py | 5 +++++ scripts/wk/repairs/win.py | 22 +++++++++++++++++++--- 2 files changed, 24 insertions(+), 3 deletions(-) diff --git a/scripts/wk/cfg/repairs.py b/scripts/wk/cfg/repairs.py index 03693dae..837186f3 100644 --- a/scripts/wk/cfg/repairs.py +++ b/scripts/wk/cfg/repairs.py @@ -59,11 +59,16 @@ BLEACH_BIT_CLEANERS = ( 'windows_explorer.run', 'windows_explorer.thumbnails', ) +CUSTOM_POWER_PLAN_NAME = f'{KIT_NAME_FULL} Power Plan' POWER_PLANS = { 'Balanced': '381b4222-f694-41f0-9685-ff5bb260df2e', 'Custom': '01189998-8199-9119-725c-ccccccccccc3', 'High Performance': '8c5e7fda-e8bf-4a96-9a85-a6e23a8c635c', } +POWER_PLAN_SLEEP_TIMEOUTS = { + 'Balanced': ('1800', '900'), + 'High Performance': ('0', '0'), + } REG_UAC_DEFAULTS_WIN7 = { 'HKLM': { r'Software\Microsoft\Windows\CurrentVersion\Policies\System': ( diff --git a/scripts/wk/repairs/win.py b/scripts/wk/repairs/win.py index 61445f64..ddad6ba8 100644 --- a/scripts/wk/repairs/win.py +++ b/scripts/wk/repairs/win.py @@ -16,7 +16,9 @@ from wk.cfg.repairs import ( AUTO_REPAIR_DELAY_IN_SECONDS, AUTO_REPAIR_KEY, BLEACH_BIT_CLEANERS, + CUSTOM_POWER_PLAN_NAME, POWER_PLANS, + POWER_PLAN_SLEEP_TIMEOUTS, REG_UAC_DEFAULTS_WIN7, REG_UAC_DEFAULTS_WIN10, WIDTH, @@ -732,6 +734,7 @@ def auto_set_custom_power_plan(group, name): """Set custom power plan.""" result = TRY_PRINT.run( 'Set Custom Power Plan...', create_custom_power_plan, + enable_sleep=False, keep_display_on=True, ) save_settings(group, name, result=result) @@ -1068,19 +1071,24 @@ def run_tdsskiller(): # OS Built-in Functions -def create_custom_power_plan(keep_display_on=False): +def create_custom_power_plan(enable_sleep=True, keep_display_on=False): """Create new power plan and set as active.""" custom_guid = POWER_PLANS['Custom'] + sleep_timeouts = POWER_PLAN_SLEEP_TIMEOUTS['High Performance'] + if enable_sleep: + sleep_timeouts = POWER_PLAN_SLEEP_TIMEOUTS['Balanced'] # Duplicate High Performance plan cmd = [ 'powercfg', '-DuplicateScheme', POWER_PLANS['High Performance'], custom_guid, ] - run_program(cmd) + proc = run_program(cmd, check=False) + if proc.returncode and 'GUID already exists' not in proc.stderr: + LOG.error("Failed to create custom power plan.\n Details: %s", proc) # Change the name - cmd = ['powercfg', '-ChangeName', custom_guid, KIT_NAME_FULL] + cmd = ['powercfg', '-ChangeName', custom_guid, CUSTOM_POWER_PLAN_NAME] run_program(cmd) # Set as active plan @@ -1100,6 +1108,14 @@ def create_custom_power_plan(keep_display_on=False): ] run_program(cmd) + # Set sleep values + for arg, value in zip( + ('-SetacValueIndex', '-SetdcValueIndex'), sleep_timeouts): + cmd = [ + 'powercfg', arg, custom_guid, 'SUB_SLEEP', 'STANDBYIDLE', value, + ] + run_program(cmd) + def create_system_restore_point(): """Create System Restore point."""