How to run/test AppDaemon app code by manually running it?

Is there a way to run my AppDaemon app python code manually? Probably moreso I would have helper files and functions that are used by the AppDaemon app so after a way to quickly iterate, and run that specific helper file’s function, say from a python console, or perhaps I create a testMyFunctions.py file that I want to run, and it word hook into test my AppDaemon app functions.

The functions would have calls in it such as
* self.toggle(“light.living_room”)

if you want a test file then do just like i do:
add this file to your apps dir:

import appdaemon.plugins.hass.hassapi as hass

class test(hass.Hass):

    def initialize(self):
        self.log("just for testing")


    def timercallback(self,kwargs):
        return

add a test.yaml with this:

test:
  module: test
  class: test

running an app without running AD would be impossible. (unless it hass no functions like you want to test)
but everytime you change this file it will take result immediatly, so its pretty good for testing.

ok thanks Rene,

What about logging out of helper files that you call? I’m not quite sure how to be able to call “self.log(“just for testing”)” from a helper file used within my AppDaemon dev files, such that it will go to the AppDaemon log file?

self.log only works in apps (or in files that have imported an appdaemon plugin.
so then you need to use other methods of logging.

i only call apps and existing libs. and ask andrew i can do almost anything he doesnt expect :wink:
what can you do in a helper file, that you cant do in an app?
and you can call any app from any other app.

really just using the approach of breaking out functionality across multiple files, do you don’t have one big file with heaps of functions.

So sounds like I need to just look up/use a python logger then. Would have been nice to get the log entries into the same log file (i.e. the default appdaemon log file)

cant you import your big file with your functions in an app?
or even better, rewrite it to an app?
from that moment you can use get_app() and use every function in every app.

so in my app I have for example:

from pvoutput import PvOutputApi
...
api = PvOutputApi(api_key="mykey", system_id="myid")
api.update_status(power_generation=self.solar, power_consumption=self.power)

So what I’d like to be able to do is put “log” statements within the pvoutput.py file, and within it’s update_status function for example.

from pvoutput import PvOutputApi
import appdaemon.plugins.hass.hassapi as hass

class test(hass.Hass):

    def initialize(self):
        self.log("just for testing")
    def just_a_def(self):
        api = PvOutputApi(api_key="mykey", system_id="myid")
        api.update_status(power_generation=self.solar, power_consumption=self.power)
        self.log(api.update_status)

thanks for the example - what I want to be able to do is put logging through the actual update_status function itself (which is in the pvoutput.py file

  def ADlog(self, text):
    with open("your log file", 'a') as log:
        log.write(text + "\n")

you can add this to your pvoutput.py
from that moment you can ad anything to your AD log by using:

self.ADlog(“what you want to say”)

that sounds good Rene - thanks for that - I’m assuming there would be no problems from the log file getting written to from multiple places then - I’ll try it out soon

if you log to much from different places to the same log, all that can happen is that they screw each other up.
if 2 programs write to the log at the same time, it could generate an error if the file is written to in the exact same time. or they could overwrite each others logging.

with a relativ small log like this, that doesnt get entries on a secondly base, the case that that happens is small.

From the Python logging manual

Multiple calls to getLogger() with the same name will always return a reference to the same Logger object.

A quick grep through the appdaemon code shows that

    conf.logger = logging.getLogger("log1")

So if you do this in your helper file

import logging

logger = logging.getLogger("log1")

You should be able to log to the same place as appdaemon. Theoretically, I haven’t actually done it.

2 Likes

ok - thanks