#:########################################################################################:#
# TITLE: OUTSIDE ENTRANCE MULTISENSOR
# zorruno.com layout v1.1 2026
#:########################################################################################:#
# REPO:
# https://home.fox.co.nz/gitea/zorruno/zorruno-homeassistant/src/branch/master/esphome/esp-entmulti.yaml
#:########################################################################################:#
# VERSIONS:
# V1.2 2026-02-25 Updated yaml to zorruno layout V1.1
# V1.1 2026-02-23
# - Add full header + project metadata block
# - Fix LD2140 -> LD2410 typos throughout
# - Normalize booleans and tidy naming consistency
# - Prefix LD2410 entity names with friendly name for cleaner HA entities
# - Add LD2410 buttons (factory reset/restart/query) + text sensors (fw version/bt mac)
# - Add composite Occupancy sensor (mmWave OR PIR) with delayed-off
# - Correct UART comments (LD2410 uses 256000 baud out-of-box in ESPHome)
# V1.0 2026-02-xx Initial version (as provided)
#:########################################################################################:#
# HARDWARE:
# - D1 Mini ESP32 (ESP32)
# - HI-LINK LD2410 mmWave presence sensor (UART)
# - PIR sensor on GPIO13
# - 2x RF vibration inputs (bins) on GPIO4 and GPIO15
#
# NOTES:
# - ESPHome 2026.1+ requires API encryption and OTA SHA256 authentication.
#   Ensure your common package files are updated accordingly.
# - LD2410 via ESPHome uses 256000 baud by default; parity NONE, stop_bits 1.
# - GPIO2/GPIO15 are strapping pins on ESP32. You are already suppressing warnings,
#   but be aware boot behavior can be affected by external circuitry on these pins.
#:########################################################################################:#
# OPERATION NOTES:
# - Provides LD2410 presence (moving/still/target), plus PIR motion and 2x vibration inputs.
# - Composite Occupancy sensor is mmWave OR PIR with delayed-off for stable lighting triggers.
# - Device is sensor-driven only (no timeclocks); SNTP is optional for diagnostics/time entities.
#:########################################################################################:#
# OFFLINE NOTES:
# a) Home Assistant OFFLINE, but Network and MQTT ONLINE
#    - Sensors continue running locally
#    - MQTT (if enabled/online) continues publishing; HA entities unavailable until HA returns
# b) MQTT OFFLINE (but WiFi/Network and HA API ONLINE)
#    - Sensors remain available to Home Assistant via API
#    - MQTT-based automations/commands will not work while MQTT is down
# c) Entire WiFi/Network OFFLINE
#    - No HA API and no MQTT
#    - Sensors continue running locally; automation depends on local logic only
#    - Accurate time is NOT needed (SNTP not needed); loss of network does not affect timing
#:########################################################################################:#

#:########################################################################################:#
# 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-entmulti"
  friendly_name: "Outside Entrance Multisensor"
  description_comment: "D1 Mini ESP32 outside entranceway with mmWave presence, PIR and more (Layout V1.1)"
  device_area: "Entranceway" # Allows ESP device to be automatically linked to an Area in Home Assistant.

  # Project Naming (shown in ESPHome dashboard / HA device info)
  project_name: "zorruno.esp-entmulti"
  project_version: "v1.2"

  # Passwords & Secrets
  api_key: !secret esp-entmulti_api_key
  ota_pass: !secret esp-entmulti_ota_pass
  static_ip_address: !secret esp-entmulti_ip # unfortunately you can't use substitutions inside secrets names
  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}

  # MQTT LOCAL Controls (default to this device name to avoid accidental cross-device topics)
  #mqtt_local_device_name: "${device_name}"
  #mqtt_local_command_topic: "${mqtt_command_main_topic}/${mqtt_local_device_name}"
  #mqtt_local_status_topic: "${mqtt_status_main_topic}/${mqtt_local_device_name}"

  # Device Settings
  log_level: "INFO"          # NONE, ERROR, WARN, INFO, DEBUG, VERBOSE, VERY_VERBOSE
  update_interval: "60s"     # general sensor update interval (if/when used)

#:########################################################################################:#
# 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}" # appears on the ESPHome page in HA
  area: "${device_area}"
  project:
    name: "${project_name}"
    version: "${project_version}"
  min_version: 2026.2.0

#:########################################################################################:#
# ESP PLATFORM AND FRAMEWORK:
# https://esphome.io/components/esp32.html
#:########################################################################################:#
esp32:
  board: esp32dev
  framework:
    #type: arduino
    type: esp-idf
    version: recommended

#:########################################################################################:#
# 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 (keeps UART free for sensors)

#:########################################################################################:#
# UART: LD2410 Serial
# https://esphome.io/components/uart.html
#:########################################################################################:#
uart:
  id: ld2410_uart
  rx_pin: GPIO16
  tx_pin: GPIO17
  baud_rate: 256000
  data_bits: 8
  stop_bits: 1
  parity: NONE

