#:########################################################################################:#
# TITLE: OFFICE USB HUB POWER
# zorruno.com layout v1.1 2026
#:########################################################################################:#
# REPO:
# https://home.fox.co.nz/gitea/zorruno/zorruno-homeassistant/src/branch/master/esphome/esp-officeusbhubpower.yaml
#:########################################################################################:#
# VERSIONS:
# V1.2 2026-07-20 Simplified local MQTT command and status topics to the device root
# V1.1 2026-02-25 Updated yaml to zorruno layout V1.1
# V1.0 2025-06-14 Initial Version
#:########################################################################################:#
# HARDWARE:
# Sonoff Basic R1 (ESP8266)
# - Relay: GPIO12
# - Button: GPIO03
# - Status LED: GPIO13 (active-low on many Sonoff variants)
#:########################################################################################:#
# OPERATION NOTES:
# - Simple power control for an office USB hub power supply.
# - Local button toggles the relay.
# - MQTT control is supported for ON/OFF.
#:########################################################################################:#
# MQTT COMMANDS:
# Command (set relay):
# - ${mqtt_command_topic} -> ON, OFF
#
# Status (published from template sensor):
# - ${mqtt_status_topic} -> ON, OFF
#:########################################################################################:#
# OFFLINE NOTES:
# a) Home Assistant OFFLINE, but Network and MQTT ONLINE
#    - Device remains controllable via MQTT command topic
#    - Status publishes still occur via MQTT
#    - HA entities unavailable until HA returns
# b) MQTT OFFLINE (but WiFi/Network and HA API ONLINE)
#    - Device remains controllable from Home Assistant via API (relay/button entities)
#    - MQTT commands and status publishes will not work while MQTT is down
# c) Entire WiFi/Network OFFLINE
#    - No HA API and no MQTT control available
#    - Local button control continues to work
#    - Accurate time is NOT needed (SNTP not needed)
#:########################################################################################:#

#:########################################################################################:#
# 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-officeusbhubpower"
  friendly_name: "Office USB Hub Power"
  description_comment: "Office USB Hub Power Supply control in the Office (On wall) :: Sonoff Basic (Layout V1.1)"
  device_area: "Office"

  # Project Naming
  project_name: "Sonoff Technologies.Sonoff Basic V1"
  project_version: "v1.2"

  # Passwords & Secrets (Unfortunately, you cannot use substitutions inside secret names)
  api_key: !secret esp-api_key
  ota_pass: !secret esp-ota_pass
  static_ip_address: !secret esp-officeusbhubpower_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}

  # MQTT Topics
  mqtt_command_main_topic: !secret mqtt_command_main_topic
  mqtt_status_main_topic: !secret mqtt_status_main_topic
  mqtt_device_name: "office-usbhub-power"
  mqtt_command_topic: "${mqtt_command_main_topic}/${mqtt_device_name}"
  mqtt_status_topic: "${mqtt_status_main_topic}/${mqtt_device_name}"

  # Device Settings
  relay_icon: "mdi:generator-portable"
  log_level: "ERROR"
  update_interval: "60s"

  # Entity Naming
  relay_1_name: "USB Hub Power Supply"
  button_1_name: "Power Button"

#:########################################################################################:#
# 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

#:########################################################################################:#
# ESPHOME:
# https://esphome.io/components/esphome.html
#:########################################################################################:#
esphome:
  name: "${device_name}"
  friendly_name: "${friendly_name}"
  comment: "${description_comment}"
  area: "${device_area}"
  project:
    name: "${project_name}"
    version: "${project_version}"

#:########################################################################################:#
# ESP PLATFORM AND FRAMEWORK:
# https://esphome.io/components/esp8266.html
#:########################################################################################:#
esp8266:
  board: esp01_1m
  restore_from_flash: true

preferences:
  flash_write_interval: 5min

mdns:
  disabled: false

#:########################################################################################:#
# LOGGING:
# https://esphome.io/components/logger.html
#:########################################################################################:#
logger:
  level: "${log_level}"
  baud_rate: 0

#:########################################################################################:#
# SWITCH:
# Sonoff Basic R1 Relay Switch is GPIO12
# https://esphome.io/components/switch/gpio.html
#:########################################################################################:#
switch:
  - platform: gpio
    name: "${relay_1_name}"
    pin: GPIO12
    id: relay1
    restore_mode: RESTORE_DEFAULT_ON
    icon: "${relay_icon}"

#:########################################################################################:#
# BINARY SENSORS:
# Sonoff Basic R1 Button is GPIO03
# https://esphome.io/components/binary_sensor/gpio.html
#:########################################################################################:#
binary_sensor:
  - platform: gpio
    pin:
      number: GPIO03
      mode: INPUT_PULLUP
      inverted: true
    name: "${button_1_name}"
    id: power_button
    filters:
      - delayed_on: 20ms
    on_click:
      - min_length: 20ms
        max_length: 500ms
        then:
          - lambda: |-
              if (id(relay1).state) {
                id(relay1).turn_off();
              } else {
                id(relay1).turn_on();
              }

  # Mimics actual relay status (not directly controllable)
  - platform: template
    name: "${relay_1_name} Status"
    lambda: |-
      return id(relay1).state;
    on_press:
      - mqtt.publish:
          topic: "${mqtt_status_topic}"
          payload: "ON"
          retain: false
    on_release:
      - mqtt.publish:
          topic: "${mqtt_status_topic}"
          payload: "OFF"
          retain: false

#:########################################################################################:#
# STATUS LED:
# Sonoff Basic R1 LED is GPIO13
# https://esphome.io/components/status_led.html
#:########################################################################################:#
status_led:
  pin:
    number: GPIO13
    inverted: true

#:########################################################################################:#
# MQTT:
# Device-specific MQTT command triggers (in addition to common MQTT config)
# https://esphome.io/components/mqtt.html
#:########################################################################################:#
mqtt:
  on_message:
    - topic: "${mqtt_command_topic}"
      payload: "ON"
      then:
        - switch.turn_on: relay1

    - topic: "${mqtt_command_topic}"
      payload: "OFF"
      then:
        - switch.turn_off: relay1
