Garage door control with ESP32

Hi there,

I have a garage door that have contacts to control it.
This works like this :

One push : Open the door
Another push : Stop the door
Third push : Close the door
New push : Open the door

I have one sensor (Magnetic sensor big door) that turn closed if the door is opened, it is on the bottom of the door so it just help to know if the door is closed or not.

Here is my code to control the garage door :

substitutions:
  device_name: garage-door
  platform: esp32
  board: nodemcu-32s

  open_duration: "15000"  # ms
  close_duration: "20000"  # ms
  cover_device_class: garage

  cover_switch_pin: GPIO18
  active_switch_duration: 500ms
  push_interval: "1000"

  endstop_pin: GPIO26
  endstop_smalldoor_pin: GPIO25
  debounce_time: 500ms

packages:
  common: !include common/config_base.yaml
  wifi: !include common/wifi_base.yaml
  ota: !include common/ota_base.yaml

esphome:
  name: $device_name
  platform: $platform
  board: $board

switch:
  - platform: gpio
    pin: $cover_switch_pin
    name: "Cover Switch"
    id: cover_switch
    inverted: true
    internal: true
    restore_mode: ALWAYS_OFF 

binary_sensor:
  - platform: gpio
    pin:
      number: $endstop_pin
      mode: INPUT_PULLUP
      inverted: false
    filters:
      - delayed_on: 100ms
      - delayed_off: 100ms
    name: "Magnetic Sensor (Big door)"
    id: endstop

  - platform: gpio
    pin:
      number: $endstop_smalldoor_pin
      mode: INPUT_PULLUP
      inverted: false
    name: "Magnetic Sensor (Small door)"
    id: endstop_small
    
cover:
  - platform: template
    name: $device_name
    id: garage_door
    device_class: garage
    optimistic: false
    has_position: true
    assumed_state: false
    lambda: |-
      if (id(endstop).state) {
        return COVER_OPEN;
      } else {
        return COVER_CLOSED;
      }
    open_action:
      - switch.turn_on: cover_switch
      - delay: 0.5s
      - switch.turn_off: cover_switch
    close_action:
      - switch.turn_on: cover_switch
      - delay: 0.5s
      - switch.turn_off: cover_switch      
    stop_action:
      - switch.turn_on: cover_switch
      - delay: 0.5s
      - switch.turn_off: cover_switch 
      
# Enable logging
logger:
  level: INFO

At the beginning of the code I have written the open_duration and close_duration.

My question, how can I modify this code to get the percentage of opening/closing ? And how can I get it works in these scenarios in example (To account for the previous button press state):

Push on up arrow to open the garage (so 1 push on the relay)
Push on down arrow to close the garage (2 push on the relay)

Push on up arrow to open the garage (1 push on the relay)
Push on stop arrow to stop the garage (1 push on the relay)
push on up arrow to open the garage (2 push on the relay)

Thanks