def onDisconnect(self, Connection): if (self._connection != Connection): return Domoticz.Debug("MqttClient::onDisonnect") Domoticz.Error("Disconnected from MQTT Server: {}:{}".format( Connection.Address, Connection.Port) ) self.close() if self.on_mqtt_disconnected_cb != None: self.on_mqtt_disconnected_cb()
def onConnect(self, Connection, Status, Description): if (self._connection != Connection): return if (Status == 0): Domoticz.Log("Connected to MQTT Server: {}:{}".format( Connection.Address, Connection.Port) ) Domoticz.Debug("MQTT CLIENT ID: '" + self.client_id + "'") self._connection.Send({'Verb': 'CONNECT', 'ID': self.client_id}) else: Domoticz.Error("Failed to connect to: {}:{}, Description: {}".format( Connection.Address, Connection.Port, Description) )
def __str__(self): Domoticz.Debug("MqttClient::__str__") if (self._connection != None): return str(self._connection) else: return "None"
def _open(self): Domoticz.Debug("MqttClient::open") if (self._connection != None): self.close() self.isConnected = False self._connection = Domoticz.Connection( Name=self.address, Transport="TCP/IP", Protocol="MQTTS" if self.port == "8883" else "MQTT", Address=self.address, Port=self.port ) self._connection.Connect()
def close(self): Domoticz.Debug("MqttClient::close") if self._connection != None and self._connection.Connected(): self._connection.Send({ 'Verb' : 'DISCONNECT' }) self._connection.Disconnect() self._connection = None self.isConnected = False
def subscribe(self, topics): Domoticz.Debug("MqttClient::subscribe") subscriptionlist = [] for topic in topics: subscriptionlist.append({'Topic': topic, 'QoS': 0}) if (self._connection == None or not self.isConnected): self._open() else: self._connection.Send({'Verb': 'SUBSCRIBE', 'Topics': subscriptionlist})
def publish(self, topic, payload, retain=0): Domoticz.Debug("MqttClient::publish " + topic + " (" + payload + ")") if (self._connection == None or not self.isConnected): self._open() else: self._connection.Send({ 'Verb': 'PUBLISH', 'Topic': topic, 'Payload': bytearray(payload, 'utf-8'), 'Retain': retain })
def __init__(self, address, port, client_id, on_mqtt_connected_cb, on_mqtt_disconnected_cb, on_mqtt_message_cb, on_mqtt_subscribed_cb): Domoticz.Debug("MqttClient::__init__") self.address = address self.port = port self.client_id = client_id if client_id != "" else self._generate_mqtt_client_id() self.on_mqtt_connected_cb = on_mqtt_connected_cb self.on_mqtt_disconnected_cb = on_mqtt_disconnected_cb self.on_mqtt_subscribed_cb = on_mqtt_subscribed_cb self.on_mqtt_message_cb = on_mqtt_message_cb self._open()
def onStart(self): self.debugging = Parameters["Mode6"] if self.debugging == "Verbose": Domoticz.Debugging(2 + 4 + 8 + 16 + 64) if self.debugging == "Debug": Domoticz.Debugging(2) self.install() self.base_topic = Parameters["Mode1"].strip() mqtt_server_address = Parameters["Address"].strip() mqtt_server_port = Parameters["Port"].strip() mqtt_client_id = Parameters["Mode3"].strip() self.mqttClient = MqttClient(mqtt_server_address, mqtt_server_port, mqtt_client_id, self.onMQTTConnected, self.onMQTTDisconnected, self.onMQTTPublish, self.onMQTTSubscribed) self.api = API(self.onApiCommand) self.devices_manager = DevicesManager() self.groups_manager = GroupsManager()
def onHeartbeat(self): if self._connection is None or (not self._connection.Connecting() and not self._connection.Connected() or not self.isConnected): Domoticz.Debug("MqttClient::Reconnecting") self._open() else: self.ping()
def error(message): return Domoticz.Error(message)
def log(message): return Domoticz.Log(message)
def set_configuration(value): return Domoticz.Configuration(value)
def get_configuration(): return Domoticz.Configuration()
def create_device(**kwargs): device = Domoticz.Unit(**kwargs) device.Create() return device
def debug(message): return Domoticz.Debug(message)