Python: Status eines Devices abfragen und Kommando absetzen

Begonnen von dero, 09 November 2013, 09:41:06

Vorheriges Thema - Nächstes Thema

dero



import telnetlib

control_host= <host>
control_port= <telnet port> (7072)
control_password= <telnet password>

def FHEM_Value(dvc):
tn= telnetlib.Telnet( control_host, control_port, 5 )
tn.read_until( "Password: " )
tn.write( control_password + "\n" )
tn.write( "{'=' . Value('" + dvc + "')}\n" )
tn.read_until( "=" )
res= tn.read_until( "\r" )[ :-1 ]
tn.close()
return res

def FHEM_Cmd(cmd):
tn= telnetlib.Telnet( control_host, control_port, 5 )
tn.read_until( "Password: " )
tn.write( control_password + "\n" )
tn.write( cmd + "\n" )
# the following makes sure that we don't close the connection before the cmd was executed... (synchronization point)
tn.write( "{ '=' }\n" )
tn.read_until( "=" )
tn.close()