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

110 lines
3.1 KiB
Nix
Raw Normal View History

2020-04-15 21:47:21 +02:00
{ pkgs, lib, ... }:
let
# https://www.zigbee2mqtt.io/devices/AC10787.html
allDevices = {
"light_1" = {
id = "0x7cb03eaa0a0347b5";
2020-04-16 00:22:29 +02:00
groups = [ "floor_room" "floor_room_essential" "floor_room_lights" ];
2020-04-15 21:47:21 +02:00
};
"light_2" = {
id = "0x7cb03eaa0a0387b9";
2020-04-16 00:22:29 +02:00
groups = [ "floor_room" "floor_room_lights" ];
2020-04-15 21:47:21 +02:00
};
"light_3" = {
id = "0x7cb03eaa0a033a86";
2020-04-16 01:26:02 +02:00
groups = [ "living_room" "living_room_essential" "living_room_lights" ];
2020-04-15 21:47:21 +02:00
};
"light_4" = {
id = "0x7cb03eaa0a04aabf";
2020-04-16 00:22:29 +02:00
groups = [ "bath_room" "bath_room_essential" "bath_room_lights" ];
2020-04-15 21:47:21 +02:00
};
2020-04-16 00:22:29 +02:00
"light_5" = { id = "0x7cb03eaa0a0346e4"; };
2020-04-15 21:47:21 +02:00
"light_6" = { id = "0x7cb03eaa0a034b46"; };
"light_7" = { id = "0x7cb03eaa0a033b4f"; };
"light_8" = {
id = "0x7cb03eaa0a0384d3";
2020-04-16 00:22:29 +02:00
groups = [ "bed_room" "bed_room_essential" "bed_room_lights" ];
2020-04-15 21:47:21 +02:00
};
};
in {
services.zigbee2mqtt.devices = 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:
{ ... }: {
2020-04-15 23:55:21 +02:00
name = "link_${name}";
2020-04-15 21:47:21 +02:00
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:
{ ... }: {
2020-04-15 23:55:21 +02:00
name = "update_${name}";
2020-04-15 21:47:21 +02:00
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:
{ ... }: {
control = "hidden";
2020-04-16 00:10:12 +02:00
entities = [
"light.${name}"
"sensor.link_${name}"
"binary_sensor.update_${name}"
];
2020-04-15 21:47:21 +02:00
}) 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;
};
};
}