From de9bb43c2676e161662212b8b68e4d2f164553cc Mon Sep 17 00:00:00 2001 From: bbedward Date: Fri, 19 Sep 2025 11:34:16 -0400 Subject: [PATCH] janky battery health workaround --- Services/BatteryService.qml | 39 ++++++++++++++++++++++++++++++++++--- 1 file changed, 36 insertions(+), 3 deletions(-) diff --git a/Services/BatteryService.qml b/Services/BatteryService.qml index d6b7d279..7bc52a97 100644 --- a/Services/BatteryService.qml +++ b/Services/BatteryService.qml @@ -4,6 +4,7 @@ pragma ComponentBehavior: Bound import QtQuick import Quickshell +import Quickshell.Io import Quickshell.Services.UPower Singleton { @@ -24,9 +25,8 @@ Singleton { return `${Math.round(device.healthPercentage)}%` } - if (device.energyCapacity > 0 && device.energy > 0) { - const healthPercent = (device.energyCapacity / 90.0045) * 100 - return `${Math.round(healthPercent)}%` + if (batteryHealthUpower > 0) { + return `${batteryHealthUpower}%` } return "N/A" @@ -62,6 +62,39 @@ Singleton { return btDevices } + readonly property string capacityBaseCmd: `upower -i /org/freedesktop/UPower/devices/battery_DEVICE_REPLACE_ME | awk -F': *' '/^\\s*capacity:/ {gsub(/%/,"",$2); print $2; exit}'` + property var batteryHealthUpower: -1 + + Component.onCompleted: { + // ! TODO - quickshell doesnt seem to expose health correctly all the time, so this is a janky workaround + for (const device of UPower.devices.values) { + if (device.isLaptopBattery) { + batteryCapacityProcess.command = ["sh", "-c", capacityBaseCmd.replace("DEVICE_REPLACE_ME", device.nativePath)] + console.log("Executing battery capacity command: " + batteryCapacityProcess.command) + batteryCapacityProcess.running = true + break + } + } + } + + Process { + id: batteryCapacityProcess + running: false + + stdout: StdioCollector { + onStreamFinished: { + console.log("Battery capacity (upower) raw: " + text) + const capacity = parseFloat(text.trim()) + if (!isNaN(capacity) && capacity > 0 && capacity <= 100) { + root.batteryHealthUpower = Math.round(capacity) + console.log("Battery health (upower): " + root.batteryHealthUpower + "%") + } else { + root.batteryHealthUpower = -1 + } + } + } + } + function formatTimeRemaining() { if (!batteryAvailable) { return "Unknown"