Skip to content
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

Refactor charging function #702

Merged
merged 1 commit into from
May 25, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 32 additions & 38 deletions auto_cpufreq/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -316,47 +316,41 @@ def charging():
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
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

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
power_supply_type_path = Path(power_supply_path + supply + "/type")
if not power_supply_type_path.exists():
continue
with open(power_supply_type_path) as f:
supply_type = f.read()[:-1]

if supply_type == "Mains":
# we found an AC
power_supply_online_path = Path(power_supply_path + supply + "/online")
if not power_supply_online_path.exists():
continue
with open(power_supply_online_path) as f:
val = int(f.read()[:-1])
if val == 1:
# we are definitely charging
return True
elif supply_type == "Battery":
# we found a battery, check if its being discharged
power_supply_status_path = Path(power_supply_path + supply + "/status")
if not power_supply_status_path.exists():
continue
with open(power_supply_status_path) as f:
val = str(f.read()[:-1])
if val == "Discharging":
# we found a discharging battery
return False

# we cannot determine discharging state, assume we are on powercable
return True
Expand Down