diff --git a/configs/pepe/home-assistant.nix b/configs/pepe/home-assistant.nix index c25bb10..961c8da 100644 --- a/configs/pepe/home-assistant.nix +++ b/configs/pepe/home-assistant.nix @@ -52,7 +52,9 @@ in { automation = let - # todo : at night only turn trigger essential groups + allRooms = + [ "living_room" "floor_room" "bath_room" "bed_room" "kitchen_room" ]; + roomPresents = { # group of the room that should be turned on roomGroup, @@ -117,6 +119,74 @@ in { ]; } ]; + + situationSwitch = { fromRoomGroup, toRoomGroup, presentsGroup + , fromSituation, toSituation, + # brightness for dimable lights + brightness ? 255 }: [ + { + alias = + "${fromSituation} -> ${toSituation} for ${fromRoomGroup} -> ${toRoomGroup} on"; + trigger = { + platform = "state"; + entity_id = "input_select.situation"; + from = fromSituation; + to = toSituation; + }; + condition = { + condition = "state"; + entity_id = "group.${presentsGroup}"; + state = "on"; + }; + action = [ + { + service = "switch.turn_off"; + data.entity_id = "group.${fromRoomGroup}"; + } + { + service = "light.turn_off"; + data.entity_id = "group.${fromRoomGroup}"; + } + { + service = "switch.turn_on"; + data.entity_id = "group.${toRoomGroup}"; + } + { + service = "light.turn_on"; + data = { + entity_id = "group.${toRoomGroup}"; + brightness = brightness; + }; + } + ]; + } + { + alias = + "${fromSituation} -> ${toSituation} for ${fromRoomGroup} -> ${toRoomGroup} off"; + trigger = { + platform = "state"; + entity_id = "input_select.situation"; + from = fromSituation; + to = toSituation; + }; + condition = { + condition = "state"; + entity_id = "group.${presentsGroup}"; + state = "off"; + }; + action = [ + { + service = "switch.turn_off"; + data.entity_id = "group.${fromRoomGroup}"; + } + { + service = "light.turn_off"; + data.entity_id = "group.${fromRoomGroup}"; + } + ]; + } + ]; + in lib.flatten (map roomPresents (lib.flatten (map (name: [ { roomGroup = "${name}_lights"; @@ -130,8 +200,7 @@ in { situation = "essential"; brightness = 30; } - ]) [ "living_room" "floor_room" "bath_room" "bed_room" "kitchen_room" ]))) - ++ [ + ]) allRooms))) ++ [ # control situation with buttons { @@ -166,7 +235,23 @@ in { }; }; } - ]; + ] ++ (lib.flatten (map situationSwitch (lib.flatten (map (name: [ + { + presentsGroup = "${name}_present"; + fromSituation = "dark"; + toSituation = "essential"; + fromRoomGroup = "${name}_lights"; + toRoomGroup = "${name}_essential"; + brightness = 30; + } + { + presentsGroup = "${name}_present"; + fromSituation = "essential"; + toSituation = "dark"; + fromRoomGroup = "${name}_essential"; + toRoomGroup = "${name}_lights"; + } + ]) allRooms)))); group = let create_room = { name, description }: {