#:########################################################################################:#
# STATUS LED:
# https://esphome.io/components/status_led.html
#:########################################################################################:#
status_led:
  pin:
    number: GPIO2
    ignore_strapping_warning: true
    inverted: false

#:########################################################################################:#
# LD2410:
# https://esphome.io/components/sensor/ld2410.html
#:########################################################################################:#
ld2410:
  id: ld2410_1
  uart_id: ld2410_uart

#:########################################################################################:#
# NUMBER:
# https://esphome.io/components/number/
#:########################################################################################:#
number:
  - platform: ld2410
    ld2410_id: ld2410_1

    timeout:
      name: "${friendly_name} LD2410 Timeout"
      entity_category: config

    light_threshold:
      name: "${friendly_name} LD2410 Light Threshold"
      entity_category: config

    max_move_distance_gate:
      name: "${friendly_name} LD2410 Max Move Distance Gate"
      entity_category: config

    max_still_distance_gate:
      name: "${friendly_name} LD2410 Max Still Distance Gate"
      entity_category: config

    g0:
      move_threshold:
        name: "${friendly_name} LD2410 G0 Move Threshold"
        entity_category: config
      still_threshold:
        name: "${friendly_name} LD2410 G0 Still Threshold"
        entity_category: config
    g1:
      move_threshold:
        name: "${friendly_name} LD2410 G1 Move Threshold"
        entity_category: config
      still_threshold:
        name: "${friendly_name} LD2410 G1 Still Threshold"
        entity_category: config
    g2:
      move_threshold:
        name: "${friendly_name} LD2410 G2 Move Threshold"
        entity_category: config
      still_threshold:
        name: "${friendly_name} LD2410 G2 Still Threshold"
        entity_category: config
    g3:
      move_threshold:
        name: "${friendly_name} LD2410 G3 Move Threshold"
        entity_category: config
      still_threshold:
        name: "${friendly_name} LD2410 G3 Still Threshold"
        entity_category: config
    g4:
      move_threshold:
        name: "${friendly_name} LD2410 G4 Move Threshold"
        entity_category: config
      still_threshold:
        name: "${friendly_name} LD2410 G4 Still Threshold"
        entity_category: config
    g5:
      move_threshold:
        name: "${friendly_name} LD2410 G5 Move Threshold"
        entity_category: config
      still_threshold:
        name: "${friendly_name} LD2410 G5 Still Threshold"
        entity_category: config
    g6:
      move_threshold:
        name: "${friendly_name} LD2410 G6 Move Threshold"
        entity_category: config
      still_threshold:
        name: "${friendly_name} LD2410 G6 Still Threshold"
        entity_category: config
    g7:
      move_threshold:
        name: "${friendly_name} LD2410 G7 Move Threshold"
        entity_category: config
      still_threshold:
        name: "${friendly_name} LD2410 G7 Still Threshold"
        entity_category: config
    g8:
      move_threshold:
        name: "${friendly_name} LD2410 G8 Move Threshold"
        entity_category: config
      still_threshold:
        name: "${friendly_name} LD2410 G8 Still Threshold"
        entity_category: config

#:########################################################################################:#
# SELECT:
# https://esphome.io/components/select/index.html
#:########################################################################################:#
select:
  - platform: ld2410
    ld2410_id: ld2410_1

    distance_resolution:
      name: "${friendly_name} LD2410 Distance Resolution"
      entity_category: config
    baud_rate:
      name: "${friendly_name} LD2410 Baud Rate"
      entity_category: config
    light_function:
      name: "${friendly_name} LD2410 Light Function"
      entity_category: config
    out_pin_level:
      name: "${friendly_name} LD2410 Out Pin Level"
      entity_category: config

