################################################################################
# PACKAGE: Downstairs Lights Bulk Activation
# FILE: packages/downstairs_bulk_lights_on_from_stairs.yaml
#
# Version: 1.1
# Date: 2026-07-20
#
# Notes:
# - Uses the virtual switch:
#     switch.central_stair_lightswitch_top_2_downstairs_lights_bulk_activation
#
# - When the virtual switch turns ON:
#     Turns ON the listed downstairs lights, but only if each light is currently OFF.
#
# - When the virtual switch turns OFF:
#     Turns OFF the listed downstairs lights, but only if each light is currently ON.
#
# - No action is taken unless:
#     input_boolean.downstairs_flat_occupied is OFF.
################################################################################

automation:
  - id: downstairs_lights_bulk_activation
    alias: "Downstairs Lights Bulk Activation"
    description: >
      V1.1 - 2026-07-20: Bulk ON/OFF control for downstairs lights from the
      central stair lightswitch virtual switch. Only operates when the downstairs
      flat is not occupied.

    mode: single

    trigger:
      - platform: state
        entity_id: switch.central_stair_lightswitch_top_2_downstairs_lights_bulk_activation
        to:
          - "on"
          - "off"

    condition:
      - condition: state
        entity_id: input_boolean.downstairs_flat_occupied
        state: "off"

    action:
      - choose:
          ######################################################################
          # BULK ACTIVATION ON:
          # Turn lights ON, but only where they are currently OFF.
          ######################################################################
          - conditions:
              - condition: state
                entity_id: switch.central_stair_lightswitch_top_2_downstairs_lights_bulk_activation
                state: "on"
            sequence:
              - repeat:
                  for_each:
                    - switch.esp_centralstairs_bottom_relay_1_main_stair_lights_lower
                    - switch.esp_downstloungemain_relay_1_main_lights
                    - switch.esp_downstkitchlights_relay_2_kitchen_light
                    - switch.esp_downstkitchlights_relay_1_dining_light
                  sequence:
                    - condition: template
                      value_template: "{{ is_state(repeat.item, 'off') }}"
                    - service: switch.turn_on
                      target:
                        entity_id: "{{ repeat.item }}"

          ######################################################################
          # BULK ACTIVATION OFF:
          # Turn lights OFF, but only where they are currently ON.
          ######################################################################
          - conditions:
              - condition: state
                entity_id: switch.central_stair_lightswitch_top_2_downstairs_lights_bulk_activation
                state: "off"
            sequence:
              - repeat:
                  for_each:
                    - switch.esp_centralstairs_bottom_relay_1_main_stair_lights_lower
                    - switch.esp_downstloungemain_relay_1_main_lights
                    - switch.esp_downstkitchlights_relay_2_kitchen_light
                    - switch.esp_downstkitchlights_relay_1_dining_light
                  sequence:
                    - condition: template
                      value_template: "{{ is_state(repeat.item, 'on') }}"
                    - service: switch.turn_off
                      target:
                        entity_id: "{{ repeat.item }}"
