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

145 lines
3.5 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-09 16:51:39 +02:00
icon = "mdi:lightbulb-on";
2020-04-12 21:05:55 +02:00
room = "bed_room";
type = "light";
2020-04-09 16:51:39 +02:00
};
"pal02" = {
2020-04-13 12:50:25 +02:00
label = "Schlafzimmer Lampe Bett";
2020-04-09 16:51:39 +02:00
icon = "mdi:lightbulb-on";
2020-04-12 21:05:55 +02:00
room = "bed_room";
type = "light";
2020-04-09 16:51:39 +02:00
};
"pal03" = {
2020-04-12 21:05:55 +02:00
label = "Wohnzimmer Lampe";
2020-04-09 16:51:39 +02:00
icon = "mdi:lightbulb-on";
2020-04-12 21:05:55 +02:00
room = "living_room";
type = "light";
2020-04-09 16:51:39 +02:00
};
"pal04" = {
2020-04-13 12:50:25 +02:00
label = "Schlafzimmer Lampe Rechts";
2020-04-09 16:51:39 +02:00
icon = "mdi:lightbulb-on";
2020-04-13 12:50:25 +02:00
room = "bed_room";
2020-04-12 21:05:55 +02:00
type = "light";
2020-04-09 16:51:39 +02:00
};
"pal05" = {
label = "TV";
icon = "mdi:television";
2020-04-12 21:05:55 +02:00
room = "living_room";
type = "device";
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-09 16:51:39 +02:00
icon = "mdi:lightbulb-on";
2020-04-12 21:05:55 +02:00
room = "kitchen_room";
type = "light";
2020-04-09 16:51:39 +02:00
};
"pal07" = {
label = "Nummer 7";
icon = "mdi:power-plug-off";
};
"pal08" = {
label = "Nummer 8";
icon = "mdi:power-plug-off";
};
};
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:
{ label, icon, ... }: {
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
# ---------------
switch = builtins.map (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";
}) (builtins.attrNames sonoffSwitches);
# discover state on init
# ----------------------
automation = [{
alias = "Sonoff initial Power state";
trigger = {
platform = "homeassistant";
event = "start";
};
action = builtins.map (name: {
service = "mqtt.publish";
data = {
topic = "cmnd/${lib.toUpper name}/power";
payload = "";
};
}) (builtins.attrNames sonoffSwitches);
}];
# append to scripts
# -----------------
2020-04-08 16:43:09 +02:00
script.turn_all_off.sequence = [
{
alias = "turn off sonoff lights";
2020-04-08 16:43:09 +02:00
service = "switch.turn_off";
data.entity_id = "group.all_lights";
}
{
alias = "turn off tv";
2020-04-08 16:43:09 +02:00
service = "switch.turn_off";
data.entity_id = "group.tv";
}
];
script.turn_all_lights_off.sequence = [{
alias = "turn off sonoff lights";
service = "switch.turn_off";
2020-04-08 16:43:09 +02:00
data.entity_id = "group.all_lights";
}];
2020-04-12 14:04:15 +02:00
# append to groups
# ----------------
2020-04-12 21:05:55 +02:00
group = let
flatSonoffSwitches =
lib.mapAttrsToList (name: values: { name = name; } // values)
sonoffSwitches;
allLights' = lib.filter ({ type ? "unknown", ... }: type == "light")
flatSonoffSwitches;
allLights = {
all_lights.entities = map ({ name, ... }: "switch.${name}") allLights';
2020-04-08 16:43:09 +02:00
};
allRooms' = lib.groupBy ({ room ? "unknown", ... }: room) allLights';
allRooms = lib.mapAttrs (_: devices: {
entities = map ({ name, ... }: "switch.${name}") devices;
}) allRooms';
2020-04-12 21:05:55 +02:00
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" ]; };
2020-04-08 16:43:09 +02:00
};
};
}