da-rth / yasb

A highly configurable cross-platform (Windows) status bar written in Python.
MIT License
1.39k stars 81 forks source link

[BUG] Battery State is None in Desktop PCs #141

Open ChriWt opened 5 months ago

ChriWt commented 5 months ago

Expected Behavior

If the user is on a desktop PC rather than a laptop, psutil will not be able to detect a battery sensor and will return None.

Expected Behavior:

  1. Battery Present (Laptop): Returns an object with battery details like charge level and time remaining.
  2. Battery Absent (Desktop PC): Implement checks or use exception handling (try-except) to avoid AttributeError errors.

Current Behavior

Because the state of the battery is not verified, this triggers AttributeError exceptions (see yasb.log).

Possible Solution

This is the solution that I've implemented on my code: Inside: "yasb\src\core\widgets\yasb\battery.py"

alt_class = "alt" if self._show_alt_label else "" self._battery_state = psutil.sensors_battery()

    if self._battery_state is None:
        # With this the user is still able to customize the widget
        active_label.setProperty("class", f"label {alt_class} status-high")
        return

Steps to Reproduce

Run YASB on a Desktop PC

Context (Environment)

Platform: Windows 11 Python Version: 3.12.2 Psutil Version: 5.9.1 yasb.log