Is there a way to use a function or subroutine in ESPHome?

I use packages to simplify my ESPHome code, but I was wondering if I could call a block of code instead of repeating it in every yaml file?

I think use Script Component

I don’t think a script would work. I want to externalize three components and as I understand it script can only execute one component. I am looking for something like a simple #include which would at compile time import the contents of the included file.

This is what I would like to reduce to a single line:

# Status light
# lit, with quick off’s: No connection
# steady flashing: WiFi connection
# short ‘on’ pulses: connected to HA API
#
interval:
  - interval: 2s
    then:
      if:
        condition:
          wifi.connected:
        then:
          - if:
              condition:
                api.connected:
              then:
                - light.turn_on:
                    id: status_led
                    flash_length: 1950ms
              else:
                - light.turn_on:
                    id: status_led
                    flash_length: 1000ms
        else:
          - light.turn_on:
              id: status_led
              flash_length: 50ms

light:
  - platform: binary
    id: status_led
    internal: true
    output: status_led_out

output:
  - id: status_led_out
    platform: gpio
    pin: ${led_pin}

As I type this I realize that an #include like this would preclude the use of the output: or light: components in my yaml file.

Never mind. I was just looking for a way to reduce the number of lines in my device yaml file. Just using two packages has reduced my yaml file that I can print most of them on one page:

packages:
  wifi: !include common/wifi.yaml
  device_base: !include common/esp8266.yaml