Import my own class

Hello,

I would like to know how to import my own class, and how to store them, and how to use them.

something like this:

.\mymodule\myclass.py
    def myclass():
        def __init__(self,prop):
            self.prop=prop

        def someMethod(self, someArg):
            # do crazy things

And in the Appdaemon module:

import hassapi as hass
import datetime
from mymodule.myclass import myclass

class MyApp(hass.Hass):
    def initialize(self):
        self.myobj = myclass(prop='data')
        self.listen_state(self.cb, "sencor.name", new = "on")

    def cb(self, entity, attribute, old, new, kwargs):
        ret = self.myobj.someMethod(someArgs=new)
        ....

Of course, I tried, but that doesn’t work. The import fails, I managed to get it to work sometimes, but it isn’t ‘refreshed’ when I modify it.
I also tried to include the module as an app in the yaml file, but it brings some new problems.

I think that I am missing some core concept of AD, can someone explain me please ?

Thanks for your help

Try to include it as a global module in your apps.yaml file. see here Writing AppDaemon Apps — AppDaemon 4.2.1 documentation

Alternatively you can write custom functions that you’d like to re-use throughout your applications and use the get_app() method to pull them in. AppDaemon API Reference — AppDaemon 4.2.1 documentation

Thanks for taking the time to respond. I already read the doc, but it wasn’t clear for me.

so for anyone having a hard time with this like me, here is what works:

in your app.yaml, add your modules like this :

global_modules:
  - myclass
  - myotherclass

don’t try to insert a directory here. adding mymodule.myclass won’t work, neither mymodule\myclass nor myclass.py … just the filename without the py :wink:

then when declaring your app in the app.yaml

myapp:
  module: myapp
  class: MyApp
  global_dependencies:
    - myclass

and in myapp.py, import ignoring the directory:

import hassapi as hass
import datetime
from myclass import myclass

class MyApp(hass.Hass):
    def initialize(self):
        self.myobj = myclass(prop='data')
        self.listen_state(self.cb, "sencor.name", new = "on")

    def cb(self, entity, attribute, old, new, kwargs):
        ret = self.myobj.someMethod(someArgs=new)

Why choose that way vs

self.myclass = self.get_app('myclass')

?
Generally just curios.

I have no idea of what it is doing.

What do you get in self.myclass ? the class ? an instance ? myclass inside another class instance ?

You would get an app which is a class instance itself.