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

110 lines
2.9 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";
groups = [ "floor_room_essential" ];
};
"light_2" = {
id = "0x7cb03eaa0a0387b9";
groups = [ "floor_room" ];
};
"light_3" = {
id = "0x7cb03eaa0a033a86";
groups = [ "living_room" ];
};
"light_4" = {
id = "0x7cb03eaa0a04aabf";
groups = [ "bath_room_essential" ];
};
"light_5" = {
id = "0x7cb03eaa0a0346e4";
groups = [ "living_room" ];
};
"light_6" = { id = "0x7cb03eaa0a034b46"; };
"light_7" = { id = "0x7cb03eaa0a033b4f"; };
"light_8" = {
id = "0x7cb03eaa0a0384d3";
groups = [ "bed_room" ];
};
};
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:
{ ... }: {
name = 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 = 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:
{ ... }: {
control = "hidden";
entities =
[ "light.${name}" "sensor.${name}" "binary_sensor.${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;
};
};
}