-
-
Save haydenjames/2bf5b2834906c079fca0f51bd3472638 to your computer and use it in GitHub Desktop.
Auto Update for power-profiles-daemon
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Initialize the last known state to prevent redundant actions | |
LAST_LEVEL="" | |
dbus-monitor --system "type='signal',path='/org/freedesktop/UPower/devices/battery_BAT0',member='PropertiesChanged'" | while read LINE; do | |
echo ${LINE} | grep battery_BAT0 | grep -q PropertiesChanged | |
if [ $? -eq 0 ]; then | |
BATT_STAT=$(dbus-send --print-reply=literal --system --dest=org.freedesktop.UPower /org/freedesktop/UPower/devices/battery_BAT0 org.freedesktop.DBus.Properties.Get string:org.freedesktop.UPower.Device string:State | awk '{ print $3; }') | |
# Determine the desired power level | |
if [ $BATT_STAT -eq 1 ] || [ $BATT_STAT -eq 4 ]; then | |
LEVEL="balanced" # Use balanced when on AC | |
elif [ $BATT_STAT -eq 5 ]; then | |
LEVEL="balanced" # Use balanced for other states if needed | |
else | |
LEVEL="power-saver" # Use power-saver when on battery | |
fi | |
# Apply changes only if the level has changed | |
if [ "$LEVEL" != "$LAST_LEVEL" ]; then | |
echo "Changing power level to ${LEVEL}" | |
gdbus call --system --dest net.hadess.PowerProfiles --object-path /net/hadess/PowerProfiles --method org.freedesktop.DBus.Properties.Set 'net.hadess.PowerProfiles' 'ActiveProfile' "<'${LEVEL}'>" > /dev/null | |
[[ $? -ne 0 ]] && echo "Could not change power level to ${LEVEL}!" | |
LAST_LEVEL="$LEVEL" # Update the last known state | |
fi | |
fi | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment