#:########################################################################################:#
# TITLE: MAIN DISHWASHER POWER
# zorruno.com layout v1.1 2026
#:########################################################################################:#
# REPO:
# https://home.fox.co.nz/gitea/zorruno/zorruno-homeassistant/src/branch/master/esphome/esp-maindishwasherpower.yaml
#:########################################################################################:#
# VERSIONS:
# V3.5 2026-03-11 Updated yaml to zorruno layout V1.1
# V3.4 2025-07-30 Changed to Athom V3
# V1.1 2025-06-12 package added for energy entities
# V1.0 2025-06-10 YAML Tidyups
#:########################################################################################:#
# HARDWARE:
# - Athom Smart Plug V3
# - ESP32-C3 based
# - Relay output on GPIO5
# - Local power button on GPIO3
# - Power monitoring via Athom V3 common package
# - package_import_url reference: github://athom-tech/athom-configs/athom-smart-plug.yaml
#:########################################################################################:#
# OPERATION NOTES:
# - Controls power to the main dishwasher
# - Uses Athom Smart Plug V3 hardware and Athom V3 common power monitor package
# - Relay is turned ON at boot
# - Local power button toggles the relay
# - Relay status is exposed as a template binary sensor
# - Power/energy monitoring entities are provided by the included Athom V3 package
#:########################################################################################:#
# OFFLINE NOTES:
# a) Home Assistant offline (network and MQTT online):
#    - Device continues operating normally
#    - Relay can still be controlled locally by the hardware power button
#    - MQTT remains available if your broker is online
#
# b) MQTT offline (or HA/MQTT offline):
#    - Device continues operating locally
#    - Relay control and local power monitoring continue to work
#
# c) Entire WiFi/Network offline:
#    - Device continues operating locally
#    - Relay control and local power monitoring continue to work
#    - Accurate time is not needed; SNTP is not required for operation
#:########################################################################################:#

#:########################################################################################:#
# SUBSTITUTIONS:
# Specific device variable substitutions
# If NOT using a secrets file, just replace these with the passwords etc (in quotes)
#:########################################################################################:#
substitutions:
  # Device Naming
  device_name: "esp-maindishwasherpower"
  friendly_name: "Main Dishwasher Power"
  description_comment: "Main Dishwasher Power Monitor :: Athom Smart Plug Power Monitor V3 (Layout V1.1)"
  device_area: "Kitchen"

  # Project Naming
  project_name: "Athom Technology.Smart Plug V3"
  project_version: "v3.5"

  # Passwords
  api_key: !secret esp-maindishwasherpower_api_key
  ota_pass: !secret esp-maindishwasherpower_ota_pass
  static_ip_address: !secret esp-maindishwasherpower_ip

  # If we are changing IP addresses, you must update the current IP address here, otherwise it remains
  # Don't forget to switch it back when changed.
  current_ip_address: ${static_ip_address}

  # Device Settings
  log_level: "INFO"
  update_interval: "60s"

  # Relay / Load Settings
  relay_icon: "mdi:dishwasher"
  current_limit: "10" # Current Limit in Amps. AU Plug = 10. IL, BR, EU, UK, US Plug = 16.

#:########################################################################################:#
# PACKAGES:
# Included Common Packages
# https://esphome.io/components/packages.html
#:########################################################################################:#
packages:
  #### WIFI, Network (Static/DHCP/IPV6 etc), Fallback AP, Safemode ####
  common_wifi: !include
    file: common/network_common.yaml
    vars:
      local_device_name: "${device_name}"
      local_static_ip_address: "${static_ip_address}"
      local_current_ip_address: "${current_ip_address}"
      local_ota_pass: "${ota_pass}"

  #### HOME ASSISTANT API (choose encryption or no encryption options) ####
  common_api: !include
    file: common/api_common.yaml
    #file: common/api_common_noencryption.yaml
    vars:
      local_api_key: "${api_key}"

  #### MQTT ####
  common_mqtt: !include
    file: common/mqtt_common.yaml
    vars:
      local_device_name: "${device_name}"

  #### WEB PORTAL ####
  #common_webportal: !include common/webportal_common.yaml

  #### SNTP (Only use if you want/need accurate timeclocks) ####
  common_sntp: !include common/sntp_common.yaml

  #### DIAGNOSTICS Sensors ####
  diag_basic: !include common/include_basic_diag_sensors.yaml
  diag_more: !include common/include_more_diag_sensors.yaml
  diag_debug: !include common/include_debug_diag_sensors.yaml
  diag_resetcount: !include common/include_resetcount_diag_sensors.yaml

  #### DEVICE SPECIFIC INCLUDED PACKAGES ####
  common_athompowermonv3: !include
    file: common/athompowermonv3_common.yaml
    vars:
      local_current_limit: "${current_limit}"

#:########################################################################################:#
# ESPHOME:
# https://esphome.io/components/esphome.html
#:########################################################################################:#
esphome:
  name: "${device_name}"
  friendly_name: "${friendly_name}"
  comment: "${description_comment}"
  area: "${device_area}"
  name_add_mac_suffix: false
  min_version: 2024.6.0
  project:
    name: "${project_name}"
    version: "${project_version}"
  platformio_options:
    board_build.mcu: esp32c3
    board_build.variant: esp32c3
    board_build.flash_mode: dio
  on_boot:
    priority: 200
    then:
      - switch.turn_on: relay

#:########################################################################################:#
# ESP PLATFORM AND FRAMEWORK:
# https://esphome.io/components/esp32.html
#:########################################################################################:#
esp32:
  board: esp32-c3-devkitm-1
  flash_size: 4MB
  variant: ESP32C3
  framework:
    type: esp-idf
    version: recommended

preferences:
  flash_write_interval: 5min

esp32_improv:
  authorizer: none

#:########################################################################################:#
# LOGGER:
# https://esphome.io/components/logger.html
#:########################################################################################:#
logger:
  level: "${log_level}"
  #baud_rate: 0 # set to 0 for no logging via UART, needed if you are using it for other serial things (eg PZEM)
  #esp8266_store_log_strings_in_flash: false
  #tx_buffer_size: 64

#:########################################################################################:#
# BINARY SENSORS:
# https://esphome.io/components/binary_sensor/
#:########################################################################################:#
binary_sensor:
  - platform: gpio
    pin:
      number: GPIO3
      mode: INPUT_PULLUP
      inverted: true
    name: "Power Button"
    id: power_button
    filters:
      - delayed_on: 20ms
    on_click:
      - min_length: 20ms
        max_length: 500ms
        then:
          - switch.toggle: relay

  - platform: template
    name: "Relay Status"
    lambda: |-
      return id(relay).state;

#:########################################################################################:#
# SWITCH COMPONENT:
# https://esphome.io/components/switch/
#:########################################################################################:#
switch:
  - platform: gpio
    name: "Power Output"
    pin: GPIO5
    id: relay
    restore_mode: RESTORE_DEFAULT_ON
    #internal: true
    icon: "${relay_icon}"