1{ lib, config, ... }:
2
3with lib;
4let
5 cfg = config.modules.automation;
6
7 frontendType = types.submodule {
8 options = {
9 enable = mkOption {
10 default = false;
11 example = true;
12 description = "Whether to enable Zigbee2MQTT's frontend.";
13 type = types.bool;
14 };
15 port = mkOption {
16 default = 8124;
17 example = 8124;
18 description = "The port to use for Zigbee2MQTT's frontend.";
19 type = types.port;
20 };
21 };
22 };
23in {
24 options.modules.automation.zigbee = {
25 enable = mkOption {
26 default = false;
27 example = true;
28 description = "Whether to enable the Zigbee2MQTT service.";
29 type = types.bool;
30 };
31
32 permitJoin = mkOption {
33 default = false;
34 description = "Permit new devices to join indefinitely (Not Recommended)";
35 type = types.bool;
36 };
37
38 serialPort = mkOption {
39 default = "/dev/ttyUSB0";
40 example = "/dev/ttyUSB0";
41 description = "The serial port for the USB Zigbee adapter.";
42 type = types.str;
43 };
44
45 frontend = mkOption {
46 default = {};
47 description = "Zigbee2MQTT's frontend options.";
48 type = frontendType;
49 };
50 };
51
52 config = mkIf (cfg.enable && cfg.zigbee.enable) {
53 users.users.zigbee2mqtt.extraGroups = mkIf cfg.mqtt.enable [
54 config.users.users.mosquitto.name
55 ];
56
57 services.zigbee2mqtt = {
58 enable = true;
59 settings = {
60 permit_join = cfg.zigbee.permitJoin;
61 serial = {
62 adapter = "zstack";
63 port = cfg.zigbee.serialPort;
64 };
65 frontend = if cfg.zigbee.frontend.enable then cfg.zigbee.frontend else false;
66 ota.disable_automatic_update_check = true;
67 mqtt = mkIf cfg.mqtt.enable {
68 server = "mqtts://localhost:${toString cfg.mqtt.port}";
69 reject_unauthorized = false;
70 ca = cfg.mqtt.cafile;
71 key = cfg.mqtt.keyfile;
72 cert = cfg.mqtt.certfile;
73 };
74 advanced = {
75 log_level = "warning";
76 log_output = ["console"];
77 };
78 };
79 };
80
81 systemd.services.zigbee2mqtt = mkIf cfg.mqtt.enable {
82 wants = [config.systemd.services.mosquitto.name];
83 after = [config.systemd.services.mosquitto.name];
84 };
85 };
86}