cosmetics

This commit is contained in:
Ingolf Wagner 2020-04-16 02:27:39 +02:00
parent afd0a285cd
commit fdc24d1a36
No known key found for this signature in database
GPG key ID: 76BF5F1928B9618B

View file

@ -4,49 +4,37 @@ let
sonoffSwitches = { sonoffSwitches = {
"pal01" = { "pal01" = {
label = "Schlafzimmer Lampe Links"; label = "Schlafzimmer Lampe Links";
icon = "mdi:lightbulb-on"; icon = "mdi:lightbulb";
room = "bed_room_lights"; groups = [ "all_lights" "bed_room_lights" "bed_room" ];
type = "light";
}; };
"pal02" = { "pal02" = {
label = "Wohnzimmer Lampe"; label = "Wohnzimmer Lampe";
icon = "mdi:lightbulb-on"; icon = "mdi:lightbulb";
room = "living_room_lights"; groups = [ "all_lights" "living_room_lights" "living_room" ];
type = "light";
}; };
"pal03" = { "pal03" = {
label = "Wohnzimmer Lampe"; label = "Wohnzimmer Lampe";
icon = "mdi:lightbulb-on"; icon = "mdi:lightbulb";
room = "living_room_lights"; groups = [ "all_lights" "living_room_lights" "living_room" ];
type = "light";
}; };
"pal04" = { "pal04" = {
label = "Schlafzimmer Lampe Rechts"; label = "Schlafzimmer Lampe Rechts";
icon = "mdi:lightbulb-on"; icon = "mdi:lightbulb";
room = "bed_room_lights"; groups = [ "all_lights" "bed_room_lights" "bed_room" ];
type = "light";
}; };
"pal05" = { "pal05" = {
label = "TV"; label = "TV";
icon = "mdi:television"; icon = "mdi:television";
room = "living_room_lights"; groups = [ "tv" "living_room" ];
type = "device";
device = "tv"; device = "tv";
}; };
"pal06" = { "pal06" = {
label = "Küchen Lampe"; label = "Küchen Lampe";
icon = "mdi:lightbulb-on"; icon = "mdi:lightbulb";
room = "kitchen_room_lights"; groups = [ "all_lights" "kitchen_room_lights" "kitchen_room" ];
type = "light";
};
"pal07" = {
label = "Nummer 7";
icon = "mdi:power-plug-off";
};
"pal08" = {
label = "Nummer 8";
icon = "mdi:power-plug-off";
}; };
"pal07" = { label = "Nummer 7"; };
"pal08" = { label = "Nummer 8"; };
}; };
toSwitch = name: "switch.${name}"; toSwitch = name: "switch.${name}";
@ -60,7 +48,7 @@ in {
# nicer names # nicer names
# ----------- # -----------
homeassistant.customize = lib.mapAttrs' (entity: homeassistant.customize = lib.mapAttrs' (entity:
{ label, icon, ... }: { { label, icon ? "mdi:power-plug-off", ... }: {
name = toSwitch entity; name = toSwitch entity;
value = { value = {
friendly_name = label; friendly_name = label;
@ -70,16 +58,17 @@ in {
# define switches # define switches
# --------------- # ---------------
switch = builtins.map (name: { switch = lib.mapAttrsToList (name:
name = name; { ... }: {
platform = "mqtt"; name = name;
command_topic = "cmnd/${lib.toUpper name}/POWER"; platform = "mqtt";
state_topic = "stat/${lib.toUpper name}/POWER"; command_topic = "cmnd/${lib.toUpper name}/POWER";
payload_on = "ON"; state_topic = "stat/${lib.toUpper name}/POWER";
payload_off = "OFF"; payload_on = "ON";
state_on = "ON"; payload_off = "OFF";
state_off = "OFF"; state_on = "ON";
}) (builtins.attrNames sonoffSwitches); state_off = "OFF";
}) sonoffSwitches;
# discover state on init # discover state on init
# ---------------------- # ----------------------
@ -89,56 +78,27 @@ in {
platform = "homeassistant"; platform = "homeassistant";
event = "start"; event = "start";
}; };
action = builtins.map (name: { action = lib.mapAttrsToList (name:
service = "mqtt.publish"; { ... }: {
data = { service = "mqtt.publish";
topic = "cmnd/${lib.toUpper name}/power"; data = {
payload = ""; topic = "cmnd/${lib.toUpper name}/power";
}; payload = "";
}) (builtins.attrNames sonoffSwitches); };
}]; }) sonoffSwitches;
# append to scripts
# -----------------
script.turn_all_off.sequence = [
{
alias = "turn off sonoff lights";
service = "switch.turn_off";
data.entity_id = "group.all_lights";
}
{
alias = "turn off tv";
service = "switch.turn_off";
data.entity_id = "group.tv";
}
];
script.turn_all_lights_off.sequence = [{
alias = "turn off sonoff lights";
service = "switch.turn_off";
data.entity_id = "group.all_lights";
}]; }];
# append to groups # append to groups
# ---------------- # ----------------
group = let group = let
flatSonoffSwitches = # sort lights into given groups.
lib.mapAttrsToList (name: values: { name = name; } // values) sortedInGroups = let
sonoffSwitches; groupEntries = lib.zipAttrs (lib.flatten (lib.mapAttrsToList (name:
allLights' = lib.filter ({ type ? "unknown", ... }: type == "light") { groups ? [ ], ... }:
flatSonoffSwitches; map (groupName: { "${groupName}" = "switch.${name}"; }) groups)
allLights = { sonoffSwitches));
all_lights.entities = map ({ name, ... }: "switch.${name}") allLights'; in lib.mapAttrs (name: entities: { inherit entities; }) groupEntries;
}; in sortedInGroups;
allRooms' = lib.groupBy ({ room ? "unknown", ... }: room) allLights';
allRooms = lib.mapAttrs (_: devices: {
entities = map ({ name, ... }: "switch.${name}") devices;
}) allRooms';
in allRooms // allLights // {
# this is a quick hack. it should not be the norm to define this stuff down here
tv = { entities = builtins.map toSwitch [ "pal05" ]; };
};
}; };
} }