Python Dependencies not working

Hello all,

I started using AppDaemon a few days ago because I realised that regular HA automations weren’t going to be powerful enough for what I want to accomplish.
Being an OOP style developer, I immediately tried to go full Java-mode and create a bunch of classes, in order to keep my code clear. However, AppDaemon clearly had other plans. I’ll spare you the details of my specific case, so instead let’s say I have a class a.py, that needs to instantiate some other objects, say b.py. Seems simple enough, yet it took me all of yesterday to get it to work. But work it did!

… did.

Today, I made some changes to my code, breaking it completely. Now, no matter what I do, I cannot get it to work.

Lets assume the most basic scenario:

a.py:

import hassapi as hass

class A(hass.Hass):

  def initialize(self):
    b = B()

b.py:

import hassapi as hass

class B(hass.Hass):

  def initialize(self):
    self.turn_on("light.keuken_5")

apps.yaml:

B:
  module: b
  class: B

A:
  module: a
  class: A
  dependencies: B

As you can probably guess, even something as simple as this doesn’t work. It gives the following error:

2024-11-01 19:26:04.562120 WARNING A: Traceback (most recent call last): File "/usr/lib/python3.11/site-packages/appdaemon/app_management.py", line 162, in initialize_app await utils.run_in_executor(self, init) File "/usr/lib/python3.11/site-packages/appdaemon/utils.py", line 304, in run_in_executor response = future.result() ^^^^^^^^^^^^^^^ File "/usr/lib/python3.11/concurrent/futures/thread.py", line 58, in run result = self.fn(*self.args, **self.kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/config/apps/a.py", line 7, in initialize b = B() ^^^ TypeError: Hass.__init__() missing 7 required positional arguments: 'ad', 'name', 'logging', 'args', 'config', 'app_config', and 'global_vars' 

While trying to fix this, I learned that the way the classes are defined (the “class B(hass.Hass):” part) means that class B inherits class hass.Hass. So if I’m trying to instantiate B, it makes sense that it says that the constructor of B doesn’t get all the required arguments to instantiate the Hass object. I just have no idea how to get around that, or how I got around it yesterday. Thanks, brain.

If I just reload b.py, no errors are thrown and my kitchen lights turn on, as intended. I guess that makes sense too tho

I’m pretty new to python, but I’ve been programming for 10+ years. I always found myself liking C++ and Java/Kotlin a lot better than Python, but since it’s basically the only option, and supposed to be easy, I thought I’d take the plunge. I’m sure I can get a lot of work done once I have the basics figured out, but without the ability to use classes, It’s going to be very messy.

I’m hoping someone here can help me out, or point me at a resource that explains what I’m doing wrong.

Thanks in advance!

Right, so 15 minutes after posting this, I seem to have found a solution…

a.py:

import hassapi as hass

class A(hass.Hass):

  def initialize(self):
    b = B(self)
    b.test()

b.py:

import hassapi as hass

class B:

  def __init__(self, hass):
    self.hass = hass

  def test(self):
    self.hass.turn_on("light.keuken_5")

I also removed B from the apps.yaml file, and now, when I run a.py, no errors are thrown and my kitchen lights turn on!

I’m still curious to hear if this is the right way to do this. If not, please let me know! I’m always eager to learn more