web_server:
  include_internal: true
  local: true

esphome:
  name: garage-gate
  friendly_name: "Garage Gate"
  min_version: 2026.2.0
  project:
    name: ratgdo.v32disco_secplus2
    version: "32disco"

esp32:
  board: esp32dev
  framework:
    type: arduino

external_components:
  - source:
      type: local
      path: components
    components: [ ratgdo ]

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

safe_mode:

preferences:
  flash_write_interval: 1min

ratgdo:
  id: garage_gate_controller
  input_gdo_pin: GPIO21
  output_gdo_pin: GPIO17

time:
  - platform: sntp
    id: sntp_time
    servers: !secret sntp_servers
    timezone: Europe/Zurich

binary_sensor:
  - platform: template
    name: "Sync failed"
    lambda: 'return *id(garage_gate_controller).sync_failed;'
    device_class: problem
    entity_category: diagnostic

sensor:
  - platform: ratgdo
    type: openings
    entity_category: diagnostic
    name: "Openings"
    unit_of_measurement: "openings"
    icon: mdi:open-in-app
  - platform: ratgdo
    type: distance
    name: "Vehicle distance actual"
    unit_of_measurement: "mm"
    internal: true
    filters:
      - throttle: 1ms
      - heartbeat: 1ms
  - platform: adc
    pin: GPIO34
    name: "Voltage"
    attenuation: auto
    update_interval: 60s
    filters:
    - calibrate_linear:
        - 1.16 -> 5
        - 2.783 -> 12

lock:
  - platform: ratgdo
    name: "Lock remotes"

switch:
  - platform: ratgdo
    type: led
    pin: GPIO2
    name: "LED"
    entity_category: config
  - platform: ratgdo
    type: led
    pin: GPIO23
    name: "LASER"
    entity_category: config

number:
  - platform: ratgdo
    type: opening_duration
    entity_category: config
    name: "Opening duration"
    unit_of_measurement: "s"
  - platform: ratgdo
    type: closing_duration
    entity_category: config
    name: "Closing duration"
    unit_of_measurement: "s"

cover:
  - platform: ratgdo
    id: garage_gate
    device_class: garage
    name: None
    on_state_change:
      - if:
          condition:
            lambda: 'return id(garage_gate).current_operation == COVER_OPERATION_CLOSING;'
          then:
            - script.execute: play_siren
          else:
            - script.stop: play_siren
            - rtttl.stop:

light:
  - platform: ratgdo
    name: "Light"

button:
  - platform: template
    entity_category: diagnostic
    name: "Query status"
    on_press:
      then:
        lambda: !lambda |-
          id(garage_gate_controller).query_status();
  - platform: template
    name: "Query openings"
    entity_category: diagnostic
    on_press:
      then:
        lambda: !lambda |-
          id(garage_gate_controller).query_openings();
  - platform: template
    name: "Sync"
    entity_category: diagnostic
    on_press:
      then:
        lambda: !lambda |-
          id(garage_gate_controller).sync();
  - platform: template
    name: "Play Alert"
    on_press:
      then:
        - rtttl.play: "alert:d=8,o=5,b=120:a,p,a,p,a,p,4b,p"

output:
  - platform: ledc
    pin: GPIO33
    id: garage_gate_ledc

rtttl:
  - output: garage_gate_ledc

script:
  - id: play_siren
    mode: restart
    then:
      - while:
          condition:
            lambda: 'return id(garage_gate).current_operation == COVER_OPERATION_CLOSING;'
          then:
            - rtttl.play: "siren:d=8,o=6,b=100:e,d#,e,d#,e,d#,e,d#"
            - wait_until:
                condition:
                  not:
                    rtttl.is_playing:
