esp8266:
  board: esp8285
  # Sonoff 4CH Pro R3

esphome:
  name: irrigation
  friendly_name: "Irrigation"
  # Adding area prepends it to entity IDs. Ridiculous.
  #area: "Living room"

logger:

packages:
  mqtt: !include templates/mqtt.yaml
  ota: !include templates/ota.yaml
  wifi: !include templates/wifi.yaml
  uptime: !include templates/uptime.yaml
  restart: !include templates/restart.yaml
  syslog: !include templates/syslog.yaml

binary_sensor:
  - platform: gpio
    name: "Garden Peach button"
    entity_category: diagnostic
    pin:
      number: GPIO0
      mode: INPUT_PULLUP
      inverted: True
    on_press:
      - switch.toggle: "relay_1"
  - platform: gpio
    name: "Garden Berries button"
    entity_category: diagnostic
    pin:
      number: GPIO9
      mode: INPUT_PULLUP
      inverted: True
    on_press:
      - switch.toggle: "relay_2"
  - platform: gpio
    name: "Shower button"
    entity_category: diagnostic
    pin:
      number: GPIO10
      mode: INPUT_PULLUP
      inverted: True
    on_press:
      - switch.toggle: "relay_3"
  - platform: gpio
    name: "Button 4"
    entity_category: diagnostic
    pin:
      number: GPIO14
      mode: INPUT_PULLUP
      inverted: True
    on_press:
      - switch.toggle: "relay_4"

  - platform: template
    name: "Garden Peach relay"
    entity_category: diagnostic
    lambda: 'return id(relay_1).state;'
  - platform: template
    name: "Garden Berries relay"
    entity_category: diagnostic
    lambda: 'return id(relay_2).state;'
  - platform: template
    name: "Shower relay"
    entity_category: diagnostic
    lambda: 'return id(relay_3).state;'
  - platform: template
    name: "Relay 4"
    entity_category: diagnostic
    lambda: 'return id(relay_4).state;'

switch:
  - platform: gpio
    pin: GPIO12
    id: "relay_1"
    internal: true
    restore_mode: ALWAYS_OFF
  - platform: gpio
    pin: GPIO5
    id: "relay_2"
    internal: true
    restore_mode: ALWAYS_OFF
  - platform: gpio
    name: "Shower"
    pin: GPIO4
    id: "relay_3"
    restore_mode: ALWAYS_OFF
  - platform: gpio
    internal: true
    pin: GPIO15
    id: "relay_4"
    restore_mode: ALWAYS_OFF

sprinkler:
  - id: garden
    main_switch:
      name: "Garden"
      id: garden_main
    auto_advance_switch: "Garden Auto Advance"
    valve_open_delay: 10s
    valves:
      - valve_switch: "Garden Peach"
        valve_switch_id: relay_1
        run_duration: 2h # also update in spreadsheet and below
      - valve_switch: "Garden Berries"
        valve_switch_id: relay_2
        run_duration: 2h # also update in spreadsheet and below

button:
  - platform: template
    name: "Nightly cycle"
    on_press:
      - sprinkler.clear_queued_valves: garden

      # Pre-moisturizing
      - sprinkler.queue_valve:
          id: garden
          valve_number: 0
          run_duration: 10min
      - sprinkler.queue_valve:
          id: garden
          valve_number: 1
          run_duration: 10min

      # Main watering (uses default 2h duration)
      - sprinkler.queue_valve:
          id: garden
          valve_number: 0
          run_duration: 110min # 2h-above, also update in spreadsheet
      - sprinkler.queue_valve:
          id: garden
          valve_number: 1
          run_duration: 110min # 2h-above, also update in spreadsheet

      - sprinkler.start_from_queue: garden

status_led:
  pin:
    number: GPIO13
    inverted: True
