#:########################################################################################:#
# TITLE: LAUNDRY ENVIRONMENT - SONOFF DEV
# zorruno.com layout v1.1 2026
#:########################################################################################:#
# REPO:
# https://home.fox.co.nz/gitea/zorruno/zorruno-homeassistant/src/branch/master/esphome/esp-laundryenv.yaml
#:########################################################################################:#
# VERSIONS:
# V1.2 2026-04-25 Added PIR MQTT light timer control, updated notes, added PIR debounce filters
# V1.1 2026-03-11 Updated yaml to zorruno layout V1.1
# V1.0 2025-03-28 Initial Version
#:########################################################################################:#
# HARDWARE:
# - Sonoff DEV
# - ESP8266 based
# - DHT temperature and humidity sensor on GPIO14
# - PIR input on GPIO13
# - Smoke detector input on GPIO5
# - Reference: https://wiki.iteadstudio.com/Sonoff_DEV
#:########################################################################################:#
# OPERATION NOTES:
# - Provides laundry temperature and humidity monitoring
# - Provides a PIR motion binary sensor
# - Provides a smoke detector binary sensor
# - PIR Detected publishes an MQTT On command to the Laundry Lights device
# - PIR Clear publishes an MQTT timer payload to the Laundry Lights device
# - The Laundry Lights device performs the actual relay control and timer countdown
# - The remote timer payload is in seconds and should match the accepted range on the light switch
# - This device does not directly control a relay
#:########################################################################################:#
# MQTT COMMANDS:
# - Remote light command topic:
#   - ${mqtt_remote_device1_command_topic}
# - PIR Detected sends:
#   - ${mqtt_remote_device1_command1}
# - PIR Clear sends:
#   - ${mqtt_remote_device1_command2}
# - The timer payload is in seconds
#:########################################################################################:#
# OFFLINE NOTES:
# a) Home Assistant offline (network and MQTT online):
#    - Device continues to read and publish local sensor states
#    - PIR, smoke, temperature, and humidity sensing continue locally
#    - PIR MQTT light control continues if the MQTT broker and target device are online
#
# b) MQTT offline (or HA/MQTT offline):
#    - Device continues sampling sensors locally
#    - PIR, smoke, temperature, and humidity sensing continue locally
#    - PIR MQTT light control is unavailable
#
# c) Entire WiFi/Network offline:
#    - Device continues sampling sensors locally
#    - No HA or MQTT connectivity will be available
#    - PIR MQTT light control is unavailable
#    - 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-laundryenv"
  friendly_name: "Laundry Environment"
  description_comment: "Laundry Environment Sensor, PIR and Smoke Detector using Sonoff DEV (Layout V1.1)"
  device_area: "Laundry"

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

  # Passwords and Secrets
  api_key: !secret esp-laundryenv_api_key
  ota_pass: !secret esp-laundryenv_ota_pass
  static_ip_address: !secret esp-laundryenv_ip
  mqtt_command_main_topic: !secret mqtt_command_main_topic
  mqtt_status_main_topic: !secret mqtt_status_main_topic

  # 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"
  entity_prefix: "Laundry"

  # Remote light timer payloads, in seconds.
  remote_timer_lights_payload: "120"

  # MQTT REMOTE Controls
  mqtt_remote_device1_name: "laundry-lights"
  mqtt_remote_device1_command_topic: "${mqtt_command_main_topic}/${mqtt_remote_device1_name}"
  mqtt_remote_device1_command1: "On"
  mqtt_remote_device1_command2: "${remote_timer_lights_payload}"

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

#:########################################################################################:#
# 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
  early_pin_init: false
  board_flash_mode: dout

#:########################################################################################:#
# LOGGER:
# https://esphome.io/components/logger.html
#:########################################################################################:#
logger:
  level: "${log_level}"
  #baud_rate: 0
  #esp8266_store_log_strings_in_flash: false
  #tx_buffer_size: 64

#:########################################################################################:#
# SENSORS:
# https://esphome.io/components/sensor/
#:########################################################################################:#
sensor:
  - platform: dht
    pin: GPIO14
    temperature:
      name: "${entity_prefix} Temperature"
    humidity:
      name: "${entity_prefix} Humidity"
    update_interval: "${update_interval}"

#:########################################################################################:#
# SCRIPT COMPONENT:
# MQTT commands sent to the remote Laundry Lights device
# https://esphome.io/components/script.html
#:########################################################################################:#
script:
  - id: send_remote_light_on_commands
    mode: restart
    then:
      - mqtt.publish:
          topic: "${mqtt_remote_device1_command_topic}"
          payload: "${mqtt_remote_device1_command1}"
          qos: 0
          retain: false

  - id: send_remote_light_timer_commands
    mode: restart
    then:
      - mqtt.publish:
          topic: "${mqtt_remote_device1_command_topic}"
          payload: "${mqtt_remote_device1_command2}"
          qos: 0
          retain: false

#:########################################################################################:#
# BINARY SENSORS:
# https://esphome.io/components/binary_sensor/
#:########################################################################################:#
binary_sensor:
  - platform: gpio
    id: laundry_pir
    pin:
      number: GPIO13
      mode:
        input: true
        pullup: false
    name: "${entity_prefix} PIR"
    device_class: motion
    filters:
      - delayed_on: 100ms
      - delayed_off: 1s
    on_press:
      - script.execute: send_remote_light_on_commands
    on_release:
      - script.execute: send_remote_light_timer_commands

  - platform: gpio
    id: laundry_smoke_detector
    pin:
      number: GPIO5
      mode:
        input: true
        pullup: true
      inverted: true
    name: "${entity_prefix} Smoke Detector"
    device_class: smoke