Internal Documentation

These notes are intended to assist anyone that wants to understand AppDaemon’s internals better. Most modules are used from within the AppDaemon object, which is a centralized depository of configuration information and references to the other objects and subsystems within AppDaemon.

appdaemon object

admin

admin_loop

app_management

callbacks

dashboard

events

Module to handle all events within AppDaemon.

class appdaemon.events.Events(ad: AppDaemon)

Encapsulate event handling.

async add_event_callback(name, namespace, cb, event, **kwargs)

Adds a callback for an event which is called internally by apps.

Parameters
  • name (str) – Name of the app.

  • namespace (str) – Namespace of the event.

  • cb – Callback function.

  • event (str) – Name of the event.

  • **kwargs – List of values to filter on, and additional arguments to pass to the callback.

Returns

None or the reference to the callback handle.

async cancel_event_callback(name, handle)

Cancels an event callback.

Parameters
  • name (str) – Name of the app or module.

  • handle – Previously supplied callback handle for the callback.

Returns

None.

async fire_event(namespace, event, **kwargs)

Fires an event.

If the namespace does not have a plugin associated with it, the event will be fired locally. If a plugin is associated, the firing of the event will be delegated to the plugin, under the understanding that when the event is fired, the plugin will notify appdaemon that it occurred, usually via the system the plugin is communicating with.

Parameters
  • namespace (str) – Namespace for the event to be fired in.

  • event (str) – Name of the event.

  • **kwargs – Arguments to associate with the event.

Returns

None.

async has_log_callback(name)

Returns True if the app has a log callback, False otherwise.

Used to prevent callback loops. In the calling logic, if this function returns True the resulting logging event will be suppressed.

Parameters

name (str) – Name of the app.

async info_event_callback(name, handle)

Gets the information of an event callback.

Parameters
  • name (str) – Name of the app or subsystem.

  • handle – Previously supplied handle for the callback.

Returns

A dictionary of callback entries or rise a ValueError if an invalid handle is provided.

async process_event(namespace, data)

Processes an event that has been received either locally or from a plugin.

Parameters
  • namespace (str) – Namespace the event was fired in.

  • data – Data associated with the event.

Returns

None.

async process_event_callbacks(namespace, data)

Processes a pure event callback.

Locate any callbacks that may be registered for this event, check for filters and if appropriate, dispatch the event for further checking and eventual action.

Parameters
  • namespace (str) – Namespace of the event.

  • data – Data associated with the event.

Returns

None.

logging

class appdaemon.logging.AppNameFormatter(fmt=None, datefmt=None, style=None)

Logger formatter to add ‘appname’ as an interpolatable field.

format(record)

Format the specified record as text.

The record’s attribute dictionary is used as the operand to a string formatting operation which yields the returned string. Before formatting the dictionary, a couple of preparatory steps are carried out. The message attribute of the record is computed using LogRecord.getMessage(). If the formatting string uses the time (as determined by a call to usesTime(), formatTime() is called to format the event time. If there is exception information, it is formatted using formatException() and appended to the message.

class appdaemon.logging.DuplicateFilter(logger, threshold, delay, timeout)
filter(record)

Determine if the specified record is to be logged.

Returns True if the record should be logged, or False otherwise. If deemed appropriate, the record may be modified in-place.

class appdaemon.logging.LogSubscriptionHandler(ad: AppDaemon, type)

Handle apps that subscribe to logs.

This Handler requires that it’s formatter is an instance of AppNameFormatter.

emit(record)

Emit a record.

If a formatter is specified, it is used to format the record. The record is then written to the stream with a trailing newline. If exception information is present, it is formatted using traceback.print_exception and appended to the stream. If the stream has an ‘encoding’ attribute, it is used to determine how to do the output to the stream.

AppDaemon main() module.

AppDaemon module that contains main() along with argument parsing, instantiation of the AppDaemon and HTTP Objects, also creates the loop and kicks everything off

class appdaemon.__main__.ADMain

Class to encapsulate all main() functionality.

handle_sig(signum, frame)

Function to handle signals.

SIGUSR1 will result in internal info being dumped to the DIAG log SIGHUP will force a reload of all apps SIGINT and SIGTEM both result in AD shutting down

Parameters
  • signum – Signal number being processed.

  • frame – frame - unused

Returns

None.

init_signals()

Setup signal handling.

main()

Initial AppDaemon entry point.

Parse command line arguments, load configuration, set up logging.

run(appdaemon, hadashboard, admin, aui, api, http)

Start AppDaemon up after initial argument parsing.

Parameters
  • appdaemon – Config for AppDaemon Object.

  • hadashboard – Config for HADashboard Object.

  • admin – Config for admin Object.

  • aui – Config for aui Object.

  • api – Config for API Object

  • http – Config for HTTP Object

Returns

None.

stop()

Called by the signal handler to shut AD down.

Returns

None.

appdaemon.__main__.main()

Called when run from the command line.

main

class appdaemon.plugin_management.PluginBase(ad: AppDaemon, name, args)

Base class for plugins to set up _logging

scheduler

state

stream

thread_async

class appdaemon.thread_async.ThreadAsync(ad: AppDaemon)

Module to translate from the thread world to the async world via queues

threading

utility_loop

Module to handle utility functions within AppDaemon.

class appdaemon.utility_loop.Utility(ad: AppDaemon)

Class that includes the utility loop.

Checks for file changes, overdue threads, thread starvation, and schedules regular state refreshes.

async loop()

The main utility loop.

Loops until stop() is called, checks for file changes, overdue threads, thread starvation, and schedules regular state refreshes.

stop()

Called by the AppDaemon object to terminate the loop cleanly

Returns

None

utils

class appdaemon.utils.AttrDict(*args, **kwargs)

Dictionary subclass whose entries can be accessed by attributes (as well as normally).

static from_nested_dict(data)

Construct nested AttrDicts from nested dictionaries.

class appdaemon.utils.EntityStateAttrs(dict)
class appdaemon.utils.PersistentDict(filename, safe, *args, **kwargs)

Dict-like object that uses a Shelf to persist its contents.

update([E, ]**F) None.  Update D from mapping/iterable E and F.

If E present and has a .keys() method, does: for k in E: D[k] = E[k] If E present and lacks .keys() method, does: for (k, v) in E: D[k] = v In either case, this is followed by: for k, v in F.items(): D[k] = v

class appdaemon.utils.StateAttrs(dict)
appdaemon.utils.get_object_size(obj, seen=None)

Recursively finds size of objects