Fix power profile auto-switching on USB-C only machines (#5444)

* Fix power profile auto-switching on USB-C only machines

* Small nits

* Stick with single unit name

---------

Co-authored-by: David Heinemeier Hansson <david@hey.com>
This commit is contained in:
Mikko Nyman
2026-05-02 11:47:02 +02:00
committed by GitHub
co-authored by David Heinemeier Hansson
parent d80c98f025
commit 62f6980852
3 changed files with 32 additions and 5 deletions
+23 -3
View File
@@ -1,11 +1,31 @@
#!/bin/bash
# omarchy:summary=Set the power profile to the requested level, falling back to balanced
# omarchy:args=<ac|battery>
# omarchy:args=[autodetect|ac|battery]
action="${1-}"
# Auto-detect when called with no argument: treat any Mains or USB
# power-supply device reporting online=1 as "on AC". This handles
# USB-C only laptops where the legacy AC device may not fire udev
# events, and also avoids false negatives from per-port USB-C devices
# that are present-but-empty (online=0) while another port supplies power.
if [[ -z $action || $action == "autodetect" ]]; then
action=battery
for ps in /sys/class/power_supply/*; do
[[ -r $ps/online && -r $ps/type ]] || continue
type=$(cat "$ps/type")
[[ $type == "Mains" || $type == "USB" ]] || continue
if [[ $(cat "$ps/online") == "1" ]]; then
action=ac
break
fi
done
fi
mapfile -t profiles < <(powerprofilesctl list | awk '/^\s*[* ]\s*[a-zA-Z0-9\-]+:$/ { gsub(/^[*[:space:]]+|:$/,""); print }')
case "$1" in
case "$action" in
ac)
# Prefer performance, fall back to balanced
if [[ " ${profiles[*]} " == *" performance "* ]]; then
@@ -17,4 +37,4 @@ case "$1" in
battery)
powerprofilesctl set balanced
;;
esac
esac