---
substitutions:
  device: gate
  name: Gate
  area: Outside
  comment: "${area} | Gate"

esphome:
  name: mcu-${device}
  friendly_name: ${name}
  area: ${area}
  comment: ${comment}

esp32:
  variant: esp32
  framework:
    type: esp-idf

packages:
  common: !include common/common.yaml

# PINOUTS: M5ATOM
# IR        G12
# NEO       G27
# BUTTON    G39
# I2C0 SDA  G26 white
# I2C0 SCL  G32
# I2C1 SDA  G25 red
# I2C1 SCL  G21
# UART0 RX  G3
# UART0 TX  G1
# RS485 RX  G33
# RS485 TX  G23
# RELAY-1   G22
# RELAY-2   G19

button:
  - platform: output
    name: Open
    id: gate_open
    output: relay_1
    duration: 200ms
  - platform: output
    name: Close
    id: gate_close
    output: relay_2
    duration: 200ms

output:
  - platform: gpio
    pin: 22
    id: relay_1
  - platform: gpio
    pin: 19
    id: relay_2

switch:
  - platform: template
    name: Open
    turn_on_action:
      - button.press: gate_open
  - platform: template
    name: Close
    turn_on_action:
      - button.press: gate_close
  - platform: template
    name: Auto Open
    id: auto_open
    optimistic: true
    restore_mode: RESTORE_DEFAULT_ON
  - platform: template
    name: Auto Close
    id: auto_close
    optimistic: true
    restore_mode: RESTORE_DEFAULT_ON
  - platform: template
    name: Enable Auris
    id: enable_auris
    optimistic: true
    restore_mode: RESTORE_DEFAULT_ON
  - platform: template
    name: Enable Yaris
    id: enable_yaris
    optimistic: true
    restore_mode: RESTORE_DEFAULT_ON
  - platform: template
    name: Enable Honda PCX125
    id: enable_pcx125
    optimistic: true
    restore_mode: RESTORE_DEFAULT_ON
  - platform: template
    name: Enable Suzuki DL650
    id: enable_dl650
    optimistic: true
    restore_mode: RESTORE_DEFAULT_ON

globals:
  - id: beacon_count
    type: int
    restore_value: no
    initial_value: '0'

esp32_ble_tracker:
  id: ${device}_ble_tracker
  scan_parameters:
    duration: 1min

binary_sensor:
  - platform: ble_presence
    id: ibeacon_auris
    ibeacon_uuid: !secret ble_beacon_toyota_auris_uuid
    name: "Vehicle iBeacon Auris"
    timeout: 2min
    on_press: !include {file: common/gate_beacon_open.yaml, vars: {enable_id: enable_auris}}
    on_release: !include {file: common/gate_beacon_close.yaml, vars: {enable_id: enable_auris}}
  - platform: ble_presence
    id: ibeacon_yaris
    ibeacon_uuid: !secret ble_beacon_toyota_yaris_uuid
    name: "Vehicle iBeacon Yaris"
    timeout: 2min
    on_press: !include {file: common/gate_beacon_open.yaml, vars: {enable_id: enable_yaris}}
    on_release: !include {file: common/gate_beacon_close.yaml, vars: {enable_id: enable_yaris}}
  - platform: ble_presence
    id: ibeacon_pcx125
    ibeacon_uuid: !secret ble_beacon_honda_uuid
    name: "Vehicle iBeacon Honda PCX125"
    timeout: 2min
    on_press: !include {file: common/gate_beacon_open.yaml, vars: {enable_id: enable_pcx125}}
    on_release: !include {file: common/gate_beacon_close.yaml, vars: {enable_id: enable_pcx125}}
  - platform: ble_presence
    id: ibeacon_dl650
    ibeacon_uuid: !secret ble_beacon_suzuki_uuid
    name: "Vehicle iBeacon Suzuki DL650"
    timeout: 2min
    on_press: !include {file: common/gate_beacon_open.yaml, vars: {enable_id: enable_dl650}}
    on_release: !include {file: common/gate_beacon_close.yaml, vars: {enable_id: enable_dl650}}
  - platform: gpio
    id: gate_ogi
    pin:
      number: 32
      inverted: true
      mode:
        input: true
    filters:
      - delayed_on: 10ms
    entity_category: diagnostic
    on_multi_click:
      - timing:
          - OFF for at least 1500ms
        then:
          - select.set:
              id: gate_state
              option: Closed
          - logger.log: "OGI OFF => GATE CLOSED"
        invalid_cooldown: 100ms
      - timing:
          - ON for at least 400ms
          - OFF for at least 400ms
          - ON for at least 400ms
          - OFF for at least 400ms
        then:
          - select.set:
              id: gate_state
              option: Opening
          - logger.log: "OGI SLOW BLINK => GATE OPENING"
        invalid_cooldown: 100ms
      - timing:
          - ON for at most 300ms
          - OFF for at most 300ms
          - ON for at most 300ms
          - OFF for at most 300ms
        then:
          - select.set:
              id: gate_state
              option: Closing
          - logger.log: "OGI FAST BLINK => GATE CLOSING"
        invalid_cooldown: 100ms
      - timing:
          - ON for at least 1500ms
        then:
          - select.set:
              id: gate_state
              option: Open
          - logger.log: "OGI ON => GATE OPEN"
        invalid_cooldown: 100ms

select:
  - platform: template
    name: State
    id: gate_state
    options:
      - Open
      - Closed
      - Opening
      - Closing
    initial_option: Closed
    optimistic: true
