fix(pisugarx): guard against None self.ps in on_loaded and on_ui_update

on_loaded crashes with AttributeError when PiSugar is not connected
because it accesses self.ps without a None check. Also uses direct
dict access on self.options which crashes if keys are missing.

on_ui_update crashes when checking battery_plugged outside the
self.ready guard, hitting self.ps.get_battery_power_plugged on None.

Changes:
- Wrap self.ps access in on_loaded with None guard
- Use self.options.get() with safe defaults
- Move battery_plugged inside self.ready + self.ps check
- Downgrade "PiSugar is not ready" from info to debug

Fixes #507

Signed-off-by: PwnPacker <4704376+CoderFX@users.noreply.github.com>
This commit is contained in:
CoderFX
2026-03-11 01:02:07 +02:00
parent 8b1297d836
commit 2246a91590

View File

@@ -600,9 +600,12 @@ class PiSugar(plugins.Plugin):
f"[PiSugarX] Rotation is {'enabled' if self.rotation_enabled else 'disabled'}.")
logging.info(
f"[PiSugarX] Default display (when rotation disabled): {self.default_display}")
self.ps.lowpower_shutdown = self.options['lowpower_shutdown']
self.ps.lowpower_shutdown_level = self.options['lowpower_shutdown_level']
self.ps.max_charge_voltage_protection = self.options['max_charge_voltage_protection']
if self.ps is not None:
self.ps.lowpower_shutdown = self.options.get('lowpower_shutdown', False)
self.ps.lowpower_shutdown_level = self.options.get('lowpower_shutdown_level', 10)
self.ps.max_charge_voltage_protection = self.options.get('max_charge_voltage_protection', False)
else:
logging.warning("[PiSugarX] PiSugar not connected during on_loaded, skipping config")
def on_ready(self, agent):
try:
@@ -811,11 +814,13 @@ class PiSugar(plugins.Plugin):
capacity = 0
voltage = 0.00
temp = 0
logging.info(f"[PiSugarX] PiSugar is not ready")
logging.debug("[PiSugarX] PiSugar is not ready")
# Check if battery is plugged in
battery_plugged = self.safe_get(
self.ps.get_battery_power_plugged, default=False)
# Check if battery is plugged in (only when ready and ps is available)
battery_plugged = False
if self.ready and self.ps is not None:
battery_plugged = self.safe_get(
self.ps.get_battery_power_plugged, default=False)
if battery_plugged:
# If plugged in, display "CHG"