light-control: create a proper service

This commit is contained in:
Ingolf Wagner 2020-06-01 14:34:23 +02:00
parent d76dd45c67
commit cc61b5c900
No known key found for this signature in database
GPG key ID: 76BF5F1928B9618B
5 changed files with 203 additions and 277 deletions

View file

@ -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"
}
}
]
}

View file

@ -1,14 +1,155 @@
{ pkgs, lib, config, ... }:
let
lightControlConfig = pkgs.writeText "light-control.json"
(lib.fileContents <assets/light-control.json>);
{ 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" ];
})
];
};
}

View file

@ -5,6 +5,8 @@
./later/syncthing.nix
#./later/nextcloud.nix
./services/light-control.nix
./services/castget.nix
./services/home-assistant.nix
./services/lektor.nix

View file

@ -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}
'';
};
};
}

View file

@ -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";
};