Rewrote charging check function (#124)

* charging state fix

* comment
This commit is contained in:
Bruno Al 2020-09-28 04:02:12 -05:00 committed by GitHub
parent 5120cbd0a1
commit c0af7d69f9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 20 additions and 6 deletions

View File

@ -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
else:
state = bat_info.power_plugged
power_dir = "/sys/class/power_supply/"
return state
# 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:
battery_state = None
# if both ac-adapter and battery states are unknown default to not charging
return ac_state or battery_state
def get_avail_gov():