File tree 1 file changed +19
-5
lines changed
1 file changed +19
-5
lines changed Original file line number Diff line number Diff line change @@ -107,13 +107,27 @@ def charging():
107
107
"""
108
108
get charge state: is battery charging or discharging
109
109
"""
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
113
126
else :
114
- state = bat_info . power_plugged
127
+ battery_state = None
115
128
116
- return state
129
+ # if both ac-adapter and battery states are unknown default to not charging
130
+ return ac_state or battery_state
117
131
118
132
119
133
def get_avail_gov ():
You can’t perform that action at this time.
0 commit comments