substitutions:
  mdns_name: panasonic-heatpump
  device_name: Panasonic Heatpump
  device_description: Reads/Controls your Panasonic Aquarea Heatpump over UART

  pin_rx_hp: GPIO37  # heatpump reads data (RX) on this pin    (yellow)
  pin_tx_hp: GPIO39  # heatpump sends data (TX) on this pin    (green)
  pin_tx_wm: GPIO18  # WiFi module sends data (TX) on this pin (white)
  pin_rx_wm: GPIO16  # WiFi module reads data (RX) on this pin (blue)

  # CN-CNT Pinout (from top to bottom)
  # 1 - +5V (250mA)
  # 2 - 0-5V TX (from heatpump)
  # 3 - 0-5V RX (to heatpump)
  # 4 - +12V (250mA)
  # 5 - GND

esp32:
  board: lolin_s2_mini
  variant: ESP32S2
  framework:
    type: esp-idf

esphome:
  name: $mdns_name
  friendly_name: $device_name
  comment: $device_description
  includes:
    - prototypes/panasonic_heatpump/decode.h
    - prototypes/panasonic_heatpump/decode.cpp

ota:
  platform: esphome
  password: !secret ota_password
  id: ota_pass

api:
  encryption:
    key: !secret ha_api_key

wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_password
  ap:
    ssid: "${device_name} Fallback"
    password: !secret fallback_password

web_server:
  port: 80
  auth:
    username: !secret webserver_username
    password: !secret webserver_password

captive_portal:

improv_serial:

logger:
  level: DEBUG

uart:
  - id: uart_heatpump
    # switch rx and tx to read what the heatpump is sending
    tx_pin: $pin_rx_hp
    rx_pin: $pin_tx_hp
    baud_rate: 9600
    data_bits: 8
    parity: EVEN
    stop_bits: 1
    debug:
      direction: RX
      dummy_receiver: true # needed if no uart component is available
      after:
        bytes: 203
      sequence:
        - lambda: |-
            ESP_LOGD("custom", "Heatpump:");
            UARTDebug::log_hex(direction, bytes, ',');
            if (bytes.size() == 203)
            {
              id(top0).publish_state(getBoolState(getBit7and8(bytes[4])));
              id(top1).publish_state(getPumpFlow(bytes[169], bytes[170]));
              id(top2).publish_state(getBoolState(getBit1and2(bytes[4])));
              id(top3).publish_state(getBoolState(getBit1and2(bytes[7])));
              id(top4).publish_state(getTextState(OpModeDesc, getOpMode(bytes[6])));
              // ...
              // (for more topics see heishamon_decode.xlsx or code-gen-result.md)
            }
        - uart.write: # Write rx to tx.
            id: uart_cztaw1
            data: !lambda return bytes;
  - id: uart_cztaw1
    # switch rx and tx to read what the WiFi module is sending
    tx_pin: $pin_rx_wm
    rx_pin: $pin_tx_wm
    baud_rate: 9600
    data_bits: 8
    parity: EVEN
    stop_bits: 1
    debug:
      direction: RX
      dummy_receiver: true # needed if no uart component is available
      after:
        bytes: 111
      sequence:
        - lambda: |-
            ESP_LOGD("custom", "CZ-TAW1:");
            UARTDebug::log_hex(direction, bytes, ',');
        - uart.write: # Write rx to tx.
            id: uart_heatpump
            data: !lambda return bytes;

### if no CZ-TAW1 is connected the Panasonic Query (request message) can be send cyclic by ESPHome
# interval:
#  - interval: 5s
#    then:
#      - uart.write:
#          id: uart_heatpump
#          data: [ 0x71, 0x6c, 0x01, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x12 ]

sensor:
  - platform: wifi_signal
    name: WiFi Signal

  - platform: template
    id: top1
    name: "Pump Flow"
    unit_of_measurement: l/min
    lambda: "return {};"
  # ...
  # (for more topics see heishamon_decode.xlsx or code-gen-result.md)

text_sensor:
  - platform: version
    hide_timestamp: true
    name: ESPHome Version
  - platform: wifi_info
    ip_address:
      name: IP Addresse
      icon: mdi:ip

  - platform: template
    id: top4
    name: "Operating Mode State"
    lambda: "return {};"
  # ...
  # (for more topics see heishamon_decode.xlsx or code-gen-result.md)

binary_sensor:
  - platform: status
    name: Status

  - platform: template
    id: top0
    name: "Heatpump State"
    lambda: "return {};"
  - platform: template
    id: top2
    name: "Force DHW State"
    lambda: "return {};"
  - platform: template
    id: top3
    name: "Quiet Mode Schedule"
    lambda: "return {};"
  # ...
  # (for more topics see heishamon_decode.xlsx or code-gen-result.md)

button:
  - platform: restart
    name: Restart
