############################################################################################
# PACKAGE: Xmas Tree Water Warning Light
# FILE: xmas_tree_water_warning.yaml
#
# PURPOSE:
# - Monitors a water sensor.
# - If the sensor reads FALSE (off) AND the warning light is enabled:
#   1) Blink immediately when the sensor first goes false
#   2) Continue blinking every 5 minutes while it remains false
#
# DESIGN GUARANTEE:
# - The warning light ALWAYS ends in the same state it was in
#   before the automation started.
#
# IMPORTANT:
# - Home Assistant templates cannot safely use YAML anchors inside states().
# - If you change the entity ids below, update BOTH:
#   - the anchors, and
#   - the two template strings in conditions/action variables.
############################################################################################

############################################################################################
# VARIABLES (YAML ANCHORS) - HA SAFE CONTAINER
# - We store anchors under a valid customize dictionary.
# - Customizing a non-existent entity is harmless.
############################################################################################
homeassistant:
  customize:
    sensor.xmas_tree_package_anchors:
      water_sensor_input: &water_sensor_input binary_sensor.xmas_tree_water_x32ld_water_leak
      warning_light_output: &warning_light_output switch.spare_tuya_smart_plug_zigbee_x31sp

############################################################################################
# ENABLE CONTROL
############################################################################################
input_boolean:
  enable_xmas_tree_warning_light:
    name: Enable Xmas Tree Warning Light
    icon: mdi:alarm-light-outline

############################################################################################
# MIRRORED BINARY SENSOR (AS REQUESTED)
############################################################################################
template:
  - binary_sensor:
      - name: Enable Xmas Tree Warning Light
        unique_id: enable_xmas_tree_warning_light_binary_sensor
        icon: mdi:alarm-light-outline
        state: "{{ is_state('input_boolean.enable_xmas_tree_warning_light', 'on') }}"

############################################################################################
# AUTOMATION: Flash warning light immediately and then every 5 minutes while sensor is FALSE
############################################################################################
automation:
  - id: xmas_tree_water_warning_light_flash
    alias: "Xmas Tree - Water Warning Light Flash"
    description: >
      If enabled and the water sensor reads false (off), blink immediately on transition
      to false, and also every 5 minutes while it stays false.
    mode: single
    trigger:
      # Immediate blink when the water sensor transitions to FALSE (off)
      - platform: state
        entity_id: *water_sensor_input
        to: "off"

      # Repeat reminder every 5 minutes while condition remains true
      - platform: time_pattern
        minutes: "/5"

    condition:
      # Only run if the warning feature is enabled
      - condition: state
        entity_id: input_boolean.enable_xmas_tree_warning_light
        state: "on"

      # Only run if water sensor is FALSE (off) and valid
      - condition: template
        value_template: "{{ states('binary_sensor.xmas_tree_water_x32ld_water_leak') == 'off' }}"

      # Only run if warning light output is available
      - condition: template
        value_template: "{{ states('switch.spare_tuya_smart_plug_zigbee_x31sp') not in ['unknown','unavailable'] }}"

    action:
      # Capture original state so we can restore it at the end
      - variables:
          original_state: "{{ states('switch.spare_tuya_smart_plug_zigbee_x31sp') }}"

      # Blink pattern:
      # - Toggle for 1 second
      # - Toggle back for 1 second
      # - Repeat 5 times
      - repeat:
          count: 5
          sequence:
            - service: homeassistant.toggle
              target:
                entity_id: *warning_light_output
            - delay:
                seconds: 1
            - service: homeassistant.toggle
              target:
                entity_id: *warning_light_output
            - delay:
                seconds: 1

      # Restore the original state explicitly
      - choose:
          - conditions:
              - condition: template
                value_template: "{{ original_state == 'on' }}"
            sequence:
              - service: homeassistant.turn_on
                target:
                  entity_id: *warning_light_output
        default:
          - service: homeassistant.turn_off
            target:
              entity_id: *warning_light_output
