pepe: zigbee2mqtt refactoring finished

This commit is contained in:
Ingolf Wagner 2020-04-15 23:55:21 +02:00
parent 2531d47293
commit 53acb610b1
No known key found for this signature in database
GPG key ID: 76BF5F1928B9618B
5 changed files with 222 additions and 201 deletions

View file

@ -1,210 +1,15 @@
{ pkgs, lib, config, ... }: { 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 {
imports = [ imports = [
./mqtt.nix ./mqtt.nix
./zigbee2mqtt/service.nix ./zigbee2mqtt/service.nix
./zigbee2mqtt/lights.nix ./zigbee2mqtt/lights.nix
./zigbee2mqtt/motion.nix ./zigbee2mqtt/motion.nix
./zigbee2mqtt/buttons.nix ./zigbee2mqtt/buttons.nix
./zigbee2mqtt/temperatur.nix
./zigbee2mqtt/doors.nix
]; ];
services.zigbee2mqtt.enable = true; services.zigbee2mqtt.enable = true;
services.zigbee2mqtt.password = lib.fileContents <secrets/zigbee/password>; services.zigbee2mqtt.password = lib.fileContents <secrets/zigbee/password>;
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);
};
};
} }

View file

@ -1,6 +1,12 @@
{ pkgs, lib, ... }: { pkgs, lib, ... }:
let 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 # https://www.zigbee2mqtt.io/devices/WXKG12LM.html
allDevices = { allDevices = {
"button_1" = { "button_1" = {

View file

@ -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;
};
};
}

View file

@ -61,7 +61,7 @@ in {
sensor = with lib; sensor = with lib;
mapAttrsToList (name: mapAttrsToList (name:
{ ... }: { { ... }: {
name = name; name = "link_${name}";
platform = "mqtt"; platform = "mqtt";
state_topic = "zigbee2mqtt/${name}"; state_topic = "zigbee2mqtt/${name}";
availability_topic = "zigbee2mqtt/bridge/state"; availability_topic = "zigbee2mqtt/bridge/state";
@ -71,7 +71,7 @@ in {
binary_sensor = lib.mapAttrsToList (name: binary_sensor = lib.mapAttrsToList (name:
{ ... }: { { ... }: {
name = name; name = "update_${name}";
platform = "mqtt"; platform = "mqtt";
state_topic = "zigbee2mqtt/${name}"; state_topic = "zigbee2mqtt/${name}";
availability_topic = "zigbee2mqtt/bridge/state"; availability_topic = "zigbee2mqtt/bridge/state";
@ -89,7 +89,7 @@ in {
{ ... }: { { ... }: {
control = "hidden"; control = "hidden";
entities = entities =
[ "light.${name}" "sensor.${name}" "binary_sensor.${name}" ]; [ "light.${name}" "sensor.link_${name}" "binary_sensor.update_${name}" ];
}) allDevices; }) allDevices;
# sort lights into given groups. # sort lights into given groups.

View file

@ -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;
};
};
}