Over time I developed some standard on how to program my ESP boards. I like to reuse the same code and dislike having to edit each device individually when I find a better way to do something.
One way to address this is to have a file with the common code and to include it in the YAML specific to the device.
In the example below, the code I use for the S21 outlet is common to all of them, except for calibrations, names, and other settings. I have all those as substitutions
in the device specific file so I don’t have to create custom variants of the main template when I need to change something.
substitutions:
devicename: s31-outlet-6
devicename_no_dashes: s31_outlet_6
friendly_devicename: "Spare Outlet"
device_description: "Spare Outlet"
voltage_cal: "0.0"
restore_mode_setting: RESTORE_DEFAULT_ON
#restore_mode: Control how the relay attempts to restore state on bootup.
#RESTORE_DEFAULT_OFF - Attempt to restore state and default to OFF if not possible to restore.
#RESTORE_DEFAULT_ON - Attempt to restore state and default to ON.
#RESTORE_INVERTED_DEFAULT_OFF - Attempt to restore state inverted from the previous state and default to OFF.
#RESTORE_INVERTED_DEFAULT_ON - Attempt to restore state inverted from the previous state and default to ON.
#ALWAYS_OFF - Always initialize the pin as OFF on bootup.
#ALWAYS_ON - Always initialize the pin as ON on bootup.
# Interval of how often the wifi info is updated
update_interval_wifi: "60s"
# Basic Config
esphome:
name: ${devicename}
comment: ${device_description}
platform: ESP8266
board: esp01_1m
<<: !include s31-outlet-template.yaml
What I’d like to see is something like a code snippet library where I save the WIFI section of the code and just need to pick it from a list to add it to the device YAML. While this can be done manually, it would be easier and helpful as the library grows to have a list, and ESPHome do the inclusion (ie.: <<: !include wifi-settings.yaml
)