all repos

dotfiles @ 5f71cccef34230cd8e90d22f9a8015da7d55759d

i use rach linux btw
2 files changed, 100 insertions(+), 0 deletions(-)
automatically change the power profile depending on the connection and
bat percentage
Author: Oleksandr Smirnov olexsmir@gmail.com
Committed at: 2025-06-13 16:16:11 +0300
Parent: 170e4a6
A bin/power-monitor.sh
···
        
        1
        +#!/usr/bin/env bash

      
        
        2
        +# ~~stolen~~ inspired by https://github.com/linuxmobile/kaku/blob/niri/home/services/system/power-monitor.nix

      
        
        3
        +

      
        
        4
        +set -euo pipefail

      
        
        5
        +

      
        
        6
        +STARTUP_WAIT=0

      
        
        7
        +

      
        
        8
        +log() {

      
        
        9
        +  echo "[$(date '+%Y-%m-%d %H:%M:%S')] $*" >&2

      
        
        10
        +}

      
        
        11
        +

      
        
        12
        +get_battery_path() {

      
        
        13
        +  local bat_path

      
        
        14
        +  bat_path=$(echo /sys/class/power_supply/BAT*)

      
        
        15
        +  if [[ ! -d "$bat_path" ]]; then

      
        
        16
        +    log "No battery found"

      
        
        17
        +    exit 1

      
        
        18
        +  fi

      
        
        19
        +  echo "$bat_path"

      
        
        20
        +}

      
        
        21
        +

      
        
        22
        +readonly BAT="$(get_battery_path)"

      
        
        23
        +readonly BAT_STATUS="$BAT/status"

      
        
        24
        +readonly BAT_CAP="$BAT/capacity"

      
        
        25
        +readonly LOW_BAT_PERCENT=30

      
        
        26
        +

      
        
        27
        +readonly AC_PROFILE="performance"

      
        
        28
        +readonly BAT_PROFILE="balanced"

      
        
        29
        +readonly LOW_BAT_PROFILE="power-saver"

      
        
        30
        +

      
        
        31
        +for file in "$BAT_STATUS" "$BAT_CAP"; do

      
        
        32
        +  if [[ ! -f "$file" ]]; then

      
        
        33
        +    log "Required file not found: $file"

      
        
        34
        +    exit 1

      
        
        35
        +  fi

      
        
        36
        +done

      
        
        37
        +

      
        
        38
        +if ! command -v powerprofilesctl >/dev/null 2>&1; then

      
        
        39
        +  log "powerprofilesctl not found"

      
        
        40
        +  exit 1

      
        
        41
        +fi

      
        
        42
        +

      
        
        43
        +if [[ -n "''${STARTUP_WAIT:-}" ]]; then

      
        
        44
        +  sleep "$STARTUP_WAIT"

      
        
        45
        +fi

      
        
        46
        +

      
        
        47
        +get_power_profile() {

      
        
        48
        +  local status capacity

      
        
        49
        +  status=$(cat "$BAT_STATUS")

      
        
        50
        +  capacity=$(cat "$BAT_CAP")

      
        
        51
        +

      
        
        52
        +  if [[ "$status" == "Discharging" ]]; then

      
        
        53
        +    if [[ "$capacity" -gt $LOW_BAT_PERCENT ]]; then

      
        
        54
        +      echo "$BAT_PROFILE"

      
        
        55
        +    else

      
        
        56
        +      echo "$LOW_BAT_PROFILE"

      
        
        57
        +    fi

      
        
        58
        +  else

      
        
        59
        +    echo "$AC_PROFILE"

      
        
        60
        +  fi

      
        
        61
        +}

      
        
        62
        +

      
        
        63
        +apply_profile() {

      
        
        64
        +  local profile=$1

      
        
        65
        +  log "Setting power profile to $profile"

      
        
        66
        +  if ! powerprofilesctl set "$profile"; then

      
        
        67
        +    log "Failed to set power profile"

      
        
        68
        +    return 1

      
        
        69
        +  fi

      
        
        70
        +}

      
        
        71
        +

      
        
        72
        +log "Starting power monitor"

      
        
        73
        +prev_profile=""

      
        
        74
        +

      
        
        75
        +while true; do

      
        
        76
        +  current_profile=$(get_power_profile)

      
        
        77
        +

      
        
        78
        +  if [[ "$prev_profile" != "$current_profile" ]]; then

      
        
        79
        +    apply_profile "$current_profile"

      
        
        80
        +    prev_profile=$current_profile

      
        
        81
        +  fi

      
        
        82
        +

      
        
        83
        +  if ! inotifywait -qq "$BAT_STATUS" "$BAT_CAP"; then

      
        
        84
        +    log "inotifywait failed, sleeping for 5 seconds before retry"

      
        
        85
        +    sleep 5

      
        
        86
        +  fi

      
        
        87
        +done

      
A config/systemd/user/power-monitor.service
···
        
        1
        +[Unit]

      
        
        2
        +Description = Power Monitor

      
        
        3
        +After = power-profiles-daemon.service

      
        
        4
        +Wants = power-profiles-daemon.service

      
        
        5
        +

      
        
        6
        +[Service]

      
        
        7
        +Type = simple

      
        
        8
        +ExecStart = /home/olex/bin/power-monitor.sh

      
        
        9
        +Restart = on-failure

      
        
        10
        +RestartSec = 5s

      
        
        11
        +

      
        
        12
        +[Install]

      
        
        13
        +WantedBy=default.target