From a36600126b0c9c22f16c56029765d8ce06f7a7a2 Mon Sep 17 00:00:00 2001 From: Przemek Grondek Date: Thu, 14 Jul 2022 21:06:42 +0200 Subject: [PATCH] Add battery charge limiter for laptop --- .../system/battery-charge-threshold.service | 12 ++++++++++++ roles/laptop/tasks/battery.yml | 18 ++++++++++++++++++ roles/laptop/tasks/main.yml | 1 + 3 files changed, 31 insertions(+) create mode 100644 roles/laptop/files/lib/systemd/system/battery-charge-threshold.service create mode 100644 roles/laptop/tasks/battery.yml create mode 100644 roles/laptop/tasks/main.yml diff --git a/roles/laptop/files/lib/systemd/system/battery-charge-threshold.service b/roles/laptop/files/lib/systemd/system/battery-charge-threshold.service new file mode 100644 index 0000000..be18e16 --- /dev/null +++ b/roles/laptop/files/lib/systemd/system/battery-charge-threshold.service @@ -0,0 +1,12 @@ +[Unit] +Description=Set the battery charge threshold +After=multi-user.target +StartLimitBurst=0 + +[Service] +Type=oneshot +Restart=on-failure +ExecStart=/bin/bash -c 'echo 80 > /sys/class/power_supply/BAT0/charge_control_end_threshold' + +[Install] +WantedBy=multi-user.target \ No newline at end of file diff --git a/roles/laptop/tasks/battery.yml b/roles/laptop/tasks/battery.yml new file mode 100644 index 0000000..c6ad9bb --- /dev/null +++ b/roles/laptop/tasks/battery.yml @@ -0,0 +1,18 @@ +- name: Check if laptop has option to limit battery + stat: + path: /sys/class/power_supply/BAT0/charge_control_end_threshold + register: battery_limiter_available + +- when: battery_limiter_available.stat.exists + block: + - name: Copy service to limit battery charge + become: yes + copy: + src: lib/systemd/system/battery-charge-threshold.service + dest: /lib/systemd/system/battery-charge-threshold.service + + - name: Enable battery limit service + become: yes + systemd: + name: battery-charge-threshold + enabled: yes diff --git a/roles/laptop/tasks/main.yml b/roles/laptop/tasks/main.yml new file mode 100644 index 0000000..cdda24d --- /dev/null +++ b/roles/laptop/tasks/main.yml @@ -0,0 +1 @@ +- import_tasks: battery.yml