Skip to content

Commit c0af7d6

Browse files
authored
Rewrote charging check function (#124)
* charging state fix * comment
1 parent 5120cbd commit c0af7d6

File tree

1 file changed

+19
-5
lines changed

1 file changed

+19
-5
lines changed

source/core.py

+19-5
Original file line numberDiff line numberDiff line change
@@ -107,13 +107,27 @@ def charging():
107107
"""
108108
get charge state: is battery charging or discharging
109109
"""
110-
bat_info = psutil.sensors_battery()
111-
if bat_info is None:
112-
state = True
110+
power_dir = "/sys/class/power_supply/"
111+
112+
# AC adapter states: 0, 1, unknown
113+
ac_info = getoutput(f"grep . {power_dir}A*/online").splitlines()
114+
# if there's one ac-adapter on-line, ac_state is True
115+
ac_state = any(['1' in ac.split(':')[-1] for ac in ac_info])
116+
117+
# Possible values: Charging, Discharging, Unknown
118+
battery_info = getoutput(f"grep . {power_dir}BAT*/status")
119+
120+
# need to explicitly check for each state in this order
121+
# considering multiple batteries
122+
if "Discharging" in battery_info:
123+
battery_state = False
124+
elif "Charging" in battery_info:
125+
battery_state = True
113126
else:
114-
state = bat_info.power_plugged
127+
battery_state = None
115128

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

118132

119133
def get_avail_gov():

0 commit comments

Comments
 (0)