esphome:
  name: homemaster-microplc
  name_add_mac_suffix: true
  friendly_name: HomeMaster MicroPLC
  comment: HomeMaster MicroPLC
  min_version: 2026.7.0
  project:
    name: homemaster.microplc
    version: "1.3.1"

esp32:
  variant: esp32
  board: esp32dev                       # Target board type (generic ESP32 DevKit)
  flash_size: 16MB
  framework:
    type: esp-idf                       # Use ESP-IDF (official Espressif framework)
    version: recommended                # Recommended stable version

logger:
  baud_rate: 115200                     # Serial logging baud rate

api:                                    # Enable ESPHome API for Home Assistant integration
  encryption:
    # No `key:` here — intentional and required. The key is set during
    # provisioning. A key baked into a publicly published factory binary
    # would be identical on every unit, i.e. equivalent to no key at all.

provisioning:
  timeout: 15min
  on_timeout:
    then:
      - logger.log: "Provisioning window closed. Power-cycle the device to reopen it."

http_request:

ota:
  - platform: esphome
  - platform: http_request

update:
  - platform: http_request
    id: firmware_update
    name: "Firmware Update"
    source: https://isystemsautomation.github.io/homemaster-dev/MicroPLC/Firmware/manifest.json
    update_interval: 6h

wifi:
  on_connect:
    then:
      - delay: 10s
      - component.update: firmware_update

improv_serial:                          # Allows setup via Improv over Serial

esp32_improv:
  authorizer: none                      # No additional authorization required for Improv

dashboard_import:
  package_import_url: github://isystemsautomation/homemaster-dev/MicroPLC/Firmware/microplc.yaml@main
  import_full_config: true
  # Allows importing this YAML from GitHub into ESPHome Dashboard

# RS-485 / Modbus RTU bus. The UART is exposed below as `uart_modbus`.
# Users can attach their own `modbus_controller:` blocks to integrate
# field devices (energy meters, expansion modules, etc.).
uart:
  id: uart_modbus
  tx_pin: GPIO17
  rx_pin: GPIO16
  baud_rate: 19200

time:
  - platform: pcf8563                   # Real-time clock (RTC) module via I2C
    id: pcf8563_time
    address: 0x51                       # I2C address of PCF8563
  - platform: homeassistant
    id: ha_time
    # instead try to synchronize via network repeatedly ...
    on_time_sync:
      then:
              # ... and update the RTC when the synchronization was successful
        - pcf8563.write_time

i2c:
  - id: bus_a
    sda: 32                             # I2C SDA pin
    scl: 33                             # I2C SCL pin
    scan: true                          # Scan for devices at startup

one_wire:
  - platform: gpio
    id: ow_bus_1
    pin: GPIO4                          # Pin for 1-Wire devices (e.g., DS18B20 sensors)

binary_sensor:
  - platform: status
    id: esp_status
    name: "ESP Status"
    entity_category: diagnostic

  - platform: gpio
    id: di_1
    name: "DI #1"
    pin:
      number: GPIO36

sensor:
  - platform: uptime
    id: esp_uptime
    internal: true
    update_interval: 60s

  - platform: wifi_signal
    id: wifi_signal_db
    name: "WiFi Signal"
    update_interval: 60s
    entity_category: diagnostic

  - platform: internal_temperature
    id: esp32_temperature
    name: "ESP32 Temperature"
    update_interval: 60s
    entity_category: diagnostic

  - platform: dallas_temp
    id: ow_bus_1_temperature
    one_wire_id: ow_bus_1
    name: "1-Wire Bus 1 Temperature"
    unit_of_measurement: "°C"

switch:
  - platform: gpio
    name: "Relay"                       # Relay switch exposed to Home Assistant
    pin: 26
    id: relay1

button:
  - platform: restart
    name: "Restart"
    entity_category: config

text_sensor:
  - platform: template
    id: esp_uptime_human
    name: "ESP Uptime Human"
    entity_category: diagnostic
    update_interval: 60s
    lambda: |-
      if (isnan(id(esp_uptime).state)) {
        return {};
      }
      int total_seconds = (int) id(esp_uptime).state;
      int days = total_seconds / 86400;
      int hours = (total_seconds % 86400) / 3600;
      if (days > 0) {
        return {to_string(days) + "d " + to_string(hours) + "h"};
      }
      int minutes = (total_seconds % 3600) / 60;
      if (hours > 0) {
        return {to_string(hours) + "h " + to_string(minutes) + "m"};
      }
      return {to_string(minutes) + "m"};

  - platform: version
    id: esphome_version
    name: "ESPHome Version"
    entity_category: diagnostic

  - platform: wifi_info
    ip_address:
      id: esp_ip_address
      name: "ESP IP Address"
      entity_category: diagnostic

status_led:
  pin:
    number: GPIO25                      # Pin for status LED
    inverted: true                      # Inverted logic (LED ON when pin LOW)
    id: st_led
