Skip to content

Commit

Permalink
Added delay option to MQTT connection info
Browse files Browse the repository at this point in the history
  • Loading branch information
FlyingDiver committed Jan 19, 2023
1 parent 5ece062 commit dc4e91f
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 5 deletions.
2 changes: 1 addition & 1 deletion MQTT Connector.indigoPlugin/Contents/Info.plist
Expand Up @@ -3,7 +3,7 @@
<plist version="1.0">
<dict>
<key>PluginVersion</key>
<string>2022.0.5</string>
<string>2022.1.0</string>
<key>ServerApiVersion</key>
<string>3.0</string>
<key>IwsApiVersion</key>
Expand Down
Expand Up @@ -61,7 +61,12 @@
<Field id="certFileLabel" type="label" fontColor="darkgray" visibleBindingId="section" visibleBindingValue="connect">
<Label>Non-absolute path (no leading slash) is relative to the Indigo install directory.</Label>
</Field>

<Field id="delay" type="textfield" defaultValue="0" visibleBindingId="section" visibleBindingValue="connect">
<Label>Delay:</Label>
</Field>
<Field id="delay_note" type="label" fontColor="darkgray" visibleBindingId="section" visibleBindingValue="connect">
<Label>Delay starting this device. Generally only used when this is an MQTT Broker device.</Label>
</Field>
<!-- This section for managing topic subscriptions for this broker -->

<Field id="subscriptions_newTopic" type="textfield" visibleBindingId="section" visibleBindingValue="subscriptions" >
Expand Down
Expand Up @@ -5,6 +5,7 @@
import time
import logging
import indigo
import threading
from os.path import exists

import paho.mqtt.client as mqtt
Expand All @@ -25,6 +26,7 @@ def __init__(self, device):

self.username = device.pluginProps.get('username', None).strip()
self.password = device.pluginProps.get('password', None).strip()
self.delay = int(device.pluginProps.get('delay', 0))

self.logger.debug(f"{device.name}: Broker __init__ address = {self.address}, port = {self.port}, protocol = {self.protocol}, transport = {self.transport}")

Expand Down Expand Up @@ -65,15 +67,17 @@ def __init__(self, device):
self.client.on_subscribe = self.on_subscribe
self.client.on_unsubscribe = self.on_unsubscribe

threading.Timer(self.delay, lambda: self.do_connect(device)).start()

def do_connect(self, device):
self.logger.debug(f"{device.name}: do_connect")
try:
self.client.connect(self.address, self.port, 60)
except Exception as e:
self.logger.debug(f"{device.name}: Broker connect error: {e}")
device.updateStateOnServer(key="status", value="Connection Failed")
device.updateStateImageOnServer(indigo.kStateImageSel.SensorTripped)
self.connected = False
else:
self.connected = True
self.client.loop_start()

def disconnect(self):
Expand Down Expand Up @@ -123,7 +127,6 @@ def on_disconnect(self, client, userdata, rc):
self.logger.error(f"{device.name}: Disconnected with result code {rc}")
device.updateStateOnServer(key="status", value="Disconnected {}".format(rc))
device.updateStateImageOnServer(indigo.kStateImageSel.SensorTripped)
self.connected = False

def on_message(self, client, userdata, msg):
device = indigo.devices[self.deviceID]
Expand Down

0 comments on commit dc4e91f

Please sign in to comment.