esphome:
  name: garage
  platform: ESP8266
  board: nodemcuv2

logger:
  level: INFO

ota:
  password: "143fd33979958ada2bb06df1622d5a4d"

wifi:
  ssid: !secret ssid
  password: !secret pass
  fast_connect: true

mqtt:
  broker: henry.int.jnf.me
  username: esphome
  password: esphome
  topic_prefix: esphome/garage

dallas:
  - pin: D7

sensor:
  - platform: dallas
    address: 0x7701143100966C28
    name: "Freezer"
    id: garage_freezer
  - platform: dallas
    address: 0x200114310A086F28
    name: "Garage"
    id: garage_temp
  - platform: ultrasonic
    trigger_pin: D5
    echo_pin: D8
    id: garage_spot_1
    internal: true
  - platform: ultrasonic
    trigger_pin: D3
    echo_pin: D4
    id: garage_spot_2
    internal: true

text_sensor:
  - platform: template
    name: "Garage Door State"
    id: garage_door_state
    lambda: !lambda |-
      if (id(garage_door_open).state && id(garage_door_closed).state) {
        return {"Error"};
      } else if (id(garage_door_open).state) {
        return {"Open"};
      } else if (id(garage_door_closed).state) {
        return {"Closed"};
      } else return {"Moving"};

binary_sensor:
  - platform: gpio
    pin: D0
    name: "Garage Door"
    device_class: door
    id: garage_door_open
    filters:
      - invert:
    on_press:
      then:
        - lambda: |-
            id(garage_door_state).publish_state("Open");
    on_release:
      then:
        - lambda: |-
            id(garage_door).current_operation = COVER_OPERATION_CLOSING;
            id(garage_door).publish_state();
            id(garage_door_state).publish_state("Moving");
  - platform: gpio
    pin: D1
    id: garage_door_closed
    filters:
      - invert:
    internal: true
    on_press:
      then:
        - lambda: |-
            id(garage_door_state).publish_state("Closed");
    on_release:
      then:
        - lambda: |-
            id(garage_door).current_operation = COVER_OPERATION_OPENING;
            id(garage_door).publish_state();
            id(garage_door_state).publish_state("Moving");
  - platform: gpio
    pin: D2
    name: "Side Door"
    device_class: door
    filters:
      - invert
      - delayed_off: 5s
  
  - platform: template
    name: "Left Parking Spot"
    device_class: occupancy
    lambda: |-
      if (id(garage_spot_1).state < 2) {
        return true;
      } else {
        return false;
      }
  - platform: template
    name: "Right Parking Spot"
    device_class: occupancy
    lambda: |-
      if (id(garage_spot_2).state < 2) {
        return true;
      } else {
        return false;
      }


switch:
  - platform: gpio
    pin:
      number: D6
      inverted: true
    id: garage_door_switch
    on_turn_on:
      - delay: 500ms
      - switch.turn_off: garage_door_switch
    internal: true

cover:
  - platform: template
    name: "Garage Door"
    id: garage_door
    device_class: garage
    lambda: !lambda |-
      if (id(garage_door_open).state) {
        return COVER_OPEN;
      } else if (id(garage_door_closed).state) {
        return COVER_CLOSED;
      } else return {};
    open_action:
      - switch.turn_on: garage_door_switch
      - delay: 3s
      - if:
          condition:
            binary_sensor.is_on: garage_door_closed
          then: 
            - switch.turn_on: garage_door_switch
    close_action:
      - switch.turn_on: garage_door_switch
      - delay: 3s
      - if:
          condition:
            binary_sensor.is_on: garage_door_open
          then: 
            - switch.turn_on: garage_door_switch