From c4640031dd553dbb5e0bdc8a2739ac598a158981 Mon Sep 17 00:00:00 2001
From: makefu <github@syntax-fehler.de>
Date: Thu, 4 Apr 2019 09:50:16 +0200
Subject: [PATCH] ma bureautomation: remove faulty motion detector

---
 .../bureautomation/automation/10h_timer.nix   |  5 +-
 .../automation/bureau-shutdown.nix            | 27 ++++----
 .../bureautomation/automation/nachtlicht.nix  |  4 +-
 .../bureautomation/device_tracker/openwrt.nix | 14 ++++
 2configs/bureautomation/devices/users.nix     | 17 +++++
 2configs/bureautomation/hass.nix              | 65 ++++++++++++++-----
 2configs/bureautomation/person/team.nix       | 29 +++++++++
 2configs/bureautomation/switch/rfbridge.nix   | 17 +++++
 .../bureautomation/switch/tasmota_switch.nix  |  2 +-
 9 files changed, 144 insertions(+), 36 deletions(-)
 create mode 100644 2configs/bureautomation/device_tracker/openwrt.nix
 create mode 100644 2configs/bureautomation/devices/users.nix
 create mode 100644 2configs/bureautomation/person/team.nix
 create mode 100644 2configs/bureautomation/switch/rfbridge.nix

