# Example configuration entry

# wiring: 
# D4  GPIO2   - D/C
# D5  GPIO14  - SCK
# D6  GPIO12  - MISO
# D7  GPIO13  - MOSI
# D8  GPIO15  - TFT_CS
# GND         - Gnd
# 3V          - VCC, LITE, RESET

esphome:
  name: espdisplay
  
api:

ota:
  safe_mode: true

esp8266:
  board: nodemcuv2
 
font:
  - file: 'arial.ttf'
    id: arial10
    size: 10

  - file: 'arial.ttf'
    id: arial11
    size: 11

  - file: 'arial.ttf'
    id: arial12
    size: 12

  - file: 'arial.ttf'
    id: arial14
    size: 14
    
  - file: 'arial.ttf'
    id: arial20
    size: 20

color:
  - id: red100
    red: 100%
    green: 0%
    blue: 0%

  - id: blue100
    red: 0%
    green: 0%
    blue: 100%

  - id: green100
    red: 0%
    green: 100%
    blue: 0%
    
  - id: yellow
    red: 0%
    green: 100%
    blue: 100%

  - id: gray
    red: 50%
    green: 50%
    blue: 50%
    
  - id: lightgray
    red: 80%
    green: 80%
    blue: 80%

  - id: timecolor
    red: 80%
    green: 100%
    blue: 90%

  - id: ghcolor
    red: 60%
    green: 70%
    blue: 100%

  - id: azcolor
    red: 100%
    green: 60%
    blue: 60%
  
time:
  - platform: sntp
    id: nettime
  
# Enable logging
logger:
  level: DEBUG 

spi:
  clk_pin: GPIO14  # D5
  mosi_pin: GPIO13 # D7

wifi:
  ssid: "bram"
  password: "0263648951574780"
  
sensor:
  - platform: wifi_signal
    id: WiFiSignal
    name: "WiFi_Signal"
    update_interval: 60s
    
  - platform: homeassistant
    id: wg_temperature
    entity_id: sensor.wg_temperature

  - platform: homeassistant
    id: gh_temperature
    entity_id: sensor.gh_temperaturemp

  - platform: homeassistant
    id: az_temperature
    entity_id: sensor.rpibram_temp

  - platform: homeassistant
    id: stromaktuell
    entity_id: sensor.stromverbrauch
    
text_sensor:
  - platform: wifi_info
    ip_address:
      name: ESP IP Address
    mac_address:
      name: ESP Mac Wifi Address

#graph:
# - id: stromgraf
#   sensor: WiFiSignal
#   duration: 1h
#   width: 120
#   height: 60 
  # - id: multi_temperature_graph
    # duration: 1h
    # x_grid: 10min
    # y_grid: 1.0     # degC/div
    # width: 120
    # height: 70
    # traces:
      # - sensor: stromaktuell
        # line_type: SOLID
        # line_thickness: 2
        # color: wgcolor

display:
  - platform: st7735
    model: "INITR_BLACKTAB" # "INITR_18BLACKTAB"
    reset_pin: D4
    cs_pin: D8
    dc_pin: D4
    rotation: 90
    device_width: 128
    device_height: 160
    col_start: 0
    row_start: 0
    eight_bit_color: true
    update_interval: 5s
    lambda: |-
        it.rectangle(0, 0, 160, 128, gray);
        it.rectangle(0, 0, 80, 54, gray);
        it.rectangle(80, 0, 160, 54, gray);
        it.rectangle(0, 54, 80, 54, gray);
        it.rectangle(80, 54, 80, 54, gray);

        it.print(8, 2, id(arial10), lightgray, "Wintergarten");
        it.print(84, 2, id(arial10), lightgray, "Gewaechshaus");
        it.print(6, 56, id(arial10), lightgray, "Arbeitszimmer");
        it.print(83, 56, id(arial10), lightgray, "Stromverbrauch");
        
        it.printf(6, 20, id(arial20), "%.1f", id(wg_temperature).state);
        it.print(52, 26, id(arial14), "°C");

        it.printf(86, 20, id(arial20), "%.1f", id(gh_temperature).state);
        it.print(138, 26, id(arial14), "°C");

        it.printf(6, 74, id(arial20), "%.1f", id(az_temperature).state);
        it.print(52, 80, id(arial14), "°C");
        
        if (id(stromaktuell).state > 1.0) {
            it.printf(84, 74, id(arial20), red100, "%.3f", id(stromaktuell).state); 
            it.print(136, 80, id(arial14), red100, "kW");
        }
        else {
            it.printf(84, 74, id(arial20), "%.3f", id(stromaktuell).state);
            it.print(136, 80, id(arial14), "kW");
        }
        //it.printf(84, 74, id(arial20), "%.3f", id(stromaktuell).state);

        it.strftime(4, 112, id(arial12), timecolor, "%d.%m.%Y %H:%M", id(nettime).now());
        it.printf(120, 112, id(arial10), timecolor, "%.0f dBm", id(WiFiSignal).state);
        //it.graph(2, 100, id(stromgraf));
