substitutions:
  name: wallbox-esp32-lcd
  friendly_name: Display-esp32-Wallbox

esphome:
  name: ${name}
  friendly_name: ${friendly_name}
  min_version: 2024.6.0
  name_add_mac_suffix: false
  project:
    name: esphome.web
    version: dev

esp32:
  board: esp32dev
  framework:
    type: arduino

# Enable logging
logger:
  level: DEBUG

# Enable Home Assistant API
api:

# Allow Over-The-Air updates
ota:
- platform: esphome

# Allow provisioning Wi-Fi via serial
improv_serial:

wifi:
  ssid: "Votre-SSID"
  password: "Votre mot de passe"

  ap:
    ssid: "ESP32-Wallbox"
    password: "12345678"

# In combination with the `ap` this allows the user
# to provision wifi credentials to the device via WiFi AP.
captive_portal:

dashboard_import:
  package_import_url: github://esphome/example-configs/esphome-web/esp32.yaml@main
  import_full_config: true

# Sets up Bluetooth LE (Only on ESP32) to allow the user
# to provision wifi credentials to the device.
# esp32_improv:
#   authorizer: none

# To have a "next url" for improv serial
web_server:

font:
  - file: "arial.ttf"
    id: arial
    size: 17

  - file: "/config/esphome/Roboto-Regular.ttf"
    id: roboto
    size: 30  # Taille de la police pour "Watts"

i2c:
  sda: GPIO21
  scl: GPIO22

sensor:
  - platform: homeassistant
    id: wallbox_power
    entity_id: sensor.wallbox_16a_wallbox_16a_power
  - platform: homeassistant
    id: wallbox_daily
    entity_id: sensor.wallbox_daily
  - platform: homeassistant
    id: pylontech_soc_minimum
    entity_id: sensor.pylontech_soc_minimum
  - platform: homeassistant
    id: pv_production
    entity_id: sensor.pv_production

globals:
  - id: last_update_time
    type: unsigned long
    restore_value: no
    initial_value: '0'
  - id: display_state
    type: int
    restore_value: no
    initial_value: '0'

time:
  - platform: homeassistant
    id: homeassistant_time

interval:
  - interval: 5s
    then:
      - lambda: |-
          float rssi = WiFi.RSSI();
          ESP_LOGD("wifi_signal", "Puissance du Signal WiFi: %.1f dBm", rssi);

display:
  - platform: ssd1306_i2c
    model: "SSD1306 128x64"
    reset_pin: GPIO17
    address: 0x3C
    id: my_display
    lambda: |-
      unsigned long current_time = millis();
      if (current_time - id(last_update_time) >= 3000) {  // 3 seconds interval
        id(last_update_time) = current_time;
        id(display_state) = (id(display_state) + 1) % 5;
      }
      if (id(display_state) == 0) {
        it.print(0, 0, id(arial), TextAlign::LEFT, "Conso. voiture");
        float power = id(wallbox_power).state;
        if (!isnan(power)) {
          if (power >= 1000) {
            power /= 1000;
            it.printf(0, 28, id(roboto), TextAlign::LEFT, "%.1f kW", power);
          } else {
            it.printf(0, 28, id(roboto), TextAlign::LEFT, "%.0f W", power);
          }
        } else {
          it.print(0, 28, id(roboto), TextAlign::LEFT, "N/A");
        }
      } else if (id(display_state) == 1) {
        it.print(0, 0, id(arial), TextAlign::LEFT, "Energie jour");
        float daily = id(wallbox_daily).state;
        if (!isnan(daily)) {
          if (daily >= 1000) {
            daily /= 1000;
            it.printf(0, 28, id(roboto), TextAlign::LEFT, "%.1f kWh", daily);
          } else {
            it.printf(0, 28, id(roboto), TextAlign::LEFT, "%.0f kWh", daily);
          }
        } else {
          it.print(0, 28, id(roboto), TextAlign::LEFT, "N/A");
        }
      } else if (id(display_state) == 2) {
        it.print(0, 0, id(arial), TextAlign::LEFT, "Batterie SOC");
        float soc = id(pylontech_soc_minimum).state;
        if (!isnan(soc)) {
          it.printf(0, 28, id(roboto), TextAlign::LEFT, "%.0f %% ", soc);
        } else {
          it.print(0, 28, id(roboto), TextAlign::LEFT, "N/A");
        }
      } else if (id(display_state) == 3) {
        it.print(0, 0, id(arial), TextAlign::LEFT, "Panneaux sol");
        float pv = id(pv_production).state;
        if (!isnan(pv)) {
          if (pv >= 1000) {
            pv /= 1000;
            it.printf(0, 28, id(roboto), TextAlign::LEFT, "%.1f kW", pv);
          } else {
            it.printf(0, 28, id(roboto), TextAlign::LEFT, "%.0f W", pv);
          }
        } else {
          it.print(0, 28, id(roboto), TextAlign::LEFT, "N/A");
        }
      } else if (id(display_state) == 4) {
        it.print(0, 0, id(arial), TextAlign::LEFT, "Time");
        auto time = id(homeassistant_time).now();
        if (time.is_valid()) {
          it.strftime(0, 28, id(roboto), TextAlign::LEFT, "%H:%M", time);
        } else {
          it.print(0, 28, id(roboto), TextAlign::LEFT, "N/A");
        }
      }


# Création d'un bouton de redémarrage de l'esp en cas de plantée de l'écran
switch:
  - platform: restart
    name: "Wallbox ESP32-Vroom restart"
