From c0af7d69f9f6ed7da0733f8aee065e0dd34c9d9d Mon Sep 17 00:00:00 2001 From: Bruno Al Date: Mon, 28 Sep 2020 04:02:12 -0500 Subject: [PATCH] Rewrote charging check function (#124) * charging state fix * comment --- source/core.py | 26 ++++++++++++++++++++------ 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/source/core.py b/source/core.py index 2935ee1..329ccaa 100644 --- a/source/core.py +++ b/source/core.py @@ -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():