-
-
Notifications
You must be signed in to change notification settings - Fork 291
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
SunkenHero Code optimization & addition of battery percentage #432
Changes from 9 commits
4e05c4b
84f5ade
64840df
87388f9
564dd37
8619190
087c0cc
1294eee
dcbb406
66c83ff
16cbb3d
03544d0
c1e3598
9bc31ea
e316b76
a5c32a1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -42,9 +42,6 @@ | |
) | ||
CPUS = os.cpu_count() | ||
|
||
# ignore these devices under /sys/class/power_supply/ | ||
POWER_SUPPLY_IGNORELIST = ["hidpp_battery"] | ||
|
||
# Note: | ||
# "load1m" & "cpuload" can't be global vars and to in order to show correct data must be | ||
# decraled where their execution takes place | ||
|
@@ -200,60 +197,14 @@ def charging(): | |
""" | ||
get charge state: is battery charging or discharging | ||
""" | ||
power_supply_path = "/sys/class/power_supply/" | ||
power_supplies = os.listdir(Path(power_supply_path)) | ||
# sort it so AC is 'always' first | ||
power_supplies = sorted(power_supplies) | ||
|
||
# check if we found power supplies. on a desktop these are not found | ||
# and we assume we are on a powercable. | ||
if len(power_supplies) == 0: | ||
# nothing found found, so nothing to check | ||
return True | ||
# we found some power supplies, lets check their state | ||
else: | ||
for supply in power_supplies: | ||
# Check if supply is in ignore list | ||
ignore_supply = any(item in supply for item in POWER_SUPPLY_IGNORELIST) | ||
# If found in ignore list, skip it. | ||
if ignore_supply: | ||
continue | ||
return True if psutil.sensors_battery().power_plugged else False | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Using psutil to get charging status There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As I mentioned in other comment this was all added with #308 as battery state would not be showing properly on certain devices, still thinking if we might need some of it or if |
||
try: | ||
with open(Path(power_supply_path + supply + "/type")) as f: | ||
supply_type = f.read()[:-1] | ||
if supply_type == "Mains": | ||
# we found an AC | ||
try: | ||
with open(Path(power_supply_path + supply + "/online")) as f: | ||
val = int(f.read()[:-1]) | ||
if val == 1: | ||
# we are definitely charging | ||
return True | ||
except FileNotFoundError: | ||
# we could not find online, check next item | ||
continue | ||
elif supply_type == "Battery": | ||
# we found a battery, check if its being discharged | ||
try: | ||
with open(Path(power_supply_path + supply + "/status")) as f: | ||
val = str(f.read()[:-1]) | ||
if val == "Discharging": | ||
# we found a discharging battery | ||
return False | ||
except FileNotFoundError: | ||
# could not find status, check the next item | ||
continue | ||
else: | ||
# continue to next item because current is not | ||
# "Mains" or "Battery" | ||
continue | ||
except FileNotFoundError: | ||
# could not find type, check the next item | ||
continue | ||
|
||
# we cannot determine discharging state, assume we are on powercable | ||
return True | ||
def battery_percentage(): | ||
""" | ||
get batery percentage | ||
""" | ||
return psutil.sensors_battery().percent | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added Function to get Battery Percentage There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Another reference on why it was the way it was done in the past #308 |
||
|
||
def get_avail_gov(): | ||
|
@@ -993,10 +944,10 @@ def set_autofreq(): | |
|
||
# determine which governor should be used | ||
if charging(): | ||
print("Battery is: charging\n") | ||
print("Battery: " + str(battery_percentage()) + "%\tcharging\n") | ||
set_performance() | ||
else: | ||
print("Battery is: discharging\n") | ||
print("Battery: " + str(battery_percentage()) + "%\tdischarging\n") | ||
set_powersave() | ||
AdnanHodzic marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
|
||
|
@@ -1009,12 +960,12 @@ def mon_autofreq(): | |
|
||
# determine which governor should be used | ||
if charging(): | ||
print("Battery is: charging\n") | ||
print("Battery: " + str(battery_percentage()) + "%\tcharging\n") | ||
get_current_gov() | ||
print(f'Suggesting use of "{get_avail_performance()}" governor') | ||
mon_performance() | ||
else: | ||
print("Battery is: discharging\n") | ||
print("Battery: " + str(battery_percentage()) + "%\tdischarging\n") | ||
get_current_gov() | ||
print(f'Suggesting use of "{get_avail_powersave()}" governor') | ||
mon_powersave() | ||
|
@@ -1119,16 +1070,9 @@ def sysinfo(): | |
core = cpu_core[cpu] | ||
cpu_temp_index = core_temp_labels.index(f"Core {core}") | ||
temp_per_cpu[i] = core_temp["coretemp"][cpu_temp_index].current | ||
elif "k10temp" in core_temp: | ||
# https://www.kernel.org/doc/Documentation/hwmon/k10temp | ||
temp_per_cpu = [core_temp["k10temp"][0].current] * online_cpu_count | ||
elif "zenpower" in core_temp: | ||
# https://github.com/AdnanHodzic/auto-cpufreq/issues/145#issuecomment-763294009 | ||
temp_per_cpu = [core_temp["zenpower"][0].current] * online_cpu_count | ||
elif "acpitz" in core_temp: | ||
temp_per_cpu = [core_temp["acpitz"][0].current] * online_cpu_count | ||
elif "thinkpad" in core_temp: | ||
temp_per_cpu = [core_temp["thinkpad"][0].current] * online_cpu_count | ||
else: | ||
temp = list(psutil.sensors_temperatures()) | ||
temp_per_cpu = [core_temp[temp[0]][0].current] * online_cpu_count | ||
except Exception as e: | ||
AdnanHodzic marked this conversation as resolved.
Show resolved
Hide resolved
|
||
print(repr(e)) | ||
pass | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think its not needed anymore, but i am not sure
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we do still need it, check #332 PR for reference.