From 3477cd94b21cf24f171a2b09a82f326938d34902 Mon Sep 17 00:00:00 2001 From: Ingolf Wagner Date: Sun, 15 Jan 2023 10:43:10 +0100 Subject: [PATCH] zigbee2mqtt and hass setup --- flake.lock | 8 +- nixos/machines/pepe/configuration.nix | 3 +- nixos/machines/pepe/hass.nix | 16 ++ nixos/machines/pepe/home-assistant.nix | 11 - nixos/machines/pepe/home-assistant/mqtt.nix | 31 --- .../pepe/home-assistant/zigbee2mqtt.nix | 100 ------- .../home-assistant/zigbee2mqtt/buttons.nix | 250 ------------------ .../zigbee2mqtt/configurationHelper.nix | 12 - .../pepe/home-assistant/zigbee2mqtt/doors.nix | 101 ------- .../home-assistant/zigbee2mqtt/fyrtur.nix | 57 ---- .../home-assistant/zigbee2mqtt/heater.nix | 85 ------ .../pepe/home-assistant/zigbee2mqtt/leds.nix | 48 ---- .../home-assistant/zigbee2mqtt/lights.nix | 99 ------- .../home-assistant/zigbee2mqtt/motion.nix | 76 ------ .../home-assistant/zigbee2mqtt/repeater.nix | 21 -- .../home-assistant/zigbee2mqtt/service.nix | 119 --------- .../home-assistant/zigbee2mqtt/temperatur.nix | 81 +----- nixos/machines/pepe/mqtt.nix | 12 + nixos/machines/pepe/zigbee2mqtt.nix | 61 +++++ nixos/system/all/nginx-landingpage.nix | 15 ++ 20 files changed, 111 insertions(+), 1095 deletions(-) create mode 100644 nixos/machines/pepe/hass.nix delete mode 100644 nixos/machines/pepe/home-assistant/mqtt.nix delete mode 100644 nixos/machines/pepe/home-assistant/zigbee2mqtt.nix delete mode 100644 nixos/machines/pepe/home-assistant/zigbee2mqtt/buttons.nix delete mode 100644 nixos/machines/pepe/home-assistant/zigbee2mqtt/configurationHelper.nix delete mode 100644 nixos/machines/pepe/home-assistant/zigbee2mqtt/doors.nix delete mode 100644 nixos/machines/pepe/home-assistant/zigbee2mqtt/fyrtur.nix delete mode 100644 nixos/machines/pepe/home-assistant/zigbee2mqtt/heater.nix delete mode 100644 nixos/machines/pepe/home-assistant/zigbee2mqtt/leds.nix delete mode 100644 nixos/machines/pepe/home-assistant/zigbee2mqtt/lights.nix delete mode 100644 nixos/machines/pepe/home-assistant/zigbee2mqtt/motion.nix delete mode 100644 nixos/machines/pepe/home-assistant/zigbee2mqtt/repeater.nix delete mode 100644 nixos/machines/pepe/home-assistant/zigbee2mqtt/service.nix create mode 100644 nixos/machines/pepe/mqtt.nix create mode 100644 nixos/machines/pepe/zigbee2mqtt.nix diff --git a/flake.lock b/flake.lock index 3d7ece6..15a228e 100644 --- a/flake.lock +++ b/flake.lock @@ -816,11 +816,11 @@ "private_assets": { "flake": false, "locked": { - "lastModified": 1666443661, - "narHash": "sha256-8cAQush+8XSIa4UZi3MtJljaUjg/tUUBypny/CHvGBQ=", + "lastModified": 1673774076, + "narHash": "sha256-lKzJ0lpLPR/zm7JvnMeISAGk3Au1RKxKXO6t6cn5gW8=", "ref": "main", - "rev": "ff82fc815f2699a29ed549337e77d77971ad6cfe", - "revCount": 3, + "rev": "f51b139fd76988e9c31792e39bfd88085477654a", + "revCount": 4, "type": "git", "url": "ssh://gitea@git.ingolf-wagner.de/palo/nixos-private-assets.git" }, diff --git a/nixos/machines/pepe/configuration.nix b/nixos/machines/pepe/configuration.nix index 8ff221c..d4a279c 100644 --- a/nixos/machines/pepe/configuration.nix +++ b/nixos/machines/pepe/configuration.nix @@ -7,7 +7,8 @@ ./mail-fetcher.nix ./packages.nix - #./home-assistant.nix + ./hass.nix + ./zigbee2mqtt.nix #./kodi.nix ./syncthing.nix ./tinc.nix diff --git a/nixos/machines/pepe/hass.nix b/nixos/machines/pepe/hass.nix new file mode 100644 index 0000000..d3ab27c --- /dev/null +++ b/nixos/machines/pepe/hass.nix @@ -0,0 +1,16 @@ +{ config, lib, pkgs, ... }: +{ + + virtualisation.oci-containers = { + backend = "podman"; + containers.homeassistant = { + volumes = [ "home-assistant:/config" ]; + environment.TZ = "Europe/Berlin"; + image = "ghcr.io/home-assistant/home-assistant:stable"; # Warning: if the tag does not change, the image will not be updated + extraOptions = [ + "--network=host" + ]; + }; + }; + +} diff --git a/nixos/machines/pepe/home-assistant.nix b/nixos/machines/pepe/home-assistant.nix index 9c4e5ea..36a7e1e 100644 --- a/nixos/machines/pepe/home-assistant.nix +++ b/nixos/machines/pepe/home-assistant.nix @@ -1,17 +1,6 @@ { pkgs, config, lib, ... }: { imports = [ - #./home-assistant/mpd.nix - #./home-assistant/timer.nix - ./home-assistant/light-control.nix - ./home-assistant/iot-control.nix - ./home-assistant/chaospott.nix - ./home-assistant/kodi.nix - ./home-assistant/mqtt.nix - ./home-assistant/sonoff.nix - ./home-assistant/stocks.nix - ./home-assistant/weather.nix - ./home-assistant/workday.nix ./home-assistant/zigbee2mqtt.nix ]; diff --git a/nixos/machines/pepe/home-assistant/mqtt.nix b/nixos/machines/pepe/home-assistant/mqtt.nix deleted file mode 100644 index f82584b..0000000 --- a/nixos/machines/pepe/home-assistant/mqtt.nix +++ /dev/null @@ -1,31 +0,0 @@ -{ lib, ... }: { - services.homeAssistantConfig.mqtt = { - - # discovery = false; - # for mosquitto - broker = "127.0.0.1"; - username = lib.fileContents ; - password = lib.fileContents ; - - }; - - services.mosquitto = { - enable = true; - host = "0.0.0.0"; - users = { - homeassistant = { - password = lib.fileContents ; - acl = [ "topic readwrite #" ]; - }; - zigbee = { - password = lib.fileContents ; - acl = [ "topic readwrite #" ]; - }; - lightcontrol = { - password = "password"; - acl = [ "topic readwrite #" ]; - }; - }; - }; - -} diff --git a/nixos/machines/pepe/home-assistant/zigbee2mqtt.nix b/nixos/machines/pepe/home-assistant/zigbee2mqtt.nix deleted file mode 100644 index 019093e..0000000 --- a/nixos/machines/pepe/home-assistant/zigbee2mqtt.nix +++ /dev/null @@ -1,100 +0,0 @@ -{ pkgs, lib, config, ... }: -# no need to set ZIGBEE2MQTT_DATA anymore -assert lib.versionOlder lib.version "21.03"; - -{ - imports = [ - ./mqtt.nix - ./zigbee2mqtt/service.nix - ./zigbee2mqtt/buttons.nix - ./zigbee2mqtt/configurationHelper.nix - ./zigbee2mqtt/doors.nix - ./zigbee2mqtt/fyrtur.nix - ./zigbee2mqtt/heater.nix - ./zigbee2mqtt/leds.nix - ./zigbee2mqtt/lights.nix - ./zigbee2mqtt/motion.nix - ./zigbee2mqtt/repeater.nix - ./zigbee2mqtt/temperatur.nix - ]; - - custom.services.zigbee2mqtt = { - enable = true; - #package = pkgs.own_zigbee2mqtt; - #package = unstable.zigbee2mqtt; - package = pkgs.unstable.zigbee2mqtt.overrideAttrs (old: rec { - version = "1.18.1"; - src = pkgs.fetchFromGitHub { - owner = "Koenkk"; - repo = "zigbee2mqtt"; - rev = version; - sha256 = "1x73k346ayik5hv5axa3nvmd82mgwyrpxqv3dxnffi8aa1r8pf8x"; - }; - }); - config = { - - # Home Assistant integration (MQTT discovery) - homeassistant = false; - - # allow new devices to join - permit_join = false; - - # 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 = lib.fileContents ; - }; - - # Serial settings - serial = { - #port = "/dev/ttyACM0"; - port = "/dev/ttyUSB0"; - # disable LED of CC2531 USB sniffer - #disable_led = true; - }; - - # you own network key, - # 16 numbers between 0 and 255 - # see https://www.zigbee2mqtt.io/how_tos/how_to_secure_network.html - advanced.network_key = import ; - advanced.log_output = [ "console" ]; - - advanced.pan_id = 1337; - - # add last seen information - advanced.last_seen = "ISO_8601_local"; - - # configure web ui - frontend.port = 9666; - frontend.host = "0.0.0.0"; - experimental.new_api = true; - - }; - }; - - #systemd.services.zigbee2mqtt.environment = { - # ZIGBEE2MQTT_DATA = "/var/lib/zigbee2mqtt"; - #}; - - services.nginx = { - enable = true; - recommendedProxySettings = true; - virtualHosts = { - "zigbee2mqtt.pepe.private" = { - serverAliases = [ "zigbee.pepe.private" ]; - locations."/" = { - proxyPass = "http://localhost:${ - toString config.custom.services.zigbee2mqtt.config.frontend.port - }"; - proxyWebsockets = true; - }; - }; - }; - }; - -} diff --git a/nixos/machines/pepe/home-assistant/zigbee2mqtt/buttons.nix b/nixos/machines/pepe/home-assistant/zigbee2mqtt/buttons.nix deleted file mode 100644 index 7d5d6d5..0000000 --- a/nixos/machines/pepe/home-assistant/zigbee2mqtt/buttons.nix +++ /dev/null @@ -1,250 +0,0 @@ -{ pkgs, lib, ... }: -let - - # we create 3 input_boolean which get toggled by the 3 types of buttons pressed. - # input_boolean.single_${name} : single click - # input_boolean.double_${name} : double click - # input_boolean.hold_${name} : hold - # if you override these input (via states) you have to create the input yourself - - # https://www.zigbee2mqtt.io/devices/WXKG12LM.html - allDevices = { - "button_a1" = { - id = "0x00158d0002b04f65"; - #groups = [ "living_room" ]; - states.single = "input_boolean.situation_toggle"; - states.hold = "input_boolean.printer_toggle"; - states.double = "input_boolean.windows_up"; - }; - "button_a2" = { - id = "0x00158d0002b04f09"; - #groups = [ "bed_room" ]; - states.single = "input_boolean.situation_toggle"; - states.hold = "input_boolean.printer_toggle"; - states.double = "input_boolean.windows_up"; - }; - "button_a3" = { - id = "0x00158d0002b00e04"; - #groups = [ "bed_room" ]; - states.single = "input_boolean.situation_toggle"; - states.hold = "input_boolean.printer_toggle"; - states.double = "input_boolean.windows_up"; - }; - }; - -in -{ - - services.zigbee2mqttConfiguration = lib.mapAttrs' - (name: - { id, ... }: { - name = id; - value = { - retain = false; - friendly_name = name; - }; - }) - allDevices; - - services.homeAssistantConfig = { - - # define input_boolean - # -------------------- - # which get toggled by the buttons - input_boolean = - let stripEmpty = lib.filter (a: a != { }); - in builtins.listToAttrs (stripEmpty (lib.flatten (lib.mapAttrsToList - (name: - { states ? { }, ... }: [ - (lib.optionalAttrs (!lib.hasAttr "single" states) { - name = "single_${name}"; - value = { icon = "mdi:toggle-switch"; }; - }) - (lib.optionalAttrs (!lib.hasAttr "double" states) { - name = "double_${name}"; - value = { icon = "mdi:toggle-switch"; }; - }) - (lib.optionalAttrs (!lib.hasAttr "hold" states) { - name = "hold_${name}"; - value = { icon = "mdi:toggle-switch"; }; - }) - ]) - allDevices))); - - # define meta information sensors - sensor = lib.flatten (lib.mapAttrsToList - (name: - { ... }: [ - { - platform = "mqtt"; - name = name; - icon = "mdi:toggle-switch"; - state_topic = "zigbee2mqtt/${name}"; - availability_topic = "zigbee2mqtt/bridge/state"; - value_template = "{{ value_json.click }}"; - } - { - name = "battery_${name}"; - platform = "mqtt"; - state_topic = "zigbee2mqtt/${name}"; - availability_topic = "zigbee2mqtt/bridge/state"; - unit_of_measurement = "%"; - icon = "mdi:battery-10"; - value_template = "{{ value_json.battery }}"; - } - { - name = "link_${name}"; - platform = "mqtt"; - state_topic = "zigbee2mqtt/${name}"; - availability_topic = "zigbee2mqtt/bridge/state"; - icon = "mdi:signal"; - unit_of_measurement = "lqi"; - value_template = "{{ value_json.linkquality }}"; - } - ]) - allDevices); - - binary_sensor = lib.mapAttrsToList - (name: - { ... }: { - name = name; - platform = "mqtt"; - device_class = "motion"; - state_topic = "zigbee2mqtt/${name}"; - availability_topic = "zigbee2mqtt/bridge/state"; - payload_on = true; - payload_off = false; - value_template = "{{ value_json.occupancy }}"; - }) - allDevices; - - # create groups - # ------------- - #group = let - # # to have nice panels for every device - # sensorGroups = lib.mapAttrs (name: - # { states ? { }, ... }: - # let - # entityIds = { single ? "input_boolean.single_${name}" - # , double ? "input_boolean.double_${name}" - # , hold ? "input_boolean.hold_${name}", ... }: [ - # single - # double - # hold - # ]; - # in { - # entities = [ "sensor.${name}" ] ++ (entityIds states) - # ++ [ "sensor.battery_${name}" "sensor.link_${name}" ]; - # }) allDevices; - # # sort lights into given groups. - # sortedInGroups = let - # groupEntries = lib.zipAttrs (lib.flatten (lib.mapAttrsToList (name: - # { groups ? [ ], states ? { }, ... }: - # map (groupName: { - # "${groupName}" = if (lib.hasAttr "single" states) then - # states.single - # else - # "input_boolean.single_${name}"; - # }) groups) allDevices)); - # in lib.mapAttrs (name: entities: { inherit entities; }) groupEntries; - #in sortedInGroups // sensorGroups // { - # all_sensors.entities = - # lib.mapAttrsToList (name: { ... }: "binary_sensor.${name}") allDevices; - #}; - - # create automation - # ----------------- - automation = - let - - # single click - toggle_single_button_input = lib.mapAttrsToList - (name: - { states ? { }, ... }: - let - entityId = - if (lib.hasAttr "single" states) then - states.single - else - "input_boolean.single_${name}"; - in - { - alias = "toggle single click ${name}"; - trigger = { - platform = "mqtt"; - topic = "zigbee2mqtt/${name}"; - }; - condition = { - condition = "template"; - value_template = ''{{ "single" == trigger.payload_json.click}}''; - }; - action = { - service = "input_boolean.toggle"; - data.entity_id = entityId; - }; - }) - allDevices; - - # double click - toggle_double_button_input = lib.mapAttrsToList - (name: - { states ? { }, ... }: - let - entityId = - if (lib.hasAttr "double" states) then - states.double - else - "input_boolean.double_${name}"; - in - { - alias = "toggle double click ${name}"; - trigger = { - platform = "mqtt"; - topic = "zigbee2mqtt/${name}"; - }; - condition = { - condition = "template"; - value_template = ''{{ "double" == trigger.payload_json.click}}''; - }; - action = { - service = "input_boolean.toggle"; - data.entity_id = entityId; - }; - }) - allDevices; - - # hold - toggle_hold_button_input = lib.mapAttrsToList - (name: - { states ? { }, ... }: - let - entityId = - if (lib.hasAttr "hold" states) then - states.hold - else - "input_boolean.hold_${name}"; - in - { - alias = "toggle hold ${name}"; - trigger = { - platform = "mqtt"; - topic = "zigbee2mqtt/${name}"; - }; - condition = { - condition = "template"; - value_template = ''{{ "hold" == trigger.payload_json.action}}''; - }; - action = { - service = "input_boolean.toggle"; - data.entity_id = entityId; - }; - }) - allDevices; - - in - lib.flatten (toggle_single_button_input ++ toggle_double_button_input - ++ toggle_hold_button_input); - - }; - -} diff --git a/nixos/machines/pepe/home-assistant/zigbee2mqtt/configurationHelper.nix b/nixos/machines/pepe/home-assistant/zigbee2mqtt/configurationHelper.nix deleted file mode 100644 index 7d10025..0000000 --- a/nixos/machines/pepe/home-assistant/zigbee2mqtt/configurationHelper.nix +++ /dev/null @@ -1,12 +0,0 @@ -{ config, lib, pkgs, ... }: -with lib; -let cfg = config.services.zigbee2mqttConfiguration; -in { - options.services.zigbee2mqttConfiguration = mkOption { - type = with types; attrs; - description = '' - device definitions - ''; - }; - config = { services.zigbee2mqtt.config.devices = cfg; }; -} diff --git a/nixos/machines/pepe/home-assistant/zigbee2mqtt/doors.nix b/nixos/machines/pepe/home-assistant/zigbee2mqtt/doors.nix deleted file mode 100644 index 2298e21..0000000 --- a/nixos/machines/pepe/home-assistant/zigbee2mqtt/doors.nix +++ /dev/null @@ -1,101 +0,0 @@ -{ pkgs, lib, ... }: -let - - # https://www.zigbee2mqtt.io/devices/MCCGQ11LM.html - allDevices = { - "door_sensor_1" = { id = "0x00158d000312dc52"; }; - "door_sensor_2" = { id = "0x00158d000316d5bf"; }; - "door_sensor_3" = { id = "0x00158d0002f9516f"; }; - "door_sensor_4" = { id = "0x00158d00031383b9"; }; - "door_sensor_5" = { id = "0x00158d0003120d3e"; }; - }; - -in -{ - - services.zigbee2mqttConfiguration = lib.mapAttrs' - (name: - { id, ... }: { - name = id; - value = { - retain = false; - friendly_name = name; - }; - }) - allDevices; - - services.homeAssistantConfig = { - - # define meta information sensors - sensor = lib.flatten (lib.mapAttrsToList - (name: - { ... }: [ - { - name = "battery_${name}"; - platform = "mqtt"; - state_topic = "zigbee2mqtt/${name}"; - availability_topic = "zigbee2mqtt/bridge/state"; - unit_of_measurement = "%"; - icon = "mdi:battery-10"; - value_template = "{{ value_json.battery }}"; - } - { - name = "link_${name}"; - platform = "mqtt"; - state_topic = "zigbee2mqtt/${name}"; - availability_topic = "zigbee2mqtt/bridge/state"; - icon = "mdi:signal"; - unit_of_measurement = "lqi"; - value_template = "{{ value_json.linkquality }}"; - } - ]) - allDevices); - - binary_sensor = lib.mapAttrsToList - (name: - { ... }: { - name = name; - platform = "mqtt"; - device_class = "door"; - state_topic = "zigbee2mqtt/${name}"; - availability_topic = "zigbee2mqtt/bridge/state"; - payload_on = false; - payload_off = true; - value_template = "{{ value_json.contact}}"; - }) - allDevices; - - # create groups - # ------------- - group = - let - # to have nice panels for every device - sensorGroups = lib.mapAttrs - (name: - { ... }: { - entities = [ - "binary_sensor.${name}" - "sensor.battery_${name}" - "sensor.link_${name}" - ]; - }) - allDevices; - # sort lights into given groups. - sortedInGroups = - let - groupEntries = lib.zipAttrs (lib.flatten (lib.mapAttrsToList - (name: - { groups ? [ ], ... }: - map (groupName: { "${groupName}" = "binary_sensor.${name}"; }) groups) - allDevices)); - in - lib.mapAttrs (name: entities: { inherit entities; }) groupEntries; - in - sortedInGroups // sensorGroups // { - all_sensors.entities = - lib.mapAttrsToList (name: { ... }: "binary_sensor.${name}") allDevices; - }; - - }; - -} diff --git a/nixos/machines/pepe/home-assistant/zigbee2mqtt/fyrtur.nix b/nixos/machines/pepe/home-assistant/zigbee2mqtt/fyrtur.nix deleted file mode 100644 index f490df4..0000000 --- a/nixos/machines/pepe/home-assistant/zigbee2mqtt/fyrtur.nix +++ /dev/null @@ -1,57 +0,0 @@ -{ pkgs, lib, ... }: -let - - # https://www.zigbee2mqtt.io/devices/E1757.html - allDevices = { - "office_fyrtur_1" = { id = "0x680ae2fffe64fa40"; }; - "office_fyrtur_2" = { id = "0x680ae2fffe91d234"; }; - "bedroom_fyrtur_1" = { id = "0x680ae2fffe6e9f41"; }; - "broken_fyrtur_1" = { id = "0x680ae2fffe8f6411"; }; - }; - - # -t "zigbee2mqtt/fyrtur1/set" -m '{"position":100}' - # -t "zigbee2mqtt/fyrtur1/set" -m '{"position":15}' -in -{ - - services.zigbee2mqttConfiguration = lib.mapAttrs' - (name: - { id, ... }: { - name = id; - value = { - retain = false; - friendly_name = name; - transition = 0.1; - }; - }) - allDevices; - - services.homeAssistantConfig = { - - sensor = lib.flatten (lib.mapAttrsToList - (name: - { ... }: [ - { - name = "battery_${name}"; - platform = "mqtt"; - state_topic = "zigbee2mqtt/${name}"; - availability_topic = "zigbee2mqtt/bridge/state"; - unit_of_measurement = "%"; - icon = "mdi:battery-10"; - value_template = "{{ value_json.battery }}"; - } - { - name = "link_${name}"; - platform = "mqtt"; - state_topic = "zigbee2mqtt/${name}"; - availability_topic = "zigbee2mqtt/bridge/state"; - icon = "mdi:signal"; - unit_of_measurement = "lqi"; - value_template = "{{ value_json.linkquality }}"; - } - ]) - allDevices); - - }; - -} diff --git a/nixos/machines/pepe/home-assistant/zigbee2mqtt/heater.nix b/nixos/machines/pepe/home-assistant/zigbee2mqtt/heater.nix deleted file mode 100644 index 426cf11..0000000 --- a/nixos/machines/pepe/home-assistant/zigbee2mqtt/heater.nix +++ /dev/null @@ -1,85 +0,0 @@ -{ pkgs, lib, ... }: -let - - # https://www.zigbee2mqtt.io/devices/SPZB0001.html - allDevices = { - "office_heater_1" = { id = "0x00158d00032f5ee4"; }; # office - "office_heater_2" = { id = "0x00158d00032f5f9f"; }; # office (kitchen) - "bedroom_heater_1" = { id = "0x00158d00032f6d1e"; }; # bed room - "storage_heater_1" = { id = "0x00158d00032f604d"; }; # abstell raum - }; - - # -t "zigbee2mqtt/heater3/set" -m '{"system_mode":"auto","current_heating_setpoint":23}' - # -t "zigbee2mqtt/heater3/set" -m '{"system_mode":"off"}' - -in -{ - - services.zigbee2mqttConfiguration = lib.mapAttrs' - (name: - { id, ... }: { - name = id; - value = { - legacy = false; - retain = false; - friendly_name = name; - transition = 1; - debounce = 0.5; - filtered_attributes = [ - "battery_low" - "eurotronic_host_flags" - "eurotronic_system_mode" - #"occupied_heating_setpoint" - #"pi_heating_demand" - #"unoccupied_heating_setpoint" - ]; - }; - }) - allDevices; - - services.homeAssistantConfig = { - - sensor = lib.flatten (lib.mapAttrsToList - (name: - { ... }: [ - { - name = "battery_${name}"; - platform = "mqtt"; - state_topic = "zigbee2mqtt/${name}"; - availability_topic = "zigbee2mqtt/bridge/state"; - unit_of_measurement = "%"; - icon = "mdi:battery-10"; - value_template = "{{ value_json.battery }}"; - } - { - name = "link_${name}"; - platform = "mqtt"; - state_topic = "zigbee2mqtt/${name}"; - availability_topic = "zigbee2mqtt/bridge/state"; - icon = "mdi:signal"; - unit_of_measurement = "lqi"; - value_template = "{{ value_json.linkquality }}"; - } - { - platform = "mqtt"; - name = "temperature_${name}"; - state_topic = "zigbee2mqtt/${name}"; - availability_topic = "zigbee2mqtt/bridge/state"; - unit_of_measurement = "°C"; - device_class = "temperature"; - value_template = "{{ value_json.local_temperature }}"; - } - { - platform = "mqtt"; - name = "pi_heating_demand_${name}"; - state_topic = "zigbee2mqtt/${name}"; - availability_topic = "zigbee2mqtt/bridge/state"; - unit_of_measurement = "%"; - value_template = "{{ value_json.pi_heating_demand }}"; - } - ]) - allDevices); - - }; - -} diff --git a/nixos/machines/pepe/home-assistant/zigbee2mqtt/leds.nix b/nixos/machines/pepe/home-assistant/zigbee2mqtt/leds.nix deleted file mode 100644 index bd99fa0..0000000 --- a/nixos/machines/pepe/home-assistant/zigbee2mqtt/leds.nix +++ /dev/null @@ -1,48 +0,0 @@ -{ pkgs, lib, ... }: -let - - # https://www.zigbee2mqtt.io/devices/GL-C-007-1ID.html - allDevices = { - "led_1" = { id = "0x00124b001f7a5be9"; }; - "led_2" = { id = "0x00124b001ee958b3"; }; - }; - - # -t "zigbee2mqtt/led_1/set" -m '{"state":"ON","transition":0, "color_temp":255}' - # -t "zigbee2mqtt/led_1/set" -m '{"state":"OFF","transition":0, "color_temp":255}' - # -t "zigbee2mqtt/led_1/set" -m '{"state":"ON","brightness":255,"color":{"hex":"#00FFFF"}}' - # -t "zigbee2mqtt/led_1/set" -m '{"state":"OFF"}' -in -{ - - services.zigbee2mqttConfiguration = lib.mapAttrs' - (name: - { id, ... }: { - name = id; - value = { - retain = false; - friendly_name = name; - transition = 1; - }; - }) - allDevices; - - services.homeAssistantConfig = { - - light = lib.mapAttrsToList - (name: - { ... }: { - platform = "mqtt"; - name = name; - state_topic = "zigbee2mqtt/${name}"; - availability_topic = "zigbee2mqtt/bridge/state"; - command_topic = "zigbee2mqtt/${name}/set"; - value_template = "{{ value_json.click }}"; - brightness = true; - color_temp = true; - schema = "json"; - }) - allDevices; - - }; - -} diff --git a/nixos/machines/pepe/home-assistant/zigbee2mqtt/lights.nix b/nixos/machines/pepe/home-assistant/zigbee2mqtt/lights.nix deleted file mode 100644 index 8b9cb3e..0000000 --- a/nixos/machines/pepe/home-assistant/zigbee2mqtt/lights.nix +++ /dev/null @@ -1,99 +0,0 @@ -{ pkgs, lib, ... }: -let - - # https://www.zigbee2mqtt.io/devices/AC10787.html - allDevices = { - "light_1" = { id = "0x7cb03eaa0a0347b5"; }; - "light_2" = { id = "0x7cb03eaa0a0387b9"; }; - "light_3" = { id = "0x7cb03eaa0a033a86"; }; - "light_4" = { id = "0x7cb03eaa0a04aabf"; }; - "light_5" = { id = "0x7cb03eaa0a0346e4"; }; - "light_6" = { id = "0x7cb03eaa0a034b46"; }; - "light_7" = { id = "0x7cb03eaa0a033b4f"; }; - "light_8" = { id = "0x7cb03eaa0a0384d3"; }; - }; - -in -{ - - services.zigbee2mqttConfiguration = lib.mapAttrs' - (name: - { id, ... }: { - name = id; - value = { - retain = false; - friendly_name = name; - osram_set_transition = 2; # time in seconds (integer or float) - }; - }) - allDevices; - - services.homeAssistantConfig = { - - light = lib.mapAttrsToList - (name: - { ... }: { - platform = "mqtt"; - name = name; - state_topic = "zigbee2mqtt/${name}"; - availability_topic = "zigbee2mqtt/bridge/state"; - command_topic = "zigbee2mqtt/${name}/set"; - value_template = "{{ value_json.click }}"; - brightness = true; - color_temp = true; - schema = "json"; - }) - allDevices; - - # sensor = with lib; - # mapAttrsToList (name: - # { ... }: { - # name = "link_${name}"; - # platform = "mqtt"; - # state_topic = "zigbee2mqtt/${name}"; - # availability_topic = "zigbee2mqtt/bridge/state"; - # icon = "mdi:signal"; - # value_template = "{{ value_json.linkquality}}"; - # }) allDevices; - - # binary_sensor = lib.mapAttrsToList (name: - # { ... }: { - # name = "update_${name}"; - # platform = "mqtt"; - # state_topic = "zigbee2mqtt/${name}"; - # availability_topic = "zigbee2mqtt/bridge/state"; - # payload_on = true; - # payload_off = false; - # value_template = "{{ value_json.update_available }}"; - # }) allDevices; - - # # create groups - # # ------------- - # group = let - - # # to have nice panels for every device - # lightGroups = lib.mapAttrs (name: - # { ... }: { - # entities = [ - # "light.${name}" - # "sensor.link_${name}" - # "binary_sensor.update_${name}" - # ]; - # }) allDevices; - - # # sort lights into given groups. - # sortedInGroups = let - # groupEntries = lib.zipAttrs (lib.flatten (lib.mapAttrsToList (name: - # { groups ? [ ], ... }: - # map (groupName: { "${groupName}" = "light.${name}"; }) groups) - # allDevices)); - # in lib.mapAttrs (name: entities: { inherit entities; }) groupEntries; - - # in sortedInGroups // lightGroups // { - # all_lights.entities = - # lib.mapAttrsToList (name: { ... }: "light.${name}") allDevices; - # }; - - }; - -} diff --git a/nixos/machines/pepe/home-assistant/zigbee2mqtt/motion.nix b/nixos/machines/pepe/home-assistant/zigbee2mqtt/motion.nix deleted file mode 100644 index cddb06c..0000000 --- a/nixos/machines/pepe/home-assistant/zigbee2mqtt/motion.nix +++ /dev/null @@ -1,76 +0,0 @@ -{ pkgs, lib, ... }: -let - - # https://www.zigbee2mqtt.io/devices/RTCGQ01LM.html - allDevices = { - - "motion_sensor_1" = { id = "0x00158d0002fbd451"; }; - "motion_sensor_2" = { id = "0x00158d0002f9a6b8"; }; - "motion_sensor_3" = { id = "0x00158d0002f04522"; }; - "motion_sensor_4" = { id = "0x00158d0002f9a558"; }; - "motion_sensor_5" = { id = "0x00158d0002f9a56f"; }; - "motion_sensor_6" = { id = "0x00158d0002f9a5cb"; }; - "motion_sensor_7" = { id = "0x00158d0002f9a6aa"; }; - "motion_sensor_8" = { id = "0x00158d0002f04637"; }; - }; - -in -{ - - services.zigbee2mqttConfiguration = lib.mapAttrs' - (name: - { id, timeout ? 65, ... }: { - name = id; - value = { - retain = false; - friendly_name = name; - # should not be set below 60 seconds - occupancy_timeout = timeout; - }; - }) - allDevices; - - services.homeAssistantConfig = { - - # define meta information sensors - binary_sensor = lib.flatten (lib.mapAttrsToList - (name: - { ... }: [{ - name = "${name}"; - platform = "mqtt"; - state_topic = "zigbee2mqtt/${name}"; - availability_topic = "zigbee2mqtt/bridge/state"; - value_template = "{{ value_json.occupancy }}"; - #icon = "mdi:battery-10"; - payload_on = true; - payload_off = false; - device_class = "motion"; - }]) - allDevices); - - # define meta information sensors - sensor = lib.flatten (lib.mapAttrsToList - (name: - { ... }: [ - { - name = "battery_${name}"; - platform = "mqtt"; - state_topic = "zigbee2mqtt/${name}"; - availability_topic = "zigbee2mqtt/bridge/state"; - unit_of_measurement = "%"; - icon = "mdi:battery-10"; - value_template = "{{ value_json.battery }}"; - } - { - name = "link_${name}"; - platform = "mqtt"; - state_topic = "zigbee2mqtt/${name}"; - availability_topic = "zigbee2mqtt/bridge/state"; - icon = "mdi:signal"; - unit_of_measurement = "lqi"; - value_template = "{{ value_json.linkquality }}"; - } - ]) - allDevices); - }; -} diff --git a/nixos/machines/pepe/home-assistant/zigbee2mqtt/repeater.nix b/nixos/machines/pepe/home-assistant/zigbee2mqtt/repeater.nix deleted file mode 100644 index 48fa89e..0000000 --- a/nixos/machines/pepe/home-assistant/zigbee2mqtt/repeater.nix +++ /dev/null @@ -1,21 +0,0 @@ -{ pkgs, lib, ... }: -let - - # https://www.zigbee2mqtt.io/devices/SPZB0001.html - allDevices = { - "repeater1" = { id = "0x680ae2fffe6e7dc1"; }; - "repeater2" = { id = "0x680ae2fffe8e240d"; }; - "repeater3" = { id = "0x680ae2fffe404f60"; }; - "repeater4" = { id = "0x680ae2fffe8e2e71"; }; - }; - -in -{ - services.zigbee2mqttConfiguration = lib.mapAttrs' - (name: - { id, ... }: { - name = id; - value = { friendly_name = name; }; - }) - allDevices; -} diff --git a/nixos/machines/pepe/home-assistant/zigbee2mqtt/service.nix b/nixos/machines/pepe/home-assistant/zigbee2mqtt/service.nix deleted file mode 100644 index 6c606f2..0000000 --- a/nixos/machines/pepe/home-assistant/zigbee2mqtt/service.nix +++ /dev/null @@ -1,119 +0,0 @@ -{ config, lib, pkgs, ... }: - -with lib; - -let - cfg = config.custom.services.zigbee2mqtt; - - configJSON = pkgs.writeText "configuration.json" (builtins.toJSON - (recursiveUpdate cfg.config config.services.zigbee2mqtt.config)); - configFile = - pkgs.runCommand "configuration.yaml" { preferLocalBuild = true; } '' - ${pkgs.remarshal}/bin/json2yaml -i ${configJSON} -o $out - ''; - - # the default config contains all required settings, - # so the service starts up without crashing. - defaultConfig = { - homeassistant = false; - permit_join = false; - mqtt = { - base_topic = "zigbee2mqtt"; - server = "mqtt://localhost:1883"; - }; - serial.port = "/dev/ttyACM0"; - # put device configuration into separate file because configuration.yaml - # is copied from the store on startup - devices = "devices.yaml"; - }; -in -{ - options.custom.services.zigbee2mqtt = { - enable = mkEnableOption "enable zigbee2mqtt service"; - - package = mkOption { - description = "ignored"; - default = pkgs.zigbee2mqtt.override { dataDir = cfg.dataDir; }; - defaultText = "pkgs.zigbee2mqtt"; - type = types.package; - }; - - dataDir = mkOption { - description = "Zigbee2mqtt data directory"; - default = "/var/lib/zigbee2mqtt"; - type = types.path; - }; - - config = mkOption { - default = { }; - type = with types; nullOr attrs; - example = literalExample '' - { - homeassistant = config.services.home-assistant.enable; - permit_join = true; - serial = { - port = "/dev/ttyACM1"; - }; - } - ''; - description = '' - Your configuration.yaml as a Nix attribute set. - ''; - }; - }; - - config = mkIf (cfg.enable) { - - virtualisation.oci-containers.containers.zigbee2mqtt = { - image = "koenkk/zigbee2mqtt:1.18.1"; - volumes = [ - "${cfg.dataDir}:/app/data" - #"/run/udev:/run/udev:ro" - ]; - extraOptions = [ - "--device=${cfg.config.serial.port}" # /dev/ttyUSB0 - "--network=host" - #"--privileged=true" - ]; - environment = { TZ = "Europe/Amsterdam"; }; - #ports = [ "127.0.0.1:${toString frontPort}:80" ]; - }; - - # create config before staring container - systemd.services.docker-zigbee2mqtt = { - preStart = '' - cp --no-preserve=mode ${configFile} "${cfg.dataDir}/configuration.yaml" - ''; - }; - - #systemd.services.zigbee2mqtt = { - # description = "Zigbee2mqtt Service"; - # wantedBy = [ "multi-user.target" ]; - # after = [ "network.target" ]; - # environment.ZIGBEE2MQTT_DATA = cfg.dataDir; - # serviceConfig = { - # ExecStart = "${cfg.package}/bin/zigbee2mqtt"; - # User = "zigbee2mqtt"; - # WorkingDirectory = cfg.dataDir; - # Restart = "on-failure"; - # ProtectSystem = "strict"; - # ReadWritePaths = cfg.dataDir; - # PrivateTmp = true; - # RemoveIPC = true; - # }; - # preStart = '' - # cp --no-preserve=mode ${configFile} "${cfg.dataDir}/configuration.yaml" - # ''; - #}; - - #users.users.zigbee2mqtt = { - # home = cfg.dataDir; - # createHome = true; - # group = "zigbee2mqtt"; - # extraGroups = [ "dialout" ]; - # uid = config.ids.uids.zigbee2mqtt; - #}; - - #users.groups.zigbee2mqtt.gid = config.ids.gids.zigbee2mqtt; - }; -} diff --git a/nixos/machines/pepe/home-assistant/zigbee2mqtt/temperatur.nix b/nixos/machines/pepe/home-assistant/zigbee2mqtt/temperatur.nix index e3f5e3e..4513e65 100644 --- a/nixos/machines/pepe/home-assistant/zigbee2mqtt/temperatur.nix +++ b/nixos/machines/pepe/home-assistant/zigbee2mqtt/temperatur.nix @@ -16,7 +16,7 @@ let in { - services.zigbee2mqttConfiguration = lib.mapAttrs' + services.zigbee2mqtt.settings = lib.mapAttrs' (name: { id, ... }: { name = id; @@ -27,85 +27,6 @@ in }) allDevices; - services.homeAssistantConfig = { - - # define meta information sensors - sensor = lib.flatten (lib.mapAttrsToList - (name: - { ... }: [ - { - platform = "mqtt"; - name = name; - state_topic = "zigbee2mqtt/${name}"; - availability_topic = "zigbee2mqtt/bridge/state"; - unit_of_measurement = "°C"; - device_class = "temperature"; - value_template = "{{ value_json.temperature }}"; - } - { - platform = "mqtt"; - name = "humidity_${name}"; - state_topic = "zigbee2mqtt/${name}"; - availability_topic = "zigbee2mqtt/bridge/state"; - unit_of_measurement = "%"; - device_class = "humidity"; - value_template = "{{ value_json.humidity }}"; - } - #{ - # platform = "mqtt"; - # name = "pressure_${name}"; - # state_topic = "zigbee2mqtt/${name}"; - # availability_topic = "zigbee2mqtt/bridge/state"; - # unit_of_measurement = "hPa"; - # device_class = "pressure"; - # value_template = "{{ value_json.pressure }}"; - #} - { - name = "battery_${name}"; - platform = "mqtt"; - state_topic = "zigbee2mqtt/${name}"; - availability_topic = "zigbee2mqtt/bridge/state"; - unit_of_measurement = "%"; - icon = "mdi:battery-10"; - value_template = "{{ value_json.battery }}"; - } - { - name = "link_${name}"; - platform = "mqtt"; - state_topic = "zigbee2mqtt/${name}"; - availability_topic = "zigbee2mqtt/bridge/state"; - icon = "mdi:signal"; - unit_of_measurement = "lqi"; - value_template = "{{ value_json.linkquality }}"; - } - ]) - allDevices); - - # create groups - # ------------- - #group = let - # # to have nice panels for every device - # sensorGroups = lib.mapAttrs (name: - # { ... }: { - # entities = [ - # "sensor.${name}" - # "sensor.humidity_${name}" - # #"sensor.pressure_${name}" - # "sensor.battery_${name}" - # "sensor.link_${name}" - # ]; - # }) allDevices; - # # sort lights into given groups. - # sortedInGroups = let - # groupEntries = lib.zipAttrs (lib.flatten (lib.mapAttrsToList (name: - # { groups ? [ ], ... }: - # map (groupName: { "${groupName}" = "sensor.${name}"; }) groups) - # allDevices)); - # in lib.mapAttrs (name: entities: { inherit entities; }) groupEntries; - #in sortedInGroups // sensorGroups // { - # all_sensors.entities = - # lib.mapAttrsToList (name: { ... }: "sensor.${name}") allDevices; - #}; }; diff --git a/nixos/machines/pepe/mqtt.nix b/nixos/machines/pepe/mqtt.nix new file mode 100644 index 0000000..6da92bd --- /dev/null +++ b/nixos/machines/pepe/mqtt.nix @@ -0,0 +1,12 @@ +{ lib, ... }: { + + services.mosquitto = { + enable = true; + listeners = [{ + acl = [ "pattern readwrite #" ]; + omitPasswordAuth = true; + settings.allow_anonymous = true; + }]; + }; + +} diff --git a/nixos/machines/pepe/zigbee2mqtt.nix b/nixos/machines/pepe/zigbee2mqtt.nix new file mode 100644 index 0000000..e873256 --- /dev/null +++ b/nixos/machines/pepe/zigbee2mqtt.nix @@ -0,0 +1,61 @@ +{ pkgs, lib, config, private_assets, ... }: + +{ + imports = [ ./mqtt.nix ]; + + services.zigbee2mqtt = { + enable = true; + #package = pkgs.unstable.zigbee2mqtt.overrideAttrs (old: rec { + # version = "1.18.1"; + # src = pkgs.fetchFromGitHub { + # owner = "Koenkk"; + # repo = "zigbee2mqtt"; + # rev = version; + # sha256 = "1x73k346ayik5hv5axa3nvmd82mgwyrpxqv3dxnffi8aa1r8pf8x"; + # }; + #}); + settings = { + + # Home Assistant integration (MQTT discovery) + homeassistant = true; + + # allow new devices to join + permit_join = true; + + # 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 = lib.fileContents "${private_assets}/zigbee/home-assistant-password"; + }; + + # Serial settings + serial = { + #port = "/dev/ttyACM0"; + port = "/dev/ttyUSB0"; + # disable LED of CC2531 USB sniffer + #disable_led = true; + }; + + # you own network key, + # 16 numbers between 0 and 255 + # see https://www.zigbee2mqtt.io/how_tos/how_to_secure_network.html + advanced.network_key = import "${private_assets}/zigbee/networkKey.nix"; + advanced.log_output = [ "console" ]; + advanced.pan_id = 1337; + # add last seen information + advanced.last_seen = "ISO_8601_local"; + + # configure web ui + frontend.port = 9666; + frontend.host = "0.0.0.0"; + experimental.new_api = true; + + }; + }; + +} diff --git a/nixos/system/all/nginx-landingpage.nix b/nixos/system/all/nginx-landingpage.nix index 8131d04..66814c8 100644 --- a/nixos/system/all/nginx-landingpage.nix +++ b/nixos/system/all/nginx-landingpage.nix @@ -30,6 +30,21 @@ "pepe" ]); } + { + text = "IoT"; + items = [ + { + label = "HomeAssistant"; + href = "http://pepe.private:8123/"; + image = "https://media.giphy.com/media/fyLi0OuWysotq/giphy.gif"; + } + { + label = "Zigbee2Mqtt"; + href = "http://pepe.private:9666/"; + image = "https://media.giphy.com/media/fyLi0OuWysotq/giphy.gif"; + } + ]; + } { title = "Various Links"; items = [