# =============================================================================
#  shh-c6-zigbee.yaml — XIAO ESP32-C6 + CHT832X (DFRobot SEN0546) σε Zigbee
#  Smart Home Hellas · https://smarthomehellas.gr/blog/xiao-esp32-c6-wifi-thread-zigbee/
#
#  Χρειάζεται Zigbee coordinator στο Home Assistant (ZHA ή Zigbee2MQTT),
#  ESPHome 2026.4.0+ και το partitions_zb.csv ΔΙΠΛΑ σε αυτό το αρχείο (/config/esphome/).
#  Καλωδίωση (pinout Seeed): SDA -> D4 (GPIO22), SCL -> D5 (GPIO23), VCC -> 3V3, GND -> GND
#  Δεν χρειάζεται κανένα secret.
# =============================================================================

substitutions:
  device_name: shh-c6-zigbee
  friendly_name: "XIAO C6 Zigbee"

esphome:
  name: ${device_name}
  friendly_name: ${friendly_name}
  min_version: 2026.4.0     # native zigbee component

esp32:
  board: seeed_xiao_esp32c6
  partitions: partitions_zb.csv   # ΑΠΑΡΑΙΤΗΤΟ: το csv στον ίδιο φάκελο με το yaml
  framework:
    type: esp-idf          # υποχρεωτικό στο C6 (με Arduino δεν χτίζει)

logger:
  level: INFO
  hardware_uart: USB_SERIAL_JTAG

# ΠΡΟΣΟΧΗ: Zigbee end device = ΔΕΝ υπάρχει IP δίκτυο.
# Εδώ ΔΕΝ μπαίνουν: wifi, api, ota, web_server, time/sntp.
# Οι τιμές πάνε στον coordinator μέσω Zigbee clusters, όχι μέσω του HA API.

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

# ---------- Zigbee (native component) ----------
# Οι αισθητήρες που θέλουμε να «βγουν» στο Zigbee παίρνουν report: force.
# Έγκυρες τιμές για το report: force, enable, coordinator, default (ΟΧΙ true).
zigbee:

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

  # Παράγωγα -> ΤΟΠΙΚΑ μόνο (δεν υπάρχει standard Zigbee cluster για αυτά).
  # Τα κρατάμε για το log· δεν παίρνουν report και δεν εμφανίζονται στο HA.
  - 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"

# ---------- Κουμπιά ----------
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 (Install -> Plug into this computer). Logs από το USB.
# * ZHA -> Add device / Permit join. Κάνε το pair ΚΟΝΤΑ στον coordinator (4-6 m) και μετά
#   μετακίνησε τη συσκευή στην τελική της θέση.
# * «Failed to join»: πριν ψάξεις οτιδήποτε άλλο, άλλαξε τροφοδοτικό. Το C6 τραβάει
#   αιχμές 200-300 mA στην εκπομπή· φτηνά τροφοδοτικά και power banks κάνουν brownout.
# * Αλλαγή config -> συχνά χρειάζεται re-pairing (delete + permit join ξανά).
