From 3c9a0c52f3eee3bdb1c058ad19c515cf9f0ff442 Mon Sep 17 00:00:00 2001 From: Marco Rodolfi Date: Wed, 13 Sep 2023 11:15:25 +0200 Subject: [PATCH] Exclude external batteries from sensors_battery The single check for battery in power_supply was causing sometimes to grab external device battery statuses since they have the form of hid__battery, which match the bat filter. However they do not have an ACPI path to the system itself. This takes into account that detail and properly exclude them from being included as an internal battery. --- psutil/_pslinux.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/psutil/_pslinux.py b/psutil/_pslinux.py index 0f102cbfa..e0221ede4 100644 --- a/psutil/_pslinux.py +++ b/psutil/_pslinux.py @@ -1471,9 +1471,13 @@ def multi_bcat(*paths): except ValueError: return ret.strip() return None + # The final check verifies if the reported battery is internal + # since the existence of the device/path file represent an available + # ACPI path to the battery itself. + bats = [x for x in os.listdir(POWER_SUPPLY_PATH) if x.startswith('BAT') + or 'battery' in x.lower() and + os.path.exists(os.path.join(POWER_SUPPLY_PATH, x, "device/path"))] - bats = [x for x in os.listdir(POWER_SUPPLY_PATH) if x.startswith('BAT') or - 'battery' in x.lower()] if not bats: return None # Get the first available battery. Usually this is "BAT0", except