From 95ba1f42bde87cd08fa88fcf95ddabdae6c83755 Mon Sep 17 00:00:00 2001 From: PurpleWazard <155469001+PurpleWazard@users.noreply.github.com> Date: Thu, 9 May 2024 02:10:15 -0500 Subject: [PATCH] Add warnings for charge thresholds (#679) * add warning messages to thinkpad * added warnings to laptop_acpi * added warnings to ideapad_laptop * formatted the battery output and added stats to --debug and --status * formated --stats better with battery info * removed blank lines in battery info * fixed typo: --stats battery info --- auto_cpufreq/battery_scripts/battery.py | 2 -- auto_cpufreq/battery_scripts/ideapad_acpi.py | 33 +++++++++++-------- .../battery_scripts/ideapad_laptop.py | 18 +++++----- auto_cpufreq/battery_scripts/thinkpad.py | 33 +++++++++++-------- auto_cpufreq/bin/auto_cpufreq.py | 2 ++ 5 files changed, 50 insertions(+), 38 deletions(-) diff --git a/auto_cpufreq/battery_scripts/battery.py b/auto_cpufreq/battery_scripts/battery.py index 8b15b7d..6f7294b 100644 --- a/auto_cpufreq/battery_scripts/battery.py +++ b/auto_cpufreq/battery_scripts/battery.py @@ -43,5 +43,3 @@ def battery_get_thresholds(): else: return - - diff --git a/auto_cpufreq/battery_scripts/ideapad_acpi.py b/auto_cpufreq/battery_scripts/ideapad_acpi.py index ee69d1a..3c114ef 100644 --- a/auto_cpufreq/battery_scripts/ideapad_acpi.py +++ b/auto_cpufreq/battery_scripts/ideapad_acpi.py @@ -5,11 +5,12 @@ from auto_cpufreq.utils.config import config def set_battery(value, mode, bat): - try: + path = f"/sys/class/power_supply/BAT{bat}/charge_{mode}_threshold" + if os.path.isfile(path): subprocess.check_output( - f"echo {value} | tee /sys/class/power_supply/BAT{bat}/charge_{mode}_threshold", shell=True, text=True) - except Exception as e: - print(f"Error writing to file_path: {e}") + f"echo {value} | tee {path}", shell=True, text=True) + else: + print(f"WARNING: {path} does NOT exist") def get_threshold_value(mode): @@ -33,27 +34,31 @@ def ideapad_acpi_setup(): if not conf["battery"]["enable_thresholds"] == "true": return - battery_count = len([name for name in os.listdir( - "/sys/class/power_supply/") if name.startswith('BAT')]) + if os.path.exists("/sys/class/power_supply/"): + battery_count = len([name for name in os.listdir( + "/sys/class/power_supply/") if name.startswith('BAT')]) - for bat in range(battery_count): - set_battery(get_threshold_value("start"), "start", bat) - set_battery(get_threshold_value("stop"), "stop", bat) + for bat in range(battery_count): + set_battery(get_threshold_value("start"), "start", bat) + set_battery(get_threshold_value("stop"), "stop", bat) + else: + print("WARNING: could NOT access /sys/class/power_supply") def ideapad_acpi_print_thresholds(): battery_count = len([name for name in os.listdir( "/sys/class/power_supply/") if name.startswith('BAT')]) - print(f"number of batteries = {battery_count}") + print("\n-------------------------------- Battery Info ---------------------------------\n") + print(f"battery count = {battery_count}") for b in range(battery_count): try: with open(f'/sys/class/power_supply/BAT{b}/charge_start_threshold', 'r') as f: - print(f'battery{b} start threshold is set to {f.read()}') + print(f'battery{b} start threshold = {f.read()}', end="") f.close() with open(f'/sys/class/power_supply/BAT{b}/charge_stop_threshold', 'r') as f: - print(f'battery{b} stop threshold is set to {f.read()}') + print(f'battery{b} stop threshold = {f.read()}', end="") f.close() - except Exception as e: - print(f"Error reading battery thresholds: {e}") + except Exception: + print(f"ERROR: failed to read battery {b} thresholds") diff --git a/auto_cpufreq/battery_scripts/ideapad_laptop.py b/auto_cpufreq/battery_scripts/ideapad_laptop.py index 77304be..7d3cce9 100644 --- a/auto_cpufreq/battery_scripts/ideapad_laptop.py +++ b/auto_cpufreq/battery_scripts/ideapad_laptop.py @@ -5,11 +5,12 @@ from auto_cpufreq.utils.config import config def set_battery(value, mode, bat): - try: + path = f"/sys/class/power_supply/BAT{bat}/charge_{mode}_threshold" + if os.path.exists(path): subprocess.check_output( f"echo {value} | tee /sys/class/power_supply/BAT{bat}/charge_{mode}_threshold", shell=True, text=True) - except Exception as e: - print(f"Error writing to file_path: {e}") + else: + print(f"WARNING: {path} does NOT exist") def get_threshold_value(mode): @@ -84,16 +85,17 @@ def ideapad_laptop_print_thresholds(): battery_count = len([name for name in os.listdir( "/sys/class/power_supply/") if name.startswith('BAT')]) - print(f"number of batteries = {battery_count}") + print("\n-------------------------------- Battery Info ---------------------------------\n") + print(f"battery count = {battery_count}") for b in range(battery_count): try: with open(f'/sys/class/power_supply/BAT{b}/charge_start_threshold', 'r') as f: - print(f'battery{b} start threshold is set to {f.read()}') + print(f'battery{b} start threshold = {f.read()}', end="") f.close() with open(f'/sys/class/power_supply/BAT{b}/charge_stop_threshold', 'r') as f: - print(f'battery{b} stop threshold is set to {f.read()}') + print(f'battery{b} stop threshold = {f.read()}', end="") f.close() - except Exception as e: - print(f"Error reading battery thresholds: {e}") + except Exception: + print(f"ERROR: failed to read battery thresholds: {e}") diff --git a/auto_cpufreq/battery_scripts/thinkpad.py b/auto_cpufreq/battery_scripts/thinkpad.py index cab5133..575e612 100644 --- a/auto_cpufreq/battery_scripts/thinkpad.py +++ b/auto_cpufreq/battery_scripts/thinkpad.py @@ -5,11 +5,12 @@ from auto_cpufreq.utils.config import config def set_battery(value, mode, bat): - try: + path = f"/sys/class/power_supply/BAT{bat}/charge_{mode}_threshold" + if os.path.isfile(path): subprocess.check_output( - f"echo {value} | tee /sys/class/power_supply/BAT{bat}/charge_{mode}_threshold", shell=True, text=True) - except Exception as e: - print(f"Error writing to file_path: {e}") + f"echo {value} | tee {path}", shell=True, text=True) + else: + print(f"WARNING: {path} does NOT exist") def get_threshold_value(mode): @@ -33,27 +34,31 @@ def thinkpad_setup(): if not conf["battery"]["enable_thresholds"] == "true": return - battery_count = len([name for name in os.listdir( - "/sys/class/power_supply/") if name.startswith('BAT')]) + if os.path.exists("/sys/class/power_supply/"): + battery_count = len([name for name in os.listdir( + "/sys/class/power_supply/") if name.startswith('BAT')]) - for bat in range(battery_count): - set_battery(get_threshold_value("start"), "start", bat) - set_battery(get_threshold_value("stop"), "stop", bat) + for bat in range(battery_count): + set_battery(get_threshold_value("start"), "start", bat) + set_battery(get_threshold_value("stop"), "stop", bat) + else: + print("WARNING /sys/class/power_supply/ does NOT esixt") def thinkpad_print_thresholds(): battery_count = len([name for name in os.listdir( "/sys/class/power_supply/") if name.startswith('BAT')]) - print(f"number of batteries = {battery_count}") + print("\n-------------------------------- Battery Info ---------------------------------\n") + print(f"battery count = {battery_count}") for b in range(battery_count): try: with open(f'/sys/class/power_supply/BAT{b}/charge_start_threshold', 'r') as f: - print(f'battery{b} start threshold is set to {f.read()}') + print(f'battery{b} start threshold = {f.read()}', end="") f.close() with open(f'/sys/class/power_supply/BAT{b}/charge_stop_threshold', 'r') as f: - print(f'battery{b} stop threshold is set to {f.read()}') + print(f'battery{b} stop threshold = {f.read()}', end="") f.close() - except Exception as e: - print(f"Error reading battery thresholds: {e}") + except Exception: + print(f"ERROR: failed to read battery {b} thresholds") diff --git a/auto_cpufreq/bin/auto_cpufreq.py b/auto_cpufreq/bin/auto_cpufreq.py index 26cef3f..4343b8d 100755 --- a/auto_cpufreq/bin/auto_cpufreq.py +++ b/auto_cpufreq/bin/auto_cpufreq.py @@ -154,6 +154,7 @@ def main(config, daemon, debug, update, install, remove, live, log, monitor, sta else: gnome_power_detect() tlp_service_detect() + battery_get_thresholds() read_stats() elif log: deprecated_log_msg() @@ -165,6 +166,7 @@ def main(config, daemon, debug, update, install, remove, live, log, monitor, sta # ToDo: add status of GNOME Power Profile service status config_info_dialog() root_check() + battery_get_thresholds() cpufreqctl() footer() distro_info()