# Onkyo RI receive test rig
#
# Bench validation for the `onkyori` dumper and the `on_onkyori:` trigger.
# Standalone: there is no `api:` block and nothing here talks to Home
# Assistant. Companion to ri-transmit-test.yaml, which transmits.
#
#   esphome run ri-receive-test.yaml
#
# Wiring: RI is 5V TTL on a 3.5mm mono jack, tip is data and sleeve is ground.
# ESP32 pins are NOT 5V tolerant, so bring the tip down through a 10k/20k
# divider to ground (5V * 20k/30k = 3.33V). Use a divider, not a BSS138 board,
# which is never high-impedance and would hold a bus it only listens to.
#
# Measure tip to sleeve before wiring, amp on and nothing else plugged in. ~0V
# is the expected idle. ~5V means pulses are active-low: set `inverted: true`.
#
# Read `dump: raw` before `dump: onkyori`, or a silent decoder cannot be told
# from wrong polarity, bad wiring, or an amp that never transmitted.
#
# Not every button emits. The A-9050 sends power (0x0EA off, 0x0EF on ~5 s
# after the press), input select for RI-bound inputs, a dimmer counter, and the
# remote's Dock/Tuner section. Volume, mute, tone and the CD section send
# nothing, which is the usual reason a receive test looks broken.

substitutions:
  device_name: onkyo-ri-receive-test
  # A separate pin from ri-transmit-test.yaml's transmit pin, so the two rigs
  # differ by a reflash rather than a re-wire. That is a firmware convenience
  # only: the two paths still must not share the RI wire.
  ri_rx_pin: GPIO16

esphome:
  name: ${device_name}
  friendly_name: "Onkyo RI Receive"
  comment: "Bench test rig for the Onkyo RI decode protocol."

esp32:
  board: esp32dev
  framework:
    type: esp-idf

logger:
  # The dumpers log at INFO, but DEBUG catches the component's own chatter too.
  level: DEBUG

ota:
  - platform: esphome
    password: !secret ota_password

wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_password

external_components:
  # Onkyo RI is not in any ESPHome release yet. Remove this once #18595 merges.
  - source: github://pr#18595
    components: [remote_base]

remote_receiver:
  pin:
    number: ${ri_rx_pin}
    # No internal pull-up. Whatever defines the idle level lives outside the
    # ESP32 — the divider, or the amp itself.
    mode:
      input: true
      pullup: false
    # Flip to true if the raw dump comes out with marks and spaces swapped.
    inverted: false

  # Raw first, deliberately. See the header.
  dump:
    - raw
    - onkyori

  # 25% is the default and is what OnkyoRIProtocol::decode() itself uses. RI's
  # 1000us and 2000us spaces are far enough apart that this cannot confuse them.
  tolerance: 25%

  # Longest gap inside a frame is a 2000us space, and LIRC puts the gap between
  # frames at 67ms. 15ms sits clear of both, so held buttons arrive as separate
  # frames rather than one run-on blob.
  idle: 15ms

  # Shortest real pulse is 1000us, so anything under 200us is noise.
  filter: 200us

  on_onkyori:
    then:
      - logger.log:
          format: "Onkyo RI: decoded 0x%03X"
          args: ["x.data"]
