#:########################################################################################:#
# TITLE: ROCK TUMBLER POWER MONITOR
# zorruno.com layout v1.1 2026
#:########################################################################################:#
# REPO:
# https://home.fox.co.nz/gitea/zorruno/zorruno-homeassistant/src/branch/master/esphome/esp-rocktumbler.yaml
#:########################################################################################:#
# VERSIONS:
# V1.4 2026-02-25 Updated yaml to zorruno layout V1.1
# V1.3 2025-08-27 Changed to Athom V2
# V1.2 2025-06-15 Changed to Athom V1
# V1.1 2025-06-12 Tidyups and packages added
#:########################################################################################:#
# HARDWARE:
# - Athom Smart Plug V2 (ESP8285) with power monitoring + relay
# - Reference config: github://athom-tech/athom-configs/athom-smart-plug.yaml
#:########################################################################################:#
# OPERATION NOTES:
# - This device provides power monitoring (via common Athom V2 package) and a controllable relay.
# - "Power On State" select controls what the relay does after reboot:
#   - Always Off
#   - Always On
#   - Restore Previous State
# - Power monitoring sensor update interval is set via substitutions.
#:########################################################################################:#
# MQTT COMMANDS:
# - None device-specific in this YAML (uses common_mqtt package for standard device MQTT support).
#:########################################################################################:#
# OFFLINE NOTES:
# a) Home Assistant OFFLINE, but Network and MQTT ONLINE
#    - Device continues operating and can be controlled via MQTT (if your common_mqtt exposes it).
#    - Power monitoring continues locally and publishes via MQTT if configured.
# b) MQTT OFFLINE (but WiFi/Network and HA API ONLINE)
#    - Device remains controllable via Home Assistant API.
#    - MQTT publishing/commands will fail while MQTT is down.
# c) Entire WiFi/Network OFFLINE
#    - Device continues operating locally (relay + power monitoring).
#    - SNTP is not needed (no timeclocks used in this YAML).
#:########################################################################################:#

#:########################################################################################:#
# 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-rocktumbler"
  friendly_name: "Rock Tumbler Power"
  description_comment: "Rock Tumbler Power :: Athom Smart Plug Power V2 (Layout V1.1)"
  device_area: "Laundry" # Allows ESP device to be automatically linked to an 'Area' in Home Assistant.

  # Project Naming
  project_name: "Athom Technology.Smart Plug V2"
  project_version: "v1.4" # Updated yaml to zorruno layout V1.1

  # Passwords (unfortunately you cannot use substitutions inside secrets names)
  api_key: !secret esp-api_key
  ota_pass: !secret esp-ota_pass
  static_ip_address: !secret esp-rocktumbler_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 General Settings
  log_level: "NONE" # NONE, ERROR, WARN, INFO, DEBUG (Default), VERBOSE, VERY_VERBOSE
  update_interval: "60s" # update time for for general sensors etc

  # Device Specific Settings (Athom Power Monitor Plug V2)
  relay_icon: "mdi:power-socket-au"
  current_limit: "10" # Current Limit in Amps. AU Plug = 10. IL, BR, EU, UK, US Plug = 16.
  sensor_update_interval: "10s"
  relay_restore_mode: RESTORE_DEFAULT_ON
  hide_energy_sensor: "true"

#:########################################################################################:#
# 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_ota_pass: "${ota_pass}"
      local_current_ip_address: "${current_ip_address}"

  #### 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: Athom Power Monitor Plug V2 ####
  common_athompowermonV2: !include
    file: common/athompowermonv2_common.yaml
    vars:
      local_friendly_name: "${friendly_name}"
      local_current_limit: "${current_limit}"
      sensor_update_interval: "${sensor_update_interval}"
      relay_restore_mode: "${relay_restore_mode}"
      power_plug_type: "${relay_icon}"
      hide_energy_sensor: "${hide_energy_sensor}"

#:########################################################################################:#
# ESPHOME:
# https://esphome.io/components/esphome.html
#:########################################################################################:#
esphome:
  name: "${device_name}"
  friendly_name: "${friendly_name}"
  comment: "${description_comment}" # Appears on the esphome page in HA
  area: "${device_area}"
  name_add_mac_suffix: false
  min_version: 2024.6.0
  project:
    name: "${project_name}"
    version: "${project_version}"
  on_boot:
    priority: 600
    then:
      - select.set_index:
          id: power_mode
          index: !lambda |-
            return id(restore_mode) - 1;
      - lambda: |-
          switch (id(restore_mode)) {
            case 1: {
              id(relay).turn_off();
              break;
            }
            case 2: {
              id(relay).turn_on();
              break;
            }
            default: {
              break;
            }
          }

#:########################################################################################:#
# ESP PLATFORM AND FRAMEWORK:
# https://esphome.io/components/esp8266/
#:########################################################################################:#
esp8266:
  board: esp8285
  restore_from_flash: true # mainly for calculating cumulative energy, but not that important here
  early_pin_init: true

preferences:
  flash_write_interval: 5min

mdns:
  disabled: false

#:########################################################################################:#
# LOGGING: ESPHome Logging Enable
# 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)

#:########################################################################################:#
# GLOBAL VARIABLES:
# https://esphome.io/guides/automations.html#global-variables
#:########################################################################################:#
globals:
  # 0 = Always_Off, 1 = Restore_Power_Off, 2 = Always_On
  - id: restore_mode
    type: int
    restore_value: yes
    initial_value: "0"

#:########################################################################################:#
# SELECT COMPONENT:
# https://esphome.io/components/select/index.html
#:########################################################################################:#
select:
  - platform: template
    name: "Power On State"
    id: power_mode
    optimistic: true
    icon: mdi:electric-switch
    options:
      - "Always Off"
      - "Always On"
      - "Restore Previous State"
    on_value:
      then:
        - lambda: |-
            id(restore_mode) = i + 1;
