auto-cpufreq/auto_cpufreq/battery_scripts/ideapad_acpi.py

43 lines
1.8 KiB
Python
Raw Normal View History

#!/usr/bin/env python3
import os
import subprocess
Rework config and reload config on file change/creation/deletion (#663) * add config.py and config_event_handler.py also introduces the utils folder * update config imports and variables * add 'pyinotify' dependency * config: check for changes using threading * config: handle errors and new eventsx * config: set_path even if file doesn't exist and make new ConfigParser on every update * fix get_config call * config: check for changes on moved file * call notifier.start() manually to prevent hanging * config: update comments * battery: fix config imports * config: fix config deletion detection * Add load from user config in XDG_CONFIG_HOME if available (#672) * Add load from user config from in XDG_CONFIG_HOME if available This update introduces the flexibility to load the configuration file from multiple locations, prioritizing user preferences and system standards. Previously, the configuration was strictly read from a hardcoded system path (`/etc/auto-cpufreq.conf`). Now, the application first checks if the user has specified a configuration file path via command line arguments. If not, it looks for a configuration file in the user's config directory (`$XDG_CONFIG_HOME/auto-cpufreq/auto-cpufreq.conf`). If neither is found, it defaults to the original system-wide configuration file. This allows users to add their auto-cpufreq configuration to their dotfiles. * If --config is set but invalid, exit with error * Remove redundant empty string check on config file path * Remove duplicate isfile check for config path See also: https://github.com/AdnanHodzic/auto-cpufreq/pull/672#discussion_r1548003119 * Update configuration options in README See also: #672 * config: move find_config_file function and fix finding home directory * auto_cpufreq: fix hanging on --daemon, --live, and --monitor * swap pyinotify for patched version --------- Co-authored-by: Steven Braun <steven.braun.mz@gmail.com>
2024-04-30 08:35:53 +02:00
from auto_cpufreq.utils.config import config
POWER_SUPPLY_DIR = "/sys/class/power_supply/"
def set_battery(value, mode, bat):
path = f"{POWER_SUPPLY_DIR}BAT{bat}/charge_{mode}_threshold"
if os.path.isfile(path): subprocess.check_output(f"echo {value} | tee {path}", shell=True, text=True)
else: print(f"WARNING: {path} does NOT exist")
def get_threshold_value(mode):
Rework config and reload config on file change/creation/deletion (#663) * add config.py and config_event_handler.py also introduces the utils folder * update config imports and variables * add 'pyinotify' dependency * config: check for changes using threading * config: handle errors and new eventsx * config: set_path even if file doesn't exist and make new ConfigParser on every update * fix get_config call * config: check for changes on moved file * call notifier.start() manually to prevent hanging * config: update comments * battery: fix config imports * config: fix config deletion detection * Add load from user config in XDG_CONFIG_HOME if available (#672) * Add load from user config from in XDG_CONFIG_HOME if available This update introduces the flexibility to load the configuration file from multiple locations, prioritizing user preferences and system standards. Previously, the configuration was strictly read from a hardcoded system path (`/etc/auto-cpufreq.conf`). Now, the application first checks if the user has specified a configuration file path via command line arguments. If not, it looks for a configuration file in the user's config directory (`$XDG_CONFIG_HOME/auto-cpufreq/auto-cpufreq.conf`). If neither is found, it defaults to the original system-wide configuration file. This allows users to add their auto-cpufreq configuration to their dotfiles. * If --config is set but invalid, exit with error * Remove redundant empty string check on config file path * Remove duplicate isfile check for config path See also: https://github.com/AdnanHodzic/auto-cpufreq/pull/672#discussion_r1548003119 * Update configuration options in README See also: #672 * config: move find_config_file function and fix finding home directory * auto_cpufreq: fix hanging on --daemon, --live, and --monitor * swap pyinotify for patched version --------- Co-authored-by: Steven Braun <steven.braun.mz@gmail.com>
2024-04-30 08:35:53 +02:00
conf = config.get_config()
return conf["battery"][f"{mode}_threshold"] if conf.has_option("battery", f"{mode}_threshold") else (0 if mode == "start" else 100)
def ideapad_acpi_setup():
Rework config and reload config on file change/creation/deletion (#663) * add config.py and config_event_handler.py also introduces the utils folder * update config imports and variables * add 'pyinotify' dependency * config: check for changes using threading * config: handle errors and new eventsx * config: set_path even if file doesn't exist and make new ConfigParser on every update * fix get_config call * config: check for changes on moved file * call notifier.start() manually to prevent hanging * config: update comments * battery: fix config imports * config: fix config deletion detection * Add load from user config in XDG_CONFIG_HOME if available (#672) * Add load from user config from in XDG_CONFIG_HOME if available This update introduces the flexibility to load the configuration file from multiple locations, prioritizing user preferences and system standards. Previously, the configuration was strictly read from a hardcoded system path (`/etc/auto-cpufreq.conf`). Now, the application first checks if the user has specified a configuration file path via command line arguments. If not, it looks for a configuration file in the user's config directory (`$XDG_CONFIG_HOME/auto-cpufreq/auto-cpufreq.conf`). If neither is found, it defaults to the original system-wide configuration file. This allows users to add their auto-cpufreq configuration to their dotfiles. * If --config is set but invalid, exit with error * Remove redundant empty string check on config file path * Remove duplicate isfile check for config path See also: https://github.com/AdnanHodzic/auto-cpufreq/pull/672#discussion_r1548003119 * Update configuration options in README See also: #672 * config: move find_config_file function and fix finding home directory * auto_cpufreq: fix hanging on --daemon, --live, and --monitor * swap pyinotify for patched version --------- Co-authored-by: Steven Braun <steven.braun.mz@gmail.com>
2024-04-30 08:35:53 +02:00
conf = config.get_config()
if not (conf.has_option("battery", "enable_thresholds") and conf["battery"]["enable_thresholds"] == "true"): return
if os.path.exists(POWER_SUPPLY_DIR):
battery_count = len([name for name in os.listdir(POWER_SUPPLY_DIR) 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)
else: print(f"WARNING: could NOT access {POWER_SUPPLY_DIR}")
def ideapad_acpi_print_thresholds():
battery_count = len([name for name in os.listdir(POWER_SUPPLY_DIR) if name.startswith('BAT')])
print("\n-------------------------------- Battery Info ---------------------------------\n")
print(f"battery count = {battery_count}")
for b in range(battery_count):
try:
with open(f'{POWER_SUPPLY_DIR}BAT{b}/charge_start_threshold', 'r') as f:
print(f'battery{b} start threshold = {f.read()}', end="")
with open(f'{POWER_SUPPLY_DIR}BAT{b}/charge_stop_threshold', 'r') as f:
print(f'battery{b} stop threshold = {f.read()}', end="")
except Exception: print(f"ERROR: failed to read battery {b} thresholds")