powermon.mqttbroker.mqttbroker module

mqtt broker

  • provides MqttBroker class

class powermon.mqttbroker.mqttbroker.MqttBroker(name, port=None, username=None, password=None)

Bases: object

MqttBroker class - wraps connecting, subscribing and publishing to a mqtt broker

property adhoc_result_topic: str

the topic to publish adhoc results to

property adhoc_topic: str

the topic to listen for any adhoc command

connect() None

connect to mqtt broker

classmethod from_config(config: dict = None) Self

builds the mqtt broker object from a config dict

Parameters:

config (dict, optional) – Defaults to None which will disable class.

Returns:

configured (or disabled if config is None) MqttBroker class

Return type:

Self

on_connect(client, userdata, flags, rc)

callback for connect

on_disconnect(client, userdata, rc)

callback for disconnect

post_adhoc_command(command_code: str) None

shortcut function to publish an adhoc command

post_adhoc_result(payload: str) None

shortcut function to publish the results of an adhoc command

publish(topic: str, payload: str) None
publish messages to the defined mqtt broker
  • if broker name is ‘screen’ will write to stdout instead

Parameters:
  • topic (str) – topic to publish to.

  • payload (str) – content to publish.

start() None

start the mqtt broker

stop() None

stop the mqtt broker

subscribe(topic: str, callback: Callable) None

subscribe to a topic on the mqtt broker

Parameters:
  • topic (str) – topic to subscribe to

  • callback (Callable) – function to call when a message is received