binary_sensor:
  - platform: template
    trigger_on_initial_state: true
    id: binary_sensor_some_binary_sensor
    name: "Random binary"
    lambda: return (random_uint32() & 1) == 0;
    filters:
      - invert:
      - delayed_on: 100ms
      - delayed_off: 100ms
      # Templated, delays for 1s (1000ms) only if a reed switch is active
      - delayed_on_off: !lambda "return 1000;"
      - delayed_on_off:
          time_on: 10s
          time_off: !lambda "return 1000;"
      - autorepeat:
          - delay: 1s
            time_off: 100ms
            time_on: 900ms
          - delay: 5s
            time_off: 100ms
            time_on: 400ms
      - lambda: |-
          if (id(binary_sensor_some_binary_sensor).state) {
            return x;
          }
          return {};
      - settle: 100ms
      - timeout: 10s

    on_state_change:
      then:
        - logger.log:
            format: "Old state was %s"
            args: ['x_previous.has_value() ? ONOFF(x_previous) : "Unknown"']
        - logger.log:
            format: "New state is %s"
            args: ['x.has_value() ? ONOFF(x) : "Unknown"']
        - binary_sensor.invalidate_state: binary_sensor_some_binary_sensor

  # Test autorepeat with default configuration (no timings)
  - platform: template
    id: autorepeat_default
    name: "Autorepeat Default"
    filters:
      - autorepeat:

  # Test autorepeat with single timing entry
  - platform: template
    id: autorepeat_single
    name: "Autorepeat Single"
    filters:
      - autorepeat:
          - delay: 2s
            time_off: 200ms
            time_on: 800ms

  # Test autorepeat with three timing entries
  - platform: template
    id: autorepeat_multiple
    name: "Autorepeat Multiple"
    filters:
      - autorepeat:
          - delay: 500ms
            time_off: 50ms
            time_on: 950ms
          - delay: 2s
            time_off: 100ms
            time_on: 900ms
          - delay: 10s
            time_off: 200ms
            time_on: 800ms

  # Test on_multi_click with single click
  - platform: template
    id: multi_click_single
    name: "Multi Click Single"
    on_multi_click:
      - timing:
          - state: true
            min_length: 50ms
            max_length: 350ms
        then:
          - logger.log: "Single click detected"

  # Test on_multi_click with double click
  - platform: template
    id: multi_click_double
    name: "Multi Click Double"
    on_multi_click:
      - timing:
          - state: true
            min_length: 50ms
            max_length: 350ms
          - state: false
            min_length: 50ms
            max_length: 350ms
          - state: true
            min_length: 50ms
            max_length: 350ms
        then:
          - logger.log: "Double click detected"

  # Test on_multi_click with complex pattern (5 events)
  - platform: template
    id: multi_click_complex
    name: "Multi Click Complex"
    on_multi_click:
      - timing:
          - state: true
            min_length: 50ms
            max_length: 350ms
          - state: false
            min_length: 50ms
            max_length: 350ms
          - state: true
            min_length: 50ms
            max_length: 350ms
          - state: false
            min_length: 50ms
            max_length: 350ms
          - state: true
            min_length: 50ms
        then:
          - logger.log: "Complex pattern detected"

  # Test on_multi_click with custom invalid_cooldown
  - platform: template
    id: multi_click_cooldown
    name: "Multi Click Cooldown"
    on_multi_click:
      - timing:
          - state: true
            min_length: 100ms
            max_length: 500ms
        invalid_cooldown: 2s
        then:
          - logger.log: "Click with custom cooldown"
