# MCU Atlas original ESPHome config for the automatic chicken-coop door.
esphome:
  name: coop-door
  friendly_name: Coop door

esp32:
  board: seeed_xiao_esp32c3
  framework:
    type: arduino

logger:
api:
  encryption:
    key: !secret api_key
ota:
  - platform: esphome
    password: !secret ota_password
wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_password
  ap:
    ssid: "Coop-Door Fallback"

# Time from Home Assistant; if HA is down the schedule keeps running
# on-device and only the clock drifts.
time:
  - platform: homeassistant
    id: coop_time

# Sunrise/sunset computed on the ESP32 from your lat/long.
sun:
  latitude: !secret coop_latitude
  longitude: !secret coop_longitude
  on_sunrise:
    then:
      - delay: !lambda "return (uint32_t)(id(open_delay).state * 60000);"
      - if:
          condition:
            and:
              - switch.is_on: auto_open
              - cover.is_closed: coop_door
          then:
            - cover.open: coop_door
  on_sunset:
    then:
      - delay: !lambda "return (uint32_t)(id(close_delay).state * 60000);"
      - cover.close: coop_door   # unconditional: dusk always locks the flock in

binary_sensor:
  # D2/GPIO4 - top reed: magnet present = door fully open
  - platform: gpio
    pin:
      number: 4
      inverted: true
      mode:
        input: true
        pullup: true
    name: "Door fully open"
    id: reed_open
  # D3/GPIO5 - bottom reed: stable 1 s so the door seats in its latch
  # before anything reports LOCKED
  - platform: gpio
    pin:
      number: 5
      inverted: true
      mode:
        input: true
        pullup: true
    name: "Door locked"
    id: reed_closed
    filters:
      - delayed_on: 1s
  # D9/GPIO10 - weatherproof override button, works with no network
  - platform: gpio
    pin:
      number: 10
      inverted: true
      mode:
        input: true
        pullup: true
    name: "Override button"
    id: override_button
    filters:
      - delayed_on: 10ms
    on_press:
      then:
        - cover.toggle: coop_door

switch:
  # D4/GPIO6 and D5/GPIO7 to L298N IN1/IN2. Interlock: both motor pins
  # can never be high at once (100 ms guard on direction change).
  - platform: gpio
    pin: GPIO6
    id: motor_up
    interlock: [motor_down]
    interlock_wait_time: 100ms
  - platform: gpio
    pin: GPIO7
    id: motor_down
    interlock: [motor_up]
    interlock_wait_time: 100ms
  # The original build's most-wanted feature: disable auto-open remotely.
  - platform: template
    name: "Auto open at sunrise"
    id: auto_open
    optimistic: true
    restore_mode: ALWAYS_ON

number:
  - platform: template
    name: "Open delay (minutes after sunrise)"
    id: open_delay
    min_value: 0
    max_value: 120
    initial_value: 30
    step: 1
    optimistic: true
    restore_value: true
    mode: box
  - platform: template
    name: "Close delay (minutes after sunset)"
    id: close_delay
    min_value: 0
    max_value: 120
    initial_value: 15
    step: 1
    optimistic: true
    restore_value: true
    mode: box

cover:
  - platform: feedback
    name: "Coop door"
    id: coop_door
    device_class: door
    open_action:
      - switch.turn_off: motor_down
      - switch.turn_on: motor_up
    close_action:
      - switch.turn_off: motor_up
      - switch.turn_on: motor_down
    stop_action:
      - switch.turn_off: motor_up
      - switch.turn_off: motor_down
    open_duration: 8s
    close_duration: 8s
    max_duration: 10s   # hard force-stop: motor never grinds past an obstruction
    open_endstop: reed_open
    close_endstop: reed_closed