#:########################################################################################:#
# SENSOR:
# https://esphome.io/components/sensor/
#:########################################################################################:#
sensor:
  - platform: ld2410
    ld2410_id: ld2410_1

    light:
      name: "${friendly_name} LD2410 Light"
      entity_category: diagnostic
    moving_distance:
      name: "${friendly_name} LD2410 Moving Distance"
      entity_category: diagnostic
    still_distance:
      name: "${friendly_name} LD2410 Still Distance"
      entity_category: diagnostic
    moving_energy:
      name: "${friendly_name} LD2410 Move Energy"
      entity_category: diagnostic
    still_energy:
      name: "${friendly_name} LD2410 Still Energy"
      entity_category: diagnostic
    detection_distance:
      name: "${friendly_name} LD2410 Detection Distance"
      entity_category: diagnostic

    g0:
      move_energy:
        name: "${friendly_name} LD2410 G0 Move Energy"
        entity_category: diagnostic
      still_energy:
        name: "${friendly_name} LD2410 G0 Still Energy"
        entity_category: diagnostic
    g1:
      move_energy:
        name: "${friendly_name} LD2410 G1 Move Energy"
        entity_category: diagnostic
      still_energy:
        name: "${friendly_name} LD2410 G1 Still Energy"
        entity_category: diagnostic
    g2:
      move_energy:
        name: "${friendly_name} LD2410 G2 Move Energy"
        entity_category: diagnostic
      still_energy:
        name: "${friendly_name} LD2410 G2 Still Energy"
        entity_category: diagnostic
    g3:
      move_energy:
        name: "${friendly_name} LD2410 G3 Move Energy"
        entity_category: diagnostic
      still_energy:
        name: "${friendly_name} LD2410 G3 Still Energy"
        entity_category: diagnostic
    g4:
      move_energy:
        name: "${friendly_name} LD2410 G4 Move Energy"
        entity_category: diagnostic
      still_energy:
        name: "${friendly_name} LD2410 G4 Still Energy"
        entity_category: diagnostic
    g5:
      move_energy:
        name: "${friendly_name} LD2410 G5 Move Energy"
        entity_category: diagnostic
      still_energy:
        name: "${friendly_name} LD2410 G5 Still Energy"
        entity_category: diagnostic
    g6:
      move_energy:
        name: "${friendly_name} LD2410 G6 Move Energy"
        entity_category: diagnostic
      still_energy:
        name: "${friendly_name} LD2410 G6 Still Energy"
        entity_category: diagnostic
    g7:
      move_energy:
        name: "${friendly_name} LD2410 G7 Move Energy"
        entity_category: diagnostic
      still_energy:
        name: "${friendly_name} LD2410 G7 Still Energy"
        entity_category: diagnostic
    g8:
      move_energy:
        name: "${friendly_name} LD2410 G8 Move Energy"
        entity_category: diagnostic
      still_energy:
        name: "${friendly_name} LD2410 G8 Still Energy"
        entity_category: diagnostic

#:########################################################################################:#
# SWITCH:
# https://esphome.io/components/switch/
#:########################################################################################:#
switch:
  - platform: ld2410
    ld2410_id: ld2410_1

    engineering_mode:
      name: "${friendly_name} LD2410 Engineering Mode"
      entity_category: config

    # Only useful if you have a B/C model (bluetooth hardware present)
    #bluetooth:
    #  name: "${friendly_name} LD2410 Bluetooth Enable"
    #  entity_category: config

#:########################################################################################:#
# BUTTON:
# https://esphome.io/components/button/index.html
#:########################################################################################:#
button:
  - platform: ld2410
    ld2410_id: ld2410_1

    factory_reset:
      name: "${friendly_name} LD2410 Factory Reset"
      entity_category: config
    restart:
      name: "${friendly_name} LD2410 Restart"
      entity_category: config
    query_params:
      name: "${friendly_name} LD2410 Query Params"
      entity_category: diagnostic

#:########################################################################################:#
# TEXT SENSOR:
# https://esphome.io/components/text_sensor/
#:########################################################################################:#
text_sensor:
  - platform: ld2410
    ld2410_id: ld2410_1

    version:
      name: "${friendly_name} LD2410 Firmware Version"
      entity_category: diagnostic
    mac_address:
      name: "${friendly_name} LD2410 Bluetooth MAC"
      entity_category: diagnostic

#:########################################################################################:#
# BINARY SENSOR:
# https://esphome.io/components/binary_sensor/
#:########################################################################################:#
binary_sensor:
  # LD2410 presence states
  - platform: ld2410
    ld2410_id: ld2410_1

    has_target:
      name: "${friendly_name} Presence"
      id: mmwave_presence
    has_moving_target:
      name: "${friendly_name} Moving Target"
      id: mmwave_moving
    has_still_target:
      name: "${friendly_name} Still Target"
      id: mmwave_still
    out_pin_presence_status:
      name: "${friendly_name} LD2410 Out Pin Presence Status"
      entity_category: diagnostic

  # Standard PIR Sensor
  - platform: gpio
    id: pir_motion
    pin:
      number: GPIO13
      mode:
        input: true
        pullup: false
      inverted: false
    filters:
      - delayed_on: 50ms
    name: "${friendly_name} PIR Sensor"
    device_class: motion

  # RF Input from Vibration Sensor (Green Bin)
  - platform: gpio
    id: green_bin_motion
    pin:
      number: GPIO4
      mode:
        input: true
        pullup: true
      inverted: true
    filters:
      - delayed_on: 20ms
    name: "${friendly_name} Green Bin Motion"
    device_class: vibration

  # RF Input from Vibration Sensor (Red Bin)
  - platform: gpio
    id: red_bin_motion
    pin:
      number: GPIO15
      mode:
        input: true
        pullup: true
      ignore_strapping_warning: true
      inverted: true
    filters:
      - delayed_on: 20ms
    name: "${friendly_name} Red Bin Motion"
    device_class: vibration

  # Composite occupancy (mmWave OR PIR) for lighting/automation use
  - platform: template
    name: "${friendly_name} Occupancy"
    device_class: occupancy
    lambda: |-
      return id(mmwave_presence).state || id(pir_motion).state;
    filters:
      - delayed_off: 30s