# LEGACY REFERENCE (ESPHome 2025.5 – 2026.7). Kept for readers on the
# 2025.5-era syntax; annotated 2026-11-19 (Connie, MCU Atlas). On ESPHome
# 2026.8+ the wake-word models list is optional (runtime models) and 2026.11
# adds prefer_internal_ram — current guidance and version-gated snippets live
# on the MCU Atlas page and beside this file:
#   runtime-models-2026-8.yaml, prefer-internal-ram-2026-11.yaml.
# DO NOT copy the on_client_disconnected -> micro_wake_word.stop handler
# below: the reporter of esphome issue #22361 found it fires on API client
# churn and makes the deaf-satellite problem worse.
#
# MCU Atlas migration example — bring a custom wake-word satellite up to
# ESPHome 2025.5.0+ voice_assistant syntax. Written by Connie, MCU Atlas,
# checked 2026-11-15. Base: esphome/wake-word-voice-assistants
# m5stack-atom-echo.yaml (Apache-2.0), pinned next to this file.
#
# What this demonstrates (all ESPHome 2025.5.0+, per the release post
# https://esphome.io/blog/2025/05/21/esphome-2025-5/):
#   1. Microphone config moved UNDER the component that uses it (breaking
#      change esphome#8645): channels + gain_factor now live in
#      voice_assistant.microphone. gain_factor: 4 matches the old
#      32-bits-per-sample behaviour.
#   2. Home Assistant can start a conversation and continue it without the
#      wake word; conversation_timeout resets the stored conversation_id
#      context (default 300s).
#   3. microWakeWord model enable/disable actions: every model in the models
#      list carries an id, and micro_wake_word.enable_model /
#      disable_model target that id (switch below).
#   4. Naming the micro_wake_word component in voice_assistant lets Home
#      Assistant switch the active on-device wake word model.
esphome:
  name: wake-word-satellite
  friendly_name: Wake word satellite
  min_version: 2025.5.0

esp32:
  board: m5stack-atom
  cpu_frequency: 240MHz
  framework:
    type: esp-idf

logger:
api:
ota:
  - platform: esphome
wifi:
  ap:
captive_portal:

i2s_audio:
  - id: i2s_audio_bus
    i2s_lrclk_pin: GPIO33
    i2s_bclk_pin: GPIO19

microphone:
  - platform: i2s_audio
    id: echo_microphone
    i2s_din_pin: GPIO23
    adc_type: external
    pdm: true
    sample_rate: 16000
    correct_dc_offset: true

speaker:
  - platform: i2s_audio
    id: echo_speaker
    i2s_dout_pin: GPIO22
    dac_type: external
    bits_per_sample: 16bit
    sample_rate: 16000
    channel: stereo  # mono sounds poor on the Echo
    buffer_duration: 60ms

media_player:
  - platform: speaker
    id: echo_media_player
    name: None
    announcement_pipeline:
      speaker: echo_speaker
      format: WAV

# --------------------------------------------------------------------------
# The 2025.5+ part. Compare with a pre-2025.5 config: microphone settings
# used to sit on the microphone/platform block only, voice_assistant took a
# bare microphone id, and there was no micro_wake_word link here.
# --------------------------------------------------------------------------
voice_assistant:
  id: va
  # 4) lets Home Assistant change which wake word model is enabled
  micro_wake_word:
  # 1) microphone source config lives here since 2025.5 (esphome#8645)
  microphone:
    microphone: echo_microphone
    channels: 0
    gain_factor: 4
  media_player: echo_media_player
  noise_suppression_level: 2
  auto_gain: 31dBFS
  # 2) how long the conversation context survives before HA resets it
  conversation_timeout: 2min
  # Re-arm detection on the paths this minimal example handles. The official
  # config (m5stack-atom-echo.yaml next to this file) covers more paths --
  # see the known-issues note on the MCU Atlas page (esphome issue #22361).
  on_client_connected:
    - delay: 2s  # give the api server time to settle
    - micro_wake_word.start:
  on_client_disconnected:
    - micro_wake_word.stop:
  on_error:
    - delay: 2s
    - micro_wake_word.start:

micro_wake_word:
  vad:
  # 3) ids let the enable/disable actions (and Home Assistant) target a model
  models:
    - model: okay_nabu
      id: okay_nabu
    - model: hey_mycroft
      id: hey_mycroft
  on_wake_word_detected:
    - voice_assistant.start:
        wake_word: !lambda return wake_word;

switch:
  - platform: template
    name: Use hey_mycroft instead of okay_nabu
    id: use_mycroft
    optimistic: true
    restore_mode: ALWAYS_OFF
    on_turn_on:
      # microWakeWord model enable/disable actions (ESPHome 2025.5.0)
      - micro_wake_word.disable_model: okay_nabu
      - micro_wake_word.enable_model: hey_mycroft
    on_turn_off:
      - micro_wake_word.disable_model: hey_mycroft
      - micro_wake_word.enable_model: okay_nabu
