addons-example
addons-example copied to clipboard
Add ability to restart/rebuild addon from vscode
Working from within vscode, it is helpful to have the ability to immediately run the code you have just edited, and watch the output. Shortening this iteration cycle makes a big difference to productivity.
Currently it seems the normal method is to start and stop addons via the web UI, and view logs in a non-live way via the web-based log view.
This PR adds two 'tasks' to the vscode command palette within the development environment:
- Start Addon - starts the addon by invoking the ha cli through docker, then following the docker container console output.
- Rebuild and start Addon - same as above, but triggers a reinstall of the addon through the ha cli first.
The grep is needed to get the slug name of the addon which is configured by the user in config.yaml
Note that it is necessary for HA to be running (e.g. via first running 'Start Home Assistant') before triggering either of the above two tasks from the palette. Also note that this PR makes no difference to the way the addon operates. It's just to help vscode users during development.

In both cases it is possible to use vscode's built-in 'Tasks: Restart Running Task' which effectively stops and restarts the addon.
The end result is that ctrl-shift-p,<enter>,<enter> without leaving vscode is enough to iterate.
Update: I'm slightly less happy with this than I was.
Since creating the PR I've realised that the template repo is probably intended for a collection of add-ons, not just one.
If you have just one addon it works well, but if you have more then one, there's not an easy way to configure which one to start/rebuild.
Perhaps it makes more sense to define the slug explicitly for the example addon, then let the developer duplicate that section of tasks.json if they have multiple addons in the repo.
While there is only 1 addon here, that's generally not the case for an actual repository. You can define inputs for tasks like https://github.com/home-assistant/frontend/blob/dev/.vscode/tasks.json#L202 that will fit well with what you try to do here.
@ludeeus I tried this and it works, but when I call the function I have to manually type the addon directory name in to make it run.
It's slightly better if I set the reevaluateOnRerun property to false, but I'm still asked to type it if I choose 'restart running task'.
I have updated the PR with this now. Can you think of any improvements?
@ludeeus If I understood you correctly, this should be ready now.
Please take a look at the requested changes, and use the Ready for review button when you are done, thanks :+1: