#!/usr/bin/env python
# -*- coding: utf-8 -*-
import socket
import time
linknx_host="ip.for.my.linknxhost"
linknx_port=1028
linknx_write_success=""
linknx_read_first="" # First part
linknx_read_second="\n\4"
# name from linknx, for example: light_hobby
# value in linknx on/off to turn lights on and off
def write(name, value):
try:
s = socket.socket()
s.connect((linknx_host,linknx_port))
s.send(bytes("\n\4","utf-8"))
res = s.recv(128).decode("utf-8")
s.close()
except:
return False
else:
if res[0:len(linknx_write_success)] == linknx_write_success:
return True
else:
return False
# read status from linknx name
def read(name):
try:
s = socket.socket()
s.connect((linknx_host,linknx_port))
s.send(bytes("\n\4","utf-8"))
res = s.recv(128).decode("utf-8")
s.close()
except:
return ""
else:
if res[0:len(linknx_read_first)] == linknx_read_first:
rest=res[len(linknx_read_first):]
res=rest[0:len(rest)-len(linknx_read_second)]
return res
else:
return ""
# dim up linknx name (must be of type 3.007, usually suffixed _dim)
def dim_up(name):
write(name,"up")
time.sleep(5)
write(name,"stop")
# dim down linknx name (must be of type 3.007, usually suffixed _dim)
def dim_down(name):
write(name,"down")
time.sleep(5)
write(name,"stop")