aboutsummaryrefslogtreecommitdiff
path: root/nixos/modules/home-assistant.nix
blob: 056778529380069004f3b563803aab14b585934a (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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
{
  config,
  lib,
  pkgs,
  ...
}:
with lib; let
  cnf = config.cynerd.home-assistant;
in {
  options = {
    cynerd.home-assistant = mkEnableOption "Enable Home Assistant and Bigclown";
  };

  config = mkIf cnf {
    services.mosquitto = {
      enable = true;
      listeners = [
        {
          users = {
            cynerd = {
              acl = ["readwrite #"];
              passwordFile = "/run/secrets/mosquitto.cynerd.pass";
            };
            telegraf = {
              acl = ["read bigclown/node/#"];
              passwordFile = "/run/secrets/mosquitto.telegraf.pass";
            };
            homeassistant = {
              acl = [
                "readwrite homeassistant/#"
                "readwrite bigclown/#"
                "readwrite zigbee2mqtt/#"
              ];
              passwordFile = "/run/secrets/mosquitto.homeassistant.pass";
            };
            bigclown = {
              acl = ["readwrite bigclown/#"];
              passwordFile = "/run/secrets/mosquitto.bigclown.pass";
            };
            zigbee2mqtt = {
              acl = [
                "readwrite homeassistant/#"
                "readwrite zigbee2mqtt/#"
              ];
              passwordFile = "/run/secrets/mosquitto.zigbee2mqtt.pass";
            };
          };
        }
      ];
    };
    networking.firewall.allowedTCPPorts = [
      1883 # Mosquitto
    ];

    services.bigclown = {
      gateway = {
        enable = true;
        device = "/dev/ttyUSB0";
        environmentFile = "/run/secrets/bigclown.env";
        baseTopicPrefix = "bigclown/";
        mqtt = {
          username = "bigclown";
          password = "@PASS_MQTT@";
        };
      };
    };

    systemd.services.bigclown-leds = {
      description = "Bigclown LEDs control";
      wantedBy = ["multi-user.target"];
      wants = ["mosquitto.service"];
      serviceConfig.ExecStart = "${pkgs.bigclown-leds}/bin/bigclown-leds /run/secrets/bigclown-leds.ini";
    };

    services.telegraf.extraConfig = {
      outputs.influxdb_v2 = [
        {
          urls = ["http://cynerd.cz:8086"];
          token = "$INFLUX_TOKEN";
          organization = "personal";
          bucket = "bigclown";
          tagpass.source = ["bigclown"];
        }
      ];
      inputs.mqtt_consumer = let
        consumer = data_type: topics: {
          tags = {source = "bigclown";};
          servers = ["tcp://localhost:1883"];
          inherit topics;
          username = "telegraf";
          password = "$MQTT_PASSWORD";
          data_format = "value";
          inherit data_type;
          topic_parsing = [
            {
              topic = "bigclown/node/+/+/+/+";
              measurement = "_/_/_/_/_/measurement";
              tags = "_/_/device/field/_/_";
            }
          ];
        };
      in [
        (consumer "float" [
          "bigclown/node/+/battery/+/voltage"
          "bigclown/node/+/thermometer/+/temperature"
          "bigclown/node/+/hygrometer/+/relative-humidity"
          "bigclown/node/+/lux-meter/+/illuminance"
          "bigclown/node/+/barometer/+/pressure"
          "bigclown/node/+/pir/+/event-count"
          "bigclown/node/+/push-button/+/event-count"
        ])
        (consumer "boolean" [
          "bigclown/node/+/flood-detector/+/alarm"
        ])
      ];
      processors.pivot = [
        {
          tag_key = "field";
          value_key = "value";
          tagpass.source = ["bigclown"];
        }
      ];
    };
    systemd.services.telegraf.wants = ["mosquitto.service"];

    services.home-assistant = {
      enable = false;
      openFirewall = true;
      configDir = "/var/lib/hass";
      config = {
        homeassistant = {
          name = "SPT";
          latitude = "!secret latitude";
          longitude = "!secret longitude";
          elevation = "!secret elevation";
          time_zone = "Europe/Prague";
          country = "CZ";
        };
        http.server_port = 8808;
        mqtt = {
          sensor = import ./home-assistant/sensors.nix;
          light = import ./home-assistant/light.nix;
        };
        default_config = {};
        automation = "!include automations.yaml";
      };
      extraComponents = ["met"];
      package = pkgs.home-assistant.override {
        extraPackages = pkgs:
          with pkgs; [
            securetar
            pyipp
          ];
        packageOverrides = self: super: {
          scapy = super.scapy.override {
            withPlottingSupport = false;
          };
          s3transfer = super.s3transfer.overridePythonAttrs (oldAttrs: {
            dontUsePytestCheck = true;
            dontUseSetuptoolsCheck = true;
          });
        };
      };
    };
  };
}