linrunner / TLP

TLP - Optimize Linux Laptop Battery Life
https://linrunner.de/tlp
GNU General Public License v2.0
2.51k stars 129 forks source link

Stay in battery mode when charging with external USB-C PD Power Bank #726

Closed norpol closed 4 months ago

norpol commented 4 months ago

Is your feature request related to a problem? Please describe.

When I'm using my USB-PD PowerBank I'd like to stay in the battery configuration and perhaps also stop charging the internal battery altogether. Unfortunately the PowerBank doesn't show up differently when looking at the udevadm monitor or lsusb output, but perhaps there are other power banks that share a USB identifier before negotiation the power delivery.

Describe the solution you'd like

A clear and concise description of:

Describe alternatives you've considered

An alternative would be a way to send tlp a message to switch to a certain configuration, perhaps via udevadm trigger or dbus?

Alternative is to just run sudo tlp bat to switch to the battery mode and setting the charge settings with tlp or /sys/class/power_supply/BAT0/charge_control_end_threshold manually.

Alternative workaround for now. Run powerbank before or when connecting to a USB powerbank.

#!/usr/bin/env bash
set -ueo pipefail
bat="/sys/class/power_supply/BAT0/"
start="$(cat "${bat}/charge_control_start_threshold")"
end="$(cat "${bat}/charge_control_end_threshold")"

echo "0" | sudo tee "${bat}/charge_control_start_threshold"
echo "5" | sudo tee "${bat}/charge_control_end_threshold"

sudo tlp bat

UPOWER_AC_DEVICE='/org/freedesktop/UPower/devices/line_power_AC'

# props https://github.com/alba4k/.dotfiles/blob/e723af501e033e1e440a043f13616a301d72f675/.config/hypr/ac_detect.sh#L6
_ac_connected() {
    dbus-monitor --system "type='signal',
            sender='org.freedesktop.UPower',
            path='$UPOWER_AC_DEVICE',
            interface='org.freedesktop.DBus.Properties',member='PropertiesChanged'" \
    |& grep --line-buffered -oP 'boolean \K.*$'
}

while read -r connected; do
    if [ "${connected}" == "false" ]; then
        echo "AC disconnected"
        break
    elif [ "${connected}" == "true" ]; then
        echo "AC connected"
        continue
    else
        echo "AC unknown"
        break
    fi
done < <(_ac_connected) # break in while does not work if you pipe into `read -r`

echo "Restore previous tlp"
echo "${start}" | sudo tee "${bat}/charge_control_start_threshold"
echo "${end}" | sudo tee "${bat}/charge_control_end_threshold"
sudo tlp start