🐛 fix(battery): handle missing PowerProfiles proxy and attributes gracefully

This commit is contained in:
sankalp committed 2025-08-16 13:02:23 +05:45
1 parent 1e8c426c99
commit a663170c96
1 file changed
+16 -12
+16 -12
View File
@@ -63,7 +63,7 @@ class Battery(Service):
"balanced": "Balanced", "balanced": "Balanced",
"balance": "Balanced", "balance": "Balanced",
"performance": "Performance", "performance": "Performance",
"performance-mode": "Performance" "performance-mode": "Performance",
} }
return profile_names.get(profile, profile.title()) return profile_names.get(profile, profile.title())
@@ -140,20 +140,23 @@ class Battery(Service):
@Property(str, "readable") @Property(str, "readable")
def power_profile(self): def power_profile(self):
if self._profile_proxy: if hasattr(self, "_profile_proxy") and self._profile_proxy:
return self._profile_proxy.ActiveProfile try:
return self._profile_proxy.ActiveProfile
except Exception:
return None
return None return None
@Property(list, "readable") @Property(list, "readable")
def available_profiles(self): def available_profiles(self):
if self._profile_proxy: if hasattr(self, "_profile_proxy") and self._profile_proxy:
try: try:
profiles = [] profiles = []
for p in self._profile_proxy.Profiles: for p in self._profile_proxy.Profiles:
if hasattr(p, 'Profile'): if hasattr(p, "Profile"):
profiles.append(p.Profile) profiles.append(p.Profile)
elif isinstance(p, dict) and 'Profile' in p: elif isinstance(p, dict) and "Profile" in p:
profiles.append(p['Profile']) profiles.append(p["Profile"])
elif isinstance(p, str): elif isinstance(p, str):
profiles.append(p) profiles.append(p)
return profiles return profiles
@@ -162,17 +165,17 @@ class Battery(Service):
return [] return []
def change_power_profile(self, profile: str) -> bool: def change_power_profile(self, profile: str) -> bool:
if not self._profile_proxy: if not hasattr(self, "_profile_proxy") or not self._profile_proxy:
return False return False
# Get available profiles using the same logic as available_profiles property # Get available profiles using the same logic as available_profiles property
available_profiles = [] available_profiles = []
try: try:
for p in self._profile_proxy.Profiles: for p in self._profile_proxy.Profiles:
if hasattr(p, 'Profile'): if hasattr(p, "Profile"):
available_profiles.append(p.Profile) available_profiles.append(p.Profile)
elif isinstance(p, dict) and 'Profile' in p: elif isinstance(p, dict) and "Profile" in p:
available_profiles.append(p['Profile']) available_profiles.append(p["Profile"])
elif isinstance(p, str): elif isinstance(p, str):
available_profiles.append(p) available_profiles.append(p)
except Exception: except Exception:
@@ -194,6 +197,7 @@ class Battery(Service):
self._bus = SystemBus() self._bus = SystemBus()
self._use_psutil_fallback = False self._use_psutil_fallback = False
self._psutil_battery = None self._psutil_battery = None
self._profile_proxy = None # Initialize to None first
# Battery device # Battery device
try: try:
@@ -213,7 +217,7 @@ class Battery(Service):
except Exception: except Exception:
return # psutil battery not available either return # psutil battery not available either
# PowerProfiles # PowerProfiles - Initialize after other attributes
try: try:
self._profile_proxy = self._bus.get( self._profile_proxy = self._bus.get(
"net.hadess.PowerProfiles", "/net/hadess/PowerProfiles" "net.hadess.PowerProfiles", "/net/hadess/PowerProfiles"