#:########################################################################################:#
# TITLE: UNDERHOUSE STORAGE LIGHTS AUTO LIGHTS PACKAGE
#:########################################################################################:#
# DESCRIPTION:
#   Replaces the Node-RED automation for the underhouse storage lights.
#   Doors, PIR sensors, a remote switch, and local relay changes can start or
#   extend the storage lighting timer.
#
# VERSION:
#   V1.1 2026-06-14
#     - Door-open trigger now watches each real door contact individually.
#     - This means any door changing from closed to open turns the lights on
#       immediately, even if another door is already open.
#     - All doors closed now turns the lights off immediately.
#     - PIR occupancy can still start/restart the motion timer after that.
#
#   V1.0 2026-05-12
#     - Initial Home Assistant package version.
#
# EDIT NOTES:
#   - Add future door contacts in all places marked FUTURE DOOR.
#   - Add future PIR sensors in the PIR group marked FUTURE PIR.
#   - The generated group entities used below should be:
#       binary_sensor.underhouse_storage_door_contacts
#       binary_sensor.underhouse_storage_pir_presence
#:########################################################################################:#

binary_sensor:
  - platform: group
    name: Underhouse Storage Door Contacts
    unique_id: underhouse_storage_door_contacts
    device_class: door
    all: false
    entities:
      - binary_sensor.underhouse_stealth_door_x07rs_contact
      - binary_sensor.underhouse_north_door_door
      - binary_sensor.under_house_underhouse_east_stealth_door_door

  - platform: group
    name: Underhouse Storage PIR Presence
    unique_id: underhouse_storage_pir_presence
    device_class: occupancy
    all: false
    entities:
      - binary_sensor.underhouse_pir_sensor_south_occupancy
      - binary_sensor.underhouse_pir_sensor_north_occupancy
      # FUTURE PIR: add any extra PIR sensors here.
      # - binary_sensor.underhouse_future_pir_occupancy

timer:
  underhouse_storage_lights_runtime:
    name: Underhouse Storage Lights Runtime
    duration: "01:00:00"
    restore: true
    icon: mdi:timer-outline

script:
  underhouse_storage_lights_start_runtime:
    alias: Underhouse Storage Lights - Start Or Extend Runtime
    mode: restart
    sequence:
      - choose:
          - conditions:
              - condition: state
                entity_id: switch.esp_underhouselights_underhouse_storage_lights
                state: "off"
            sequence:
              - action: switch.turn_on
                target:
                  entity_id: switch.esp_underhouselights_underhouse_storage_lights

      - action: timer.start
        target:
          entity_id: timer.underhouse_storage_lights_runtime
        data:
          duration: "00:20:00"

  underhouse_storage_lights_stop_runtime:
    alias: Underhouse Storage Lights - Stop Runtime
    mode: restart
    sequence:
      - choose:
          - conditions:
              - condition: state
                entity_id: switch.esp_underhouselights_underhouse_storage_lights
                state: "on"
            sequence:
              - action: switch.turn_off
                target:
                  entity_id: switch.esp_underhouselights_underhouse_storage_lights

      - choose:
          - conditions:
              - condition: template
                value_template: >-
                  {{ not is_state('timer.underhouse_storage_lights_runtime', 'idle') }}
            sequence:
              - action: timer.cancel
                target:
                  entity_id: timer.underhouse_storage_lights_runtime

