Aller au contenu
kiwi

Hc2 Comme Passerelle Zwave Pour Domoticz :p

Recommended Posts

Hello les boys,

 

Comme chez Fibaro la gestion des API Lua est plus proche de la buée sur des vitres que de la stabilité qu'aurais une tesla sur une route, j'ai décidé de mon coté de n'utiliser ma HC2 que en passerelle Zwave <=> IP.

 

L'API Http étant assez stable et aimant pas mal les "Planning" de domoticz qui me permettent de faire des planning de différents actionneurs sans me casser les pieds avec des scènes, ou GEA (qui est bien en passant mais pour juste allumer un contacteur en position confort pour un module FP c'est quand même sévèrement overkill).

 

Domoticz permet de faire des actions via soit un module réél (genre au hasard, une télécommande RFX433MHz) ou via un module virtuel.

 

Exemple :

1474983119.png

Qui a comme actions :

1474983142.png

 

Alors on pourrais utiliser directement un http://hc2/ mais pour l'instant domoticz ne supporte pas l'authentification via l'URL (faut que je code la chose....)

 

Dans mon cas le script hc2fp envoi les bonnes commandes a la HC2 (il faut changer le host et les login password en haut du code).

 

J'ai 3 scripts pour l'instant :

  • hc2fp : gestion fil pilote (via un  module qubino)
  • hc2sw : gestion switch on / off (typiquement pour les fibaro plugs)
  • temperature : qui me remontent les temperatures à  coup de crontab sur domoticz.

 

Alors oui Domoticz fait du ZWave (assez bien...) mais pas aussi stable que sur l'HC2 (vous avez bien lu). En effet les Fibaro plugs m'envoient plein de données tout le temps et a priori openzwave se prends les pieds dans le tapis... mais ceci n'est qu'un exemple de ce que j'ai eu :)

 

hc2fp :

#!/usr/local/bin/python2

import json
import urllib
import urllib2
import base64
import argparse

# Parameters of HC2
username = "admin"
password = "admin"
hc2_host = "hc2" 	# Or IP

# Callaction
def hc2setvalue(id,todo):
	"Get the value of one HC2 id"
	url = 'http://'+hc2_host+'/api/callAction?deviceID='+str(id)+'&name='+str(todo)
	req = urllib2.Request(url)
	base64string = base64.encodestring('%s:%s' % (username, password)).replace('\n', '')
	req.add_header("Authorization", "Basic %s" % base64string)   
	response = urllib2.urlopen(req)
	json_object = response.read()

parser = argparse.ArgumentParser()
parser.add_argument("id",    type=int, help="ID of HC2 module to be used")
parser.add_argument("--on",  action="store_true", help="Set module to On/Confort")
parser.add_argument("--off", action="store_true", help="Set module to Off")
parser.add_argument("--eco", action="store_true", help="Set module to Eco")
parser.add_argument("--hg", action="store_true", help="Set module to Hors-Gel")

args = parser.parse_args()

if args.on and args.off and args.eco and args.hg:
	print "Use only ONE argument at one time"
elif args.on:
	hc2setvalue(args.id, 'turnOn')
elif args.off:
	hc2setvalue(args.id, 'turnOff')
elif args.eco:
	hc2setvalue(args.id, 'setValue&arg1=25')
elif args.hg:
	hc2setvalue(args.id, 'setValue&arg1=15')
else:
	print "Nothing done"


hc2sw :

#!/usr/local/bin/python2

import json
import urllib
import urllib2
import base64
import argparse

# Parameters of HC2
username = "admin"
password = "admin"
hc2_host = "hc2"	# Or IP

# Callaction
def hc2setvalue(id,todo):
	"Get the value of one HC2 id"
	url = 'http://'+hc2_host+'/api/callAction?deviceID='+str(id)+'&name='+str(todo)
	req = urllib2.Request(url)
	base64string = base64.encodestring('%s:%s' % (username, password)).replace('\n', '')
	req.add_header("Authorization", "Basic %s" % base64string)   
	response = urllib2.urlopen(req)
	json_object = response.read()

parser = argparse.ArgumentParser()
parser.add_argument("id",    type=int, help="ID of HC2 module to be used")
parser.add_argument("--on",  action="store_true", help="Set module to On")
parser.add_argument("--off", action="store_true", help="Set module to On")

args = parser.parse_args()

if args.on and args.off:
	print "You cannot set --on and --off at the same time"
elif args.on:
	hc2setvalue(args.id, 'turnOn')
elif args.off:
	hc2setvalue(args.id, 'turnOff')
else:
	print "Nothing done"


temperature :

#!/usr/local/bin/python2

import json
import urllib
import urllib2
import base64

# Parameters of HC2
username = "admin"
password = "admin"
hc2_host = "hc2"	# or IP
dom_host = "localhost"
dom_port = "8080"

# General way to get value of a HC2 Module
def hc2getvalue( id ):
	"Get the value of one HC2 id"
	url = 'http://'+hc2_host+'/api/devices?id='+str(id)
	req = urllib2.Request(url)
	base64string = base64.encodestring('%s:%s' % (username, password)).replace('\n', '')
	req.add_header("Authorization", "Basic %s" % base64string)   
	response = urllib2.urlopen(req)
	json_object = response.read()
	response_dict = json.loads(json_object)
	hc2value = response_dict.get("properties", {})
	return hc2value.get("value", {})

# Generic update function for domoticz
def dom_update(id, value):
	"Update Domotics with correct values"
	url_domoticz = 'http://'+dom_host+':'+dom_port+'/json.htm?type=command&param=udevice&idx='+str(id)+'&svalue='+str(value)
	req_domoticz =  urllib2.Request(url_domoticz)
	response_domoticz = urllib2.urlopen(req_domoticz)
	json_object_domoticz = response_domoticz.read()
	response_dict_domoticz = json.loads(json_object_domoticz)
	if response_dict_domoticz.get('status',{}) == 'OK' :
		return 1
	else:
		return 0

# HC2 to Domoticz temperature and humidity stuff
def hc2temphum(hc2_temp,hc2_hum,dom_id):
	"Update Domoticz with the right stuff"

	# Get the temperature
	temp = hc2getvalue(hc2_temp)
	# Get the humitidy
	hum  = float(hc2getvalue(hc2_hum))

	hum_stat = 0
	if (hum <= 25.0):
		hum_stat = 2
	elif (hum > 60.0):
		hum_stat = 3
	elif ((hum > 25.0) and (hum <= 60.0)):
		hum_stat = 1
	else:
		hum_stat = 0

	dom_data=str(temp)+';'+str(hum)+';'+str(hum_stat)

	dom_update(dom_id,dom_data)


# Temperature SdB
hc2temphum(14,15,197)
# Temperature Entree
hc2temphum(19,20,199)
# Temperature Chambre Haut
hc2temphum(24,25,198)


Partager ce message


Lien à poster
Partager sur d’autres sites

×