Graduating from ESPHome Builder to VSCode

Mostly I’m writing this to remind “future me” of what I did to set up my coding environment using VSCode (Actually Cursor AI, but it’s the same), and a Debian Supervised install. I’ve searched high and low, Reddit forums, here, and this isn’t fully documented anywhere. Bits and pieces, but nothing newbies can follow.

Its not straightforward and it’s taken me (admittedly I’m not a pro programmer) weeks to get here. I Initially tried installing ESPHome locally on my HA system. Compiling that way takes forever for some reason. The better way is to use the docker container already running esphome.

HA’s ESPHome Builder is great for beginners. I’m into a 1,500+ line ESPHome application and switching back and forth between VSCode and ESPHome Builder is inefficient, especially since you really don’t have to leave VSCode at all.

Steps on computer you will be editing and writing code on:
1- install VSCode or Cursor AI. I use Cursor AI - it’s a game changer. See my other posts for why
2 - Install the following VSCode extensions if they aren’t already installed.

  • ESPHome
  • ESPHome Snippets
  • Home Assistant Config Helper
  • Run In Terminal
  • YAML
    Here’s a cool video showing you whats possible in VSCode with the HA extensions.

Next, find out what the name of the docker container is that runs ESPHome. I’m not a docker guy so I install Portainer which gives me a nice pretty UI that lets you do everything you can do from the command line.

this command will do the same thing docker ps -a |grep esphome
image

now that you know what the name of the ESPHome docker container is, go back to VSCode and open a terminal window. Top Menu Bar/Terminal ->New Terminal Window.

Now ssh to your Home Assistant system. I created a user called ‘esphome’ on my Debian system and allowed it to be used with SSH. Google how to do that.
ssh [email protected] (or whatever the ip address is of your HA server.

once you’re logged in via ssh, type this command:

sudo docker exec -it -w /config/esphome addon_15ef4d2f_esphome bash

you should get this prompt

root@15ef4d2f-esphome:/config/esphome#

then run the command
esphome run your-file-name.yaml

root@15ef4d2f-esphome:/config/esphome# esphome run your-file-name.yaml

and magically it compiles, uploads and starts the log, just like if you were in ESPHome Builder.

There are tons of things you can do using the command line:

To automate even further, add these lines to the end of your .bashrc file in the home directory of your SSH user (mine is esphome)

export PS1='\[\e[0;36m\]\u\[\e[0m\]@\[\e[0;33m\]\h\[\e[0m\]:\[\e[0;35m\]\w\[\e[0m>
sudo docker exec -it -w /config/esphome addon_15ef4d2f_esphome bash

those 2 lines give you a nice prompt, and automatically get you to the right place so you can execute the esphome command.

Now you don’t have to ever leave VSCode as you’re creating new firmware for your projects. I hope this helps someone else.

I know it’s gonna help future me…

3 Likes

Here’s another useful addition. Install the “multi-command” and “Run in Terminal” extensions in VSCode/Cursor.
Multi-command in settings.json

    "multiCommand.commands": [     
        {
            "command": "multiCommand.runAndToggleTerminal",
            "sequence": [
                "runInTerminal.run",
                "workbench.action.terminal.toggleTerminal"
            ]
        }
    ]

and then in your keybindings.json file, add this

    {
        "key": "ctrl+r",
        "command": "extension.multiCommand.execute",
        "args": { 
            "sequence": [
                {              
                    "command": "runInTerminal.run", 
                    "args": {"cmd": "esphome run ${relativeFile}", "match": ".*"},
                    "when": "resourceLangId == yaml"
                },
                {
                    "command": "workbench.action.terminal.toggleTerminal",
                    "when": "resourceLangId == yaml"
                }
            ]
        }
    }

Make sure you open a terminal and ssh to your HA instance first and successfully log in. the close the terminal window.
Now, press “key”: “ctrl+r” (or whatever key you want to map it to), a terminal opens AND esphome compiles the yaml file you’re currently editing. if you just want to compile and NOT watch the logs after the upload (I open a log window in different window on my 2nd monitor) use this run command instead

"args": {"cmd": "esphome run --no-logs ${relativeFile}", "match": ".*"},

or just map another key to use that command.

This took some serious time to get working correctly. But here it is now.
I’m sure this will help someone else as well as “future me”.