# =====================================================================
# CYD Home Assistant control panel — ESP32-2432S028R (Cheap Yellow Display)
# Written for MCU Atlas (mcuatlas.com). Compile-verified with ESPHome 2026.10.2.
# License: MIT — MCU Atlas. Page: mcuatlas.com/projects/cyd-home-assistant-touch-panel/
# Pin map, touch calibration and transform taken from a hardware-tested
# 2432S028R ESPHome+LVGL config (Simone7121 via anthony-spruyt,
# ESPHOME-ESP32_CYD_V2-SmartEnergyMeter) and cross-checked against the
# ESPHome component docs (ili9xxx, xpt2046, lvgl).
# =====================================================================

substitutions:
  device_name: cyd-ha-panel
  friendly_name: CYD HA Panel
  # Replace these three with your own Home Assistant entity IDs
  light_entity: "light.desk_lamp"
  switch_entity: "switch.coffee_maker"
  temp_entity: "sensor.living_room_temperature"

esphome:
  name: $device_name
  friendly_name: $friendly_name

esp32:
  board: esp32dev
  framework:
    type: arduino

logger:

api:
  encryption:
    key: !secret api_key
  on_client_connected:
    - if:
        condition:
          lambda: 'return (0 == client_info.find("Home Assistant "));'
        then:
          - lvgl.widget.show: lbl_hastatus
  on_client_disconnected:
    - if:
        condition:
          lambda: 'return (0 == client_info.find("Home Assistant "));'
        then:
          - lvgl.widget.hide: lbl_hastatus

ota:
  - platform: esphome
    password: !secret ota_password

wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_password
  ap:
    ssid: "$friendly_name Fallback"
    password: "1234567890"

captive_portal:

# ---------------------------------------------------------------
# Hardware: two SPI buses (display and touch are separate on the CYD)
# ---------------------------------------------------------------
spi:
  - id: tft
    clk_pin: GPIO14
    mosi_pin: GPIO13
    miso_pin: GPIO12
  - id: touch
    clk_pin: GPIO25
    mosi_pin: GPIO32
    miso_pin: GPIO39

output:
  - platform: ledc
    pin: GPIO21
    id: backlight_pwm

light:
  - platform: monochromatic
    output: backlight_pwm
    name: "Display backlight"
    id: backlight
    restore_mode: ALWAYS_ON

display:
  - platform: ili9xxx
    model: ILI9341
    id: yellow_screen
    spi_id: tft
    cs_pin: GPIO15
    dc_pin: GPIO2
    rotation: 0
    dimensions:
      height: 240
      width: 320
    invert_colors: false
    auto_clear_enabled: false
    update_interval: never

touchscreen:
  - platform: xpt2046
    id: yellow_touchscreen
    spi_id: touch
    cs_pin: GPIO33
    interrupt_pin: GPIO36
    update_interval: 50ms
    threshold: 400
    calibration:
      x_min: 300
      x_max: 3900
      y_min: 200
      y_max: 3700
    transform:
      swap_xy: true
      mirror_x: true
      mirror_y: true

# ---------------------------------------------------------------
# UI: LVGL, dark theme, one page of controls
# ---------------------------------------------------------------
lvgl:
  theme:
    dark_mode: true
  # Classic ESP32 has no PSRAM; ESPHome docs recommend a 25% draw buffer
  buffer_size: 25%
  top_layer:
    widgets:
      - label:
          text: "\uF1EB"
          id: lbl_hastatus
          hidden: true
          align: top_right
          x: -10
          y: 8
          text_color: 0x00FF00
  pages:
    - id: main_page
      widgets:
        - label:
            text: "Home"
            x: 10
            y: 8
            text_font: montserrat_20
            text_color: 0xFFFFFF
        - button:
            id: btn_light
            text: "Desk lamp"
            checkable: true
            x: 10
            y: 60
            width: 145
            height: 70
            on_click:
              homeassistant.action:
                action: light.toggle
                data:
                  entity_id: ${light_entity}
            checked:
              bg_color: 0x2E7D32
        - button:
            id: btn_switch
            text: "Coffee maker"
            checkable: true
            x: 165
            y: 60
            width: 145
            height: 70
            on_click:
              homeassistant.action:
                action: switch.toggle
                data:
                  entity_id: ${switch_entity}
            checked:
              bg_color: 0x2E7D32
        - label:
            text: "Living room"
            x: 10
            y: 160
            text_font: montserrat_16
            text_color: 0xFFFFFF
        - label:
            id: temp_value
            text: "--"
            x: 200
            y: 160
            text_font: montserrat_16
            text_color: 0xFFFFFF

# ---------------------------------------------------------------
# State feedback: import entity states from Home Assistant
# ---------------------------------------------------------------
binary_sensor:
  - platform: homeassistant
    id: bs_light
    entity_id: ${light_entity}
    on_state:
      - lvgl.widget.update:
          id: btn_light
          state:
            checked: !lambda 'return x;'
  - platform: homeassistant
    id: bs_switch
    entity_id: ${switch_entity}
    on_state:
      - lvgl.widget.update:
          id: btn_switch
          state:
            checked: !lambda 'return x;'

sensor:
  - platform: homeassistant
    id: ha_temp
    entity_id: ${temp_entity}
    on_value:
      - lvgl.label.update:
          id: temp_value
          text: !lambda 'return (String(x, 1) + " °C").c_str();'
