From aae112d7d9814a0a489699db382d787a147e5204 Mon Sep 17 00:00:00 2001 From: Shrinivas Kumbhar <44837339+librewish@users.noreply.github.com> Date: Sun, 7 Feb 2021 02:09:56 +0530 Subject: [PATCH] Use `psutil` in conjuction with `dmidecode` to determine `ac_state` (#168) * use psutil to determine ac_state --- auto_cpufreq/core.py | 27 ++++++++++----------------- 1 file changed, 10 insertions(+), 17 deletions(-) diff --git a/auto_cpufreq/core.py b/auto_cpufreq/core.py index 9b98b37..fd5c0fa 100644 --- a/auto_cpufreq/core.py +++ b/auto_cpufreq/core.py @@ -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():