From 2f03043e56ff95850c5cfc738a8accb73e23db89 Mon Sep 17 00:00:00 2001
From: makefu <github@syntax-fehler.de>
Date: Tue, 18 Sep 2018 02:16:12 +0200
Subject: [PATCH] ma homeautomation: add mqtt broker with acl

---
 1systems/omo/config.nix                       |  8 +-
 1systems/omo/hw/tsp.nix                       |  3 +-
 .../deployment/homeautomation/default.nix     | 79 ++++++++++++++++++-
 2configs/deployment/homeautomation/mqtt.nix   | 10 ++-
 4 files changed, 93 insertions(+), 7 deletions(-)

diff --git a/1systems/omo/config.nix b/1systems/omo/config.nix
index 8db3334..be49db0 100644
--- a/1systems/omo/config.nix
+++ b/1systems/omo/config.nix
@@ -36,12 +36,12 @@ in {
       # logs to influx
       <stockholm/makefu/2configs/stats/external/aralast.nix>
       <stockholm/makefu/2configs/stats/telegraf>
-      <stockholm/makefu/2configs/stats/telegraf/europastats.nix>
+      # <stockholm/makefu/2configs/stats/telegraf/europastats.nix>
+      <stockholm/makefu/2configs/stats/telegraf/hamstats.nix>
       <stockholm/makefu/2configs/stats/arafetch.nix>
 
       # services
       <stockholm/makefu/2configs/syncthing.nix>
-      <stockholm/makefu/2configs/mqtt.nix>
       <stockholm/makefu/2configs/remote-build/slave.nix>
       <stockholm/makefu/2configs/deployment/google-muell.nix>
       <stockholm/makefu/2configs/virtualisation/docker.nix>
@@ -70,8 +70,8 @@ in {
       # <stockholm/makefu/2configs/temp/rst-issue.nix>
 
     ];
-  makefu.full-populate = true;
-  krebs.rtorrent = {
+  makefu.full-populate =  true;
+  krebs.rtorrent = (builtins.trace (builtins.toJSON config.services.telegraf.extraConfig)) {
     downloadDir = lib.mkForce "/media/cryptX/torrent";
     extraConfig = ''
       upload_rate = 200
diff --git a/1systems/omo/hw/tsp.nix b/1systems/omo/hw/tsp.nix
index 99c5580..a289fad 100644
--- a/1systems/omo/hw/tsp.nix
+++ b/1systems/omo/hw/tsp.nix
@@ -6,7 +6,8 @@ let
     rev = "9c9b62e15e4ac11d4379e66b974f1389daf939fe";
   });
   cfg = fromJSON (readFile ../../hardware/tsp-disk.json);
-  primaryInterface = "enp1s0";
+  # primaryInterface = "enp1s0";
+  primaryInterface = "wlp2s0";
   rootDisk = "/dev/sda"; # TODO same as disko uses
 in {
   imports = [
diff --git a/2configs/deployment/homeautomation/default.nix b/2configs/deployment/homeautomation/default.nix
index bd87193..f2a3b36 100644
--- a/2configs/deployment/homeautomation/default.nix
+++ b/2configs/deployment/homeautomation/default.nix
@@ -1,6 +1,41 @@
 { pkgs, config, ... }:
+
+# Ideas:
+## wake-on-lan server
+## 
 let
   firetv = "192.168.1.238";
+  tasmota_plug = name: topic:
+  { platform = "mqtt";
+    inherit name;
+    state_topic = "/ham/${topic}/stat/POWER1";
+    command_topic = "/ham/${topic}/cmnd/POWER1";
+    availability_topic = "/ham/${topic}/tele/LWT";
+    payload_on= "ON";
+    payload_off= "OFF";
+    payload_available= "Online";
+    payload_not_available= "Offline";
+  };
+  tasmota_bme = name: topic:
+  [ { platform = "mqtt";
+      name = "${name} Temperatur";
+      state_topic = "/ham/${topic}/tele/SENSOR";
+      value_template = "{{ value_json.BME280.Temperature }}";
+      unit_of_measurement = "°C";
+    }
+    { platform = "mqtt";
+      name = "${name} Luftfeuchtigkeit";
+      state_topic = "/ham/${topic}/tele/SENSOR";
+      value_template = "{{ value_json.BME280.Humidity }}";
+      unit_of_measurement = "%";
+    }
+    { platform = "mqtt";
+      name = "${name} Luftdruck";
+      state_topic = "/ham/${topic}/tele/SENSOR";
+      value_template = "{{ value_json.BME280.Pressure }}";
+      unit_of_measurement = "hPa";
+    }
+  ];
 in {
   imports = [
     ./mqtt.nix
@@ -21,7 +56,17 @@ in {
         name = "Home"; time_zone = "Europe/Berlin";
         latitude = "48.7687";
         longitude = "9.2478";
+        elevation = 247;
       };
+      discovery = {};
+      conversation = {};
+      history = {};
+      logbook = {};
+      tts = [
+        { platform = "google";}
+      ];
+      sun.elevation = 247;
+      recorder = {};
       media_player = [
         { platform = "kodi";
           host = firetv;
@@ -30,7 +75,31 @@ in {
           # assumes python-firetv running
         }
       ];
+      mqtt = {
+        broker = "localhost";
+        port = 1883;
+        client_id = "home-assistant";
+        username = "hass";
+        password = builtins.readFile <secrets/mqtt/hass>;
+        keepalive = 60;
+        protocol = 3.1;
+        birth_message = {
+          topic = "/ham/hass/tele/LWT";
+          payload = "Online";
+          qos = 1;
+          retain = true;
+        };
+        will_message = {
+          topic = "/ham/hass/tele/LWT";
+          payload = "Offline";
+          qos = 1;
+          retain = true;
+        };
+      };
       sensor = [
+        { platform = "speedtest";
+          monitored_conditions = [ "ping" "download" "upload" ];
+        }
         { platform = "luftdaten";
           name = "Ditzingen";
           sensorid = "663";
@@ -54,9 +123,17 @@ in {
                 seconds = 0;
           };
         }
-      ];
+      ] ++ (tasmota_bme "Schlafzimmer" "schlafzimmer");
       frontend = { };
+      #group = [
+      #  { default_view = { view = "yes"; entities = [
+      #    "sensor.luftdaten"
+      #  ]}
+      #];
       http = { };
+      switch = [
+        (tasmota_plug "Lichterkette Schlafzimmer" "schlafzimmer")
+      ];
     };
     enable = true;
     #configDir = "/var/lib/hass";
diff --git a/2configs/deployment/homeautomation/mqtt.nix b/2configs/deployment/homeautomation/mqtt.nix
index 1d6a6a3..cd1c328 100644
--- a/2configs/deployment/homeautomation/mqtt.nix
+++ b/2configs/deployment/homeautomation/mqtt.nix
@@ -10,7 +10,15 @@
       hashedPassword = "$6$2DXU7W1bvqXPqxkF$vtdz5KTd/T09hmoc9LjgEGFjvpwQbQth6vlVcr5hJNLgcBHv4U03YCKC8TKXbmQAa8xiJ76xJIg25kcL+KI3tg==";
       acl = [ "topic readwrite #" ];
     };
+    users.hass = {
+      hashedPassword = "$6$SHuYGrE5kPSUc/hu$EomZ0KBy+vkxLt/6eJkrSBjYblCCeMjhDfUd2mwqXYJ4XsP8hGmZ59mMlmBCd3AvlFYQxb4DT/j3TYlrqo7cDA==";
+      acl = [ "topic readwrite #" ];
+    };
+    users.stats = {
+      hashedPassword = "$6$j4H7KXD/YZgvgNmL$8e9sUKRXowDqJLOVgzCdDrvDE3+4dGgU6AngfAeN/rleGOgaMhee2Mbg2KS5TC1TOW3tYbk9NhjLYtjBgfRkoA==";
+      acl = [ "topic read #" ];
+    };
   };
   environment.systemPackages = [ pkgs.mosquitto ];
-  networking.firewall.allowedTCPPorts = [ config.services.mosquitto.port ];
+  # port open via trusted interface
 }