From 5734162a7d30a27f7e09d78e1afdfc97ddbaa2f4 Mon Sep 17 00:00:00 2001 From: Mert Demir Date: Sun, 12 Jan 2020 09:07:22 +0300 Subject: [PATCH] add power package for better power source control (#25) * temp fix for https://github.com/giampaolo/psutil/issues/1658 * fix for missing set_performance() * add power package for better power source status detecting * remove redundant spaces Co-authored-by: Adnan Hodzic --- README.md | 6 +++--- auto-cpufreq.py | 21 +++++++++++---------- 2 files changed, 14 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index 73a5e7e..2b33969 100644 --- a/README.md +++ b/README.md @@ -49,10 +49,10 @@ Please note: this tool doesn't conflict and [works great in tandem with TLP](htt All requirements can be installed by running: -`sudo apt install python3-dev python3-pip python3-distro python3-psutil python3-click -y` +`sudo apt install python3-dev python3-pip python3-distro python3-psutil python3-click python3-power -y` Since APT packages may contain older version of necessary Python packages, please make sure to have latest version by running: -`sudo pip3 install --upgrade psutil click distro` +`sudo pip3 install --upgrade psutil click distro power` ##### Requirements installation for Redhat/Fedora and its derivatives @@ -66,7 +66,7 @@ After which you need to[ need to install rest of the requirements](https://githu If you have python3 and pip3 installed simply run: -`sudo pip3 install psutil click distro` +`sudo pip3 install psutil click distro power` Note: libraries must be installed using root user as tool will be run as root. diff --git a/auto-cpufreq.py b/auto-cpufreq.py index 464ce95..8a4016c 100755 --- a/auto-cpufreq.py +++ b/auto-cpufreq.py @@ -11,6 +11,7 @@ import time import psutil import platform import click +import power # ToDo: # - re-enable CPU fan speed display and make more generic and not only for thinkpad @@ -30,7 +31,7 @@ get_cur_gov = s.getoutput("cpufreqctl --governor") gov_state = get_cur_gov.split()[0] # get battery state -bat_state = p.sensors_battery().power_plugged +bat_state = power.PowerManagement().get_providing_power_source_type() # auto-cpufreq log file auto_cpufreq_log_file = "/var/log/auto-cpufreq.log" @@ -249,36 +250,36 @@ def set_autofreq(): print("\n" + "-" * 28 + " CPU frequency scaling " + "-" * 28 + "\n") # get battery state - bat_state = p.sensors_battery().power_plugged + bat_state = power.PowerManagement().get_providing_power_source_type() # determine which governor should be used - if bat_state == True: + if bat_state == power.POWER_TYPE_AC: print("Battery is: charging") set_performance() - elif bat_state == False: + elif bat_state == power.POWER_TYPE_BATTERY: print("Battery is: discharging") set_powersave() - else: - print("Couldn't detrmine battery status. Please report this issue.") + else: + print("Couldn't determine battery status. Please report this issue.") # make cpufreq suggestions def mon_autofreq(): print("\n" + "-" * 28 + " CPU frequency scaling " + "-" * 28 + "\n") # get battery state - bat_state = p.sensors_battery().power_plugged + bat_state = power.PowerManagement().get_providing_power_source_type() # determine which governor should be used - if bat_state == True: + if bat_state == power.POWER_TYPE_AC: print("Battery is: charging") print("Suggesting use of \"performance\" governor\nCurrently using:", gov_state) mon_performance() - elif bat_state == False: + elif bat_state == power.POWER_TYPE_BATTERY: print("Battery is: discharging") print("Suggesting use of \"powersave\" governor\nCurrently using:", gov_state) mon_powersave() else: - print("Couldn't detrmine battery status. Please report this issue.") + print("Couldn't determine battery status. Please report this issue.") # get system information def sysinfo():