diff options
author | makefu <github@syntax-fehler.de> | 2021-08-31 15:14:37 +0200 |
---|---|---|
committer | makefu <github@syntax-fehler.de> | 2021-08-31 15:24:48 +0200 |
commit | 6fa5b38bed3f8e73d6f86a844ef775735a3cad29 (patch) | |
tree | a0c171025107c1adef3c0f1a6eac5cbc65862436 /makefu/2configs/home/ham/lib | |
parent | 2c9923264e318ad83ab9ceb4a13e47112dff0d4f (diff) |
ma ham: consolidate button management
Diffstat (limited to 'makefu/2configs/home/ham/lib')
-rw-r--r-- | makefu/2configs/home/ham/lib/default.nix | 76 |
1 files changed, 76 insertions, 0 deletions
diff --git a/makefu/2configs/home/ham/lib/default.nix b/makefu/2configs/home/ham/lib/default.nix index 45c86138b..75be52737 100644 --- a/makefu/2configs/home/ham/lib/default.nix +++ b/makefu/2configs/home/ham/lib/default.nix @@ -40,5 +40,81 @@ in entity = "firetv"; }; }; + zigbee.prefix = "/ham/zigbee"; + + btn_cycle_light = light: btn: halfbright: + let + maxbright = 255; + transition = 0.2; # seconds + in + # this function implements a simple state machine based on the state and brightness of the light (light must support brightness + { + alias = "Cycle through states of ${light} via button ${btn}"; + trigger = { + platform = "state"; + entity_id = "sensor.${btn}_click"; + to = "single"; + }; + action = { + choose = [ + { + # state 0: off to half + conditions = { + condition = "template"; + value_template = ''{{ states("${light}") == "off" }}''; + }; + sequence = [ + { + service = "light.turn_on"; + data = { + entity_id = light; + brightness = halfbright; + }; + } + ]; + } + { + # state 1: half to full + conditions = { + condition = "template"; + value_template = ''{{ states('${light}') == 'on' and ( ${toString (halfbright - 1)} <= state_attr("${light}","brightness") <= ${toString (halfbright + 1)})}}''; + }; + sequence = [ + { + service = "light.turn_on"; + data = { + entity_id = light; + brightness = maxbright; + }; + } + ]; + } + { + # state 2: full to off + conditions = { + condition = "template"; + # TODO: it seems like the devices respond with brightness-1 , maybe off-by-one somewhere? + value_template = ''{{ states("${light}") == "on" and state_attr("${light}","brightness") >= ${toString (maxbright - 1)}}}''; + }; + sequence = [ + { + service = "light.turn_off"; + data = { + entity_id = light; + }; + } + ]; + } + ]; + # default: on to off + # this works because state 0 checks for "state == off" + default = [{ + service = "light.turn_off"; + data = { + entity_id = light; + }; + }]; + }; + }; } |