batteries: use actual battery name instead of index

This commit is contained in:
shadeyg56 2024-05-14 17:46:51 -05:00
parent cc6d98d8b1
commit 1f911b534c
3 changed files with 36 additions and 35 deletions

View File

@ -6,7 +6,7 @@ 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"
path = f"{POWER_SUPPLY_DIR}{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")
@ -20,23 +20,23 @@ def ideapad_acpi_setup():
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):
batteries = [name for name in os.listdir(POWER_SUPPLY_DIR) if name.startswith('BAT')]
for bat in batteries:
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')])
batteries = [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):
print(f"battery count = {len(batteries)}")
for bat in batteries:
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}/charge_start_threshold', 'r') as f:
print(f'{bat} 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="")
with open(f'{POWER_SUPPLY_DIR}{bat}/charge_stop_threshold', 'r') as f:
print(f'{bat} stop threshold = {f.read()}', end="")
except Exception: print(f"ERROR: failed to read battery {b} thresholds")
except Exception: print(f"ERROR: failed to read battery {bat} thresholds")

View File

@ -6,9 +6,9 @@ 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"
path = f"{POWER_SUPPLY_DIR}{bat}/charge_{mode}_threshold"
if os.path.exists(path):
subprocess.check_output(f"echo {value} | tee {POWER_SUPPLY_DIR}BAT{bat}/charge_{mode}_threshold", shell=True, text=True)
subprocess.check_output(f"echo {value} | tee {POWER_SUPPLY_DIR}{bat}/charge_{mode}_threshold", shell=True, text=True)
else: print(f"WARNING: {path} does NOT exist")
def get_threshold_value(mode):
@ -39,7 +39,7 @@ def ideapad_laptop_setup():
if not (conf.has_option("battery", "enable_thresholds") and conf["battery"]["enable_thresholds"] == "true"): return
battery_count = len([name for name in os.listdir(POWER_SUPPLY_DIR) if name.startswith('BAT')])
batteries = [name for name in os.listdir(POWER_SUPPLY_DIR) if name.startswith("BAT")]
if conf.has_option("battery", "ideapad_laptop_conservation_mode"):
if conf["battery"]["ideapad_laptop_conservation_mode"] == "true":
@ -48,7 +48,7 @@ def ideapad_laptop_setup():
if conf["battery"]["ideapad_laptop_conservation_mode"] == "false": conservation_mode(0)
if not check_conservation_mode():
for bat in range(battery_count):
for bat in batteries:
set_battery(get_threshold_value("start"), "start", bat)
set_battery(get_threshold_value("stop"), "stop", bat)
else: print("conservation mode is enabled unable to set thresholds")
@ -58,15 +58,16 @@ def ideapad_laptop_print_thresholds():
print("conservation mode is on")
return
battery_count = len([name for name in os.listdir(POWER_SUPPLY_DIR) if name.startswith('BAT')])
batteries = [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):
print(f"battery count = {len(batteries)}")
for bat in batteries:
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}/charge_start_threshold', 'r') as f:
print(f'{bat} 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="")
with open(f'{POWER_SUPPLY_DIR}{bat}/charge_stop_threshold', 'r') as f:
print(f'{bat} stop threshold = {f.read()}', end="")
except Exception as e: print(f"ERROR: failed to read battery {b} thresholds: {e}")
except Exception as e: print(f"ERROR: failed to read {bat} thresholds: {e}")

View File

@ -6,7 +6,7 @@ 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"
path = f"{POWER_SUPPLY_DIR}{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")
@ -20,24 +20,24 @@ def thinkpad_setup():
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')])
batteries = [name for name in os.listdir(POWER_SUPPLY_DIR) if name.startswith('BAT')]
for bat in range(battery_count):
for bat in batteries:
set_battery(get_threshold_value("start"), "start", bat)
set_battery(get_threshold_value("stop"), "stop", bat)
else: print(f"WARNING {POWER_SUPPLY_DIR} does NOT esixt")
def thinkpad_print_thresholds():
battery_count = len([name for name in os.listdir(POWER_SUPPLY_DIR) if name.startswith('BAT')])
batteries = [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):
print(f"battery count = {len(batteries)}")
for bat in batteries:
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}/charge_start_threshold', 'r') as f:
print(f'{bat} 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="")
with open(f'{POWER_SUPPLY_DIR}{bat}/charge_stop_threshold', 'r') as f:
print(f'{bat} stop threshold = {f.read()}', end="")
except Exception: print(f"ERROR: failed to read battery {b} thresholds")
except Exception: print(f"ERROR: failed to read battery {bat} thresholds")