diff --git a/roles/laptop/files/power_monitor.service b/roles/laptop/files/power_monitor.service new file mode 100644 index 0000000..d8931d4 --- /dev/null +++ b/roles/laptop/files/power_monitor.service @@ -0,0 +1,6 @@ +[Service] +Environment=STARTUP_WAIT=30s +ExecStart=/usr/local/bin/power_monitor.sh + +[Install] +WantedBy=default.target diff --git a/roles/laptop/files/power_monitor.sh b/roles/laptop/files/power_monitor.sh new file mode 100644 index 0000000..197e35f --- /dev/null +++ b/roles/laptop/files/power_monitor.sh @@ -0,0 +1,43 @@ +#! /bin/bash +# Original stolen from Kobus van Schoor: +# https://kobusvs.co.za/blog/power-profile-switching/ + +BAT=$(echo /sys/class/power_supply/BAT*) +BAT_STATUS="$BAT/status" +BAT_CAP="$BAT/capacity" +LOW_BAT_PERCENT=50 + +AC_PROFILE="desktop" +BAT_PROFILE="balanced" +LOW_BAT_PROFILE="laptop-battery-powersave" + +# wait a while if needed +[[ -z $STARTUP_WAIT ]] || sleep "$STARTUP_WAIT" + +# start the monitor loop +prev=0 + +while true; do + # read the current state + if [[ $(cat "$BAT_STATUS") == "Discharging" ]]; then + if [[ $(cat "$BAT_CAP") -gt $LOW_BAT_PERCENT ]]; then + profile=$BAT_PROFILE + else + profile=$LOW_BAT_PROFILE + fi + else + profile=$AC_PROFILE + fi + + # set the new profile + if [[ $prev != "$profile" ]]; then + echo setting power profile to $profile + # powerprofilesctl set $profile + tuned-adm profile $profile + fi + + prev=$profile + + # wait for the next power change event + inotifywait -qq "$BAT_STATUS" "$BAT_CAP" +done diff --git a/roles/laptop/handlers/main.yml b/roles/laptop/handlers/main.yml index 02ad1cc..18d48bd 100644 --- a/roles/laptop/handlers/main.yml +++ b/roles/laptop/handlers/main.yml @@ -2,5 +2,7 @@ - name: restart tlp service: name=tlp.service state=restarted -- name: restart lowbatt - service: name=lowbatt.timer state=restarted +- name: restart power monitor + service: + name: power_monitor.service + state: restarted diff --git a/roles/laptop/tasks/tuned.yml b/roles/laptop/tasks/tuned.yml index 284b7d3..bc2a74d 100644 --- a/roles/laptop/tasks/tuned.yml +++ b/roles/laptop/tasks/tuned.yml @@ -13,3 +13,38 @@ state: started tags: - tuned + +- name: Install inotify-tools + pacman: + name: inotify-tools + state: present + tags: + - tuned + +- name: Install power monitor script + copy: + src: power_monitor.sh + dest: /usr/local/bin/ + mode: 0755 + notify: + - restart power monitor + tags: + - tuned + +- name: Copy power monitor service + copy: + src: power_monitor.service + dest: /etc/systemd/system/ + notify: + - reload systemd config + - restart power monitor + tags: + - tuned + +- name: Enable and start power monitor service + service: + name: power_monitor.service + enabled: yes + state: started + tags: + - tuned