Skip to content

Commit

Permalink
port ac power detection to new method
Browse files Browse the repository at this point in the history
Signed-off-by: Sujal Singh <email.sujalsingh@gmail.com>
  • Loading branch information
sujaldev committed Apr 2, 2023
1 parent 567f4e6 commit 5b5472e
Showing 1 changed file with 9 additions and 11 deletions.
20 changes: 9 additions & 11 deletions psutil/_pslinux.py
Original file line number Diff line number Diff line change
Expand Up @@ -1472,11 +1472,7 @@ def multi_bcat(*paths):
return ret.strip()
return None

def detect_battery():
expected_files = (
"energy_now", "charge_now", "power_now", "current_now",
"energy_full", "charge_full", "capacity"
)
def detect_battery(*expected_files):
results = defaultdict(list)

for path in os.listdir(POWER_SUPPLY_PATH):
Expand All @@ -1492,7 +1488,10 @@ def detect_battery():
else:
return None

root = detect_battery()
root = detect_battery(
"energy_now", "charge_now", "power_now", "current_now",
"energy_full", "charge_full", "capacity"
)
if root is None:
return None

Expand Down Expand Up @@ -1524,11 +1523,10 @@ def detect_battery():
# Note: AC0 is not always available and sometimes (e.g. CentOS7)
# it's called "AC".
power_plugged = None
online = multi_bcat(
os.path.join(POWER_SUPPLY_PATH, "AC0/online"),
os.path.join(POWER_SUPPLY_PATH, "AC/online"))
if online is not None:
power_plugged = online == 1
online = detect_battery("online")
if online:
path = os.path.join(POWER_SUPPLY_PATH, online)
power_plugged = multi_bcat(path) == 1
else:
status = cat(root + "/status", fallback="").strip().lower()
if status == "discharging":
Expand Down

0 comments on commit 5b5472e

Please sign in to comment.