Skip to content

Commit

Permalink
Fixed plugin configuration
Browse files Browse the repository at this point in the history
Reworked validatePrefsConfigUi() and closedPrefsConfigUi() to only run startup when comm preferences actually change.
  • Loading branch information
FlyingDiver committed Apr 7, 2018
1 parent 0545523 commit 9af1860
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 48 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?xml version="1.0"?>
<PluginConfig>
<Field id="configDone" type="checkbox" defaultValue="false" hidden="true"/>
<Field id="infoLabel1" type="label">
<Label>Lutron RadioRA 2 / Caséta Plugin by Jim Lombardo.</Label>
</Field>
Expand All @@ -13,9 +14,6 @@
<Field type="checkbox" id="IP" defaultValue="false">
<Label>Enable IP communications (Checked = IP; Unchecked = Serial)</Label>
</Field>
<Field id="space1" type="label">
<Label/>
</Field>
<Field id="ip_address" type="textfield" defaultValue="" visibleBindingId="IP" visibleBindingValue="true" tooltip="Enter IP address of Lutron gateway.">
<Label>IP Address or Hostname:</Label>
</Field>
Expand Down Expand Up @@ -43,7 +41,4 @@
</Field>
<Field id="simpleSeparator3" type="separator"/>
<Field type="serialport" id="devicePort"/>
<Field id="configDone" type="checkbox" defaultValue="false" hidden="true">
<Label>Is it configured?</Label>
</Field>
</PluginConfig>
85 changes: 43 additions & 42 deletions Lutron RadioRA 2.indigoPlugin/Contents/Server Plugin/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -637,7 +637,7 @@ def validateDeviceConfigUi(self, valuesDict, typeId, devId):
return (False, valuesDict, errorsDict)

return (True, valuesDict)

def runConcurrentThread(self):

try:
Expand Down Expand Up @@ -692,10 +692,10 @@ def serialStartup(self):

self.portEnabled = False

serialUrl = self.getSerialPortUrl(self.pluginPrefs, u"devicePort")
self.logger.info(u"Serial Port URL is: " + serialUrl)
self.serialUrl = self.getSerialPortUrl(self.pluginPrefs, u"devicePort")
self.logger.info(u"Serial Port URL is: " + self.serialUrl)

self.connSerial = self.openSerial(u"Lutron RadioRA", serialUrl, 9600, stopbits=1, timeout=2, writeTimeout=1)
self.connSerial = self.openSerial(u"Lutron RadioRA", self.serialUrl, 9600, stopbits=1, timeout=2, writeTimeout=1)
if self.connSerial is None:
self.logger.error(u"Failed to open serial port")
return
Expand Down Expand Up @@ -756,53 +756,54 @@ def queryAllDevices(self):

# plugin configuration validation
def validatePrefsConfigUi(self, valuesDict):
errorDict = indigo.Dict()

badAddr = "Please use either an IP address (i.e. 1.2.3.4) or a fully qualified host name (i.e. lutron.domain.com)"
if valuesDict["IP"]:
if valuesDict["ip_address"].count('.') != 3:
errorDict = indigo.Dict()
errorDict["ip_address"] = "Please enter an IP address (i.e. 192.168.1.100)"
return (False, valuesDict, errorDict)

self.IP = valuesDict["IP"]
if valuesDict["IP"]:

if valuesDict["ip_address"].count('.') >= 3:
ipOK = True
else:
ipOK = False

try:
if ipOK:
rtn = True
if self.IP == valuesDict["IP"] and self.pluginPrefs["ip_address"] == valuesDict["ip_address"]: # no changes
self.logger.debug(u"validatePrefsConfigUi: IP Config, no changes")
valuesDict["configDone"] = True
else:
errorDict["ip_address"] = badAddr
rtn = (False, valuesDict, errDict)
except AttributeError:
rtn = (True, valuesDict)
self.logger.debug(u"validatePrefsConfigUi: IP Config changed")
valuesDict["configDone"] = False
else: # serial connection

try:
if valuesDict["configDone"]:
self.runstartup = False
else:
if ipOK and rtn:
self.logger.debug(u"Setting configDone to True")
valuesDict["configDone"] = True
self.logger.debug(u"Setting flag to run startup")
self.runstartup = True
except KeyError:
if ipOK and rtn:
self.logger.debug(u"Setting configDone to True")
serialUrl = self.getSerialPortUrl(valuesDict, u"devicePort")

if self.IP == valuesDict["IP"] and self.serialUrl == serialUrl: # no changes
self.logger.debug(u"validatePrefsConfigUi: Serial Config, no changes")
valuesDict["configDone"] = True
self.logger.exception(u"Setting flag to run startup")
self.runstartup = True
self.IP = valuesDict["IP"]
self.logger.debug(u"%s, %s, %s" % (str(rtn), str(ipOK), str(self.IP)))
return rtn
else:
self.logger.debug(u"validatePrefsConfigUi: Serial Config changed")
valuesDict["configDone"] = False

return (True, valuesDict)


def closedPrefsConfigUi(self, valuesDict, userCancelled):
if not userCancelled:
try:
self.logLevel = int(valuesDict[u"logLevel"])
except:
self.logLevel = logging.INFO
if userCancelled:
self.logger.debug(u"closedPrefsConfigUi: User Cancelled")
return

logLevel = int(valuesDict.get(u"logLevel", logging.INFO))
if logLevel != self.logLevel:
self.logLevel = logLevel
self.indigo_log_handler.setLevel(self.logLevel)
self.logger.debug(u"logLevel = " + str(self.logLevel))
self.logger.debug(u"New logLevel = {}".format(self.logLevel))


self.IP = valuesDict["IP"]
self.runstartup = not valuesDict.get("configDone", False)
self.logger.debug(u"closedPrefsConfigUi: Setting self.runstartup = {}".format(self.runstartup))

return


########################################

Expand Down

0 comments on commit 9af1860

Please sign in to comment.