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

111 lines
2.8 KiB
Nix
Raw Normal View History

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";
2020-05-03 06:55:38 +02:00
groups = [ "all_lights" "bed_room_lights" "bed_room_bright" "bed_room" ];
2020-04-09 16:51:39 +02:00
};
"pal02" = {
label = "Drucker / Scanner";
icon = "mdi:printer";
groups = [ "bed_room" "today" ];
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";
2020-05-03 06:55:38 +02:00
groups = [ "all_lights" "bed_room_lights" "bed_room_bright" "bed_room" ];
2020-04-09 16:51:39 +02:00
};
"pal05" = {
label = "TV";
icon = "mdi:television";
groups = [ "tv" "living_room" "today" ];
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"
2020-05-03 06:55:38 +02:00
"kitchen_room_bright"
"kitchen_room_essential"
"kitchen_room_lights"
2020-04-16 03:08:32 +02:00
];
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
};
}