From a312677a5822216459fcf7d7b17bbd4c84b51266 Mon Sep 17 00:00:00 2001
From: makefu <github@syntax-fehler.de>
Date: Sun, 30 Jan 2022 23:51:32 +0100
Subject: [PATCH] ma ham: commit open changes

---
 2configs/home/ham/automation/check-in.nix     |  62 ++++++++++
 2configs/home/ham/automation/flurlicht.nix    |  31 ++++-
 .../home/ham/automation/shutdown_button.nix   |  48 ++++++++
 2configs/home/ham/automation/urlaub.nix       |   8 +-
 .../wohnzimmer_rf_fernbedienung.nix           |   2 +-
 2configs/home/ham/calendar/nextcloud.nix      |   2 -
 2configs/home/ham/default.nix                 |  51 +++------
 2configs/home/ham/lib/cheat-sheet.nix         |  42 +++++++
 2configs/home/ham/lib/default.nix             |  52 +++++----
 .../home/ham/light/tint_arbeitszimmer.nix     |  13 ++-
 2configs/home/ham/light/tint_wohnzimmer.nix   |  13 ++-
 2configs/home/ham/media/firetv.nix            |  32 ++++++
 .../ham/media/remote_sound_wohnzimmer.nix     | 107 ++++++++++++++++++
 2configs/home/ham/media/sonos.nix             |   7 ++
 2configs/home/ham/tts/google.nix              |  18 +++
 15 files changed, 410 insertions(+), 78 deletions(-)
 create mode 100644 2configs/home/ham/automation/check-in.nix
 create mode 100644 2configs/home/ham/automation/shutdown_button.nix
 create mode 100644 2configs/home/ham/lib/cheat-sheet.nix
 create mode 100644 2configs/home/ham/media/firetv.nix
 create mode 100644 2configs/home/ham/media/remote_sound_wohnzimmer.nix
 create mode 100644 2configs/home/ham/media/sonos.nix
 create mode 100644 2configs/home/ham/tts/google.nix

diff --git a/2configs/home/ham/automation/check-in.nix b/2configs/home/ham/automation/check-in.nix
new file mode 100644
index 0000000..21040b4
--- /dev/null
+++ b/2configs/home/ham/automation/check-in.nix
@@ -0,0 +1,62 @@
+let
+  button = "binary_sensor.arbeitszimmer_onebutton_button";
+  light = "light.arbeitszimmer_onebutton_led";
+  at_work = "input_boolean.felix_at_work";
+  lib = import ../lib;
+  say = lib.say.living_room;
+in
+{
+  services.home-assistant.config.input_boolean.felix_at_work.name = "Felix auf Arbeit";
+  services.home-assistant.config.automation =
+    [
+      { alias = "Push Check-in Button Felix with button";
+        trigger = [
+            {
+              platform = "state";
+              entity_id = button;
+              to = "on";
+              for.seconds = 1;
+            }
+        ];
+        condition = [
+        ];
+        action =
+          [
+            { choose = [
+                {
+                  conditions = {
+                    condition = "state";
+                    entity_id = at_work;
+                    state =  "off";
+                  };
+                  sequence = [
+                    { service = "light.turn_on";
+                      target.entity_id = light;
+                      data.brightness = 200;
+                    }
+                    { service = "homeassistant.turn_on";
+                      entity_id = at_work;
+                    }
+                  ] ++ (say "Willkommen auf Arbeit");
+                }
+                {
+                  conditions = {
+                    condition = "state";
+                    entity_id = at_work;
+                    state =  "on";
+                  };
+                  sequence = [
+                    { service = "light.turn_off";
+                      target.entity_id = light;
+                    }
+                    { service = "homeassistant.turn_off";
+                      entity_id = at_work;
+                    }
+                  ] ++ (say "Endlich ist Pappa fertig mit arbeit!");
+                }
+              ];
+            }
+          ];
+      }
+    ];
+}
diff --git a/2configs/home/ham/automation/flurlicht.nix b/2configs/home/ham/automation/flurlicht.nix
index d400225..c26f7e3 100644
--- a/2configs/home/ham/automation/flurlicht.nix
+++ b/2configs/home/ham/automation/flurlicht.nix
@@ -1,5 +1,7 @@
 let
