From 1f8a4c635380f6e15493c9c9cbec3f56a851cd4e Mon Sep 17 00:00:00 2001 From: mjallen18 Date: Wed, 26 Feb 2025 19:12:28 -0600 Subject: [PATCH] mqtt --- hosts/homeassistant/homeassistant.nix | 69 +++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) diff --git a/hosts/homeassistant/homeassistant.nix b/hosts/homeassistant/homeassistant.nix index 3cb84b7..f921eeb 100644 --- a/hosts/homeassistant/homeassistant.nix +++ b/hosts/homeassistant/homeassistant.nix @@ -1,5 +1,7 @@ { config, pkgs, ... }: let + mosquittoPort = 1883; + zigbee2mqttPort = 8080; # "sha256-AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=" ha-bambulab = pkgs.stdenv.mkDerivation { pname = "ha-bambulab"; @@ -149,6 +151,15 @@ in debug = false; }; + # Add MQTT integration + mqtt = { + discovery = true; + discovery_prefix = "homeassistant"; + broker = "localhost"; # Using local Mosquitto broker + port = mosquittoPort; + # If you set up MQTT with authentication, add username and password here + }; + # https://www.home-assistant.io/integrations/ota_updater/ zha.zigpy_config.ota.z2m_remote_index = "https://raw.githubusercontent.com/Koenkk/zigbee-OTA/master/index.json"; }; @@ -186,15 +197,73 @@ in ensureDBOwnership = true; }]; }; + + # Enable and configure Mosquitto MQTT broker + services.mosquitto = { + enable = true; + listeners = [ + { + port = mosquittoPort; + users = { + # Optional: Set up authentication + # "mqttuser" = { + # password = "your-password"; + # acl = [ "pattern readwrite #" ]; + # }; + }; + # For testing, allow anonymous connections + settings.allow_anonymous = true; + } + ]; + }; + zigbee2mqtt = { enable = true; settings = { homeassistant = config.services.home-assistant.enable; permit_join = true; + # Web interface + frontend = { + port = zigbee2mqttPort; # Choose an available port + }; + # MQTT configuration + mqtt = { + base_topic = "zigbee2mqtt"; + server = "mqtt://localhost:${mosquittoPort}"; + # If using authentication: + # user = "mqttuser"; + # password = "your-password"; + }; serial = { port = "/dev/ttyUSB0"; }; }; }; + + # Enable required hardware support for the Zigbee adapter + hardware.bluetooth.enable = true; # Some adapters use Bluetooth + + # Ensure proper permissions for Zigbee USB devices + # services.udev.extraRules = '' + # # For CC2531, CC2530, CC1352P-2, CC2538 and similar adapters + # SUBSYSTEM=="tty", ATTRS{idVendor}=="0451", ATTRS{idProduct}=="16a8", SYMLINK+="zigbee", MODE="0666" + # SUBSYSTEM=="tty", ATTRS{idVendor}=="1a86", ATTRS{idProduct}=="7523", SYMLINK+="zigbee", MODE="0666" + + # # For ConBee/RaspBee by Dresden Elektronik + # SUBSYSTEM=="tty", ATTRS{idVendor}=="1cf1", ATTRS{idProduct}=="0030", SYMLINK+="zigbee", MODE="0666" + + # # For Electrolama zig-a-zig-ah (zzh!) + # SUBSYSTEM=="tty", ATTRS{idVendor}=="1a86", ATTRS{idProduct}=="7523", SYMLINK+="zigbee", MODE="0666" + # ''; + + environment.systemPackages = with pkgs; [ + mosquitto # MQTT command-line tools + usbutils # For lsusb to help identify your adapter + ]; + + networking.firewall.allowedTCPPorts = [ + mosquittoPort + zigbee2mqttPort + ]; }; }