# =====================================================================
#  示例 3：温度曲线图 UI
#  布局：顶栏当前温度 / 中间 1 小时折线图 / 底部 Min Avg Max 统计
#  硬件依赖：显示屏、SHTC3
# =====================================================================

esphome:
  name: esp32-s3-rlcd-42
  friendly_name: ESP32-S3-RLCD-4.2

esp32:
  board: esp32-s3-devkitc-1
  framework:
    type: esp-idf

psram:
  mode: octal
  speed: 80MHz

external_components:
  - source: github://kylehase/ESPHome-ST7305-RLCD
    components: [st7305_rlcd]

logger:

api:
  encryption:
    key: "7J7XVVoREEncoG39FrOxlb/ZVMRS/u7RmTc4WK2Ej7w="

ota:
  - platform: esphome
    password: "d9a7ee09077e2b8c27f2563e475e66ea"

wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_password
  ap:
    ssid: "Esp32-S3-Rlcd-42"
    password: "Wa3PMQZVj5kn"

captive_portal:

# ---------- 总线 ----------
i2c:
  - id: bus_a
    sda: GPIO13
    scl: GPIO14
    scan: true

spi:
  - id: spi_lcd
    clk_pin: GPIO11
    mosi_pin: GPIO12

# ---------- 时间 ----------
time:
  - platform: homeassistant
    id: ha_time

# ---------- 传感器（含 1h 滑窗的 Min/Avg/Max）----------
sensor:
  - platform: shtcx
    address: 0x70
    i2c_id: bus_a
    update_interval: 60s
    temperature:
      name: "Temperature"
      id: temp_sensor
    humidity:
      name: "Humidity"
      id: hum_sensor

  - platform: copy
    source_id: temp_sensor
    id: temp_min
    name: "Temperature Min (1h)"
    filters:
      - min:
          window_size: 60
          send_every: 60
          send_first_at: 1

  - platform: copy
    source_id: temp_sensor
    id: temp_max
    name: "Temperature Max (1h)"
    filters:
      - max:
          window_size: 60
          send_every: 60
          send_first_at: 1

  - platform: copy
    source_id: temp_sensor
    id: temp_avg
    name: "Temperature Avg (1h)"
    filters:
      - sliding_window_moving_average:
          window_size: 60
          send_every: 60
          send_first_at: 1

# ---------- 字体 ----------
font:
  - file: "gfonts://Roboto"
    id: font_xs
    size: 14
    glyphs: &glyphs "!\"#%()+,-_.:°0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz/ "
  - file: "gfonts://Roboto"
    id: font_sm
    size: 18
    glyphs: *glyphs

# ---------- 折线图 ----------
graph:
  - id: temp_graph
    sensor: temp_sensor
    duration: 1h
    width: 360
    height: 180
    border: true
    x_grid: 10min
    y_grid: 1.0

# ---------- 显示屏 ----------
display:
  - platform: st7305_rlcd
    id: my_display
    model: WAVESHARE_400X300
    width: 400
    height: 300
    cs_pin: GPIO40
    dc_pin: GPIO5
    reset_pin: GPIO41
    data_rate: 1MHz
    update_interval: 60s
    lambda: |-
      // 顶栏
      it.print(10, 6, id(font_sm), "Temperature - last 1 hour");
      it.printf(390, 6, id(font_sm), TextAlign::TOP_RIGHT,
                "%.1f °C", id(temp_sensor).state);
      it.line(0, 30, 400, 30);

      // 主体图表
      it.graph(20, 45, id(temp_graph));

      // 底部统计
      it.line(0, 245, 400, 245);
      it.printf(10, 255, id(font_sm),
                "Min %.1f   Avg %.1f   Max %.1f  (°C)",
                id(temp_min).state, id(temp_avg).state, id(temp_max).state);
      it.strftime(10, 280, id(font_xs), "Updated %H:%M:%S",
                  id(ha_time).now());

# ---------- 按键：BOOT 立即刷屏 ----------
binary_sensor:
  - platform: gpio
    name: "Boot Button"
    pin:
      number: GPIO0
      inverted: true
      mode: INPUT
    on_press:
      - component.update: my_display
