MQTT Interface

From Hubitat Documentation
Revision as of 13:03, 27 June 2019 by Cschwer (talk | contribs)
Jump to: navigation, search

Overview

NOTE: This interface is in alpha status and is liable to change. Once sufficient feedback has been received it will be finalized and the method names will change. Hubitat allows for opening and maintaining a connection to an MQTT broker from the hub. All code needs to be contained in a driver, there is no option to open an MQTT client connection from an app. Hubitat provides methods to connect and disconnect to the broker, subscribe and publish messages to topics. In addition it is required to create a method in the driver that accepts incoming messages (parse) and another method that will be called with any status updates for the client connection (mqttClientStatus).

Methods

Hubitat Provided Methods

mqttConnect

Signature
void hubitat.helper.InterfaceUtils.alphaV1mqttConnect(DeviceWrapper device, String broker, String clientId, String username, String password)
void hubitat.helper.InterfaceUtils.alphaV1mqttConnect(DeviceWrapper device, String broker, String clientId, String username, String password, [option name/value pairs])
Parameters
device - The current device that is calling the method.
broker - The url of the broker to connect to.
clientId - The client id to use when connecting to the broker.
username - The username to use when connecting to the broker, use null if no username is necessary.
password - The password to use when connecting to the broker, use null if no password is necessary.

mqttDisconnect

Signature
void hubitat.helper.InterfaceUtils.alphaV1mqttDisconnect(DeviceWrapper device)
Parameters
device - The current device that is calling the method.

mqttSubscribe

Signature
void hubitat.helper.InterfaceUtils.alphaV1mqttSubscribe(DeviceWrapper device, String topicFilter, int qos = 1)
Parameters
device - The current device that is calling the method.
topicFilter - The topic / filter to subscribe to.
qos - (optional, defaults to 1) The qos to use when subscribing.

mqttUnsubscribe

Signature
void hubitat.helper.InterfaceUtils.alphaV1mqttUnsubscribe(DeviceWrapper device, String topicFilter)
Parameters
device - The current device that is calling the method.
topicFilter - The topic filter to unsubscribe from.

mqttPublish

Signature
void hubitat.helper.InterfaceUtils.alphaV1mqttPublish(DeviceWrapper device, String topic, String payload, int qos = 1, boolean retained = false)
Parameters
device - The current device that is calling the method.
topic - The topic to publish the message to.
payload - The body of the message.
qos - (optional, defaults to 1) The qos to use when publishing the message.
retained - (optional, defaults to false) Retain message in queue if subscriber is not listening.

parseMqttMessage

Signature
Map hubitat.helper.InterfaceUtils.alphaV1parseMqttMessage(String stringToParse)
Parameters
stringToParse - The string coming from the MQTT brokers subscribed topic.
Returns
Map - the string passed as a parameter is split into a Map and returned.

User defined methods

parse(String message) - This method is called with any incoming messages from the subscribed topics of the MQTT broker. This is a standard method for drivers.
mqttClientStatus(String message) - This method is called with any status messages from the MQTT client connection (disconnections, errors during connect, etc)