diff --git a/2configs/bureautomation/automation/10h_timer.nix b/2configs/bureautomation/automation/10h_timer.nix
index a311d46..d86b461 100644
--- a/2configs/bureautomation/automation/10h_timer.nix
+++ b/2configs/bureautomation/automation/10h_timer.nix
@@ -2,8 +2,9 @@
   { alias = "start Felix 10h";
     trigger = {
       platform = "state";
-      entity_id = "binary_sensor.redbutton";
-      to = "on";
+      entity_id = [ "device_tracker.felix_phone" "device_tracker.felix_laptop" ];
+      from =  "not_home";
+      to = "home";
     };
     condition = {
       condition = "and";
diff --git a/2configs/bureautomation/automation/bureau-shutdown.nix b/2configs/bureautomation/automation/bureau-shutdown.nix
index 9e65743..1def5f3 100644
--- a/2configs/bureautomation/automation/bureau-shutdown.nix
+++ b/2configs/bureautomation/automation/bureau-shutdown.nix
@@ -1,9 +1,10 @@
 [
-  { alias = "Turn on Fernseher on movement";
+  { alias = "Turn on Fernseher on group home";
     trigger = {
-      platform = "state";
-      entity_id = "binary_sensor.motion";
-      to = "on";
+      condition = "state";
+      entity_id = "group.team";
+      from = "not_home";
+      to = "home";
     };
     action = {
       service = "homeassistant.turn_on";
@@ -13,15 +14,15 @@
       ];
     };
   }
-  { alias = "Turn off Fernseher 10 minutes after last movement";
+  { alias = "Turn off Fernseher after last in group left";
     trigger = [
     { # trigger when movement was detected at the time
-      platform = "state";
-      entity_id = "binary_sensor.motion";
-      to = "off";
-      for.minutes = 10;
+      condition = "state";
+      entity_id = "group.team";
+      from = "home";
+      to = "not_home";
     }
-    { # trigger at 20:00 no matter what
+    { # trigger at 18:00 no matter what
       # to avoid 'everybody left before 18:00:00'
       platform = "time";
       at = "18:00:00";
@@ -44,10 +45,10 @@
           after  = "18:00:00";
           # weekday = [ "mon" "tue" "wed" "thu" "fri" ];
         }
-        {
+        { # if anybody is still there
           condition = "state";
-          entity_id = "binary_sensor.motion";
-          state = "off";
+          entity_id = "group.team";
+          state = "not_home";
         }
       ];
     };
diff --git a/2configs/bureautomation/automation/nachtlicht.nix b/2configs/bureautomation/automation/nachtlicht.nix
index 066e9b0..2becd4a 100644
--- a/2configs/bureautomation/automation/nachtlicht.nix
+++ b/2configs/bureautomation/automation/nachtlicht.nix
@@ -9,7 +9,7 @@
           action =
           {
             service = "homeassistant.turn_off";
-            entity_id =  [ "switch.nachtlicht" ];
+            entity_id =  [ "group.nachtlicht" ];
           };
         }
 
@@ -37,7 +37,7 @@
           action =
           {
             service = "homeassistant.turn_on";
-            entity_id =  [ "switch.nachtlicht" ];
+            entity_id =  [ "group.nachtlicht" ];
           };
         }
 ]
diff --git a/2configs/bureautomation/device_tracker/openwrt.nix b/2configs/bureautomation/device_tracker/openwrt.nix
new file mode 100644
index 0000000..0db9821
--- /dev/null
+++ b/2configs/bureautomation/device_tracker/openwrt.nix
@@ -0,0 +1,14 @@
+[
+  { platform = "luci";
+    name = "router";
+    host = "192.168.8.1";
+    username = "root";
+    password = import <secrets/hass/router.nix>;
+    interval_seconds = 30; # instead of 12seconds
+    consider_home = 300; # 5 minutes timeout
+    new_device_defaults = {
+      track_new_devices = true;
+      hide_if_away = false;
+    };
+  }
+]
diff --git a/2configs/bureautomation/devices/users.nix b/2configs/bureautomation/devices/users.nix
new file mode 100644
index 0000000..305c0ca
--- /dev/null
+++ b/2configs/bureautomation/devices/users.nix
@@ -0,0 +1,17 @@
+{
+  thorsten-phone = {
+    name = "Thorsten";
+    mac = "8c:f5:a3:bc:83:a0";
+    track = true;
+    hide_if_away = true;
+  };
+  felix-laptop = {
+    name = "Felix";
+    mac = "6c:88:14:b4:43:9c";
+    track = true;
+    hide_if_away = true;
+  };
+  # b0:e5:ed:52:ee:43 - honor8
+  # 38:94:96:b0:13:c7 - android-4ef03e4f4a14b6b9
+  # ac:5f:3e:cc:b8:5e - Galaxy S7
+}
diff --git a/2configs/bureautomation/hass.nix b/2configs/bureautomation/hass.nix
index 007d00b..a104f0c 100644
--- a/2configs/bureautomation/hass.nix
+++ b/2configs/bureautomation/hass.nix
@@ -1,8 +1,9 @@
 { pkgs, lib, ... }:
 let
+  kodi-host = "192.168.8.11";
 in {
   networking.firewall.allowedTCPPorts = [ 8123 ];
-
+  state = [ "/var/lib/hass/known_devices.yaml" ];
   services.home-assistant = {
     enable = true;
     config = {
@@ -33,7 +34,8 @@ in {
           retain = true;
         };
       };
-      switch = (import ./switch/tasmota_switch.nix);
+      switch = (import ./switch/tasmota_switch.nix) ++
+               (import ./switch/rfbridge.nix);
       light =  (import ./light/statuslight.nix) ++
                (import ./light/buzzer.nix);
       timer = {
@@ -54,7 +56,12 @@ in {
         {
           platform = "kodi";
           name = "wbob";
-          host = "192.168.8.11";
+          host = kodi-host;
+        }
+      ];
+      media_player = [
+        { platform = "kodi";
+          host = kodi-host;
         }
       ];
       script = (import ./script/multi_blink.nix) {inherit lib;};
@@ -70,6 +77,10 @@ in {
       camera =
         (import ./camera/verkehrskamera.nix);
 
+      # not yet released
+      #person =
+      #  (import ./person/team.nix );
+
       frontend = { };
       http = { };
       conversation = {};
@@ -82,36 +93,55 @@ in {
         { view = "yes";
           entities = [
               "group.sensors"
+              "group.camera"
               "group.outside"
-              "group.switches"
-              "group.automation"
-              # "group.camera"
+              "group.team"
+              "group.nachtlicht"
+              # "group.switches"
             ];
           };
         automation = [
-          "timer.felix_10h"
-          "script.blitz_10s"
-          "script.buzz_red_led_fast"
-          "camera.Baumarkt"
         ];
         switches = [
           "switch.bauarbeiterlampe"
           "switch.blitzdings"
           "switch.fernseher"
           "switch.feuer"
-          "switch.nachtlicht"
           "light.status_felix"
           "light.status_daniel"
           "light.buslicht"
           "light.redbutton_buzzer"
         ];
-
-        camera = [ ];
+        team = [
+          "device_tracker.thorsten_phone"
+          "device_tracker.felix_phone"
+          "device_tracker.ecki_tablet"
+          "device_tracker.daniel_phone"
+          "device_tracker.carsten_phone"
+        #  "person.thorsten"
+        #  "person.felix"
+        #  "person.ecki"
+        #  "person.daniel"
+        ];
+        camera = [
+          "camera.Baumarkt"
+          "camera.Autobahn_Heilbronn"
+          "camera.Autobahn_Singen"
+        ];
+        nachtlicht = [
+          "switch.nachtlicht_a"
+          "switch.nachtlicht_b"
+          "switch.nachtlicht_c"
+          "switch.nachtlicht_d"
+        ];
         sensors = [
-          "binary_sensor.motion"
-          "binary_sensor.redbutton"
+          "media_player.kodi"
+          "script.blitz_10s"
+          "script.buzz_red_led_fast"
+          "timer.felix_10h"
           "sensor.easy2_dht22_humidity"
           "sensor.easy2_dht22_temperature"
+          # "binary_sensor.redbutton"
         ];
         outside = [
           # "sensor.ditzingen_pm10"
@@ -120,8 +150,7 @@ in {
           "sensor.dark_sky_humidity"
           # "sensor.dark_sky_pressure"
           "sensor.dark_sky_hourly_summary"
-          "camera.Autobahn_Heilbronn"
-          "camera.Autobahn_Singen"
+          "device_tracker.router"
         ];
       };
       # only for automation
@@ -131,7 +160,7 @@ in {
       automation = (import ./automation/bureau-shutdown.nix) ++
                    (import ./automation/nachtlicht.nix) ++
                    (import ./automation/10h_timer.nix);
-
+      device_tracker = (import ./device_tracker/openwrt.nix );
     };
   };
 }
diff --git a/2configs/bureautomation/person/team.nix b/2configs/bureautomation/person/team.nix
new file mode 100644
index 0000000..e18c421
--- /dev/null
+++ b/2configs/bureautomation/person/team.nix
@@ -0,0 +1,29 @@
+[
+  { name = "Thorsten";
+    id = 1;
+    device_trackers = [
+      "device_tracker.thorsten_phone"
+      "device_tracker.thorsten_arbeitphone"
+    ];
+  }
+  { name = "Felix";
+    id = 2;
+    device_trackers = [
+      "device_tracker.felix_phone"
+      "device_tracker.felix_laptop"
+    ];
+  }
+  { name = "Ecki";
+    id = 3;
+    device_trackers = [
+      "device_tracker.ecki_phone"
+      "device_tracker.ecki_tablet"
+    ];
+  }
+  { name = "Daniel";
+    id = 4;
+    device_trackers = [
+      "device_tracker.daniel_phone"
+    ];
+  }
+]
diff --git a/2configs/bureautomation/switch/rfbridge.nix b/2configs/bureautomation/switch/rfbridge.nix
new file mode 100644
index 0000000..1336549
--- /dev/null
+++ b/2configs/bureautomation/switch/rfbridge.nix
@@ -0,0 +1,17 @@
+let
+  topic = "rfbridge";
+  bridge = name: payload_on: payload_off:
+  { platform = "mqtt";
+    inherit name payload_on payload_off;
+    command_topic = "/bam/${topic}/cmnd/rfcode";
+    availability_topic = "/bam/${topic}/tele/LWT";
+    payload_available= "Online";
+    payload_not_available= "Offline";
+  };
+in
+[
+  (bridge "Nachtlicht A" "#414551" "#414554")
+  (bridge "Nachtlicht B" "#415151" "#415154")
+  (bridge "Nachtlicht C" "#415451" "#415454")
+  (bridge "Nachtlicht D" "#41551F" "#415514")
+]
diff --git a/2configs/bureautomation/switch/tasmota_switch.nix b/2configs/bureautomation/switch/tasmota_switch.nix
index b00a8e4..eb7b5fb 100644
--- a/2configs/bureautomation/switch/tasmota_switch.nix
+++ b/2configs/bureautomation/switch/tasmota_switch.nix
@@ -15,5 +15,5 @@ in [
   (tasmota_plug "Blitzdings" "plug2")
   (tasmota_plug "Fernseher" "plug3")
   (tasmota_plug "Feuer" "plug4")
-  (tasmota_plug "Nachtlicht" "plug5")
+  (tasmota_plug "Blaulicht" "plug5")
 ]