#:########################################################################################:#
# Garage Corner Light CCTV Motion Package                                                 #
#:########################################################################################:#
#
# TITLE:
#   Garage Corner Light CCTV Motion
#
# FILE:
#   packages/garage_corner_light_cctv_motion.yaml
#
# VERSION:
#   V1.0 2026-06-29
#
# VERSION HISTORY:
#   V1.0 2026-06-29
#     - Initial package.
#     - Turns the garage external corner light on when CCTV motion MQTT payload "1" is received.
#     - Uses a configurable timer, default 6 minutes.
#     - Any new CCTV motion restarts the timer.
#     - Adds a Home Assistant enable switch for CCTV movement triggers.
#
# PURPOSE:
#   Turns on the garage external corner light from selected CCTV motion MQTT triggers.
#
# NOTES:
#   - The light is:
#       switch.esp_garageextcornerlight_garage_external_light
#
#   - CCTV movement MQTT payload is expected to be:
#       "1"
#
#   - Turning OFF "Garage corner light movement triggers" prevents new CCTV triggers.
#   - If a timer is already running, it will still finish and turn the light off.
#
#:########################################################################################:#
# User Configuration                                                                      #
#:########################################################################################:#

input_number:
  garage_corner_light_motion_timer_minutes:
    name: "Garage Corner Light Motion Timer"
    icon: mdi:timer-outline
    min: 1
    max: 30
    step: 1
    mode: box
    unit_of_measurement: "min"
    initial: 6

input_boolean:
  garage_corner_light_movement_triggers:
    name: "Garage corner light movement triggers"
    icon: mdi:cctv
    initial: true

timer:
  garage_corner_light_motion_timer:
    name: "Garage Corner Light Motion Timer"
    icon: mdi:timer-outline
    duration: "00:06:00"

#:########################################################################################:#
# Automations                                                                             #
#:########################################################################################:#

automation:
  - id: garage_corner_light_cctv_motion_turn_on
    alias: "Garage Corner Light → CCTV motion turns light on"
    description: >
      Turns the garage corner light on when any configured CCTV motion MQTT topic
      receives payload "1". Each new movement restarts the timer.
    mode: restart
    trigger:
      - platform: mqtt
        topic: "viewroad-status/cctv/garage_camera_motion"
        payload: "1"
        id: garage_camera

      - platform: mqtt
        topic: "viewroad-status/cctv/caravan_camera_motion"
        payload: "1"
        id: caravan_camera

      - platform: mqtt
        topic: "viewroad-status/cctv/driveway_camera_motion"
        payload: "1"
        id: driveway_camera

    condition:
      - condition: state
        entity_id: input_boolean.garage_corner_light_movement_triggers
        state: "on"

    action:
      - service: switch.turn_on
        target:
          entity_id: switch.esp_garageextcornerlight_garage_external_light

      - service: timer.start
        target:
          entity_id: timer.garage_corner_light_motion_timer
        data:
          duration: >
            {% set total_minutes = states('input_number.garage_corner_light_motion_timer_minutes') | int(6) %}
            {% set hours = total_minutes // 60 %}
            {% set minutes = total_minutes % 60 %}
            {{ '%02d:%02d:00' | format(hours, minutes) }}

  - id: garage_corner_light_cctv_motion_timer_finished
    alias: "Garage Corner Light → CCTV motion timer finished"
    description: >
      Turns the garage corner light off when the CCTV motion timer finishes.
    mode: single
    trigger:
      - platform: event
        event_type: timer.finished
        event_data:
          entity_id: timer.garage_corner_light_motion_timer

    action:
      - service: switch.turn_off
        target:
          entity_id: switch.esp_garageextcornerlight_garage_external_light
