2020-04-08 16:43:09 +02:00
|
|
|
{ pkgs, config, lib, ... }:
|
2020-04-09 16:51:39 +02:00
|
|
|
let
|
2020-04-08 16:43:09 +02:00
|
|
|
|
2020-04-09 16:51:39 +02:00
|
|
|
sonoffSwitches = {
|
|
|
|
"pal01" = {
|
2020-04-13 12:50:25 +02:00
|
|
|
label = "Schlafzimmer Lampe Links";
|
2020-04-16 02:27:39 +02:00
|
|
|
icon = "mdi:lightbulb";
|
|
|
|
groups = [ "all_lights" "bed_room_lights" "bed_room" ];
|
2020-04-09 16:51:39 +02:00
|
|
|
};
|
|
|
|
"pal02" = {
|
2020-05-01 03:36:51 +02:00
|
|
|
label = "Nummer 2";
|
2020-04-16 02:27:39 +02:00
|
|
|
icon = "mdi:lightbulb";
|
2020-05-01 03:36:51 +02:00
|
|
|
# groups = [ ];
|
2020-04-09 16:51:39 +02:00
|
|
|
};
|
|
|
|
"pal03" = {
|
2020-04-12 21:05:55 +02:00
|
|
|
label = "Wohnzimmer Lampe";
|
2020-04-16 02:27:39 +02:00
|
|
|
icon = "mdi:lightbulb";
|
|
|
|
groups = [ "all_lights" "living_room_lights" "living_room" ];
|
2020-04-09 16:51:39 +02:00
|
|
|
};
|
|
|
|
"pal04" = {
|
2020-04-13 12:50:25 +02:00
|
|
|
label = "Schlafzimmer Lampe Rechts";
|
2020-04-16 02:27:39 +02:00
|
|
|
icon = "mdi:lightbulb";
|
|
|
|
groups = [ "all_lights" "bed_room_lights" "bed_room" ];
|
2020-04-09 16:51:39 +02:00
|
|
|
};
|
|
|
|
"pal05" = {
|
|
|
|
label = "TV";
|
|
|
|
icon = "mdi:television";
|
2020-04-16 02:27:39 +02:00
|
|
|
groups = [ "tv" "living_room" ];
|
2020-04-12 21:05:55 +02:00
|
|
|
device = "tv";
|
2020-04-09 16:51:39 +02:00
|
|
|
};
|
|
|
|
"pal06" = {
|
2020-04-12 21:05:55 +02:00
|
|
|
label = "Küchen Lampe";
|
2020-04-16 02:27:39 +02:00
|
|
|
icon = "mdi:lightbulb";
|
2020-04-16 03:08:32 +02:00
|
|
|
groups = [
|
|
|
|
"all_lights"
|
|
|
|
"kitchen_room_lights"
|
|
|
|
"kitchen_room_essential"
|
|
|
|
"kitchen_room"
|
|
|
|
];
|
2020-04-09 16:51:39 +02:00
|
|
|
};
|
2020-04-16 02:27:39 +02:00
|
|
|
"pal07" = { label = "Nummer 7"; };
|
|
|
|
"pal08" = { label = "Nummer 8"; };
|
2020-04-09 16:51:39 +02:00
|
|
|
};
|
2020-04-08 16:43:09 +02:00
|
|
|
|
2020-04-09 16:51:39 +02:00
|
|
|
toSwitch = name: "switch.${name}";
|
2020-04-08 16:43:09 +02:00
|
|
|
|
2020-04-09 16:51:39 +02:00
|
|
|
in {
|
2020-04-08 16:43:09 +02:00
|
|
|
|
2020-04-09 16:51:39 +02:00
|
|
|
imports = [ ./mqtt.nix ];
|
2020-04-08 16:43:09 +02:00
|
|
|
|
2020-04-09 16:51:39 +02:00
|
|
|
services.homeAssistantConfig = {
|
2020-04-08 16:43:09 +02:00
|
|
|
|
2020-04-12 14:04:15 +02:00
|
|
|
# nicer names
|
|
|
|
# -----------
|
|
|
|
homeassistant.customize = lib.mapAttrs' (entity:
|
2020-04-16 02:27:39 +02:00
|
|
|
{ label, icon ? "mdi:power-plug-off", ... }: {
|
2020-04-08 16:43:09 +02:00
|
|
|
name = toSwitch entity;
|
|
|
|
value = {
|
2020-04-12 14:04:15 +02:00
|
|
|
friendly_name = label;
|
|
|
|
icon = icon;
|
2020-04-08 16:43:09 +02:00
|
|
|
};
|
|
|
|
}) sonoffSwitches;
|
|
|
|
|
2020-04-12 14:04:15 +02:00
|
|
|
# define switches
|
|
|
|
# ---------------
|
2020-04-16 02:27:39 +02:00
|
|
|
switch = lib.mapAttrsToList (name:
|
|
|
|
{ ... }: {
|
|
|
|
name = name;
|
|
|
|
platform = "mqtt";
|
|
|
|
command_topic = "cmnd/${lib.toUpper name}/POWER";
|
|
|
|
state_topic = "stat/${lib.toUpper name}/POWER";
|
|
|
|
payload_on = "ON";
|
|
|
|
payload_off = "OFF";
|
|
|
|
state_on = "ON";
|
|
|
|
state_off = "OFF";
|
|
|
|
}) sonoffSwitches;
|
2020-04-12 14:04:15 +02:00
|
|
|
|
|
|
|
# discover state on init
|
|
|
|
# ----------------------
|
|
|
|
automation = [{
|
|
|
|
alias = "Sonoff initial Power state";
|
|
|
|
trigger = {
|
|
|
|
platform = "homeassistant";
|
|
|
|
event = "start";
|
|
|
|
};
|
2020-04-16 02:27:39 +02:00
|
|
|
action = lib.mapAttrsToList (name:
|
|
|
|
{ ... }: {
|
|
|
|
service = "mqtt.publish";
|
|
|
|
data = {
|
|
|
|
topic = "cmnd/${lib.toUpper name}/power";
|
|
|
|
payload = "";
|
|
|
|
};
|
|
|
|
}) sonoffSwitches;
|
2020-04-08 16:43:09 +02:00
|
|
|
}];
|
|
|
|
|
2020-04-12 14:04:15 +02:00
|
|
|
# append to groups
|
|
|
|
# ----------------
|
2020-04-12 21:05:55 +02:00
|
|
|
group = let
|
2020-04-16 02:27:39 +02:00
|
|
|
# sort lights into given groups.
|
|
|
|
sortedInGroups = let
|
|
|
|
groupEntries = lib.zipAttrs (lib.flatten (lib.mapAttrsToList (name:
|
|
|
|
{ groups ? [ ], ... }:
|
|
|
|
map (groupName: { "${groupName}" = "switch.${name}"; }) groups)
|
|
|
|
sonoffSwitches));
|
|
|
|
in lib.mapAttrs (name: entities: { inherit entities; }) groupEntries;
|
|
|
|
in sortedInGroups;
|
2020-04-08 16:43:09 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|