145 lines
3.5 KiB
Nix
145 lines
3.5 KiB
Nix
{ pkgs, config, lib, ... }:
|
|
let
|
|
|
|
sonoffSwitches = {
|
|
"pal01" = {
|
|
label = "Schlafzimmer Lampe";
|
|
icon = "mdi:lightbulb-on";
|
|
room = "bed_room";
|
|
type = "light";
|
|
};
|
|
"pal02" = {
|
|
label = "Schlafzimmer Lampe";
|
|
icon = "mdi:lightbulb-on";
|
|
room = "bed_room";
|
|
type = "light";
|
|
};
|
|
"pal03" = {
|
|
label = "Wohnzimmer Lampe";
|
|
icon = "mdi:lightbulb-on";
|
|
room = "living_room";
|
|
type = "light";
|
|
};
|
|
"pal04" = {
|
|
label = "Wohnzimmer Lampe";
|
|
icon = "mdi:lightbulb-on";
|
|
room = "living_room";
|
|
type = "light";
|
|
};
|
|
"pal05" = {
|
|
label = "TV";
|
|
icon = "mdi:television";
|
|
room = "living_room";
|
|
type = "device";
|
|
device = "tv";
|
|
};
|
|
"pal06" = {
|
|
label = "Küchen Lampe";
|
|
icon = "mdi:lightbulb-on";
|
|
room = "kitchen_room";
|
|
type = "light";
|
|
};
|
|
"pal07" = {
|
|
label = "Nummer 7";
|
|
icon = "mdi:power-plug-off";
|
|
};
|
|
"pal08" = {
|
|
label = "Nummer 8";
|
|
icon = "mdi:power-plug-off";
|
|
};
|
|
};
|
|
|
|
toSwitch = name: "switch.${name}";
|
|
|
|
in {
|
|
|
|
imports = [ ./mqtt.nix ];
|
|
|
|
services.homeAssistantConfig = {
|
|
|
|
# nicer names
|
|
# -----------
|
|
homeassistant.customize = lib.mapAttrs' (entity:
|
|
{ label, icon, ... }: {
|
|
name = toSwitch entity;
|
|
value = {
|
|
friendly_name = label;
|
|
icon = icon;
|
|
};
|
|
}) sonoffSwitches;
|
|
|
|
# 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
|
|
# -----------------
|
|
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";
|
|
}];
|
|
|
|
# append to groups
|
|
# ----------------
|
|
|
|
group = let
|
|
flatSonoffSwitches =
|
|
lib.mapAttrsToList (name: values: { name = name; } // values)
|
|
sonoffSwitches;
|
|
allRooms' =
|
|
lib.groupBy ({ room ? "unknown", ... }: room) flatSonoffSwitches;
|
|
allRooms = lib.mapAttrs (_: devices: {
|
|
entities = map ({ name, ... }: "switch.${name}") devices;
|
|
}) allRooms';
|
|
allLights' = lib.filter ({ type ? "unknown", ... }: type == "light")
|
|
flatSonoffSwitches;
|
|
allLights = {
|
|
all_lights.entities = map ({ name, ... }: "switch.${name}") allLights';
|
|
};
|
|
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" ]; };
|
|
};
|
|
|
|
};
|
|
|
|
}
|