automation:
  - id: underhouse_storage_lights_start_extend_runtime
    alias: Underhouse Storage Lights - Start Or Extend Runtime
    description: >-
      Turns the storage lights on and starts or restarts the timer when
      an individual door opens, PIR detects occupancy, or the remote switch
      is pressed.
    mode: restart
    trigger:
      - platform: state
        entity_id:
          - binary_sensor.underhouse_stealth_door_x07rs_contact
          - binary_sensor.underhouse_north_door_door
          # FUTURE DOOR: replace this with the actual third underhouse storage door contact.
          #- binary_sensor.underhouse_future_door_contact
        from: "off"
        to: "on"

      - platform: state
        entity_id: binary_sensor.underhouse_storage_pir_presence
        to: "on"

      - platform: state
        entity_id: binary_sensor.esp_underhouselights_underhouse_storage_lights_remote_switch
        to: "on"

    action:
      - action: script.underhouse_storage_lights_start_runtime

  - id: underhouse_storage_lights_start_timer_when_relay_turned_on
    alias: Underhouse Storage Lights - Start Timer When Relay Turned On
    description: >-
      Starts the timer if the relay is turned on locally by ESPHome or
      by another Home Assistant automation.
    mode: restart
    trigger:
      - platform: state
        entity_id: switch.esp_underhouselights_underhouse_storage_lights
        to: "on"
    action:
      - action: timer.start
        target:
          entity_id: timer.underhouse_storage_lights_runtime
        data:
          duration: "00:20:00"

  - id: underhouse_storage_lights_timer_finished
    alias: Underhouse Storage Lights - Timer Finished
    description: >-
      Turns the storage lights off after the runtime expires, even if a
      door has been left open.
    mode: single
    trigger:
      - platform: event
        event_type: timer.finished
        event_data:
          entity_id: timer.underhouse_storage_lights_runtime
    action:
      - choose:
          - conditions:
              - condition: state
                entity_id: switch.esp_underhouselights_underhouse_storage_lights
                state: "on"
            sequence:
              - action: switch.turn_off
                target:
                  entity_id: switch.esp_underhouselights_underhouse_storage_lights

  - id: underhouse_storage_lights_cancel_timer_when_relay_off
    alias: Underhouse Storage Lights - Cancel Timer When Relay Off
    description: >-
      Keeps the timer helper tidy if the relay is turned off manually, locally,
      or by another automation.
    mode: restart
    trigger:
      - platform: state
        entity_id: switch.esp_underhouselights_underhouse_storage_lights
        to: "off"
    condition:
      - condition: template
        value_template: >-
          {{ not is_state('timer.underhouse_storage_lights_runtime', 'idle') }}
    action:
      - action: timer.cancel
        target:
          entity_id: timer.underhouse_storage_lights_runtime

  - id: underhouse_storage_lights_all_doors_closed
    alias: Underhouse Storage Lights - All Doors Closed
    description: >-
      When all door contacts are closed, turn the lights off immediately.
      PIR occupancy can still turn the lights back on again afterwards.
    mode: restart
    trigger:
      - platform: state
        entity_id: binary_sensor.underhouse_storage_door_contacts
        from: "on"
        to: "off"
    action:
      - action: script.underhouse_storage_lights_stop_runtime

  - id: underhouse_storage_lights_individual_door_closed_check
    alias: Underhouse Storage Lights - Individual Door Closed Safety Check
    description: >-
      Safety cleanup. When a real door contact closes, wait 60 seconds and then
      ensure the relay is off only if all doors are closed and there is no PIR
      occupancy. The normal all-doors-closed automation above should usually
      turn the lights off immediately first.
    mode: restart
    trigger:
      - platform: state
        entity_id:
          - binary_sensor.underhouse_stealth_door_x07rs_contact
          - binary_sensor.underhouse_north_door_door
          # FUTURE DOOR: replace this with the actual third underhouse storage door contact.
          #- binary_sensor.underhouse_future_door_contact
        from: "on"
        to: "off"
    action:
      - delay: "00:01:00"
      - condition: state
        entity_id: binary_sensor.underhouse_storage_door_contacts
        state: "off"
      - condition: state
        entity_id: binary_sensor.underhouse_storage_pir_presence
        state: "off"
      - condition: state
        entity_id: switch.esp_underhouselights_underhouse_storage_lights
        state: "on"
      - action: script.underhouse_storage_lights_stop_runtime

  - id: underhouse_storage_lights_pir_clear_after_doors_closed
    alias: Underhouse Storage Lights - PIR Clear After Doors Closed
    description: >-
      If the doors are closed and PIR occupancy clears for 60 seconds, turn the
      storage lights off. This is mainly a cleanup path for PIR-triggered use.
    mode: restart
    trigger:
      - platform: state
        entity_id: binary_sensor.underhouse_storage_pir_presence
        to: "off"
        for: "00:01:00"
    condition:
      - condition: state
        entity_id: binary_sensor.underhouse_storage_door_contacts
        state: "off"
      - condition: state
        entity_id: switch.esp_underhouselights_underhouse_storage_lights
        state: "on"
    action:
      - action: script.underhouse_storage_lights_stop_runtime

  - id: underhouse_storage_lights_startup_sync
    alias: Underhouse Storage Lights - Startup Sync
    description: >-
      On Home Assistant startup, re-sync the package state so an already-open
      door or already-active PIR still controls the lights after a restart.
    mode: single
    trigger:
      - platform: homeassistant
        event: start
    action:
      - delay: "00:00:20"
      - choose:
          - conditions:
              - condition: template
                value_template: >-
                  {{
                    is_state('binary_sensor.underhouse_storage_door_contacts', 'on')
                    or is_state('binary_sensor.underhouse_storage_pir_presence', 'on')
                    or is_state('binary_sensor.esp_underhouselights_underhouse_storage_lights_remote_switch', 'on')
                  }}
            sequence:
              - action: script.underhouse_storage_lights_start_runtime

          - conditions:
              - condition: state
                entity_id: switch.esp_underhouselights_underhouse_storage_lights
                state: "on"
              - condition: state
                entity_id: timer.underhouse_storage_lights_runtime
                state: "idle"
            sequence:
              - action: timer.start
                target:
                  entity_id: timer.underhouse_storage_lights_runtime
                data:
                  duration: "00:20:00"
