Base

Common parts for a task scheduler.

class mauzr.platform.scheduler.Scheduler

Bases: object

Schedules tasks that are executed by a single runner.

on_delayed(task)

Called when a task was called to late.

Parameters:task (mauzr.platform.scheduler.Task) – A task that was executed with a delay.
on_enabled()

Called when a task was enabled.

class mauzr.platform.scheduler.Task(scheduler, target, after, single)

Bases: object

Represents a single or recurring task that can be executed.

Note that the task has to be enabled before it can run.

Parameters:
  • scheduler (mauzr.platform.scheduler.Scheduler) – The scheduler to provide information to.
  • target (function) – Function to execute
  • after (int) – Time in ms to delay the execution of the target after task has been enabled.
  • single (bool) – If True the task is only executed once. If False the task is executed with the specified delay between executions.
disable()

Disable / cancel this task. No effect if already running.

enable(instant=False, after=None)

Enable the task.

Parameters:
  • instant (bool) – If true the first time the task will be executed without delay. Use this for startup.
  • after (int) – New afer value to set.
Returns:

Self for convenience.

Return type:

mauzr.platform.scheduler.Task

enabled
Returns:True if the task in enabled.
Return type:bool
execute()

Execute the task.

pending
Returns:True if the task if currently pending for execution.
Return type:bool
pending_in
Returns:Time in milliseconds until the next execution.
Return type:int

Common base for MQTT clients.

class mauzr.platform.mqtt.Manager(core, cfgbase='mqtt', **kwargs)

Bases: object

Connect to an MQTT broker to exchange messages via topics.

Parameters:
  • core (object) – Core instance.
  • cfgbase (str) – Configuration entry for this unit.
  • kwargs (dict) – Keyword arguments that will be merged into the config.

Configuration:

  • hosts - A list of dicts containing host, port, user, password, and path to the CA certificiate.
on_connect(session_present)

Subscribe all subscriptions.

Parameters:session_present (bool) – True if the MQTT session is already created.
on_disconnect(*details)

React to disconnections from the broker.

on_message(topic, payload, retained)

Handle messages received via the mqtt broker.

Parameters:
  • topic (str) – Topic of the message.
  • payload (object) – Payload of the message.
  • retained (bool) – If True this is a retained message.
Raises:

Exception – Errors of callback.

publish(topic, payload, retain)

Publish a value to a topic.

Parameters:
  • topic (str) – Topic to publish to.
  • payload (object) – Value to publish.
  • retain (bool) – True if value shall be retained by the server.
Returns:

Return value of underlying client.

Return type:

object

Raises:

Exception – If paho fails.

setup_publish(topic, serializer, qos, default=None)

Prepare publishing to a topic.

Parameters:
  • topic (str) – Topic to publish to. May not contain wildcards.
  • serializer (object) – Serializer that converts outgoing values. If None, value is passed unprocessed.
  • qos (int) – QoS level to use.
  • default (object) – Value to publish on new sessions. May be None.
Raises:

ValueError – If topic is invalid or is already configured in a conflicting manner.

subscribe(topic, callback, serializer, qos)

Subscribe to a topic.

Parameters:
  • topic (str) – Topic to subscribe to. Currently wildcard subcriptions are only supported for whole subdirectories, e.g. a/#, not a/+, a/+/b or a/#/b. If doing so you may not subscribe to a topic in this directory explicitly.
  • callback (function) – Callback to call when a message is received. Is called with topic and the (deserialized) message data as arguments.
  • serializer (object) – Serializer that converts incoming values. If None, value is passed unprocessed.
  • qos (int) – QoS level to request.
Raises:

ValueError – If topic is invalid or already configured in a conflicting manner.