Voici le code (il est tout frais => bugs possible...)
il demande l'installation du client telnet viTalk
(point 9 et 10 de ce tuto :
https://www.bricozone.fr/t/interface-vi ... -pi.14671/ )
domo2vito.py (pensez à faire le "sudo chmod +x domo2vito.py " pour le rendre executable)
Code : Tout sélectionner
#!/usr/bin/python
# domo2vito
# JS MARTIN - 05/2016
import telnetlib
import sys
import os
import time
import requests
from requests.auth import HTTPBasicAuth
############# Parameters #################################
#~~~~~~~~~~ Parameters Domoticz ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
domoticz_ip='192.168.10.40'
domoticz_port='8080'
user=''
password=''
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Commands viTalk
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#autorized commands
commands = ["mode","saving","party","party_soll_temp","outdoor_temp","raum_soll_temp","raum_ist_temp","k_ist_temp","ww_soll_temp","ww_ist_temp","k_abgas_temp","power"]
#write only commands
write_only_commands = ["mode","saving","party","party_soll_temp","ww_soll_temp"]
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# IDX de Domoticz
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# remplacer XXX par votre idx de votre widget
dummy_idx={'mode':343, 'saving': 344, 'party':345, 'party_soll_temp':346, 'outdoor_temp':347, 'raum_soll_temp':355, 'raum_ist_temp':349, 'k_ist_temp':350, 'ww_soll_temp':351, 'ww_ist_temp':352, 'k_abgas_temp':353, 'power':354}
# mode : dummy selector switch OFF/Water/Heating (Boiler [FR:chaudiere] stand-by/water only/water+heating)
# saving : dummy switch ON/OFF (Boiler eco ON/OFF)
# party : dummy switch ON/OFF (party temporary manual control)
# party_soll_temp: dummy thermostat setpoint (party temperature)
# outdoor_temp : dummy temp (outdoor temperature)
# raum_soll_temp : dummy temp (heating setpoint [FR:consigne] temperature) - note : without vitotronic, use thermostat setpoint
# raum_ist_temp : dummy temp (heating current [FR:actuelle] temperature)
# k_ist_temp : dummy temp (boiler current temperature)
# ww_soll_temp : dummy temp (hot water setpoint temperature)
# ww_ist_temp : dummy temp (hot water current temperature)
# k_abgas_temp : dummy temp (exhaust gas [FR:gaz evacue'] temperature)
# power : dummy percentage (% boiler power)
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Mode debug
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# if debug = True => verbose
debug=True
#######################################################################
###### viTalk_connect() : connect to viTalk localhost:83
#######################################################################
def viTalk_connect():
retry=3
while retry!=0:
try:
tn = telnetlib.Telnet("localhost", 83)
print "Connected to viTalk telnet !"
return tn
except:
print "Connection ERROR - I try to restart viTalk deamon"
#I try to restart viTalk
cmd = 'sudo service vitalk restart'
os.system(cmd)
time.sleep(10)
retry-=1
else:
tn.read_until(b"$", 5)
if retry==0:
tn.close()
print "Connection ERROR - I could not restart viTalk deamon"
sys.exit()
######
#######################################################################
###### viTalk_read(cmd) : read a value
#######################################################################
def viTalk_read(cmd):
global debug
global tn
retry=3
value=""
while retry!=0:
tn.read_until(b"$", 5)
tn.write(b"g "+cmd+"\n")
value=tn.read_until(b"$", 5).strip("\n$")
if value != "":
break
retry-=1
if retry==0:
tn.close()
print(cmd+": "+value+" error")
sys.exit()
value = value.strip("\n$")
if debug:
print "get "+cmd+" : answer = "+value
return value
######
#######################################################################
###### viTalk_set(cmd,val) : unsecured write
#######################################################################
def viTalk_set(cmd,val):
global debug
global tn
tn.write(b"s "+cmd+" "+val+"\n")
tn.read_until(b"$", 5).strip("\n$")
if debug:
print("set "+cmd+" "+val)
######
#######################################################################
###### viTalk_write(cmd,val) : secured write (I check if value is
###### really updated)
#######################################################################
def viTalk_write(cmd,val):
global debug
global tn
retry=3
value=""
while retry!=0:
viTalk_set(cmd,val)
tn.read_until(b"$", 5)
value=viTalk_read(cmd)
if val==value:
break
retry-=1
if retry==0:
tn.close()
print(cmd+" "+val+" : error")
sys.exit()
if debug:
print "check last command set "+cmd+" "+val+" : OK"
#########
#######################################################################
###### check_command(cmd) : is it an authorized command ?
#######################################################################
def check_command(cmd):
global commands
global debug
try:
#check position of cmd in commands list
commands.index(cmd)
except:
print "command not found"
return False
else:
if debug:
print "this command "+cmd+" is known"
return True
####
#######################################################################
###### check_set_command(cmd) : is it an authorized write command ?
#######################################################################
def check_set_command(cmd):
global commands
global debug
try:
#check position of cmd in write only commands list
write_only_commands.index(cmd)
except:
print "set command not found (I can't write with this command)"
return False
else:
if debug:
print "this command "+cmd+" is known for writing"
return True
####
#### START ####
# arguments in command line
nb_arg=len(sys.argv)-1
if nb_arg==0:
print "HELP : "
print "domo2vito update : update all domoticz values"
print "domo2vito command : get command (e.g. domo2vito mode <=> get mode)"
print "domo2vito command value : set command value (e.g. domo2vito mode 1 <=> set mode 1)"
if nb_arg==1 and sys.argv[1]!="update":
if check_command(sys.argv[1]):
if debug:
print "get "+sys.argv[1]
tn=viTalk_connect()
viTalk_read(sys.argv[1])
tn.close()
if nb_arg==1 and sys.argv[1]=="update":
tn=viTalk_connect()
values=[]
i=0
tn.read_until(b"$", 5)
for command in commands:
tn.write(b"g "+command+"\n")
value=tn.read_until(b"$", 5).strip("\n$")
if debug:
print str(i)+") command : get "+command+ " => "+value
values.append(value)
i+=1
tn.close()
#mode (selector level : 0=OFF 10=water 20=heating)
req='http://'+domoticz_ip+':'+domoticz_port+'/json.htm?type=command¶m=switchlight&idx='+str(dummy_idx['mode'])+'&switchcmd=Set%20Level&level='+str(int(values[0])*10)
requests.get(req,auth=HTTPBasicAuth(user,password))
# saving : dummy switch ON/OFF (Boiler eco ON/OFF)
if values[1]=="0":
req='http://'+domoticz_ip+':'+domoticz_port+'/json.htm?type=command¶m=switchlight&idx='+str(dummy_idx['saving'])+'&switchcmd=Off'
else:
req='http://'+domoticz_ip+':'+domoticz_port+'/json.htm?type=command¶m=switchlight&idx='+str(dummy_idx['saving'])+'&switchcmd=On'
requests.get(req,auth=HTTPBasicAuth(user,password))
# party : dummy switch ON/OFF (party temporary manual control)
if values[2]=="0":
req='http://'+domoticz_ip+':'+domoticz_port+'/json.htm?type=command¶m=switchlight&idx='+str(dummy_idx['party'])+'&switchcmd=Off'
else:
req='http://'+domoticz_ip+':'+domoticz_port+'/json.htm?type=command¶m=switchlight&idx='+str(dummy_idx['party'])+'&switchcmd=On'
requests.get(req,auth=HTTPBasicAuth(user,password))
# party_soll_temp: dummy thermostat setpoint (party temperature)
req='http://'+domoticz_ip+':'+domoticz_port+'/json.htm?type=command¶m=udevice&idx='+str(dummy_idx['party_soll_temp'])+'&nvalue=0&svalue='+values[3]
requests.get(req,auth=HTTPBasicAuth(user,password))
# outdoor_temp : dummy temp (outdoor temperature)
req='http://'+domoticz_ip+':'+domoticz_port+'/json.htm?type=command¶m=udevice&idx='+str(dummy_idx['outdoor_temp'])+'&nvalue=0&svalue='+values[4]
requests.get(req,auth=HTTPBasicAuth(user,password))
# raum_soll_temp : dummy temp (heating setpoint [FR:consigne] temperature) - note : without vitotronic, use thermostat setpoint
req='http://'+domoticz_ip+':'+domoticz_port+'/json.htm?type=command¶m=udevice&idx='+str(dummy_idx['raum_soll_temp'])+'&nvalue=0&svalue='+values[5]
requests.get(req,auth=HTTPBasicAuth(user,password))
# raum_ist_temp : dummy temp (heating current [FR:actuelle] temperature)
req='http://'+domoticz_ip+':'+domoticz_port+'/json.htm?type=command¶m=udevice&idx='+str(dummy_idx['raum_ist_temp'])+'&nvalue=0&svalue='+values[6]
requests.get(req,auth=HTTPBasicAuth(user,password))
# k_ist_temp : dummy temp (boiler current temperature)
req='http://'+domoticz_ip+':'+domoticz_port+'/json.htm?type=command¶m=udevice&idx='+str(dummy_idx['k_ist_temp'])+'&nvalue=0&svalue='+values[7]
requests.get(req,auth=HTTPBasicAuth(user,password))
# ww_soll_temp : dummy temp (hot water setpoint temperature)
req='http://'+domoticz_ip+':'+domoticz_port+'/json.htm?type=command¶m=udevice&idx='+str(dummy_idx['ww_soll_temp'])+'&nvalue=0&svalue='+values[8]
requests.get(req,auth=HTTPBasicAuth(user,password))
# ww_ist_temp : dummy temp (hot water current temperature)
req='http://'+domoticz_ip+':'+domoticz_port+'/json.htm?type=command¶m=udevice&idx='+str(dummy_idx['ww_ist_temp'])+'&nvalue=0&svalue='+values[9]
requests.get(req,auth=HTTPBasicAuth(user,password))
# k_abgas_temp : dummy temp (exhaust gas [FR:gaz evacue'] temperature)
req='http://'+domoticz_ip+':'+domoticz_port+'/json.htm?type=command¶m=udevice&idx='+str(dummy_idx['k_abgas_temp'])+'&nvalue=0&svalue='+values[10]
requests.get(req,auth=HTTPBasicAuth(user,password))
# power : dummy percentage (% boiler power)
req='http://'+domoticz_ip+':'+domoticz_port+'/json.htm?type=command¶m=udevice&idx='+str(dummy_idx['power'])+'&nvalue=0&svalue='+values[11]
requests.get(req,auth=HTTPBasicAuth(user,password))
if nb_arg==2:
if check_set_command(sys.argv[1]):
print "set "+sys.argv[1]+" "+sys.argv[2]
tn=viTalk_connect()
viTalk_write(sys.argv[1],sys.argv[2])
tn.close()
Pour une mise à jour régulière :
ajouter dans la crontable (crontab -e) la ligne :
Code : Tout sélectionner
*/3 * * * * /home/pi/domoticz/scripts/python/domo2vito.py update
(ici toutes les 3 minutes)
Les widgets nécessaires :

- widgets.JPG (129.27 Kio) Consulté 11955 fois
La configuration des switchs est très simple : il suffit de rajouter le script python et les paramètres en arguments :

- switch_config.JPG (59.73 Kio) Consulté 11952 fois
(note : viTalk ne fonctionne pas totalement avec le mode party : c'est peut-être dû à ma commande déportée Vitotronic 300... si party est activé, il est possible de le désactiver mais pas l'inverse) ; le mode saving fonctionne (si la chaudière est en mode chauffage bien entendu).
Pour les thermostats (setpoint), je n'ai pas encore regardé. Ils se mettent bien à jour mais c'est tout pour le moment (j'aimerai configurer ce qui se passe lorsque l'on clique sur "regler").
Les températures remontent bien dans Domoticz :

- temp.JPG (53.27 Kio) Consulté 11950 fois