###############################################################################
# PACKAGE: Downstairs Flat Light Control
###############################################################################
#
# VERSION HISTORY
#
# 2026-05-07 - v1.0.2
#   - Added binary_sensor.downstairs_flat_any_lights_on.
#   - Updated all-on/all-off script to use the new binary sensor.
#   - Kept direct entity lists instead of using a Home Assistant group, to avoid
#     package merge errors from duplicate group/name keys.
#
# 2026-05-07 - v1.0.1
#   - Removed group.downstairs_flat_lights from this package to avoid duplicate
#     group/name package merge errors.
#   - Changed all-on/all-off scripts to use direct entity lists instead.
#
# 2026-05-07 - v1.0.0
#   - Tidied original package.
#   - Added package header and comments.
#   - Added internal helper script to avoid repeating the same occupancy logic.
#
###############################################################################
#
# OPERATION NOTES
#
# - input_boolean.downstairs_flat_occupied is the safety/lockout helper.
#
# - When downstairs_flat_occupied is ON:
#     - All scripts in this package do nothing.
#     - This prevents upstairs controls from changing downstairs guest lights.
#
# - When downstairs_flat_occupied is OFF:
#     - Individual scripts toggle their matching light switch.
#     - downstairs_lights_all_off only ever turns the listed lights OFF.
#     - downstairs_light_toggle_all_group turns all listed lights OFF if any are ON.
#     - downstairs_light_toggle_all_group turns all listed lights ON if they are all OFF.
#
# - binary_sensor.downstairs_flat_any_lights_on:
#     - ON when any listed downstairs light is ON.
#     - OFF when all listed downstairs lights are OFF.
#     - This replaces the need for a group.downstairs_flat_lights entity.
#
###############################################################################

###############################################################################
# INPUT BOOLEANS
###############################################################################

input_boolean:
  downstairs_flat_occupied:
    name: Downstairs Guest Occupied
    icon: mdi:home-account

###############################################################################
# TEMPLATE SENSORS
###############################################################################

template:
  - binary_sensor:
      - name: Downstairs Flat Any Lights On
        unique_id: downstairs_flat_any_lights_on
        icon: mdi:lightbulb-group
        state: >
          {{
            is_state('switch.esp_downstloungemain_relay_1_main_lights', 'on')
            or is_state('switch.esp_downstloungemain_relay_2_back_wall_lights', 'on')
            or is_state('switch.esp_downstmasterbedrmlights_relay_1_main_lights', 'on')
            or is_state('switch.esp_downstbedrm2lights_relay_1_main_lights', 'on')
            or is_state('switch.esp_downstkitchlights_relay_2_kitchen_light', 'on')
            or is_state('switch.esp_downstkitchlights_relay_1_dining_light', 'on')
            or is_state('switch.esp_downstbathswitch_relay_1_main_lights', 'on')
            or is_state('switch.esp_downstbathswitch_relay_2_cabinet_light', 'on')
          }}

###############################################################################
# SCRIPTS
###############################################################################

