l 3 power-action: add charging check
This commit is contained in:
parent
6e2cbdfb73
commit
1b238cf556
@ -25,6 +25,16 @@ let
|
||||
plans = mkOption {
|
||||
type = with types; attrsOf (submodule {
|
||||
options = {
|
||||
charging = mkOption {
|
||||
type = nullOr bool;
|
||||
default = null;
|
||||
description = ''
|
||||
check for charging status.
|
||||
null = don't care
|
||||
true = only if system is charging
|
||||
false = only if system is discharging
|
||||
'';
|
||||
};
|
||||
upperLimit = mkOption {
|
||||
type = int;
|
||||
};
|
||||
@ -53,15 +63,31 @@ let
|
||||
};
|
||||
|
||||
startScript = pkgs.writeDash "power-action" ''
|
||||
set -euf
|
||||
|
||||
power="$(${powerlvl})"
|
||||
state="$(${state})"
|
||||
${concatStringsSep "\n" (mapAttrsToList writeRule cfg.plans)}
|
||||
'';
|
||||
charging_check = plan:
|
||||
if (plan.charging == null) then "" else
|
||||
if plan.charging
|
||||
then ''&& [ "$state" = "true" ]''
|
||||
else ''&& ! [ "$state" = "true" ]''
|
||||
;
|
||||
|
||||
writeRule = _: plan:
|
||||
"if [ $power -ge ${toString plan.lowerLimit} ] && [ $power -le ${toString plan.upperLimit} ]; then ${plan.action}; fi";
|
||||
"if [ $power -ge ${toString plan.lowerLimit} ] && [ $power -le ${toString plan.upperLimit} ] ${charging_check plan}; then ${plan.action}; fi";
|
||||
|
||||
powerlvl = pkgs.writeDash "powerlvl" ''
|
||||
cat /sys/class/power_supply/BAT0/capacity
|
||||
'';
|
||||
|
||||
state = pkgs.writeDash "state" ''
|
||||
if [ "$(cat /sys/class/power_supply/BAT0/status)" = "Charging" ]
|
||||
then echo "true"
|
||||
else echo "false"
|
||||
fi
|
||||
'';
|
||||
|
||||
in out
|
||||
|
Loading…
Reference in New Issue
Block a user