From 53acb610b1e1ec9e7491f3a7063bcbe3877ead2e Mon Sep 17 00:00:00 2001 From: Ingolf Wagner Date: Wed, 15 Apr 2020 23:55:21 +0200 Subject: [PATCH] pepe: zigbee2mqtt refactoring finished --- configs/pepe/home-assistant/zigbee2mqtt.nix | 201 +----------------- .../home-assistant/zigbee2mqtt/buttons.nix | 6 + .../pepe/home-assistant/zigbee2mqtt/doors.nix | 98 +++++++++ .../home-assistant/zigbee2mqtt/lights.nix | 6 +- .../home-assistant/zigbee2mqtt/temperatur.nix | 112 ++++++++++ 5 files changed, 222 insertions(+), 201 deletions(-) create mode 100644 configs/pepe/home-assistant/zigbee2mqtt/doors.nix create mode 100644 configs/pepe/home-assistant/zigbee2mqtt/temperatur.nix diff --git a/configs/pepe/home-assistant/zigbee2mqtt.nix b/configs/pepe/home-assistant/zigbee2mqtt.nix index 8073cce..5088bc1 100644 --- a/configs/pepe/home-assistant/zigbee2mqtt.nix +++ b/configs/pepe/home-assistant/zigbee2mqtt.nix @@ -1,210 +1,15 @@ -{ pkgs, lib, config, ... }: -let - - sensors = { - temperature = { - "temperature_sensor_1" = { - id = "0x00158d0002d79220"; - room = "living_room_present"; - }; - "temperature_sensor_2" = { - id = "0x00158d0002d7913d"; - room = "living_room_present"; - }; - }; - door = { - "door_sensor_1" = { id = "0x00158d000312dc52"; }; - "door_sensor_2" = { - id = "0x00158d000316d5bf"; - room = "floor_room_present"; - }; - "door_sensor_3" = { id = "0x00158d0002f9516f"; }; - "door_sensor_4" = { - id = "0x00158d00031383b9"; - room = "floor_room_present"; - }; - "door_sensor_5" = { id = "0x00158d0003120d3e"; }; - }; - }; - - # todo : generate automatically - allSensors = with sensors; temperature // door; - -in { +{ pkgs, lib, config, ... }: { imports = [ ./mqtt.nix ./zigbee2mqtt/service.nix ./zigbee2mqtt/lights.nix ./zigbee2mqtt/motion.nix ./zigbee2mqtt/buttons.nix + ./zigbee2mqtt/temperatur.nix + ./zigbee2mqtt/doors.nix ]; services.zigbee2mqtt.enable = true; services.zigbee2mqtt.password = lib.fileContents ; - services.zigbee2mqtt.devices = lib.mapAttrs' (name: - { id, ... }: { - name = id; - value = { - retain = false; - friendly_name = name; - }; - }) (allSensors); - - services.homeAssistantConfig = { - - # define sensors - # -------------- - sensor = let - - # define temperature sensors - temperature = with 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 }}"; - } - ]) sensors.temperature; - - # define meta information sensors - informations = 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 }}"; - } - ]) allSensors; - - in lib.flatten (temperature ++ informations); - - # define binary sensors - # --------------------- - binary_sensor = let - - # define door sensors - # ------------------- - door = 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}}"; - }) sensors.door; - - in lib.flatten (door); - - # create groups - # ------------- - # to have nice panels for every device - group = let - - information = name: [ "sensor.battery_${name}" "sensor.link_${name}" ]; - - sensorTemperature = lib.mapAttrs' (name: - { ... }: { - name = name; - value = { - control = "hidden"; - entities = [ - "sensor.${name}" - "sensor.humidity_${name}" - "sensor.pressure_${name}" - ] ++ (information name); - }; - }) (sensors.temperature); - - sensorDoors = lib.mapAttrs' (name: - { ... }: { - name = name; - value = { - control = "hidden"; - entities = [ "binary_sensor.${name}" ] ++ (information name); - }; - }) (sensors.door); - - views = { - view_sensors = { - name = "Sensoren"; - control = "hidden"; - view = true; - entities = - lib.mapAttrsToList (name: { ... }: "group.${name}") allSensors; - }; - }; - - # sort sensors in room groups - # --------------------------- - allRoomsGroups = let - createGroupEntities = { sensors, entityNameing }: - let - flatSensors = - lib.mapAttrsToList (name: values: { name = name; } // values) - sensors; - allRooms' = - lib.groupBy ({ room ? "unknown", ... }: room) flatSensors; - allRooms = - lib.mapAttrs (_: devices: map entityNameing devices) allRooms'; - in allRooms; - groupEntries = { - doorGroups = createGroupEntities { - sensors = sensors.door; - entityNameing = ({ name, ... }: "binary_sensor.${name}"); - }; - }; - - # merge all group entries - in lib.mapAttrs (_: entities: { entities = lib.flatten entities; }) - (lib.zipAttrs (builtins.attrValues groupEntries)); - - in views // allRoomsGroups // sensorTemperature // sensorDoors // { - all_sensors.entities = - (lib.mapAttrsToList (name: { ... }: "sensor.${name}") - (sensors.temperature)) - ++ (lib.mapAttrsToList (name: { ... }: "binary_sensor.${name}") - sensors.door); - }; - - }; } diff --git a/configs/pepe/home-assistant/zigbee2mqtt/buttons.nix b/configs/pepe/home-assistant/zigbee2mqtt/buttons.nix index f7c81ff..fbce4ea 100644 --- a/configs/pepe/home-assistant/zigbee2mqtt/buttons.nix +++ b/configs/pepe/home-assistant/zigbee2mqtt/buttons.nix @@ -1,6 +1,12 @@ { 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_1" = { diff --git a/configs/pepe/home-assistant/zigbee2mqtt/doors.nix b/configs/pepe/home-assistant/zigbee2mqtt/doors.nix new file mode 100644 index 0000000..68fed97 --- /dev/null +++ b/configs/pepe/home-assistant/zigbee2mqtt/doors.nix @@ -0,0 +1,98 @@ +{ pkgs, lib, ... }: +let + + # https://www.zigbee2mqtt.io/devices/MCCGQ11LM.html + allDevices = { + "door_sensor_1" = { id = "0x00158d000312dc52"; }; + "door_sensor_2" = { + id = "0x00158d000316d5bf"; + groups = [ "floor_room_present" ]; + }; + "door_sensor_3" = { id = "0x00158d0002f9516f"; }; + "door_sensor_4" = { + id = "0x00158d00031383b9"; + groups = [ "floor_room_present" ]; + }; + "door_sensor_5" = { id = "0x00158d0003120d3e"; }; + }; + +in { + + services.zigbee2mqtt.devices = 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: + { ... }: { + control = "hidden"; + 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/configs/pepe/home-assistant/zigbee2mqtt/lights.nix b/configs/pepe/home-assistant/zigbee2mqtt/lights.nix index e608d5c..3fb4f23 100644 --- a/configs/pepe/home-assistant/zigbee2mqtt/lights.nix +++ b/configs/pepe/home-assistant/zigbee2mqtt/lights.nix @@ -61,7 +61,7 @@ in { sensor = with lib; mapAttrsToList (name: { ... }: { - name = name; + name = "link_${name}"; platform = "mqtt"; state_topic = "zigbee2mqtt/${name}"; availability_topic = "zigbee2mqtt/bridge/state"; @@ -71,7 +71,7 @@ in { binary_sensor = lib.mapAttrsToList (name: { ... }: { - name = name; + name = "update_${name}"; platform = "mqtt"; state_topic = "zigbee2mqtt/${name}"; availability_topic = "zigbee2mqtt/bridge/state"; @@ -89,7 +89,7 @@ in { { ... }: { control = "hidden"; entities = - [ "light.${name}" "sensor.${name}" "binary_sensor.${name}" ]; + [ "light.${name}" "sensor.link_${name}" "binary_sensor.update_${name}" ]; }) allDevices; # sort lights into given groups. diff --git a/configs/pepe/home-assistant/zigbee2mqtt/temperatur.nix b/configs/pepe/home-assistant/zigbee2mqtt/temperatur.nix new file mode 100644 index 0000000..90910b5 --- /dev/null +++ b/configs/pepe/home-assistant/zigbee2mqtt/temperatur.nix @@ -0,0 +1,112 @@ +{ pkgs, lib, ... }: +let + + # https://www.zigbee2mqtt.io/devices/WSDCGQ11LM.html + allDevices = { + "temperature_sensor_1" = { + id = "0x00158d0002d79220"; + groups = [ "living_room_present" ]; + }; + "temperature_sensor_2" = { + id = "0x00158d0002d7913d"; + groups = [ "living_room_present" ]; + }; + }; + +in { + + services.zigbee2mqtt.devices = 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: + { ... }: [ + { + 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: + { ... }: { + control = "hidden"; + 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: { ... }: "binary_sensor.${name}") allDevices; + }; + }; + +}