Configuring Pycharm as IDE over smb/cifs share for AppDaemon

Hi All,

I was wondering if someone could shed some light on my setup. I have done some minor python work in the past and I really enjoy using PyCharm. I currently have my appdaemon directory setup as a samba/cifs share and from my windows machine I use PyCharm with the project directory setup as the appdaemon directory.

I was wondering two things:

  1. Is there anyway to execute the ‘apps’ in PyCharm and get feedback instantaneously if there’s a problem with something like I would do normally?

I also like to do a lot of ‘print’ commands for easy checking of values and things like that, currently I’m just spitting out values to log but I was wondering if I could use PyCharm somehow for this instead of tailing my appdaemon log file.

Any help is appreciated!
Jon

I don’t know if this possible.

I do it the same way as you and just tail the log and the error log during development.
Isn’t this sufficient? For me this is very convenient.

I managed to get PyCharm Debugging working with Home Assistant AppDaemon.

Environment:

  • Raspberry Pi 3+ v1.2
  • Home Assistant OS 2021.11.4
  • Samba Share Add-On
  • SSH & Web Terminal Add-On configured with username: root
  • PyCharm 2021.1.3 Professional Edition (on Mac OS 11)

I used these instructions as a guide to configure PyCharm.

PyCharm Preferences (Settings):
SSH Configuration | SFTP Deployment | Samba Deployment
Deployment Mapping (for both Deployments) | Python Interpreter

Copy pycharm.app/Contents/debug-eggs/pydevd-pycharm.egg to Pi (I put it in /config/appdaemon/apps/tools/lib/pydevd-pycharm.egg)

Sample hello.py AppDaemon Code:

from sys import path
from socket import gaierror
from datetime import datetime
import hassapi as hass

PYCHARM_DEBUG = True  # False to disable debugging
PYCHARM_IDE_HOST = '192.168.7.70'  # Host running PyCharm
PYCHARM_IDE_PORT = 36000  # Port defined in PyCharm Python Debug Server
PYDEVD_PYCHARM_PATH = '/config/appdaemon/apps/tools/lib/pydevd-pycharm.egg'  # PyCharm file copied to Pi
if PYCHARM_DEBUG:
    # adding pydevd-pycharm to AppDaemon Configuration python_packages results in an error, egg file must be provided in path
    if PYDEVD_PYCHARM_PATH not in path:  # this can be called multiple times during testing, so don't keep appending path
        path.append(PYDEVD_PYCHARM_PATH)  # make sure pydevd_pycharm can be found
    import pydevd_pycharm


class DebugHass(hass.Hass):
    """ Enables PyCharm debugging in method. """
    def pycharm_debug(self, suspend: bool = False) -> None:
        """
        Call from DebugHass method to enable PyCharm debugging within the method.

        :param suspend: True to set breakpoint at settrace() line. If set to False, breakpoints must be set in PyCharm.
        """
        if PYCHARM_DEBUG:
            try:
                pydevd_pycharm.settrace(PYCHARM_IDE_HOST, port=PYCHARM_IDE_PORT, stdoutToServer=True, stderrToServer=True, suspend=suspend)
            except ConnectionRefusedError as e:
                self.log(f'Debugger not running on {PYCHARM_IDE_HOST}. {e}')
            except gaierror as e:
                self.log(f'Cannot connect to {PYCHARM_IDE_HOST} port {PYCHARM_IDE_PORT}. {e}')


class Hello(DebugHass):
    """
    Example DebugHass class to demonstrate pycharm_debug().

    To use, AppDaemon must be installed and running.
    config/appdaemon/apps directory must contain
        hello.py (this file)
        apps.yaml

    apps.yaml file must contain
    hello:
      module: hello
      class: Hello
    """
    def initialize(self):
        self.pycharm_debug()  # debug initialize method
        self.log('Hello from initialize.')
        self.run_every(self.hello_callback, datetime.now(), 5)  # call hello_callback every 5 seconds

    def hello_callback(self, kwargs):
        self.pycharm_debug()  # debug hello_callback method
        self.log('Hello from hello_callback.')

To Debug:

  • Mount Pi (via Samba) on PyCharm computer
  • Run Home Assistant Samba Share Add On
  • Run Home Assistant SSH Add On (as root)
  • Deploy hello.py (above) and apps.yaml from PyCharm to Pi in /config/appdaemon/apps
  • Set appropriate breakpoints
  • Start PyCharm Python Debug Server
  • Start Home Assistant AppDaemon (and follow the AppDaemon log to see how things are going)

When an existing AppDaemon App is running, deploying changes to files from PyCharm will cause AppDaemon to reload the changes and continue the existing debug session in many cases.

This summarizes what I got working. I’m sure there are other paths to success. Both Samba and SSH deployments are likely not necessary.

1 Like