This devcontainer sets up the environment to make it extremly easy to test this integration and with minor changes can be used for any integration. I've created this devcontainer after getting frustrated with the existing ones, offered by the community, which didn't include all the features I expected. In particular, supporting parallel development of the integration itself and an associated API library / SDK is very often developed in parallel.
Opening multiple projects inside of a devcontainer is a bit cluncky so follow these steps carefullty
- Create a parent directory that will contain both the integration and the API library.
- Create a folder for the integration. Make sure it follows the structure required by HACS, in particular it must have "custome_components" subfolder where all the integration code will reside. The .devcontainer folder should also be here.
- Create a folder for the API library. Since this library has to be pubuilshed to
pypi make sure it has a setup.py file at its root.
At this point your folder structure should look something like this:<main_folder> ┣━━━━━━ <integration folder> ┃ ┣━━━ .devcontainer ┃ ┗━━━ custom_components folder ┃ ┗━━━ the integration itself ┣━━━━━━ <library folder> ┃ ┗━━━ setup.py ┣━━━━━━ ... additional libraries ...
- Edit the .devcontainer/devcontainer.json file as follows:
- Set the "name" as appropriate
- Edit the last row in the "mounts" list to point to the name of your library folder.
- Now open ONLY the integration folder in VSCode. You will see a popup, suggesting to reopen the folder in a devcontainer. Click on that and let the container build itself. This might take a while.
- Now select File -> Add Folder to Workspace from VSCode menu and select the library folder.
- You should now have both folders in your workspace. Use File -> Save Workspace As to save the workspace TO THE INTEGRATION FOLDER, if you don't save it there it will get lost when the container is rebuilt.
If you ever need to reopen the container from sratch you can just click the workspace file. You will get a warning saying the workspace may not open properly, ignore that warning and continue.
When the devcontainer is launched the assosicated scipts will automatically:
- Install Home Assistant
- Load the main integration project + any number of library porojects into the /workspaces folder
- pip install the API libraries
- Create a /config folder and copy the files from the .devcontainer/config folder to there
- Set-up a set of proxy folder with references to the integration files under /config/custom_components. This allows Home Assistant to load the integration from its usual location which allowing vscode to debug it from the standard code location under /workspaces
Just launch the "Home Assistant" launch configuration from vscode. Setup breakpoints normally. The Home Assistant portal can be access on https://localhost:9123 of the host machine.