From cc61b5c9000de6a51cfbf20f03e938e9bf52b7fc Mon Sep 17 00:00:00 2001 From: Ingolf Wagner Date: Mon, 1 Jun 2020 14:34:23 +0200 Subject: [PATCH] light-control: create a proper service --- assets/light-control.json | 265 ------------------ configs/pepe/home-assistant/light-control.nix | 163 ++++++++++- modules/default.nix | 2 + modules/services/light-control.nix | 48 ++++ pkgs/light-control/default.nix | 2 +- 5 files changed, 203 insertions(+), 277 deletions(-) delete mode 100644 assets/light-control.json create mode 100644 modules/services/light-control.nix diff --git a/assets/light-control.json b/assets/light-control.json deleted file mode 100644 index a1160ef..0000000 --- a/assets/light-control.json +++ /dev/null @@ -1,265 +0,0 @@ -{ - "credentials": { - "host": "tcp://localhost:1883", - "user": "homeassistant", - "password": "hallo" - }, - "scenes": [ - { - "name": "default" - }, - { - "name": "outside", - "room_tracking_enabled": false - }, - { - "name": "night", - "room_tracking_enabled": false, - "brightness": 25, - "exclude_switches": [ - "stat/PAL01/RESULT", - "stat/PAL03/RESULT", - "stat/PAL04/RESULT", - "zigbee2mqtt/light_2" - ] - } - ], - "sensors": [ - { - "topic": "zigbee2mqtt/motion_sensor_2", - "key": "occupancy", - "rooms": [ - "bed_room" - ], - "invert_state": false, - "delay": { - "secs": 60, - "nanos": 0 - } - }, - { - "topic": "zigbee2mqtt/motion_sensor_7", - "key": "occupancy", - "rooms": [ - "bed_room" - ], - "invert_state": false, - "delay": { - "secs": 60, - "nanos": 0 - } - }, - { - "topic": "zigbee2mqtt/motion_sensor_1", - "key": "occupancy", - "rooms": [ - "kitchen_room" - ], - "invert_state": false, - "delay": { - "secs": 60, - "nanos": 0 - } - }, - { - "topic": "zigbee2mqtt/motion_sensor_4", - "key": "occupancy", - "rooms": [ - "living_room" - ], - "invert_state": false, - "delay": { - "secs": 60, - "nanos": 0 - } - }, - { - "topic": "zigbee2mqtt/motion_sensor_5", - "key": "occupancy", - "rooms": [ - "living_room" - ], - "invert_state": false, - "delay": { - "secs": 60, - "nanos": 0 - } - }, - { - "topic": "zigbee2mqtt/motion_sensor_5", - "key": "occupancy", - "rooms": [ - "bath_room" - ], - "invert_state": false, - "delay": { - "secs": 60, - "nanos": 0 - } - }, - { - "topic": "zigbee2mqtt/motion_sensor_8", - "key": "occupancy", - "rooms": [ - "bath_room" - ], - "invert_state": false, - "delay": { - "secs": 60, - "nanos": 0 - } - }, - { - "topic": "zigbee2mqtt/door_sensor_2", - "key": "contact", - "rooms": [ - "floor_room" - ], - "invert_state": true, - "delay": { - "secs": 90, - "nanos": 0 - } - }, - { - "topic": "zigbee2mqtt/door_sensor_4", - "key": "contact", - "rooms": [ - "floor_room" - ], - "invert_state": true, - "delay": { - "secs": 90, - "nanos": 0 - } - } - ], - "switches": [ - { - "topic": "zigbee2mqtt/light_1", - "key": "state", - "rooms": [ - "floor_room" - ], - "command": { - "command": "{\"state\":\"{{state}}\",\"brightness\":{{brightness}}}", - "init_command": null, - "topic": "zigbee2mqtt/light_1/set", - "on": "ON", - "off": "OFF" - } - }, - { - "topic": "zigbee2mqtt/light_2", - "key": "state", - "rooms": [ - "floor_room" - ], - "command": { - "command": "{\"state\":\"{{state}}\",\"brightness\":{{brightness}}}", - "init_command": null, - "topic": "zigbee2mqtt/light_2/set", - "on": "ON", - "off": "OFF" - } - }, - { - "topic": "zigbee2mqtt/light_3", - "key": "state", - "rooms": [ - "living_room" - ], - "command": { - "command": "{\"state\":\"{{state}}\",\"brightness\":{{brightness}}}", - "init_command": null, - "topic": "zigbee2mqtt/light_3/set", - "on": "ON", - "off": "OFF" - } - }, - { - "topic": "zigbee2mqtt/light_4", - "key": "state", - "rooms": [ - "bath_room" - ], - "command": { - "command": "{\"state\":\"{{state}}\",\"brightness\":{{brightness}}}", - "init_command": null, - "topic": "zigbee2mqtt/light_4/set", - "on": "ON", - "off": "OFF" - } - }, - { - "topic": "zigbee2mqtt/light_8", - "key": "state", - "rooms": [ - "bed_room" - ], - "command": { - "command": "{\"state\":\"{{state}}\",\"brightness\":{{brightness}}}", - "init_command": null, - "topic": "zigbee2mqtt/light_8/set", - "on": "ON", - "off": "OFF" - } - }, - { - "topic": "stat/PAL01/RESULT", - "key": "POWER", - "rooms": [ - "bed_room" - ], - "command": { - "command": "{{state}}", - "init_command": "(null)", - "topic": "cmnd/PAL01/POWER", - "on": "ON", - "off": "OFF" - } - }, - { - "topic": "stat/PAL03/RESULT", - "key": "POWER", - "rooms": [ - "living_room" - ], - "command": { - "command": "{{state}}", - "init_command": "(null)", - "topic": "cmnd/PAL03/POWER", - "on": "ON", - "off": "OFF" - } - }, - { - "topic": "stat/PAL04/RESULT", - "key": "POWER", - "rooms": [ - "bed_room" - ], - "command": { - "command": "{{state}}", - "init_command": "(null)", - "topic": "cmnd/PAL04/POWER", - "on": "ON", - "off": "OFF" - } - }, - { - "topic": "stat/PAL06/RESULT", - "key": "POWER", - "rooms": [ - "kitchen_room" - ], - "command": { - "command": "{{state}}", - "init_command": "(null)", - "topic": "cmnd/PAL06/POWER", - "on": "ON", - "off": "OFF" - } - } - ] -} diff --git a/configs/pepe/home-assistant/light-control.nix b/configs/pepe/home-assistant/light-control.nix index 9a40080..5b271d5 100644 --- a/configs/pepe/home-assistant/light-control.nix +++ b/configs/pepe/home-assistant/light-control.nix @@ -1,14 +1,155 @@ -{ pkgs, lib, config, ... }: -let - lightControlConfig = pkgs.writeText "light-control.json" - (lib.fileContents ); +{ pkgs, lib, config, ... }: { + #systemd.services."light-control" = { + # wantedBy = [ "multi-user.target" ]; + # environment = { RUST_LOG = "rust_iot=trace"; }; + # script = '' + # ${pkgs.light-control}/bin/rust-iot ${lightControlConfig} + # ''; + #}; -in { - systemd.services."light-control" = { - wantedBy = [ "multi-user.target" ]; - environment = { RUST_LOG = "rust_iot=trace"; }; - script = '' - ${pkgs.light-control}/bin/rust-iot ${lightControlConfig} - ''; + services.mqtt.light-control.enable = true; + services.mqtt.light-control.config = { + credentials = { + host = "tcp://localhost:1883"; + user = "homeassistant"; + password = "hallo"; + }; + scenes = [ + { name = "default"; } + { + name = "outside"; + room_tracking_enabled = false; + } + { + name = "night"; + room_tracking_enabled = false; + brightness = 25; + exclude_switches = [ + "stat/PAL01/RESULT" + "stat/PAL03/RESULT" + "stat/PAL04/RESULT" + "zigbee2mqtt/light_2" + ]; + } + ]; + sensors = let + door = { topic, rooms }: { + topic = topic; + key = "contact"; + rooms = rooms; + invert_state = true; + delay = { + secs = 90; + nanos = 0; + }; + }; + motion = { topic, rooms }: { + topic = topic; + key = "occupancy"; + rooms = rooms; + delay = { + secs = 60; + nanos = 0; + }; + }; + in [ + (motion { + topic = "zigbee2mqtt/motion_sensor_2"; + rooms = [ "bed_room" ]; + }) + (motion { + topic = "zigbee2mqtt/motion_sensor_7"; + rooms = [ "bed_room" ]; + }) + (motion { + topic = "zigbee2mqtt/motion_sensor_1"; + rooms = [ "kitchen_room" ]; + }) + (motion { + topic = "zigbee2mqtt/motion_sensor_4"; + rooms = [ "living_room" ]; + }) + (motion { + topic = "zigbee2mqtt/motion_sensor_5"; + rooms = [ "living_room" ]; + }) + (motion { + topic = "zigbee2mqtt/motion_sensor_8"; + rooms = [ "bath_room" ]; + }) + + (door { + topic = "zigbee2mqtt/door_sensor_2"; + rooms = [ "floor_room" ]; + }) + (door { + topic = "zigbee2mqtt/door_sensor_4"; + rooms = [ "floor_room" ]; + }) + ]; + switches = let + sonoff = { id, rooms }: { + topic = "stat/${id}/RESULT"; + key = "POWER"; + rooms = rooms; + command = { + command = "{{state}}"; + init_command = "(null)"; + topic = "cmnd/${id}/POWER"; + on = "ON"; + off = "OFF"; + }; + }; + light = { topic, rooms }: { + topic = topic; + key = "state"; + rooms = rooms; + command = { + command = ''{"state":"{{state}}","brightness":{{brightness}}}''; + topic = "${topic}/set"; + on = "ON"; + off = "OFF"; + }; + }; + in [ + + (light { + topic = "zigbee2mqtt/light_1"; + rooms = [ "floor_room" ]; + }) + (light { + topic = "zigbee2mqtt/light_2"; + rooms = [ "floor_room" ]; + }) + (light { + topic = "zigbee2mqtt/light_3"; + rooms = [ "living_room" ]; + }) + (light { + topic = "zigbee2mqtt/light_4"; + rooms = [ "bath_room" ]; + }) + (light { + topic = "zigbee2mqtt/light_8"; + rooms = [ "bed_room" ]; + }) + (sonoff { + id = "PAL01"; + rooms = [ "bed_room" ]; + }) + (sonoff { + id = "PAL03"; + rooms = [ "living_room" ]; + }) + (sonoff { + id = "PAL04"; + rooms = [ "bed_room" ]; + }) + (sonoff { + id = "PAL06"; + rooms = [ "kitchen_room" ]; + }) + ]; }; + } diff --git a/modules/default.nix b/modules/default.nix index f162efb..e6df87d 100644 --- a/modules/default.nix +++ b/modules/default.nix @@ -5,6 +5,8 @@ ./later/syncthing.nix #./later/nextcloud.nix + ./services/light-control.nix + ./services/castget.nix ./services/home-assistant.nix ./services/lektor.nix diff --git a/modules/services/light-control.nix b/modules/services/light-control.nix new file mode 100644 index 0000000..2fbb19b --- /dev/null +++ b/modules/services/light-control.nix @@ -0,0 +1,48 @@ +{ config, lib, pkgs, ... }: + +with lib; + +let + + cfg = config.services.mqtt.light-control; + + mkMagicMergeOption = { description ? "", example ? { }, default ? { }, ... }: + mkOption { + inherit example description default; + type = with lib.types; + let + valueType = nullOr (oneOf [ + bool + int + float + str + (attrsOf valueType) + (listOf valueType) + ]) // { + description = ""; + emptyValue.value = { }; + }; + in valueType; + }; + + lightControlConfig = + pkgs.writeText "light-control.json" (builtins.toJSON cfg.config); + +in { + + options.services.mqtt.light-control = { + enable = mkEnableOption "enable mqtt.light-control"; + config = + mkMagicMergeOption { description = "configuration of light-control"; }; + }; + + config = mkIf cfg.enable { + systemd.services."light-control" = { + wantedBy = [ "multi-user.target" ]; + environment = { RUST_LOG = "rust_iot=trace"; }; + script = '' + ${pkgs.light-control}/bin/rust-iot ${lightControlConfig} + ''; + }; + }; +} diff --git a/pkgs/light-control/default.nix b/pkgs/light-control/default.nix index c4e6d04..6bcf971 100644 --- a/pkgs/light-control/default.nix +++ b/pkgs/light-control/default.nix @@ -4,7 +4,7 @@ rustPlatform.buildRustPackage rec { name = "light-${version}"; version = "0.1.0"; src = fetchgit { - url = "https://git.ingolf-wagner.de/palo/rust-iot.git"; + url = "https://git.ingolf-wagner.de/palo/light-control.git"; rev = "180200b6085291834079322fc63c5220cac28e43"; sha256 = "0dwf6dvpgrkpgaba4ayb0z6qjk8wjmp38y5viiq7hbjg5wcbr9qf"; };