# =============================================================================
#  shh-c6-thread.yaml — XIAO ESP32-C6 + CHT832X (DFRobot SEN0546) σε Thread
#  Smart Home Hellas · https://smarthomehellas.gr/blog/xiao-esp32-c6-wifi-thread-zigbee/
#
#  Χρειάζεται Thread Border Router στο Home Assistant (π.χ. Sonoff ZBDongle-E με
#  firmware OpenThread RCP + το add-on OpenThread Border Router, Hardware flow control OFF).
#  Καλωδίωση (pinout Seeed): SDA -> D4 (GPIO22), SCL -> D5 (GPIO23), VCC -> 3V3, GND -> GND
#  Secrets που χρειάζονται (δες secrets.example.yaml): ota_password, thread_tlv
# =============================================================================

substitutions:
  device_name: shh-c6-thread
  friendly_name: "XIAO C6 Thread"

esphome:
  name: ${device_name}
  friendly_name: ${friendly_name}
  min_version: 2025.6.0     # OpenThread support

esp32:
  board: seeed_xiao_esp32c6
  framework:
    type: esp-idf          # υποχρεωτικό στο C6 (με Arduino δεν χτίζει)

logger:
  level: INFO
  hardware_uart: USB_SERIAL_JTAG

# ---------- Δίκτυο: ΟΧΙ WiFi, μόνο Thread / IPv6 ----------
network:
  enable_ipv6: true

openthread:
  device_type: MTD          # Minimal Thread Device (end device): δεν διεκδικεί ρόλο router/leader
  # Το TLV dataset του δικτύου σου, από το Home Assistant:
  #   Developer Tools -> Actions -> otbr.get_active_dataset_tlvs -> αντίγραψε το hex string
  # και βάλε το στο secrets.yaml ως thread_tlv.
  tlv: !secret thread_tlv

# ---------- Home Assistant API (τρέχει πάνω από Thread / IPv6) ----------
api:
  reboot_timeout: 0s

ota:
  - platform: esphome
    password: !secret ota_password

# ---------- I2C bus (pinout Seeed: SDA = D4 = GPIO22, SCL = D5 = GPIO23) ----------
i2c:
  sda: GPIO22
  scl: GPIO23
  frequency: 100kHz
  scan: true                # στο log θα δεις τη διεύθυνση 0x44 του CHT832X

sensor:
  # CHT832X (DFRobot SEN0546) — συμβατός με SHT3x, διεύθυνση 0x44.
  - platform: sht3xd
    temperature:
      name: "${friendly_name} Temperature"
      id: sht_temperature
      filters:
        - timeout: 180s
    humidity:
      name: "${friendly_name} Humidity"
      id: sht_humidity
      filters:
        - timeout: 180s
    address: 0x44
    update_interval: 60s

  # ---------- Παράγωγα από θερμοκρασία + υγρασία ----------
  - platform: template
    name: "${friendly_name} Absolute Humidity"
    id: sht_abs_humidity
    lambda: |-
      const float mw = 18.01534;
      const float r = 8.31447215;
      return (6.112 * powf(2.718281828, (17.67 * id(sht_temperature).state) /
        (id(sht_temperature).state + 243.5)) * id(sht_humidity).state * mw) /
        ((273.15 + id(sht_temperature).state) * r);
    accuracy_decimals: 2
    update_interval: 60s
    icon: "mdi:water"
    unit_of_measurement: "g/m³"

  - platform: template
    name: "${friendly_name} Dew Point"
    id: sht_dew_point
    lambda: |-
      return (243.5*(log(id(sht_humidity).state/100)+((17.67*id(sht_temperature).state)/
      (243.5+id(sht_temperature).state)))/(17.67-log(id(sht_humidity).state/100)-
      ((17.67*id(sht_temperature).state)/(243.5+id(sht_temperature).state))));
    unit_of_measurement: °C
    icon: "mdi:thermometer-alert"

  - platform: uptime
    name: "${friendly_name} Uptime"
    id: uptime_seconds
    entity_category: diagnostic

# ---------- Ώρα ----------
# Το SNTP θέλει δρόμο προς το internet μέσω του Border Router. Αν η ώρα μένει κενή,
# ο OTBR σου δεν κάνει external routing -> σβήσε το time block και τα text_sensor που το χρησιμοποιούν.
time:
  - platform: sntp
    id: sntp_time
    timezone: Europe/Athens

text_sensor:
  - platform: template
    name: "${friendly_name} Time"
    lambda: |-
      char str[17];
      time_t currTime = id(sntp_time).now().timestamp;
      strftime(str, sizeof(str), "%d-%m-%Y %H:%M", localtime(&currTime));
      return {str};
    update_interval: 60s

  - platform: version
    name: "${friendly_name} ESPHome Version"
    hide_timestamp: true
    entity_category: diagnostic
    disabled_by_default: true

  - platform: template
    name: "${friendly_name} Last Boot Time"
    entity_category: diagnostic
    disabled_by_default: true
    lambda: |-
      auto boot_time = id(sntp_time).now().timestamp - (time_t)id(uptime_seconds).state;
      char str[30];
      strftime(str, sizeof(str), "%d-%m-%Y %H:%M:%S", localtime(&boot_time));
      return {str};
    update_interval: 5min

# ---------- Κουμπιά ----------
button:
  - platform: restart
    name: "${friendly_name} Restart"
    entity_category: config

  - platform: factory_reset
    name: "${friendly_name} Factory Reset"
    entity_category: config
    disabled_by_default: true

# ---------- Σημειώσεις ----------
# * Πρώτο flash με καλώδιο USB-C. OTA πάνω από Thread μετά.
# * Αν το C6 δεν εμφανίζεται στο HA: έλεγξε ότι ο Border Router είναι leader στο Thread panel
#   και ότι το thread_tlv είναι από το ΙΔΙΟ δίκτυο.
# * Γνωστό θέμα: χωρίς device_type: MTD το C6 μπορεί να διεκδικήσει ρόλο leader και να
#   αναστατώσει το υπάρχον mesh.
