Change the way of checking the charging state (#308)

This commit is contained in:
bobslept 2021-12-16 08:04:24 +01:00 committed by GitHub
parent 36155de8dc
commit 3994d4a7af
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 43 additions and 22 deletions

View File

@ -165,31 +165,52 @@ def charging():
""" """
get charge state: is battery charging or discharging get charge state: is battery charging or discharging
""" """
power_dir = "/sys/class/power_supply/" power_supply_path = "/sys/class/power_supply/"
power_supplies = os.listdir(Path(power_supply_path))
computer_type = getoutput("dmidecode --string chassis-type") # check if we found power supplies. on a desktop these are not found
if computer_type in ["Notebook", "Laptop", "Convertible", "Portable"]: # and we assume we are on a powercable.
# AC adapter states: 0, 1, unknown if len(power_supplies) == 0:
ac_info = getoutput(f"grep . {power_dir}A*/online").splitlines() # nothing found found, so nothing to check
# Battery statuses: Full, Charging, Discharging, Unknown return True
battery_status = getoutput(f"grep . {power_dir}B*/status").splitlines() # we found some power supplies, lets check their state
# if there's one battery charging, or if there's one ac-adapter on-line, ac_state is True
ac_state = (all([not "Discharging" in ac for ac in battery_status]) or
any(["1" in ac.split(":")[-1] for ac in ac_info]))
else: else:
has_battery = psutil.sensors_battery() is not None for supply in power_supplies:
if has_battery: try:
power_pluggedin = psutil.sensors_battery().power_plugged with open(Path(power_supply_path + supply + "/type")) as f:
if power_pluggedin: supply_type = f.read()[:-1]
ac_state = True if supply_type == "Mains":
else: # we found an AC
ac_state = False try:
else: with open(Path(power_supply_path + supply + "/online")) as f:
ac_state = True val = int(f.read()[:-1])
if val == 1:
# if both ac-adapter and battery states are unknown default to not charging # we are definitely charging
return ac_state return True
except FileNotFoundError:
# we could not find online, check next item
continue
elif supply_type == "Battery":
# we found a battery, check if its being discharged
try:
with open(Path(power_supply_path + supply + "/status")) as f:
val = str(f.read()[:-1])
if val == "Discharging":
# we found a discharging battery
return False
except FileNotFoundError:
# could not find status, check the next item
continue
else:
# continue to next item because current is not
# "Mains" or "Battery"
continue
except FileNotFoundError:
# could not find type, check the next item
continue
# we cannot determine discharging state, assume we are on powercable
return True
def get_avail_gov(): def get_avail_gov():
f = Path("/sys/devices/system/cpu/cpu0/cpufreq/scaling_available_governors") f = Path("/sys/devices/system/cpu/cpu0/cpufreq/scaling_available_governors")