Skip to content

Commit

Permalink
Rewrote charging check function (#124)
Browse files Browse the repository at this point in the history
* charging state fix

* comment
  • Loading branch information
Haptein authored Sep 28, 2020
1 parent 5120cbd commit c0af7d6
Showing 1 changed file with 19 additions and 5 deletions.
24 changes: 19 additions & 5 deletions source/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,13 +107,27 @@ def charging():
"""
get charge state: is battery charging or discharging
"""
bat_info = psutil.sensors_battery()
if bat_info is None:
state = True
power_dir = "/sys/class/power_supply/"

# AC adapter states: 0, 1, unknown
ac_info = getoutput(f"grep . {power_dir}A*/online").splitlines()
# if there's one ac-adapter on-line, ac_state is True
ac_state = any(['1' in ac.split(':')[-1] for ac in ac_info])

# Possible values: Charging, Discharging, Unknown
battery_info = getoutput(f"grep . {power_dir}BAT*/status")

# need to explicitly check for each state in this order
# considering multiple batteries
if "Discharging" in battery_info:
battery_state = False
elif "Charging" in battery_info:
battery_state = True
else:
state = bat_info.power_plugged
battery_state = None

return state
# if both ac-adapter and battery states are unknown default to not charging
return ac_state or battery_state


def get_avail_gov():
Expand Down

0 comments on commit c0af7d6

Please sign in to comment.