diff --git a/modules/home-assistant/default.nix b/modules/home-assistant/default.nix new file mode 100644 index 00000000..426cbb1d --- /dev/null +++ b/modules/home-assistant/default.nix @@ -0,0 +1,62 @@ +{ + lib, + config, + options, + pkgs, + ... +}: +with lib; let + cfg = config.pub-solar.home-assistant; +in { + imports = [ + ./home-assistant.nix + ./mqtt.nix + ./zigbee.nix + ]; + + options.pub-solar.home-assistant = { + enable = mkOption { + description = "Control your home"; + type = types.bool; + default = false; + }; + + config = options.services.home-assistant.config; + extraComponents = options.services.home-assistant.extraComponents; + extraPackages = options.services.home-assistant.extraPackages; + + mqtt = { + enable = mkOption { + description = "use mqtt"; + type = types.bool; + default = true; + }; + + users = mkOption { + description = "mqtt users"; + # type = types.AttrSet; + default = null; + }; + }; + + zigbee2mqtt = { + enable = mkOption { + description = "Enable zigbee2mqtt"; + type = types.bool; + default = false; + }; + + device = mkOption { + description = "Device to connect to zigbee network"; + type = types.nullOr types.str; + default = null; + }; + + adapter = mkOption { + description = "Specify zigbee adapter type"; + type = types.nullOr types.str; + default = null; + }; + }; + }; +} diff --git a/modules/home-assistant/home-assistant.nix b/modules/home-assistant/home-assistant.nix new file mode 100644 index 00000000..9d6cc10e --- /dev/null +++ b/modules/home-assistant/home-assistant.nix @@ -0,0 +1,21 @@ +{ + lib, + config, + pkgs, + ... +}: +with lib; let + cfg = config.pub-solar.home-assistant; +in { + config = mkIf cfg.enable { + services.home-assistant = { + enable = true; + openFirewall = true; + extraComponents = + cfg.extraComponents + ++ lib.optionals cfg.mqtt.enable ["mqtt"]; + extraPackages = cfg.extraPackages; + config = cfg.config; + }; + }; +} diff --git a/modules/home-assistant/mqtt.nix b/modules/home-assistant/mqtt.nix new file mode 100644 index 00000000..32eb3718 --- /dev/null +++ b/modules/home-assistant/mqtt.nix @@ -0,0 +1,21 @@ +{ + lib, + config, + pkgs, + ... +}: +with lib; let + haCfg = config.pub-solar.home-assistant; + cfg = config.pub-solar.home-assistant.mqtt; +in { + config = mkIf (haCfg.enable && cfg.enable) { + networking.firewall.allowedTCPPorts = [ + 1883 # mosquitto + ]; + + services.mosquitto = { + enable = true; + listeners = [{users = cfg.users;}]; + }; + }; +} diff --git a/modules/home-assistant/zigbee.nix b/modules/home-assistant/zigbee.nix new file mode 100644 index 00000000..6b2bd1ba --- /dev/null +++ b/modules/home-assistant/zigbee.nix @@ -0,0 +1,35 @@ +{ + lib, + config, + pkgs, + ... +}: +with lib; let + haCfg = config.pub-solar.home-assistant; + cfg = config.pub-solar.home-assistant.zigbee2mqtt; +in { + config = mkIf (haCfg.enable && cfg.enable) { + networking.firewall.allowedTCPPorts = [ + 8080 # zigbee2mqtt + ]; + + #services.udev.extraRules = ''KERNEL=="${cfg.device}", OWNER="zigbee2mqtt", GROUP="zigbee2mqtt"''; + + services.zigbee2mqtt = { + enable = true; + settings = { + frontend = true; + permit_join = false; + homeassistant = true; + mqtt = { + user = "z2m"; + password = "!secrets.yaml mqtt_password"; + }; + serial = { + port = cfg.device; + adapter = mkIf (cfg.adapter != null) cfg.adapter; + }; + }; + }; + }; +}