-  licht = "light.flur_statuslight";
+  licht = [ "light.flur_statuslight" "light.wohnzimmer_status_led" ];
+  kehrwoche_color = [ 204 0 255 ]; # pink
+  nachtlicht_color = [ 255 190 0 ]; # ein dunkles rot
 in
 {
   services.home-assistant.config.automation =
@@ -16,22 +18,39 @@ in
           target.entity_id = licht;
           data = {
             brightness = 87;
-            rgbw_color = [ 255 190 0 0 ]; # ein dunkles rot
+            rgb_color = nachtlicht_color;
             #effect = "None";
           };
         }
       ];
     }
-    { alias = "Nachtlicht in Flur aus";
+    { alias = "Nachtlicht in Flur aus, Kehrwoche an";
       trigger = {
         platform = "sun";
         event = "sunrise";
       };
       action =
       [
-        {
-          service = "light.turn_off";
-          entity_id = licht;
+        { choose = [
+          {
+            conditions = {
+              condition = "state";
+              entity_id = "calendar.kehrwoche_kehrwoche";
+              state =  "on";
+            };
+            sequence = {
+              service = "light.turn_on";
+              target.entity_id = licht;
+              data = {
+                brightness = 190;
+                rgb_color = kehrwoche_color; # pink
+              };
+            };
+          }];
+          default = {
+              service = "light.turn_off";
+              entity_id = licht;
+            };
         }
       ];
     }
diff --git a/2configs/home/ham/automation/shutdown_button.nix b/2configs/home/ham/automation/shutdown_button.nix
new file mode 100644
index 0000000..4c12be2
--- /dev/null
+++ b/2configs/home/ham/automation/shutdown_button.nix
@@ -0,0 +1,48 @@
+let
+  btn = "sensor.arbeitszimmer_btn1_action";
+  all_lights = [
+    # Wohnzimmer
+    "light.wled"
+    "light.wled_2"
+    "light.wohnzimmer_komode_osram"
+    "light.wohnzimmer_schrank_osram"
+    "light.wohnzimmer_fenster_lichterkette_licht"
+
+    # Arbeitszimmer
+    "light.wled_3"
+    "light.wled_4"
+    "light.arbeitszimmer_schrank_dimmer"
+    "light.arbeitszimmer_pflanzenlicht"
+    "light.wohnzimmer_stehlampe_osram"
+
+    # Keller
+    "light.keller_osram"
+  ];
+  all_media_player = [
+    "media_player.living_room"
+    "media_player.office"
+  ];
+in {
+  services.home-assistant.config.automation =
+    [
+      { alias = "Wohnung shutdown single click";
+      trigger = [
+        {
+          platform = "state";
+          entity_id = btn;
+          to = "on";
+        }
+      ];
+      condition = [ ];
+      action = [
+        {
+          service = "home_assistant.turn_off";
+          target.entity_id = all_lights;
+        }
+        { service = "media_player.media_stop";
+          target.entity_id = all_media_player;
+        }
+      ];
+    }
+  ];
+}
diff --git a/2configs/home/ham/automation/urlaub.nix b/2configs/home/ham/automation/urlaub.nix
index a6b9be9..5029451 100644
--- a/2configs/home/ham/automation/urlaub.nix
+++ b/2configs/home/ham/automation/urlaub.nix
@@ -5,8 +5,9 @@
 
 let
   schranklicht = "light.wohnzimmer_schrank_osram";
-  fernsehlicht = "light.wohnzimmer_fernseher_led_strip";
-  final_off = "01:00";
+  weihnachtslicht = "light.wohnzimmer_fenster_lichterkette_licht";
+  fernsehlicht = "light.wled";
+  final_off = "00:37";
 
   turn_on = entity_id: at:
   { alias = "Turn on ${entity_id} at ${at}";
@@ -25,6 +26,7 @@ in
     automation =
     [
       (turn_on schranklicht "17:30")
+      (turn_on weihnachtslicht "17:30")
       (turn_on fernsehlicht "19:00")
 
       { alias = "Always turn off the urlaub lights at ${final_off}";
@@ -35,7 +37,7 @@ in
         [
           {
             service = "light.turn_off";
-            entity_id =  [ schranklicht fernsehlicht ];
+            entity_id =  [ schranklicht weihnachtslicht fernsehlicht ];
           }
         ];
       }
diff --git a/2configs/home/ham/automation/wohnzimmer_rf_fernbedienung.nix b/2configs/home/ham/automation/wohnzimmer_rf_fernbedienung.nix
index 31d9b16..b67dacb 100644
--- a/2configs/home/ham/automation/wohnzimmer_rf_fernbedienung.nix
+++ b/2configs/home/ham/automation/wohnzimmer_rf_fernbedienung.nix
@@ -135,7 +135,7 @@ in
           data.entity_id = [
             "light.wohnzimmer_fernseher_led_strip" "light.wohnzimmer_stehlampe_osram"
             "light.wohnzimmer_komode_osram" "light.wohnzimmer_schrank_osram"
-            "light.wohnzimmer_fenster_lichterkette_licht" "light.wohnzimmer_fernsehwand_led"
+            "light.wohnzimmer_fenster_lichterkette_licht" "light.wled"
           ];
         };
       })
diff --git a/2configs/home/ham/calendar/nextcloud.nix b/2configs/home/ham/calendar/nextcloud.nix
index 6035904..3eb68de 100644
--- a/2configs/home/ham/calendar/nextcloud.nix
+++ b/2configs/home/ham/calendar/nextcloud.nix
@@ -8,8 +8,6 @@ in
       platform = "caldav";
       inherit (cred) username password;
       url = "https://o.euer.krebsco.de/remote.php/dav";
-      # make calendars "all-day" before uploading:
-      #   sed -i -e 's/^\(DTSTART;.*\)T......\r$/\1\r/' -e #   's/^\(DTEND;.*\)T......\r$/\1\r/' abfall.ical
       custom_calendars = [
         {
           name = "Gelbersack";
diff --git a/2configs/home/ham/default.nix b/2configs/home/ham/default.nix
index e17cfc3..b9701f1 100644
--- a/2configs/home/ham/default.nix
+++ b/2configs/home/ham/default.nix
@@ -4,7 +4,6 @@
 ##
 let
   prefix = (import ./lib).prefix;
-  firetv_stick = "192.168.111.24";
   hassdir = "/var/lib/hass";
   unstable = import <nixpkgs-unstable> {};
 
@@ -29,6 +28,11 @@ in {
 
     ./calendar/nextcloud.nix
 
+    ./media/firetv.nix
+    ./media/sonos.nix
+    ./media/remote_sound_wohnzimmer.nix
+
+    ./automation/check-in.nix
     ./automation/fenster_auf.nix
     ./automation/firetv_restart.nix
     ./automation/light_buttons.nix
@@ -37,22 +41,25 @@ in {
     ./automation/flurlicht.nix
     #./automation/giesskanne.nix
     ./automation/pflanzen_giessen_erinnerung.nix
-    #./automation/urlaub.nix
+    ./automation/urlaub.nix
     ./automation/moodlight.nix
+    ./automation/shutdown_button.nix
 
     ./light/arbeitszimmer.nix
     ./light/schlafzimmer.nix
     ./light/wohnzimmer.nix
+
+    ./tts/google.nix
   ];
 
   services.home-assistant = {
-    package = (unstable.home-assistant.overrideAttrs (old: {
+    package = (pkgs.home-assistant.overrideAttrs (old: {
       doInstallCheck = false;
     })).override {
-      extraPackages = p: [ 
-        (p.callPackage ./deps/dwdwfsapi.nix {}) 
-        (p.callPackage ./signal-rest/pkg.nix {}) 
-        #(p.callPackage ./deps/pykodi.nix {}) 
+      extraPackages = p: [
+        (p.callPackage ./deps/dwdwfsapi.nix {})
+        (p.callPackage ./signal-rest/pkg.nix {})
+        #(p.callPackage ./deps/pykodi.nix {})
       ];
     };
 
@@ -88,13 +95,6 @@ in {
         default = "info";
       };
       rest_command = {};
-      tts = [
-        { platform = "google_translate";
-          language = "de";
-          time_memory = 57600;
-          service_name =  "google_say";
-        }
-      ];
       api = {};
       esphome = {}; # fails
       camera = [];
@@ -108,16 +108,6 @@ in {
       #    (builtins.readFile <secrets/hass/telegram-bot.json>))
       #];
       notify = [
-        #{
-        #  platform = "kodi";
-        #  name = "Kodi Wohnzimmer";
-        #  host = firetv_stick;
-        #}
-        {
-          platform = "nfandroidtv";
-          name = "FireTV Wohnzimmer";
-          host = firetv_stick;
-        }
         #{
         #  platform = "telegram";
         #  name = "telegrambot";
@@ -128,19 +118,6 @@ in {
           ];
       sun.elevation = 247;
       recorder = {};
-      media_player = [
-        { platform = "kodi";
-          name = "FireTV Stick kodi";
-          host = firetv_stick;
-        }
-        { platform = "androidtv";
-          name = "FireTV Stick";
-          device_class = "firetv";
-          # adb_server_ip = firetv_stick;
-          host = firetv_stick;
-          port = 5555;
-        }
-      ];
       mqtt = {
         broker = "localhost";
         discovery = true; #enable esphome discovery
diff --git a/2configs/home/ham/lib/cheat-sheet.nix b/2configs/home/ham/lib/cheat-sheet.nix
new file mode 100644
index 0000000..f593ef4
--- /dev/null
+++ b/2configs/home/ham/lib/cheat-sheet.nix
@@ -0,0 +1,42 @@
+# Begin
+let
+in {
+  services.home-assistant.config.automation =
+    [
+    ];
+}
+
+# example automation
+    { alias = "";
+      trigger = [
+          {
+            platform = "state";
+            entity_id = "";
+            to = "on";
+            for.seconds = 0;
+          }
+      ];
+      condition = [
+        { condition = "state";
+          entity_id = "";
+          state = "off";
+        }
+      ];
+      action =
+        [
+          { choose = [
+            {
+              conditions = {
+                condition = "state";
+                entity_id = "";
+                state =  "on";
+              };
+              sequence = [{
+                service = "home_assistant.turn_on";
+                target.entity_id = "";
+              }];
+            }];
+            default = { };
+          }
+        ];
+      }
diff --git a/2configs/home/ham/lib/default.nix b/2configs/home/ham/lib/default.nix
index 75be527..67e8e90 100644
--- a/2configs/home/ham/lib/default.nix
+++ b/2configs/home/ham/lib/default.nix
@@ -8,36 +8,50 @@ in
     tts = { message, entity }:
     [
       {
-        service = "media_player.turn_on";
-        data.entity_id = entity;
+        service = "sonos.snapshot";
+        target.entity_id = entity;
       }
       {
-        service = "media_player.play_media";
+        service = "tts.google_say";
         data = {
-          entity_id = entity;
-          media_content_type = "playlist";
-          media_content_id = "ansage";
-        };
-      }
-      {
-        service = "media_player.turn_on";
-        data.entity_id = entity;
-      }
-      { delay.seconds = 8; }
-      {
-        service = "tts.say";
-        entity_id =  entity;
-        data_template = {
+          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
   {
-    firetv = message: tts {
+    living_room = message: tts {
       inherit message;
-      entity = "firetv";
+      entity = "media_player.living_room";
+    };
+    office = message: tts {
+      inherit message;
+      entity = "media_player.office";
     };
   };
 
diff --git a/2configs/home/ham/light/tint_arbeitszimmer.nix b/2configs/home/ham/light/tint_arbeitszimmer.nix
index 37c961a..4ae91ac 100644
--- a/2configs/home/ham/light/tint_arbeitszimmer.nix
+++ b/2configs/home/ham/light/tint_arbeitszimmer.nix
@@ -11,14 +11,17 @@ let
   default_scene_1 = "Solid";
   default_color_1 = "Default";
   main_color_select_1 = "select.wled_color_palette_4";
-  light_group_1 = { entity_id = [ main_light_1 ];};
+  light_group_1.entity_id = [
+    main_light_1
+  ];
 
   # contains only the actually changeable lights
-  light_group_2 = { entity_id = [
+  light_group_2.entity_id = [
     "light.arbeitszimmer_schrank_dimmer"
-    ];
-  };
-  light_group_3 = { entity_id = [ "light.arbeitszimmer_pflanzenlicht" ]; };
+  ];
+  light_group_3.entity_id = [
+    "light.arbeitszimmer_pflanzenlicht"
+  ];
 
   statecond = cond: { # cond must be a list
     condition = "template";
diff --git a/2configs/home/ham/light/tint_wohnzimmer.nix b/2configs/home/ham/light/tint_wohnzimmer.nix
index 18e69a7..fd87cf0 100644
--- a/2configs/home/ham/light/tint_wohnzimmer.nix
+++ b/2configs/home/ham/light/tint_wohnzimmer.nix
@@ -25,16 +25,19 @@ let
   default_scene_1 = "Solid";
   default_color_1 = "Default";
   main_color_select_1 = "select.wled_color_palette";
-  light_group_1 = { entity_id = [ main_light_1 ];};
+  light_group_1.entity_id = [
+    main_light_1
+  ];
 
   # contains only the actually changeable lights 
-  light_group_2 = { entity_id = [
+  light_group_2.entity_id = [
     "light.wohnzimmer_komode_osram"
     "light.wohnzimmer_schrank_osram"
     "light.wohnzimmer_fenster_lichterkette_licht"
-    ];
-  };
-  light_group_3 = { entity_id = [ "light.wohnzimmer_stehlampe_osram" ]; };
+  ];
+  light_group_3.entity_id = [
+    "light.wohnzimmer_stehlampe_osram"
+  ];
 
   statecond = cond: { # cond must be a list
     condition = "template";
diff --git a/2configs/home/ham/media/firetv.nix b/2configs/home/ham/media/firetv.nix
new file mode 100644
index 0000000..429688c
--- /dev/null
+++ b/2configs/home/ham/media/firetv.nix
@@ -0,0 +1,32 @@
+let
+  firetv_stick = "192.168.111.24";
+in {
+  services.home-assistant.config = {
+    notify = [
+      #{
+      #  platform = "kodi";
+      #  name = "Kodi Wohnzimmer";
+      #  host = firetv_stick;
+      #}
+      {
+      platform = "nfandroidtv";
+      name = "FireTV Wohnzimmer";
+      host = firetv_stick;
+      }
+    ];
+    media_player = [
+        #{
+        #  platform = "kodi";
+        #  name = "FireTV Stick kodi";
+        #  host = firetv_stick;
+        #}
+        { platform = "androidtv";
+        name = "FireTV Stick";
+        device_class = "firetv";
+          # adb_server_ip = firetv_stick;
+          host = firetv_stick;
+          port = 5555;
+        }
+      ];
+    };
+  }
diff --git a/2configs/home/ham/media/remote_sound_wohnzimmer.nix b/2configs/home/ham/media/remote_sound_wohnzimmer.nix
new file mode 100644
index 0000000..2091ca9
--- /dev/null
+++ b/2configs/home/ham/media/remote_sound_wohnzimmer.nix
@@ -0,0 +1,107 @@
+{ lib, ...}:
+let
+  statecond = cond: { # cond must be a list
+    condition = "template";
+    value_template = "{{ trigger.to_state.state in ( " +
+      (lib.concatMapStringsSep "," (x: "'${x}'") cond) + ") }}";
+    };
+  vol_change = 0.030;
+
+  max_repeat = "30"; # max loops to repeat before bailing out
+  remote = "sensor.wohnzimmer_sound1_action";
+  player = "media_player.living_room";
+  last_state_sensor_name = "last_rotation_action";
+  last_state_sensor = "input_text.last_rotation_action";
+  #      - service: media_player.volume_set
+  #    target:
+  #      entity_id: media_player.kitchen
+  #    data:
+  #      volume_level: {{ state_attr('media_player.kitchen', 'volume_level') + 0.02 }}
+  rotate_stop = "brightness_stop";
+  rotate_right = "brightness_move_up";
+  rotate_left = "brightness_move_down" ;
+  
+  single_click = "toggle";
+  double_click = "brightness_step_up";
+  triple_click = "brightness_step_down";
+in {
+  services.home-assistant.config.input_text."${last_state_sensor_name}".name = "Last action of the wohnzimmer";
+  services.home-assistant.config.automation = [
+    {
+      trigger = {
+        platform = "state";
+        entity_id = remote;
+        to = [ rotate_stop ];
+      };
+      action = [
+          { service = "input_text.set_value";
+            target.entity_id = last_state_sensor;
+            data.value = "stop";
+          }
+      ];
+    }
+    {
+      alias = "Perform Actions with ${remote}";
+      trigger = {
+        platform = "state";
+        entity_id = remote;
+        to = [ single_click double_click triple_click rotate_left rotate_right ];
+      };
+      #mode = "queued";
+      #max = 5;
+      mode = "single";
+      #max_exceeded = "silent";
+      action = [
+        {
+          choose = [
+            {
+              conditions = statecond [ single_click ];
+              sequence = [
+                { service = "media_player.media_play_pause";
+                target.entity_id = player;
+                }
+              ];
+            }
+            {
+              conditions = statecond [ rotate_left rotate_right ];
+              sequence = let
+                vol_up = toString vol_change;
+                vol_down = toString (-1 * vol_change);
+              in [
+                {
+                  variables.nextvol = ''{% if trigger.to_state.state in ( "${rotate_left}" ) -%} ${vol_down} {% else -%} ${vol_up} {% endif -%}'';
+                  variables.state = ''{% if trigger.to_state.state in ( "${rotate_left}" ) -%} left {% else -%} right {% endif -%}'';
+                }
+                { service = "input_text.set_value";
+                  target.entity_id = last_state_sensor;
+                  data.value = ''{{ state }}'';
+                }
+                {
+                  repeat = {
+                    sequence = [
+                      { service = "media_player.volume_set";
+                        target.entity_id = player;
+                        data.volume_level = ''{{ state_attr("${player}","volume_level")  + (nextvol|float) }}'';
+                      }
+                      { delay.milliseconds = "150"; }
+                    ];
+                    while = [
+                      {
+                          condition = "template";
+                          value_template = ''{{ states("${last_state_sensor}") == state }}'';
+                      }
+                      {
+                        condition = "template";
+                        value_template = "{{ repeat.index <= ${max_repeat}}}";
+                      }
+                    ];
+                  };
+                }
+              ];
+            }
+          ];
+        }
+      ];
+    }
+  ];
+}
diff --git a/2configs/home/ham/media/sonos.nix b/2configs/home/ham/media/sonos.nix
new file mode 100644
index 0000000..5b96945
--- /dev/null
+++ b/2configs/home/ham/media/sonos.nix
@@ -0,0 +1,7 @@
+{
+  services.home-assistant.config.sonos.media_player.hosts = [ 
+    "192.168.111.30"
+    "192.168.111.31"
+  ];
+
+  }
diff --git a/2configs/home/ham/tts/google.nix b/2configs/home/ham/tts/google.nix
new file mode 100644
index 0000000..d2f7a55
--- /dev/null
+++ b/2configs/home/ham/tts/google.nix
@@ -0,0 +1,18 @@
+
+let
+in {
+  services.home-assistant.config.tts = [
+        { platform = "google_translate";
+          language = "de";
+          time_memory = 57600;
+          service_name =  "google_say";
+        }
+        #{ platform = "google_cloud";
+        #  key_file = toString <secrets/googlecloud.json>;
+        #  service_name =  "cloud_say";
+        #  language = "de-DE";
+        #  voice = "de-DE-Wavenet-B";
+        #  profiles = [ "medium-bluetooth-speaker-class-device" ];
+        #}
+    ];
+}