# Ikea Rothulth lock
esphome:
  name: rothult_lock
  platform: ESP8266
  board: esp01_1m

wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_password
  power_save_mode: HIGH
  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "Rothult Lock Fallback Hotspot"
    password: !secret wifi_fallback_password

captive_portal:

# Enable logging
logger:

# Enable Home Assistant API
api:
  password: password

ota:
  password:

binary_sensor:
  - platform: gpio
    id: locked_endstop
    name: "Locked endstop"
    pin:
      number: 5
      mode: INPUT
    on_press:
      then:
        switch.template.publish:
          id: lock
          state: ON
  - platform: gpio
    id: unlocked_endstop
    name: "Unlocked endstop"
    pin:
      number: 4
      mode: INPUT
    on_press:
      then:
        switch.template.publish:
          id: lock
          state: OFF

sensor:
  - platform: adc
    id: vcc_voltage
    name: "VCC Voltage"
    pin: VCC

switch:
  - platform: template
    name: "Lock"
    icon: mdi:file-cabinet
    id: lock
    turn_on_action:
      # - switch.turn_on: forward
      - switch.turn_on: backward
      - wait_until:
          binary_sensor.is_on: locked_endstop
      # - switch.turn_off: forward
      - switch.turn_off: backward
    turn_off_action:
      - switch.turn_on: backward
      - wait_until:
          binary_sensor.is_on: unlocked_endstop
      - switch.turn_off: backward
  - platform: gpio
    pin: 12
    id: forward
    name: "GPIO Forward"
  - platform: gpio
    pin: 14
    id: backward
    name: "GPIO Backward"