Use `psutil` in conjuction with `dmidecode` to determine `ac_state` (#168)

* use psutil to determine ac_state
This commit is contained in:
Shrinivas Kumbhar 2021-02-07 02:09:56 +05:30 committed by GitHub
parent 82b41f4ce2
commit aae112d7d9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 10 additions and 17 deletions

View File

@ -135,31 +135,24 @@ def charging():
power_dir = "/sys/class/power_supply/"
computer_type = getoutput('dmidecode --string chassis-type')
# Most common types for laptops, source: https://twitter.com/fooctrl/status/1357230198857564160
if computer_type in [ "Notebook", "Laptop", "Convertible", "Portable" ]:
# 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])
else:
# if desktop ac_state is true
ac_state = True
# 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
has_battery = psutil.sensors_battery() is not None
if has_battery == True:
power_pluggedin = psutil.sensors_battery().power_plugged
if power_pluggedin == True:
ac_state = True
else:
ac_state = False
else:
ac_state = True
# if both ac-adapter and battery states are unknown default to not charging
return ac_state or battery_state
return ac_state
def get_avail_gov():