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

105 lines
2.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" = {
# label = "Schlafzimmer Lampe Links";
# icon = "mdi:lightbulb";
# groups = [ "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" = {
# label = "Wohnzimmer Lampe";
# icon = "mdi:lightbulb";
# groups = [ "living_room" ];
#};
#"pal04" = {
# label = "Schlafzimmer Lampe Rechts";
# icon = "mdi:lightbulb";
# groups = [ "bed_room" ];
#};
2020-04-09 16:51:39 +02:00
"pal05" = {
label = "TV";
icon = "mdi:television";
groups = [ "tv" ];
2020-04-12 21:05:55 +02:00
device = "tv";
2020-04-09 16:51:39 +02:00
};
#"pal06" = {
# label = "Küchen Lampe";
# icon = "mdi:lightbulb";
# groups = ["kitchen_room"];
#};
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
};
}