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 Events(ad: appdaemon.appdaemon.AppDaemon)

Encapsulate event handling.

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.

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.

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.

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.

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.

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.

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.

logging

class DuplicateFilter(logger, threshold, delay, timeout)
filter(record)

Determine if the specified record is to be logged.

Is the specified record to be logged? Returns 0 for no, nonzero for yes. If deemed appropriate, the record may be modified in-place.

class 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 LogSubscriptionHandler(ad: appdaemon.appdaemon.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.

main

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

Base class for plugins to set up _logging

scheduler

state

stream

thread_async

class ThreadAsync(ad: appdaemon.appdaemon.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 Utility(ad: appdaemon.appdaemon.AppDaemon)

Class that includes the utility loop.

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
loop()

The main utility loop.

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

utils

class 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 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 StateAttrs(dict)
class EntityStateAttrs(dict)
write_to_file(yaml_file, **kwargs)

Used to write the app to Yaml file