diff --git a/configs/pepe/home-assistant/sonoff.nix b/configs/pepe/home-assistant/sonoff.nix index 5cae9db..d0081ce 100644 --- a/configs/pepe/home-assistant/sonoff.nix +++ b/configs/pepe/home-assistant/sonoff.nix @@ -4,49 +4,37 @@ let sonoffSwitches = { "pal01" = { label = "Schlafzimmer Lampe Links"; - icon = "mdi:lightbulb-on"; - room = "bed_room_lights"; - type = "light"; + icon = "mdi:lightbulb"; + groups = [ "all_lights" "bed_room_lights" "bed_room" ]; }; "pal02" = { label = "Wohnzimmer Lampe"; - icon = "mdi:lightbulb-on"; - room = "living_room_lights"; - type = "light"; + icon = "mdi:lightbulb"; + groups = [ "all_lights" "living_room_lights" "living_room" ]; }; "pal03" = { label = "Wohnzimmer Lampe"; - icon = "mdi:lightbulb-on"; - room = "living_room_lights"; - type = "light"; + icon = "mdi:lightbulb"; + groups = [ "all_lights" "living_room_lights" "living_room" ]; }; "pal04" = { label = "Schlafzimmer Lampe Rechts"; - icon = "mdi:lightbulb-on"; - room = "bed_room_lights"; - type = "light"; + icon = "mdi:lightbulb"; + groups = [ "all_lights" "bed_room_lights" "bed_room" ]; }; "pal05" = { label = "TV"; icon = "mdi:television"; - room = "living_room_lights"; - type = "device"; + groups = [ "tv" "living_room" ]; device = "tv"; }; "pal06" = { label = "Küchen Lampe"; - icon = "mdi:lightbulb-on"; - room = "kitchen_room_lights"; - type = "light"; - }; - "pal07" = { - label = "Nummer 7"; - icon = "mdi:power-plug-off"; - }; - "pal08" = { - label = "Nummer 8"; - icon = "mdi:power-plug-off"; + icon = "mdi:lightbulb"; + groups = [ "all_lights" "kitchen_room_lights" "kitchen_room" ]; }; + "pal07" = { label = "Nummer 7"; }; + "pal08" = { label = "Nummer 8"; }; }; toSwitch = name: "switch.${name}"; @@ -60,7 +48,7 @@ in { # nicer names # ----------- homeassistant.customize = lib.mapAttrs' (entity: - { label, icon, ... }: { + { label, icon ? "mdi:power-plug-off", ... }: { name = toSwitch entity; value = { friendly_name = label; @@ -70,16 +58,17 @@ in { # 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); + 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; # discover state on init # ---------------------- @@ -89,56 +78,27 @@ in { 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 lights"; - service = "switch.turn_off"; - data.entity_id = "group.all_lights"; - } - { - alias = "turn off tv"; - service = "switch.turn_off"; - data.entity_id = "group.tv"; - } - ]; - script.turn_all_lights_off.sequence = [{ - alias = "turn off sonoff lights"; - service = "switch.turn_off"; - data.entity_id = "group.all_lights"; + action = lib.mapAttrsToList (name: + { ... }: { + service = "mqtt.publish"; + data = { + topic = "cmnd/${lib.toUpper name}/power"; + payload = ""; + }; + }) sonoffSwitches; }]; # append to groups # ---------------- - 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'; - }; - allRooms' = lib.groupBy ({ room ? "unknown", ... }: room) allLights'; - allRooms = lib.mapAttrs (_: devices: { - entities = map ({ name, ... }: "switch.${name}") devices; - }) allRooms'; - 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" ]; }; - }; - + # 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; }; }