nixos-config/configs/pepe/home-assistant/zigbee2mqtt.nix

211 lines
6.1 KiB
Nix
Raw Normal View History

2020-04-08 16:43:09 +02:00
{ pkgs, lib, config, ... }:
let
sensors = {
temperature = {
"temperature_sensor_1" = {
id = "0x00158d0002d79220";
room = "living_room_present";
};
"temperature_sensor_2" = {
id = "0x00158d0002d7913d";
room = "living_room_present";
};
2020-04-08 16:43:09 +02:00
};
2020-04-12 19:57:49 +02:00
door = {
"door_sensor_1" = { id = "0x00158d000312dc52"; };
2020-04-14 17:29:00 +02:00
"door_sensor_2" = {
id = "0x00158d000316d5bf";
room = "floor_room_present";
};
"door_sensor_3" = { id = "0x00158d0002f9516f"; };
2020-04-14 17:29:00 +02:00
"door_sensor_4" = {
id = "0x00158d00031383b9";
room = "floor_room_present";
};
"door_sensor_5" = { id = "0x00158d0003120d3e"; };
2020-04-12 19:57:49 +02:00
};
2020-04-08 16:43:09 +02:00
};
2020-04-12 19:57:49 +02:00
# todo : generate automatically
2020-04-15 21:47:21 +02:00
allSensors = with sensors; temperature // door;
2020-04-08 16:43:09 +02:00
in {
2020-04-15 21:47:21 +02:00
imports = [
./mqtt.nix
./zigbee2mqtt/service.nix
./zigbee2mqtt/lights.nix
./zigbee2mqtt/motion.nix
./zigbee2mqtt/buttons.nix
];
services.zigbee2mqtt.enable = true;
services.zigbee2mqtt.password = lib.fileContents <secrets/zigbee/password>;
services.zigbee2mqtt.devices = lib.mapAttrs' (name:
{ id, ... }: {
name = id;
value = {
retain = false;
friendly_name = name;
};
}) (allSensors);
2020-04-08 16:43:09 +02:00
services.homeAssistantConfig = {
2020-04-12 19:57:49 +02:00
# define sensors
# --------------
2020-04-08 16:43:09 +02:00
sensor = let
2020-04-12 19:57:49 +02:00
# define temperature sensors
2020-04-08 16:43:09 +02:00
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;
2020-04-12 19:57:49 +02:00
# define meta information sensors
2020-04-08 16:43:09 +02:00
informations = lib.mapAttrsToList (name:
{ ... }: [
{
name = "battery_${name}";
platform = "mqtt";
2020-04-08 16:43:09 +02:00
state_topic = "zigbee2mqtt/${name}";
availability_topic = "zigbee2mqtt/bridge/state";
unit_of_measurement = "%";
2020-04-12 02:24:20 +02:00
icon = "mdi:battery-10";
2020-04-08 16:43:09 +02:00
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";
2020-04-08 16:43:09 +02:00
value_template = "{{ value_json.linkquality }}";
}
2020-04-15 21:47:21 +02:00
]) allSensors;
2020-04-08 16:43:09 +02:00
2020-04-15 21:47:21 +02:00
in lib.flatten (temperature ++ informations);
2020-04-08 16:43:09 +02:00
2020-04-12 19:57:49 +02:00
# define binary sensors
# ---------------------
2020-04-08 16:43:09 +02:00
binary_sensor = let
2020-04-12 19:57:49 +02:00
# 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;
2020-04-15 21:47:21 +02:00
in lib.flatten (door);
2020-04-12 19:57:49 +02:00
# create groups
# -------------
# to have nice panels for every device
2020-04-08 16:43:09 +02:00
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);
2020-04-12 19:57:49 +02:00
sensorDoors = lib.mapAttrs' (name:
{ ... }: {
name = name;
value = {
control = "hidden";
entities = [ "binary_sensor.${name}" ] ++ (information name);
};
}) (sensors.door);
2020-04-08 16:43:09 +02:00
views = {
view_sensors = {
name = "Sensoren";
control = "hidden";
view = true;
entities =
lib.mapAttrsToList (name: { ... }: "group.${name}") allSensors;
};
};
2020-04-14 17:29:00 +02:00
# 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));
2020-04-15 21:47:21 +02:00
in views // allRoomsGroups // sensorTemperature // sensorDoors // {
2020-04-12 19:57:49 +02:00
all_sensors.entities =
(lib.mapAttrsToList (name: { ... }: "sensor.${name}")
2020-04-15 21:47:21 +02:00
(sensors.temperature))
2020-04-12 19:57:49 +02:00
++ (lib.mapAttrsToList (name: { ... }: "binary_sensor.${name}")
2020-04-15 21:47:21 +02:00
sensors.door);
2020-04-12 19:57:49 +02:00
};
2020-04-08 16:43:09 +02:00
};
}