---
substitutions:
  device: pow-dishwasher
  name: Dishwasher POW
  area: Living Room & Kitchen
  comment: Kitchen | Under sink
  update_interval: 10s

esphome:
  name: mcu-${device}
  friendly_name: ${name}
  area: ${area}
  comment: ${comment}

esp32:
  variant: esp32
  toolchain: esp-idf
  framework:
    type: esp-idf
    advanced:
      minimum_chip_revision: 3.1
      sram1_as_iram: true

packages:
  common: !include common/common.yaml

uart:
  - id: cse7766_uart
    rx_pin: 16
    baud_rate: 4800
    parity: EVEN

binary_sensor:
  - platform: gpio
    pin: 0
    id: reset
    internal: true
    filters:
      - invert:
      - delayed_off: 10ms
    on_click:
      - max_length: 350ms # short press to toggle the relay
        then:
          switch.toggle: relay_1
      - min_length: 360ms # long press to cycle display info
        max_length: 3s
        then:
          - if:
              condition:
                binary_sensor.is_on: page
              then:
                binary_sensor.template.publish:
                  id: page
                  state: OFF
              else:
                binary_sensor.template.publish:
                  id: page
                  state: ON
  - platform: template # this is a fake sensor to tell the screen which info to show on display
    id: page
    publish_initial_state: true
    internal: true
  - platform: template
    name: Load
    id: load_on
    lambda: |-
      if (isnan(id(w_sensor).state)) {
        return {};
      } else if (id(w_sensor).state > 2) {
        // Running
        return true;
      } else {
        // Not running
        return false;
      }

display:
  platform: tm1621
  id: tm1621_display
  cs_pin: 25
  data_pin: 14
  read_pin: 26
  write_pin: 27
  lambda: |-
    if (id(page).state) {
      it.display_voltage(true);
      it.display_kwh(false);
      it.printf(0, "%.1f", id(v_sensor).state);
      it.printf(1, "%.1f", id(a_sensor).state);
    } else {
      it.display_voltage(false);
      it.display_kwh(true);
      it.printf(0, "%.1f", id(wh_sensor).state);
      it.printf(1, "%.1f", id(w_sensor).state);
    }

light:
  - platform: monochromatic
    id: switch_led
    output: led
    internal: True
  - platform: status_led
    id: wifi_status_led
    internal: True
    pin:
      number: 5
      inverted: True

output:
  - platform: ledc
    id: led
    pin:
      number: 18
      inverted: True

sensor:
  - platform: cse7766
    uart_id: cse7766_uart
    current:
      name: Current
      id: a_sensor
      filters:
        - throttle_average: ${update_interval}
    voltage:
      name: Voltage
      id: v_sensor
      filters:
        - throttle_average: ${update_interval}
    power:
      name: Power
      id: w_sensor
      filters:
        - throttle_average: ${update_interval}
      on_value_range:
        - above: 4.0
          then:
            - light.turn_on: switch_led
        - below: 3.0
          then:
            - light.turn_off: switch_led
    energy:
      name: Energy
      id: wh_sensor
      filters:
        - throttle_average: ${update_interval}
    power_factor:
      name: Power Factor
      id: power_factor
      filters:
        - throttle_average: ${update_interval}

  - platform: total_daily_energy
    name: Total Daily Energy
    power_id: w_sensor
    filters:
      - multiply: 0.001
    unit_of_measurement: kWh

switch:
  - platform: template
    name: Relay
    optimistic: true
    restore_mode: ALWAYS_ON
    id: relay_1
    turn_on_action:
      - switch.turn_on: relay_on
    turn_off_action:
      - switch.turn_on: relay_off

  - platform: gpio
    restore_mode: ALWAYS_OFF
    internal: true
    id: relay_on
    pin: 2
    on_turn_on:
      - delay: 500ms
      - switch.turn_off: relay_on  # bi-stable relay so no need to keep on
    interlock: [relay_off]

  - platform: gpio
    restore_mode: ALWAYS_OFF
    internal: true
    id: relay_off
    pin: 4
    on_turn_on:
      - delay: 500ms
      - switch.turn_off: relay_off # bi-stable relay so no need to keep on
    interlock: [relay_on]

time:
  - !include common/time/homeassistant.yaml
