Debugpy and Custom Components and Integrations

Hi,

I’m hoping that you can help me.

I’ve setup debugpy and connected Visual Studio Code and have it attaching and working, I can set break points in the core home assistant :smile: and it works really well.

What I would now like to do is use it to develop and debug my Custom Component here.

But I’m having problems. I added a Folder to the Workspace where the source code is located that is mapped to the Home Assistant path i.e. they are the same files in both local and remote.

This is my launch.json

{
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            // Example of attaching to my production server
            "name": "Python: Attach Remote",
            "type": "python",
            "request": "attach",
            "port": 5678,
            "host": "192.168.0.99",
            "pathMappings": [
                {
                    "localRoot": "${workspaceFolder}",
                    "remoteRoot": "/usr/src/homeassistant"
                },
                {
                    "localRoot": "Y:/custom_components/visonic",
                    "remoteRoot": "/config/custom_components/visonic"
                }
            ],
        }
    ]
}

So in the 2nd path mapping I have the local and remote paths for my custom component.

The problem that I am struggling with is that the files in the custom component cannot “import” the core files. This is the error I get
image

and this is the section of the file it refers to

I import from a file in my custom component const.py and it also fails to find that (line 18)

I cannot find any help or instructions on setting up debugpy for use with custom components, onyl for the core development.

EDIT: In case it’s relevant, I’m using Home Assistant 0.113.1 in Docker on a Synology NAS

Any help would be appreciated

Cheers
Dave

Hi Dave, did you find a way to debug in custom_components? I spent hours looking for examples, documents but could not trigger break points in custom_components.
Please share if you find any way to do it

I did get breakpoints working for custom components

Do you have this in your configuration.yaml


debugpy:
   start: true
   wait: false
   port: 5678

Set wait to true and Home Assistant waits for the connection from Visual Studio Code.

Did you need to add something to the pathMappings for it to work in custom_components?

I’ve copied the following from my notes so it might be a bit incomplete but this should help you, I’ve also replaced the HA references to the version I’m currently using.

Put this in configuration.yaml and restart Home Assistant

debugpy:
   start: true
   wait: false
   port: 5678

Download and unzip the HA source code from Github for the same version as you are running
In my case I downloaded and unzipped this (I actually cloned it)

In the …/core-2021.6.6/.vscode directory replace the content of launch.json with this

{
  // Use IntelliSense to learn about possible attributes.
  // Hover to view descriptions of existing attributes.
  // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
  "version": "0.2.0",
  "configurations": [
    {
      "name": "Home Assistant",
      "type": "python",
      "request": "launch",
      "module": "homeassistant",
      "justMyCode": false,
      "args": ["--debug", "-c", "config"]
    },
    {
        // Example of attaching to my production server
        "name": "Python: Attach Remote",
        "type": "python",
        "request": "attach",
        "port": 5678,
        "host": "192.168.0.199",
        "pathMappings": [
            {
                "localRoot": "${workspaceFolder}",
                "remoteRoot": "/usr/src/homeassistant"
            },
            {
                "localRoot": "Y:/custom_components/visonic",
                "remoteRoot": "/config/custom_components/visonic"
            }
        ],
    }
  ]
}

On your PC you need a mapped drive to your HA config directory, in my case this is Y:
In launch.json above replace the

                "localRoot": "Y:/custom_components/visonic",
                "remoteRoot": "/config/custom_components/visonic"

With your custom component paths and drive mappings
And also replace

        "host": "192.168.0.199",

With your HA host IP address.

I assume that you have Visual Studio Code installed
Start Visual Studio Code

File → Open Folder
Navigate to the core-2021.6.6 directory and click Select Folder

Within Visual Studio Code you will see the Explorer window
Select File → Add Folder To Workspace
Navigate to your custom component directory and open it
In my case this is Y:/custom_components/visonic

You should now be able to see and open/edit all HA core files and your custom component files.
Pylance gives missing import problems but I ignore them.
You can now set breakpoints in your code (and the HA core code) but remember that HA is already running. You could try setting wait: true in your configuration.yaml file.

In Visual Studio Code, on the left side find the icon for Run and Debug and select it
In the little window at the top, click the down arrow and select “Python: Attach Remote”. This is the name in launch.json above.

Click the little green triangle next to it to debug the code
On mine there’s a shortcut menu bar with step over, step in, disconnect etc for you to use.
Remember when it hits a breakpoint ALL of your HA is stopped as it uses asyncio.

Let me know if this works for you and good luck
:slight_smile:

EDIT: To update to the latest version of HA I just keep the launch.json, download the version that my HA uses and over-write the launch.json, it doesn’t take long.

1 Like

This configuration works for me like a charm. All others fails to stop at breakpoints.

Thank you for posting this !