diff --git a/auto_cpufreq/battery_scripts/battery.py b/auto_cpufreq/battery_scripts/battery.py index 8b15b7d3..6f7294b6 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 ee69d1ac..3c114ef5 100644 --- a/auto_cpufreq/battery_scripts/ideapad_acpi.py +++ b/auto_cpufreq/battery_scripts/ideapad_acpi.py @@ -5,11 +5,12 @@ 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 77304be4..7d3cce9f 100644 --- a/auto_cpufreq/battery_scripts/ideapad_laptop.py +++ b/auto_cpufreq/battery_scripts/ideapad_laptop.py @@ -5,11 +5,12 @@ 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 cab5133c..575e6121 100644 --- a/auto_cpufreq/battery_scripts/thinkpad.py +++ b/auto_cpufreq/battery_scripts/thinkpad.py @@ -5,11 +5,12 @@ 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 26cef3f3..4343b8d0 100755 --- a/auto_cpufreq/bin/auto_cpufreq.py +++ b/auto_cpufreq/bin/auto_cpufreq.py @@ -154,6 +154,7 @@ def config_info_dialog(): else: gnome_power_detect() tlp_service_detect() + battery_get_thresholds() read_stats() elif log: deprecated_log_msg() @@ -165,6 +166,7 @@ def config_info_dialog(): # ToDo: add status of GNOME Power Profile service status config_info_dialog() root_check() + battery_get_thresholds() cpufreqctl() footer() distro_info()