substitutions:
  device_name: a17130
  friendly_name: Zwembad
  displayDelay: 5s
esp32:
  board: esp32-c3-devkitm-1
  framework:
    type: esp-idf
esphome:
  name: ${device_name}  
  friendly_name: ${friendly_name}  
  min_version: 2025.5.0
  name_add_mac_suffix: true
# Enable logging
logger:
  level: DEBUG
wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_password
  manual_ip:
    static_ip: 192.168.1.165
    gateway: 192.168.1.254
    subnet: 255.255.255.0    
    dns1: 192.168.1.1
  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: !secret ap_ssid
    password: !secret ap_password
captive_portal:
web_server:
  port: 80
  version: 3
ota:
  - platform: !secret ota_platform
    password: !secret ota_password
# Enable Home Assistant API
api:

i2c:
  sda: GPIO20
  scl: GPIO21
  scan: true
  id: bus_a
  frequency: 400kHz   
font:
  - file: "fonts/Helvetica.ttf"
    id: helvetica
    size: 10
  - file: "fonts/Helvetica.ttf"
    id: small_font
    size: 9
# Display configuration (SSD1306 128x64 OLED)
display:
  - platform: ssd1306_i2c
    model: "SH1106 128x64"
    address: 0x3C
    update_interval: 0.5s
    id: my_display
    pages:
      - id: page1
        lambda: |-
          it.print(0, 0, id(helvetica), "Overzicht");
          it.printf(1,15,id(helvetica),"Sensor aanvoer: %8f.0", id(aanvoerTemp).state);          
          it.printf(1,25,id(helvetica),"Sensor retour: %8f.0", id(retourTemp).state);
          it.printf(1,35,id(helvetica),"Dak: %8f.0", id(dakTemp).state);          
          it.printf(1,45,id(helvetica),"Bad: %8f.0", id(badTemp).state);          

      - id: page2
        lambda: |-
          it.print(0, 0, id(helvetica), "Page 2");
          it.print(0, 20, id(helvetica), "Settings");
      - id: page3
        lambda: |-
          it.print(0, 0, id(helvetica), "Page 3");
          it.print(0, 20, id(helvetica), "Status");
      - id: page4
        lambda: |-
          it.print(0, 0, id(helvetica), "Hardware");
          it.printf(0, 15, id(helvetica), "Uptime: %s", id(uptime_sensor).state.c_str());
          it.printf(0, 25, id(helvetica), "IP: %s", id(ip_addr).state.c_str());
          it.printf(0, 35, id(helvetica), "WiFi RSSI: %d dBm", (int)id(wifi_signal1).state);
     
time:
  - platform: homeassistant
    id: my_time

# Button configuration
binary_sensor:
  - platform: gpio
    pin: GPIO03
    name: "Menu Button"
    on_press:
      - logger.log: "Button pressed"
      - script.execute: reset_oled_timeout          

light:
  - platform: monochromatic
    name: displayBL
    id: displayBL
    output: my_display1
 #   restore_mode: ALWAYS_ON
    initial_state:
      state: true      
    on_turn_on:
      - logger.log: 
          format: "Display On begin: %d"
          args: [ 'id(oled_on)' ]
      - lambda: id(my_display).turn_on();
 #     - delay: !lambda 'return id(displayDelay);'
      - lambda: id(my_display).turn_off();   
    on_turn_off:
      then:
      - logger.log:
          format: "Display Off: %d"
          args: [ 'id(oled_on)' ]
      - lambda: id(my_display).turn_off();     
output:
  - platform: template
    id: my_display1
    type: float
    write_action:
      then:
      - lambda: id(my_display).set_contrast(state);
      

# status_led:
#   pin:
#     number: GPIO08
#     inverted: true
# time:
#   - platform: sntp
#     id: sntp_time

# DS18B20 Temperature Sensor
one_wire:
  - platform: gpio
    pin: GPIO5
    id: bus1
  - platform: gpio
    pin: GPIO6
    id: bus2
  - platform: gpio
    pin: GPIO7
    id: bus3
  - platform: gpio
    pin: GPIO8
    id: bus4            

sensor:
  - platform: dallas_temp
    one_wire_id: bus1
    name: "aanvoer"  
    id: aanvoerTemp
    #address: 0x1b3c01d607832828
  - platform: dallas_temp
    one_wire_id: bus2
    name: "retour"  
    id: retourTemp
    #address: 0x813c01d6076ecf28 
  - platform: dallas_temp
    one_wire_id: bus3
    name: "dak"  
    id: dakTemp
    #address: 0x94041722904dff28
  - platform: dallas_temp
    one_wire_id: bus4
    name: "bad"  
    id: badTemp
    #address: 0xe6031722aa43ff28 

# Rotary encoder configuration
  - platform: rotary_encoder
    name: "Rotary Encoder"
    id: my_encoder
    pin_a: GPIO00
    pin_b: GPIO01
    min_value: 0
    max_value: 3
    resolution: 1
    on_value:
      then:
        - script.execute: reset_oled_timeout    
    on_clockwise:
      - display.page.show_next: my_display
    on_anticlockwise:
      - display.page.show_previous: my_display
  - platform: uptime
    name: Uptime Sensor
    id: uptime_seconds
    update_interval: 5s
    internal: true
    on_value:
      then:
        - lambda: |-
            int secs = int(x);
            int mins = secs / 60;
            int hrs = mins / 60;
            int days = hrs / 24;
            char buf[32];
            sprintf(buf, "%dD %02d:%02d:%02d", days, hrs % 24, mins % 60, secs % 60);
            id(uptime_sensor).publish_state(buf);

  - platform: wifi_signal
    name: "WiFi Signal"
    id: wifi_signal1
    update_interval: 5s
    internal: true

globals:
  - id: oled_on
    type: bool
    restore_value: no
    initial_value: "true"

interval:
  - interval: 1s
    then:
      - if:
          condition:
            - not:
                - script.is_running: oled_timeout
          then:
            - script.execute: oled_timeout

script:
  - id: reset_oled_timeout
    then:
      - lambda: |-
          id(oled_on) = true;
#      - display.page.show: my_display
      - lambda: id(my_display).turn_on();
      - script.stop: oled_timeout
      - script.execute: oled_timeout

  - id: oled_timeout
    mode: restart
    then:
      - delay: 5s
      - lambda: |-
          id(oled_on) = false;
#      - display.page.show: my_display
      - lambda: id(my_display).turn_off();



text_sensor:
  - platform: wifi_info
    ip_address:
      name: ESP32 IP Address
      id: ip_addr
      internal: true

  - platform: template
    name: "Uptime Readable"
    id: uptime_sensor
    internal: true
# Example configuration entry
switch:
  - platform: gpio
    pin: GPIO04
    id: relay
    inverted: true
    name: "Zwembadpomp"
    icon: "mdi:pump"
    on_turn_on:
    - delay: 500ms
    - switch.turn_off: relay    
  # - platform: gpio
  #   pin: GPIO10
  #   id: relay2
  #   inverted: true
  #   name: "Zwembadpomp 2"
  #   icon: "mdi:pump"
  #   on_turn_on:
  #   - delay: 500ms
  #   - switch.turn_off: relay2