esphome:
  name: uart-mock-ld2410-test

host:
api:
  batch_delay: 0ms  # Disable batching to receive all state updates
logger:
  level: VERBOSE

external_components:
  - source:
      type: local
      path: EXTERNAL_COMPONENT_PATH

# Dummy uart entry to satisfy ld2410's DEPENDENCIES = ["uart"]
# The actual UART bus used is the uart_mock component below
uart:
  baud_rate: 115200
  port: /dev/null

uart_mock:
  id: mock_uart
  baud_rate: 256000
  auto_start: false
  injections:
    # Phase 1 (t=100ms): Valid LD2410 normal mode data frame - happy path
    # The buffer is clean at this point, so this frame should parse correctly.
    # Moving target: 100cm, energy 50
    # Still target: 120cm, energy 25
    # Detection distance: 300cm
    # Target state: 0x03 (moving + still)
    #
    # Note: LD2410's two_byte_to_int() uses signed char, so low bytes must be
    # <=127 to produce correct values. Values >127 wrap negative.
    #
    # Frame layout (24 bytes):
    #   [0-3]  F4 F3 F2 F1 = data frame header
    #   [4-5]  0D 00       = length 13
    #   [6]    02          = data type (normal)
    #   [7]    AA          = data header marker
    #   [8]    03          = target states (moving+still)
    #   [9-10] 64 00       = moving distance 100 (0x0064)
    #   [11]   32          = moving energy 50
    #   [12-13] 78 00      = still distance 120 (0x0078)
    #   [14]   19          = still energy 25
    #   [15-16] 2C 01      = detection distance 300 (0x012C)
    #   [17]   00          = padding
    #   [18]   55          = data footer marker
    #   [19]   00          = CRC/check
    #   [20-23] F8 F7 F6 F5 = data frame footer
    - delay: 100ms
      inject_rx:
        [
          0xF4, 0xF3, 0xF2, 0xF1,
          0x0D, 0x00,
          0x02, 0xAA,
          0x03,
          0x64, 0x00,
          0x32,
          0x78, 0x00,
          0x19,
          0x2C, 0x01,
          0x00,
          0x55, 0x00,
          0xF8, 0xF7, 0xF6, 0xF5,
        ]

    # Phase 2 (t=200ms): Garbage bytes - corrupt the buffer
    # These random bytes will accumulate in the LD2410's internal buffer.
    # No footer will be found, so the buffer just grows.
    - delay: 100ms
      inject_rx: [0xDE, 0xAD, 0xBE, 0xEF, 0x00, 0x11, 0x22]

    # Phase 3 (t=300ms): Truncated frame (header + partial data, no footer)
    # More bytes accumulating in the buffer without a footer match.
    - delay: 100ms
      inject_rx: [0xF4, 0xF3, 0xF2, 0xF1, 0x0D, 0x00, 0x02, 0xAA]

    # Phase 4 (t=500ms): Overflow - inject 85 bytes of 0xFF (MAX_LINE_LENGTH=50)
    # Buffer has 15 bytes from phases 2+3.
    # Overflow math: need 35 bytes to trigger first overflow (pos 15->49),
    # then 50 more to trigger second overflow (pos 0->49). Total = 85 bytes.
    # After two overflows, buffer_pos_ = 0 with a completely clean buffer.
    # The LD2410 logs "Max command length exceeded" at each overflow.
    - delay: 200ms
      inject_rx:
        [
          0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
          0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
          0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
          0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
          0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
          0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
          0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
          0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
          0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
        ]

    # Phase 5 (t=600ms): Valid frame after overflow - recovery test
    # Buffer was reset by overflow. This valid frame should parse correctly.
    # Moving target: 50cm, energy 100
    # Still target: 75cm, energy 80
    # Detection distance: 127cm
    - delay: 100ms
      inject_rx:
        [
          0xF4, 0xF3, 0xF2, 0xF1,
          0x0D, 0x00,
          0x02, 0xAA,
          0x03,
          0x32, 0x00,
          0x64,
          0x4B, 0x00,
          0x50,
          0x7F, 0x00,
          0x00,
          0x55, 0x00,
          0xF8, 0xF7, 0xF6, 0xF5,
        ]

ld2410:
  id: ld2410_dev
  uart_id: mock_uart

sensor:
  - platform: ld2410
    ld2410_id: ld2410_dev
    moving_distance:
      name: "Moving Distance"
    still_distance:
      name: "Still Distance"
    moving_energy:
      name: "Moving Energy"
    still_energy:
      name: "Still Energy"
    detection_distance:
      name: "Detection Distance"

binary_sensor:
  - platform: ld2410
    ld2410_id: ld2410_dev
    has_target:
      name: "Has Target"
    has_moving_target:
      name: "Has Moving Target"
    has_still_target:
      name: "Has Still Target"

button:
  - platform: template
    name: "Start Scenario"
    id: start_scenario_btn
    on_press:
      - lambda: 'id(mock_uart).start_scenario();'
