zigbee2mqtt update

This commit is contained in:
Ingolf Wagner 2020-11-22 15:01:19 +01:00
parent 94d29220d8
commit edfd24448b
No known key found for this signature in database
GPG key ID: 76BF5F1928B9618B
3 changed files with 4 additions and 136 deletions

View file

@ -13,15 +13,6 @@
./zigbee2mqtt/temperatur.nix ./zigbee2mqtt/temperatur.nix
]; ];
#services.zigbee2mqtt = {
# enable = true;
# mqttPassword = lib.fileContents <secrets/zigbee/password>;
# networkKey = import <secrets/home-assistant/zigbee/networkKey>;
# # only turn on for new devices, usually turn of for security reasons.
# discover = true;
#};
# new
services.zigbee2mqtt = { services.zigbee2mqtt = {
enable = true; enable = true;
config = { config = {
@ -30,7 +21,7 @@
homeassistant = false; homeassistant = false;
# allow new devices to join # allow new devices to join
permit_join = true; permit_join = false;
# MQTT settings # MQTT settings
mqtt = { mqtt = {
@ -55,9 +46,6 @@
# see https://www.zigbee2mqtt.io/how_tos/how_to_secure_network.html # see https://www.zigbee2mqtt.io/how_tos/how_to_secure_network.html
advanced.network_key = import <secrets/home-assistant/zigbee/networkKey>; advanced.network_key = import <secrets/home-assistant/zigbee/networkKey>;
# todo : devices should be defined later
#devices = cfg.devices;
}; };
}; };

View file

@ -1,20 +1,12 @@
{ config, lib, pkgs, ... }: { config, lib, pkgs, ... }:
with lib; with lib;
let cfg = config.services.zigbee2mqttConfiguration;
let
cfg = config.services.zigbee2mqttConfiguration;
in { in {
options.services.zigbee2mqttConfiguration = mkOption { options.services.zigbee2mqttConfiguration = mkOption {
type = with types; attrs; type = with types; attrs;
description = '' description = ''
device definitions. device definitions
''; '';
}; };
config = { config = { services.zigbee2mqtt.config.devices = cfg; };
services.zigbee2mqtt.config.devices = cfg;
};
} }

View file

@ -1,112 +0,0 @@
{ config, lib, pkgs, ... }:
with lib;
let
cfg = config.services.zigbee2mqtt;
device = "/dev/ttyACM0";
dataFolder = "/srv/zigbee/data";
zigbeeConfiguration = {
# Home Assistant integration (MQTT discovery)
homeassistant = false;
# homeassistant = true;
# allow new devices to join
permit_join = cfg.discover;
# MQTT settings
mqtt = {
# MQTT base topic for zigbee2mqtt MQTT messages
base_topic = "zigbee2mqtt";
# MQTT server URL
server = "mqtt://127.0.0.1:1883";
# MQTT server authentication, uncomment if required:
user = "zigbee";
password = cfg.mqttPassword;
};
# Serial settings
serial = {
port = device;
# disable LED of CC2531 USB sniffer
disable_led = true;
};
devices = cfg.devices;
} // (lib.optionalAttrs (cfg.networkKey != null) {
advanced.network_key = cfg.networkKey;
});
configurationYaml =
pkgs.writeText "configuration.yml" (builtins.toJSON zigbeeConfiguration);
in {
options.services.zigbee2mqtt = {
enable = mkEnableOption "enable services.zigbee2mqtt";
discover = mkEnableOption "discover new devices";
mqttPassword = mkOption {
type = with types; str;
description = ''
password of the mqtt server.
'';
};
networkKey = mkOption {
type = with types; nullOr (listOf int);
description = ''
you own network key,
16 numbers between 0 and 255
see https://www.zigbee2mqtt.io/how_tos/how_to_secure_network.html
'';
example = [ 7 3 5 7 9 11 13 15 0 2 4 6 8 11 12 13 ];
};
devices = mkOption {
type = with types; attrs;
description = ''
device definitions.
'';
};
};
config = mkIf cfg.enable {
# todo : einen eigenen container bauen mit dockerTool : https://nixos.wiki/wiki/Docker
virtualisation.docker.enable = true;
systemd.services."zigbee2mqtt" = {
enable = true;
description =
"Allows you to use your Zigbee devices without the vendors bridge/gateway.";
after = [ "docker.service" ];
requires = [ "docker.service" ];
# todo : udev rule erstellen, die diesen service erst startet, dieses wanted by ist labil
wantedBy = [ "home-assistant.service" ];
preStart = ''
if [ -f ${dataFolder}/configuration.yaml ]
then
rm ${dataFolder}/configuration.yaml
fi
mkdir -p ${dataFolder}
cat ${configurationYaml} | ${pkgs.yq}/bin/yq --yaml-output '.' > ${dataFolder}/configuration.yaml
'';
restartTriggers = [ configurationYaml ];
script = ''
# delete old instance to ensure update
${pkgs.docker}/bin/docker stop zigbee2mqtt || true && ${pkgs.docker}/bin/docker rm -f zigbee2mqtt || true
# start instance
${pkgs.docker}/bin/docker run \
--network="host" \
--name zigbee2mqtt \
-v ${dataFolder}:/app/data \
--device=${device} \
koenkk/zigbee2mqtt
'';
};
};
}