esphome:
  name: wassergarten
  friendly_name: Ventil Wasser Garten

esp32:
  board: esp32-s3-devkitc-1
  framework:
    type: arduino

# Enable logging
logger:

# Enable Home Assistant API
api:
  encryption:
    key: "ahsYzpkxOKE2VqdnjNtReYKORpVC5d9RoLQbFFoPZhI="

ota:
  password: "362383d82be0679b3285c5443ed439d8"

wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_password

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "Wassergarten Fallback Hotspot"
    password: "JcRBvcoFCI2m"

captive_portal:

globals:
  - id: var_valve_open
    type: bool
    restore_value: true

binary_sensor:
  - platform: gpio
    id: switch_open
    pin:
      number: GPIO1
      mode:
        input: true
        pullup: true
      inverted: true
    on_press:
      cover.open: valve
    filters:
      delayed_on: 20ms
  - platform: gpio
    id: switch_close
    pin:
      number: GPIO2
      mode:
        input: true
        pullup: true
      inverted: true
    on_press:
      - cover.close: valve
    filters:
      delayed_on: 20ms
  - platform: gpio
    id: profet_error
    name: Driver Error
    entity_category: 'diagnostic'
    pin:
      number: GPIO7
      mode:
        input: true
        pullup: true
      inverted: true
    filters:
      delayed_on: 200ms
    on_press:
      then:
        - cover.stop: valve
  - platform: template
    device_class: opening
    name: 'Valve State'
    id: 'valve_state'
    lambda: |-
      if (id(var_valve_open) == true) {
        return true;
      } else {
        return false;
      }

button:
  - platform: restart
    name: "Reboot Node"
    entity_category: 'diagnostic'

sensor:
  - platform: adc
    id: motor_current
    name: drive current
    entity_category: 'diagnostic'
    pin: 6
    update_interval: 0.2s
    attenuation: 0dB
    device_class: CURRENT
    unit_of_measurement: A
    accuracy_decimals: 3
    filters:
      - multiply: 2.127659574 # 1 / 470 Ohm
      - delta: 0.005 # Only send data if changed

switch:
  - platform: gpio
    id: led_open
    pin:
      number: GPIO3
  - platform: gpio
    id: led_close
    pin:
      number: GPIO4
  - platform: gpio
    id: valve_open
    interlock: &interlock_group [valve_open, valve_close]
    interlock_wait_time: 20ms
    pin:
      number: GPIO8
    restore_mode: ALWAYS_OFF
  - platform: gpio
    id: valve_close
    interlock: &intelock_group
    pin:
      number: GPIO44
    restore_mode: ALWAYS_OFF

status_led:
  pin:
    # number: GPIO5
    number: GPIO21
    inverted: True

cover:
  - platform: current_based
    name: "Time-Based Cover"
    id: valve
    open_action:
      - switch.turn_on: valve_open
      - lambda: |-
          id(var_valve_open) = true; 
    open_duration: 4.5s
    open_sensor: motor_current
    open_moving_current_threshold: 0.01
    open_obstacle_current_threshold: 0.3
    close_action:
      - switch.turn_on: valve_close
    close_duration: 4.5s
    close_sensor: motor_current
    close_moving_current_threshold: 0.01
    close_obstacle_current_threshold: 0.3
    stop_action:
      - switch.turn_off: valve_open
      - switch.turn_off: valve_close
    malfunction_detection: False
    start_sensing_delay: 0.8s
    max_duration: 8s
    on_closed:
      - lambda: |-
          id(var_valve_open) = false;