script:
  downstairs_lights_all_off:
    alias: Downstairs Lights - All Off
    mode: single
    sequence:
      - condition: state
        entity_id: input_boolean.downstairs_flat_occupied
        state: "off"

      - action: homeassistant.turn_off
        target:
          entity_id:
            - switch.esp_downstloungemain_relay_1_main_lights
            - switch.esp_downstloungemain_relay_2_back_wall_lights
            - switch.esp_downstmasterbedrmlights_relay_1_main_lights
            - switch.esp_downstbedrm2lights_relay_1_main_lights
            - switch.esp_downstkitchlights_relay_2_kitchen_light
            - switch.esp_downstkitchlights_relay_1_dining_light
            - switch.esp_downstbathswitch_relay_1_main_lights
            - switch.esp_downstbathswitch_relay_2_cabinet_light

  downstairs_light_toggle_entity:
    alias: Downstairs Light Toggle - Internal Entity Toggle
    mode: queued
    max: 10
    fields:
      target_switch:
        name: Target Switch
        description: The downstairs switch entity to toggle.
        required: true
        selector:
          entity:
            domain: switch
    sequence:
      - condition: state
        entity_id: input_boolean.downstairs_flat_occupied
        state: "off"

      - action: switch.toggle
        target:
          entity_id: "{{ target_switch }}"

  downstairs_light_toggle_lounge_main:
    alias: Downstairs Light Toggle - Lounge Main Lights
    mode: queued
    max: 10
    sequence:
      - action: script.downstairs_light_toggle_entity
        data:
          target_switch: switch.esp_downstloungemain_relay_1_main_lights

  downstairs_light_toggle_lounge_back_wall:
    alias: Downstairs Light Toggle - Lounge Back Wall Lights
    mode: queued
    max: 10
    sequence:
      - action: script.downstairs_light_toggle_entity
        data:
          target_switch: switch.esp_downstloungemain_relay_2_back_wall_lights

  downstairs_light_toggle_main_bedroom_main:
    alias: Downstairs Light Toggle - Main Bedroom Main Lights
    mode: queued
    max: 10
    sequence:
      - action: script.downstairs_light_toggle_entity
        data:
          target_switch: switch.esp_downstmasterbedrmlights_relay_1_main_lights

  downstairs_light_toggle_small_bedroom_main:
    alias: Downstairs Light Toggle - Small Bedroom Main Lights
    mode: queued
    max: 10
    sequence:
      - action: script.downstairs_light_toggle_entity
        data:
          target_switch: switch.esp_downstbedrm2lights_relay_1_main_lights

  downstairs_light_toggle_kitchen:
    alias: Downstairs Light Toggle - Kitchen Lights
    mode: queued
    max: 10
    sequence:
      - action: script.downstairs_light_toggle_entity
        data:
          target_switch: switch.esp_downstkitchlights_relay_2_kitchen_light

  downstairs_light_toggle_dining:
    alias: Downstairs Light Toggle - Dining Lights
    mode: queued
    max: 10
    sequence:
      - action: script.downstairs_light_toggle_entity
        data:
          target_switch: switch.esp_downstkitchlights_relay_1_dining_light

  downstairs_light_toggle_bathroom_main:
    alias: Downstairs Light Toggle - Bathroom Main Lights
    mode: queued
    max: 10
    sequence:
      - action: script.downstairs_light_toggle_entity
        data:
          target_switch: switch.esp_downstbathswitch_relay_1_main_lights

  downstairs_light_toggle_bathroom_cabinet:
    alias: Downstairs Light Toggle - Bathroom Cabinet Lights
    mode: queued
    max: 10
    sequence:
      - action: script.downstairs_light_toggle_entity
        data:
          target_switch: switch.esp_downstbathswitch_relay_2_cabinet_light

  downstairs_light_toggle_all_group:
    alias: Downstairs Lights - All On/Off
    mode: single
    sequence:
      - condition: state
        entity_id: input_boolean.downstairs_flat_occupied
        state: "off"

      - choose:
          - conditions:
              - condition: state
                entity_id: binary_sensor.downstairs_flat_any_lights_on
                state: "on"
            sequence:
              - action: homeassistant.turn_off
                target:
                  entity_id:
                    - switch.esp_downstloungemain_relay_1_main_lights
                    - switch.esp_downstloungemain_relay_2_back_wall_lights
                    - switch.esp_downstmasterbedrmlights_relay_1_main_lights
                    - switch.esp_downstbedrm2lights_relay_1_main_lights
                    - switch.esp_downstkitchlights_relay_2_kitchen_light
                    - switch.esp_downstkitchlights_relay_1_dining_light
                    - switch.esp_downstbathswitch_relay_1_main_lights
                    - switch.esp_downstbathswitch_relay_2_cabinet_light

        default:
          - action: homeassistant.turn_on
            target:
              entity_id:
                - switch.esp_downstloungemain_relay_1_main_lights
                - switch.esp_downstloungemain_relay_2_back_wall_lights
                - switch.esp_downstmasterbedrmlights_relay_1_main_lights
                - switch.esp_downstbedrm2lights_relay_1_main_lights
                - switch.esp_downstkitchlights_relay_2_kitchen_light
                - switch.esp_downstkitchlights_relay_1_dining_light
                - switch.esp_downstbathswitch_relay_1_main_lights
                - switch.esp_downstbathswitch_relay_2_cabinet_light
