IDE debugging and testing

I have seen people been asking about IDE and debugging integration, but i cant seem to find and real replies. Is it possible to do on the fly IDE debugging with visual studio code, vs 2017 or any other IDE, not just to debug (most important), but also cpu and memory profiling to search for memory leaks.

Feel its a bit strange to have to reload HA everytime I do a change, when on other projects I can just hit a debug button and go straight to the debug line.

Any that has got this working on any on the larger IDE (with IDE I guess we talk about visual studio 2017 and pycharm), but simpler editors would be preferable.

1 Like

yea, i’ve too been looking for a way to do this, having to put logging all over a component and then restarting over and over just to understand whats happening is just so slow and feels like the stoneage.

beeing c# developer i’m used to beeing able to step through the code during runtime and even change it on the fly

The replies I get is quite cryptic. The one reply I did see today was to “run HA is debug mode and attach debugger to that”, but how can we run HA in debug mode and connect a debugger to it.

Actually I got it working in Windows, but it require pyCharm PRO (not community).

To get debugging work, create a ubuntu linux subsystem following the manual, then setup the subsystem in the pyCharm 2018.3 (not the 0.2-version) this got builtin support for linux subsystem as Python Interperter.

Then create a config / debug config on the bin/hass-file

Script path: D:\Development\ha\hass\bin\hass (or whatever you have your path)
Parameters: -c /mnt/d/Development/ha/config --debug (whatever your path is)
Python interperter: Select the linux subsystem here, NOT the windows local

Uncheck all the checkboxes under.

Then when you load it will load HA inside the IDE, and you are able to use line breaks, debugging, step back, forward, direct link to files etc.

1 Like

An example of how detailed debugging we can get out of hass.

Looking to do the same thing but with VS. Not finding any information on it.

It’s actually just as easy in vscode, just edit the path in debug config to trigger hass in an venv. Can update the guide to cover live debug in vscode too

I found this blurb on vscode issue thread covering a similar topic. Problem is, I’m using visual studio pro, not VS code.


In the meantime, here’s a decent solution for auto-activating WSL Python virtual environments that are stored in the project tree, tested with Ubuntu WSL.

First, set the VS Code integrated terminal to launch with the -l/--login flag, meaning it will load ~/.profile each time a new terminal is opened. To do that, add this command to settings.json in VS Code:

"terminal.integrated.shellArgs.windows": ["-l"],

Then, add this to the end of your ~/.profile file in WSL:

# If virtual env in ./venv or ./.venv, activate it
if [ -f "./.venv/bin/activate" ]
then
. .venv/bin/activate
elif [ -f "./venv/bin/activate" ]
then
. venv/bin/activate
fi

With a few more lines, the internal WSL terminal will also respond to local Windows (.exe) virtual environments:

# If virtual env in ./venv or ./.venv, activate it
if [ -f "./.venv/bin/activate" ]
then
. .venv/bin/activate
elif [ -f "./venv/bin/activate" ]
then
. venv/bin/activate
elif [ -f "./.venv/Scripts/activate" ]
then
. .venv/Scripts/activate
elif [ -f "./venv/Scripts/activate" ]
then
. venv/Scripts/activate
fi

* Note: Other terminals that call ~./profile after login will receive the same treatment. Also, different distributions will have varying policies around which shell profiles are called and when.

EDIT: Added snippet for handling local Windows (.exe) virtual environments.


1 Like

I’m trying to setup the IDE debugging environment in OSx with VS code.
I’ve added this to the settings.json

    // The path of the shell that the terminal uses on OS X.
    "terminal.integrated.shell.osx": "sh",

    // The command line arguments to use when on the OS X terminal.
    "terminal.integrated.shellArgs.osx": ["-l"],

This is starting my .bash_profile, which is containing also my conda settings.

# added by Anaconda3 2019.10 installer
# >>> conda init >>>
# !! Contents within this block are managed by 'conda init' !!
__conda_setup="$(CONDA_REPORT_ERRORS=false '/Users/pippo/opt/anaconda3/bin/conda' shell.bash hook 2> /dev/null)"
if [ $? -eq 0 ]; then
    \eval "$__conda_setup"
else
    if [ -f "/Users/pippo/opt/anaconda3/etc/profile.d/conda.sh" ]; then
        . "/Users/pippo/opt/anaconda3/etc/profile.d/conda.sh"
        CONDA_CHANGEPS1=false conda activate base                       
    else
        \export PATH="/Users/pippo/opt/anaconda3/bin:$PATH"
    fi
fi
unset __conda_setup
# <<< conda init <<<

Can you help?

I’ve found a solution using the PTVSD - Python Tools for Visual Studio Debug Server. https://www.home-assistant.io/integrations/ptvsd/

Is simple as adding

ptvsd:
  wait: True

and configuring the launch.json in VS Code

Guys i am new to Phython and VSC home assistant development, i was able to setup the IDE on my mac and was able to added Hello_words custom components. How do i debug the custom components from Hass to VSC code? i added PTVSD: wait: True on my configuration.yaml and also set the break point in hello_world.py. How do i trigger to test the break points. sorry for asking basic questions… Thanks for your help

Did you manage to find out how? Anybody have a YouTube instruction on the whole process or could make one? I’m also new to python but would like to do live debugging to HA on raspberry pi from my Windows pc with vscode so I could view the objects of triggers etc so I know better how to create automations.

Setup dev container, it’s all built into the repo now