I currently use Proximix Girder for home automation, but this has been discontinued so I'm looking for an alternative.
Keeping LUA as an option is appealing.
I have an extensive array of sensors and output via many protocols. Most however are serial.
As such I am trying to set up a proof of principle plugin that should be able to poll a defined TCPIP on my local network (address:port), and parse the string response into devices so I can do something with it.
These could be switches, analogue values, sensor states, and output requests.
I consider myself to be capable of picking up most programming languages to a high level, but for the life of me I just can not get my head round making this work.
I'm convinced once I get a proof of concept for some basic building block protocols (none out of the box seem to suit), I'll be on my way.
So, I've cobbled together the following from a mixture of other examples, and I can add it as an item of hardware.
It is located in:
/home/pi/domoticz/plugins/TCPIP and the filename is plugin.py
Code: Select all
"""
<plugin key="TCPIPSerial2" name="TCP IP Serial Driver2" author="JPF" version="1.0.0" >
<description>
<h2>TCP IP Serial Driver</h2><br/>
Test TCPIP Serial driver
</description>
<params>
<param field="Address" label="IP Address" width="200px" required="true" default="192.168.1.000"/>
<param field="Port" label="Port" width="200px" required="true" default="0000"/>
<param field="Mode6" label="Debug" width="150px">
<options>
<option label="None" value="0" default="true" />
<option label="Python Only" value="2"/>
<option label="Basic Debugging" value="62"/>
<option label="Basic+Messages" value="126"/>
<option label="Connections Only" value="16"/>
<option label="Connections+Python" value="18"/>
<option label="Connections+Queue" value="144"/>
<option label="All" value="-1"/>
</options>
</param>
</params>
</plugin>
"""
import Domoticz
class BasePlugin:
enabled = False
lastPolled = 0
lastResponse = 0
TCPIPConn = None
def __init__(self):
return
def onStart(self):
if Parameters["Mode6"] != "0":
Domoticz.Debugging(int(Parameters["Mode6"]))
DumpConfigToLog()
self.TCPIPConn=Domoticz.Connection(Name="TCPIP Connection", Transport="TCP/IP", Protocol="None", Address=Parameters["Address"], Port=Parameters["Port"])
self.TCPIPConn.Listen()
for Device in Devices:
UpdateDevice(Device, Devices[Device].nValue, Devices[Device].sValue, 1)
def onStop(self):
Domoticz.Log("onStop called")
def onConnect(self, Connection, Status, Description):
if (Connection == self.TCPIPConn):
if (Status == 0):
Domoticz.Log("Connected successfully to: "+Connection.Address+":"+Connection.Port)
self.TCPIPConn.Send("PollDevice\r")
else:
#nothing
self.TCPIPConn = None
def onMessage(self, Connection, Data):
strData = Data.decode("utf-8", "ignore")
self.lastResponse = 0
Domoticz.Debug("Received Data: '"+strData)
def onCommand(self, Unit, Command, Level, Hue):
Domoticz.Log("onCommand called for Unit " + str(Unit) + ": Parameter '" + str(Command) + "', Level: " + str(Level))
def onDisconnect(self, Connection):
Domoticz.Log("Hardware has disconnected.")
self.TCPIPConn = None
for Device in Devices:
UpdateDevice(Device, Devices[Device].nValue, Devices[Device].sValue, 1)
return
def onHeartbeat(self):
#nothing
global TCPIPConn
TCPIPConn = BasePlugin()
def onStart():
global TCPIPConn
TCPIPConn.onStart()
def onStop():
global TCPIPConn
TCPIPConn.onStop()
def onConnect(Connection, Status, Description):
global TCPIPConn
TCPIPConn.onConnect(Connection, Status, Description)
def onMessage(Connection, Data):
global TCPIPConn
TCPIPConn.onMessage(Connection, Data)
def onCommand(Unit, Command, Level, Hue):
global TCPIPConn
TCPIPConn.onCommand(Unit, Command, Level, Hue)
def onDisconnect(Connection):
global TCPIPConn
TCPIPConn.onDisconnect(Connection)
def onHeartbeat():
global TCPIPConn
TCPIPConn.onHeartbeat()
# Generic helper functions
def DumpConfigToLog():
for x in Parameters:
if Parameters[x] != "":
Domoticz.Debug( "'" + x + "':'" + str(Parameters[x]) + "'")
Domoticz.Debug("Device count: " + str(len(Devices)))
for x in Devices:
Domoticz.Debug("Device: " + str(x) + " - " + str(Devices[x]))
Domoticz.Debug("Device ID: '" + str(Devices[x].ID) + "'")
Domoticz.Debug("Device Name: '" + Devices[x].Name + "'")
Domoticz.Debug("Device nValue: " + str(Devices[x].nValue))
Domoticz.Debug("Device sValue: '" + Devices[x].sValue + "'")
Domoticz.Debug("Device LastLevel: " + str(Devices[x].LastLevel))
return
def UpdateDevice(Unit, nValue, sValue, TimedOut):
# Make sure that the Domoticz device still exists (they can be deleted) before updating it
if (Unit in Devices):
if (Devices[Unit].nValue != nValue) or (Devices[Unit].sValue != sValue) or (Devices[Unit].TimedOut != TimedOut):
Devices[Unit].Update(nValue=nValue, sValue=str(sValue), TimedOut=TimedOut)
Domoticz.Log("Update "+str(nValue)+":'"+str(sValue)+"' ("+Devices[Unit].Name+")")
return
def DecodeDDDMessage(Message):
# Sample discovery message
# AMXB<-SDKClass=Receiver><-Make=DENON><-Model=AVR-4306>
strChunks = Message.strip()
strChunks = strChunks[4:len(strChunks)-1].replace("<-","")
dirChunks = dict(item.split("=") for item in strChunks.split(">"))
return dirChunks
However, I'm stuck on these errors in the log:
I can see there is a variable not defined, but I just can't understand the concept of what is going on in this plugin.2019-01-23 22:46:12.229 Status: (TEST1) Started.
2019-01-23 22:46:12.403 Status: (TEST1) Entering work loop.
2019-01-23 22:46:12.403 Status: (TEST1) Initialized version 1.0.0, author 'JPF'
2019-01-23 22:46:12.405 Error: (TEST1) 'onStart' failed 'NameError':'name 'TCPIPConn' is not defined'.
2019-01-23 22:46:12.405 Error: (TEST1) ----> Line 82 in /home/pi/domoticz/plugins/TCPIP/plugin.py, function onStart
2019-01-23 22:46:21.922 Error: (TEST1) 'onHeartbeat' failed 'NameError':'name 'TCPIPConn' is not defined'.
2019-01-23 22:46:21.922 Error: (TEST1) ----> Line 106 in /home/pi/domoticz/plugins/TCPIP/plugin.py, function onHeartbeat
2019-01-23 22:46:31.940 Error: (TEST1) 'onHeartbeat' failed 'NameError':'name 'TCPIPConn' is not defined'.
2019-01-23 22:46:31.940 Error: (TEST1) ----> Line 106 in /home/pi/domoticz/plugins/TCPIP/plugin.py, function onHeartbeat
Can anyone point me in the right direction and/or assist with getting me up an running.
I have Domoticz:
Version: 4.9741
Build Hash: 195d46ab
Compile Date: 2018-07-03 09:28:39
dzVents Version: 2.4.6
Python Version: 3.4.4 (default, Apr 17 2016, 16:02:33) [GCC 5.3.1 20160409]