#:########################################################################################:#
# Package: Underhouse Entrance Auto Lights
# File:    auto_lights_underhouse_entrance.yaml
# Version: v1.2.0
# Date:    2026-05-12
#
# Purpose:
#   Replaces the Node-RED automation for the underhouse entrance lights. Uses
#   Zigbee2MQTT mmWave event triggers (enter/approach/presence) to turn lights
#   on instantly, then off after 400 seconds of sustained clear presence.
#
# Behaviour:
#   - Fast Zigbee2MQTT FP1-style event triggers (enter, approach) turn lights on.
#   - Binary presence sensor serves as a backup trigger and clearance condition.
#   - Lights turn off 400 seconds after presence clears.
#
# Version History:
#   - v1.2.0 (2026-05-12): Added fast Zigbee2MQTT FP1-style event triggers and
#     converted header to standard package format.
#
#:########################################################################################:#

automation:
  - id: underhouse_entrance_auto_lights
    alias: "Underhouse Entrance - Auto Lights"
    description: "Turns underhouse entrance lights on from Zigbee2MQTT mmWave enter/approach/presence, then off 400 seconds after presence clears."
    mode: restart
    max_exceeded: silent

    trigger:
      - platform: state
        entity_id: sensor.underhouse_mmwave_sensor_x23ms_presence_event
        to: "enter"

      - platform: state
        entity_id: sensor.underhouse_mmwave_sensor_x23ms_presence_event
        to: "approach"

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

    action:
      - alias: "Turn on the underhouse entrance lights"
        action: switch.turn_on
        target:
          entity_id: switch.esp_underhouselights_underhouse_entrance_lights

      - alias: "Wait until the mmWave presence sensor is clear"
        wait_template: >
          {{ is_state('binary_sensor.underhouse_mmwave_sensor_x23ms_presence', 'off') }}

      - alias: "Keep lights on for 400 seconds after presence clears"
        delay:
          seconds: 400

      - alias: "Only continue if presence is still clear"
        condition: state
        entity_id: binary_sensor.underhouse_mmwave_sensor_x23ms_presence
        state: "off"

      - alias: "Turn off the underhouse entrance lights"
        action: switch.turn_off
        target:
          entity_id: switch.esp_underhouselights_underhouse_entrance_lights
