# Washer bridge: listens to the Electrolux MACS bus inside a Westinghouse WWT8084J7WA top loader
# and reports to Home Assistant: state, wash stage, minutes left, programme, spin speed, lid, error.
# LISTEN ONLY: there is no tx_pin, so the ESP32 never sends anything on the bus.
#
# Tap = the four solder pads on the edge of the panel board (a parallel copy of the main-board plug):
#   +12 V (not used), +5 V, DATA (5 V, idles at 0 V), GND.  Meter yours before you solder.
# Wiring (Seeed XIAO ESP32-C3, use the silkscreen labels: D2 is GPIO4, D4 is NOT):
#   DATA -> 4k7 -> D2, with 8k2 from D2 to GND (5 V bus -> about 3.1 V at the pin)
#   +5 V -> 5V, GND -> GND, with 470 uF 16 V across 5V and GND at the XIAO
# The panel's 0 V is tied to mains neutral: NEVER plug USB into the ESP while it is wired to the
# washer. Flash it over USB on the bench first, then update over Wi-Fi only.
#
# Decoder: a local copy of github.com/agocsdaniel/esphome_electrolux_macs in ./components,
# with washer-component-patch.diff applied (build fix + state code, phase code, flags and lid).
substitutions:
  name: washer
  friendly_name: Washer

esphome:
  name: ${name}
  friendly_name: ${friendly_name}

esp32:
  board: esp32-c3-devkitm-1
  variant: esp32c3
  framework:
    type: esp-idf

external_components:
  - source:
      type: local
      path: components

logger:
  hardware_uart: USB_SERIAL_JTAG
  level: DEBUG                  # the component logs every message it hears: free protocol dumps
  logs:
    sensor: INFO
    text_sensor: INFO
    binary_sensor: INFO

api:

ota:
  - platform: esphome

wifi:
  networks:
    - ssid: !secret wifi_ssid
      password: !secret wifi_password
  power_save_mode: none
  ap:
    ssid: "Washer Bridge AP"

captive_portal:

uart:
  id: uart_macs
  rx_pin:
    number: GPIO4               # XIAO label D2 (NOT D4)
    inverted: true              # the bus idles at 0 V
    mode: INPUT
  baud_rate: 9600
  parity: EVEN
  rx_buffer_size: 512

electrolux_washing_machine_macs:
  uart_id: uart_macs

sensor:
  - platform: electrolux_washing_machine_macs
    remaining_time:
      name: "Minutes Left"
      unit_of_measurement: min
    selected_program_number:
      name: "Program Number"
      id: program_no
    spin_speed:
      name: "Spin Speed"
      unit_of_measurement: rpm
    state_code:
      name: "State Code"        # 1 idle, 2 running, 3 finished, 4 paused, 8 delayed start, 9 error, 11 standby
    phase_code:
      name: "Phase Code"        # wash stage, see the guide
    state_flags:
      name: "State Flags"       # raw byte; 0x08 = lid open on my machine
  - platform: wifi_signal
    name: "WiFi Signal"
    update_interval: 60s
  - platform: uptime
    name: "Uptime"

binary_sensor:
  - platform: electrolux_washing_machine_macs
    powered_on:
      name: "Panel On"
    running:
      name: "Running"
    lid_open:
      name: "Lid Open"          # only while the panel is on; held at its last state in standby

text_sensor:
  # Programme name from the dial position (my WWT8084J7WA). Unknown = "Program N".
  - platform: template
    name: "Program"
    icon: mdi:washing-machine
    update_interval: 2s
    lambda: |-
      if (std::isnan(id(program_no).state)) return {};
      switch ((int) id(program_no).state) {
        case 1:  return std::string("Dirty");
        case 2:  return std::string("Extra Dirty");
        case 3:  return std::string("Spin");
        case 4:  return std::string("Rinse and Spin");
        case 5:  return std::string("Machine Clean");
        case 6:  return std::string("Stain");
        case 7:  return std::string("Eco Save");
        case 8:  return std::string("Bedding");
        case 9:  return std::string("Delicate");
        case 10: return std::string("Fast");
        case 11: return std::string("Daily");
        case 12: return std::string("Full Load");
        default: return std::string("Program ") + std::to_string((int) id(program_no).state);
      }
  - platform: electrolux_washing_machine_macs
    active_alarm:
      name: "Error Code"
  - platform: debug
    reset_reason:
      name: "Reset Reason"

debug:
  update_interval: 60s
