nixos-config/configs/pepe/home-assistant/sonoff.nix
2020-04-08 16:43:09 +02:00

119 lines
2.9 KiB
Nix

{ pkgs, config, lib, ... }:
let unstablePkgs = import <nixpkgs-unstable> { };
in {
services.homeAssistantConfig = let
sonoffSwitches = {
"pal01" = {
label = "Schlafzimmer";
icon = "mdi:lightbulb-on";
};
"pal02" = {
label = "Schlafzimmer";
icon = "mdi:lightbulb-on";
};
"pal03" = {
label = "Wohnzimmer";
icon = "mdi:lightbulb-on";
};
"pal04" = {
label = "Wohnzimmer";
icon = "mdi:lightbulb-on";
};
"pal05" = {
label = "TV";
icon = "mdi:television";
};
"pal06" = {
label = "Küche";
icon = "mdi:lightbulb-on";
};
"pal07" = {
label = "Nummer 7";
icon = "mdi:power-plug-off";
};
"pal08" = {
label = "Nummer 8";
icon = "mdi:power-plug-off";
};
};
toSwitch = name: "switch.${name}";
in {
homeassistant = {
customize = lib.mapAttrs' (entity: value: {
name = toSwitch entity;
value = {
friendly_name = value.label;
icon = value.icon;
};
}) sonoffSwitches;
};
script.turn_all_off.sequence = [
{
alias = "turn off sonoff";
service = "switch.turn_off";
data.entity_id = "group.all_lights";
}
{
alias = "turn off sonoff";
service = "switch.turn_off";
data.entity_id = "group.tv";
}
];
script.turn_all_on.sequence = [{
alias = "turn on all lights";
service = "switch.turn_on";
data.entity_id = "group.all_lights";
}];
group = {
bed_room = { entities = builtins.map toSwitch [ "pal01" "pal02"]; };
living_room = { entities = builtins.map toSwitch [ "pal03" "pal04" ]; };
tv = { entities = builtins.map toSwitch [ "pal05" ]; };
kitchen = { entities = builtins.map toSwitch [ "pal06" ]; };
unknown = {
entities = builtins.map toSwitch [ "pal07" "pal08" ];
};
all_lights = {
entities = builtins.map toSwitch [ "pal01" "pal02" "pal03" "pal04" "pal05" "pal06" ];
};
};
switch = let
sonoffConfigurations = 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);
in sonoffConfigurations;
# 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);
}];
};
}