summaryrefslogtreecommitdiffstats
path: root/makefu/2configs/home/ham/lib/default.nix
blob: 67e8e902d12e80ae79f3565a2d508341197ea3c5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
let
  prefix = "/ham";
in
{
  inherit prefix;
  say = let
    # returns a list of actions to be performed on an mpd to say something
    tts = { message, entity }:
    [
      {
        service = "sonos.snapshot";
        target.entity_id = entity;
      }
      {
        service = "tts.google_say";
        data = {
          entity_id =  entity;
          inherit message;
          language = "de";
        };
      }
      #{ wait_template = "{{ is_state('${entity}' , 'playing') }}";
      #  timeout = "00:00:02";
      #}
      #{ wait_template = "{{ not is_state('${entity}' , 'playing') }}";
      #  timeout = "00:01:00";
      #}
      { delay.seconds = 1; }
      { delay = ''
        {% set duration = states.${entity}.attributes.media_duration %}
        {% if duration > 0 %}
          {% set duration = duration - 1 %}
        {% endif %}
        {% set seconds = duration % 60 %}
        {% set minutes = (duration / 60)|int % 60 %}
        {% set hours = (duration / 3600)|int %}
        {{ "%02i:%02i:%02i"|format(hours, minutes, seconds)}}

        '';
      }
      {
        service = "sonos.restore";
        target.entity_id = entity;
      }
    ];
  in
  {
    living_room = message: tts {
      inherit message;
      entity = "media_player.living_room";
    };
    office = message: tts {
      inherit message;
      entity = "media_player.office";
    };
  };

  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;
        };
      }];
    };
  };
}