It works !
This commit is contained in:
@@ -12,7 +12,7 @@ class iDiamant():
|
|||||||
|
|
||||||
access_token = ""
|
access_token = ""
|
||||||
refresh_token = ""
|
refresh_token = ""
|
||||||
liste_home_id = list()
|
volets = {}
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def getToken():
|
def getToken():
|
||||||
@@ -42,7 +42,15 @@ class iDiamant():
|
|||||||
jsonStatus = json.loads(response.text)
|
jsonStatus = json.loads(response.text)
|
||||||
homes = jsonStatus['body']['homes']
|
homes = jsonStatus['body']['homes']
|
||||||
for home in homes:
|
for home in homes:
|
||||||
iDiamant.liste_home_id.append(home['id'])
|
home_id = home['id']
|
||||||
|
modules = home['modules']
|
||||||
|
for module in modules:
|
||||||
|
if "NBR" == module['type']:
|
||||||
|
iDiamant.volets[module['id']] = {
|
||||||
|
'name':module['name'],
|
||||||
|
'bridge':module['bridge'],
|
||||||
|
'id_home':home_id
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
@@ -62,18 +70,10 @@ class iDiamant():
|
|||||||
@staticmethod
|
@staticmethod
|
||||||
def initDiscovery():
|
def initDiscovery():
|
||||||
""" Publication des config pour discovery """
|
""" Publication des config pour discovery """
|
||||||
for home_id in iDiamant.liste_home_id:
|
for volet in iDiamant.volets:
|
||||||
url = "https://api.netatmo.com/api/homestatus?home_id=" + home_id
|
topic = Constantes.mqttTopic + "/cover/" + volet + "/config"
|
||||||
headers = {"Authorization": "Bearer " + iDiamant.access_token}
|
payload = '{'
|
||||||
response = requests.get(url, headers=headers)
|
payload += '"unique_id": "' + volet + '",'
|
||||||
jsonStatus = json.loads(response.text)
|
payload += '"command_topic": "' + Constantes.mqttTopic + '/cover/' + volet + '/set"'
|
||||||
modules = jsonStatus['body']['home']['modules']
|
payload += '}'
|
||||||
for module in modules:
|
iDiamant.publish(topic, payload)
|
||||||
if "NBR" == module['type']:
|
|
||||||
id_volet = module['id']
|
|
||||||
topic = Constantes.mqttTopic + "/cover/" + id_volet + "/config"
|
|
||||||
payload = '{'
|
|
||||||
payload += '"command_topic": "' + Constantes.mqttTopic + '/cover/' + id_volet + '/set",'
|
|
||||||
payload += '"unique_id": "' + id_volet + '"'
|
|
||||||
payload += '}'
|
|
||||||
iDiamant.publish(topic, payload)
|
|
||||||
|
|||||||
69
idiamant/mqtt2idiamant.py
Normal file
69
idiamant/mqtt2idiamant.py
Normal file
@@ -0,0 +1,69 @@
|
|||||||
|
#!/usr/bin/env python
|
||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
# @Author: Jérémy BRAUD
|
||||||
|
|
||||||
|
import paho.mqtt.client as mqtt
|
||||||
|
import requests
|
||||||
|
from const import Constantes
|
||||||
|
from threading import Thread
|
||||||
|
|
||||||
|
from idiamant import iDiamant
|
||||||
|
|
||||||
|
class Mqtt2iDiamant(Thread):
|
||||||
|
""" Thread chargé de la connexion au broker MQTT """
|
||||||
|
|
||||||
|
def __init__(self):
|
||||||
|
Thread.__init__(self)
|
||||||
|
|
||||||
|
def on_connect(self, client, userdata, flags, rc):
|
||||||
|
""" Abonnement aux topics """
|
||||||
|
affichage = "Connected to MQTT with result code " + str(rc)
|
||||||
|
print(affichage)
|
||||||
|
topic = Constantes.mqttTopic + '/cover/+/set'
|
||||||
|
client.subscribe(topic)
|
||||||
|
|
||||||
|
def on_message(self, client, userdata, msg):
|
||||||
|
""" Traitement du message recu """
|
||||||
|
url = "https://api.netatmo.com/api/setstate"
|
||||||
|
headers = {"Authorization": "Bearer " + iDiamant.access_token}
|
||||||
|
topic = str(msg.topic)
|
||||||
|
id_volet = topic.replace(Constantes.mqttTopic + '/cover/', '').replace('/set', '')
|
||||||
|
|
||||||
|
payload = str(msg.payload, encoding="utf-8")
|
||||||
|
position = -1
|
||||||
|
match payload:
|
||||||
|
case 'OPEN':
|
||||||
|
position = 100
|
||||||
|
case 'CLOSE':
|
||||||
|
position = 0
|
||||||
|
case _:
|
||||||
|
position = -1
|
||||||
|
|
||||||
|
data = {
|
||||||
|
"home": {
|
||||||
|
"id": iDiamant.volets[id_volet]['id_home'],
|
||||||
|
"modules": [
|
||||||
|
{
|
||||||
|
"id": id_volet,
|
||||||
|
"target_position": position,
|
||||||
|
"bridge": iDiamant.volets[id_volet]['bridge']
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
# Appel de l'API
|
||||||
|
response = requests.post(url, json=data, headers=headers)
|
||||||
|
print(response)
|
||||||
|
|
||||||
|
def run(self):
|
||||||
|
""" Démarrage du service MQTT """
|
||||||
|
client = mqtt.Client()
|
||||||
|
if Constantes.mqttUser:
|
||||||
|
client.username_pw_set(Constantes.mqttUser, Constantes.mqttPassword)
|
||||||
|
client.on_connect = self.on_connect
|
||||||
|
client.on_message = self.on_message
|
||||||
|
client.connect(Constantes.mqttHost, Constantes.mqttPort, 60)
|
||||||
|
client.loop_forever()
|
||||||
|
|
||||||
|
|
||||||
@@ -4,17 +4,17 @@
|
|||||||
|
|
||||||
from const import Constantes
|
from const import Constantes
|
||||||
from idiamant import iDiamant
|
from idiamant import iDiamant
|
||||||
from idiamant2mqtt import iDiamant2Mqtt
|
from mqtt2idiamant import Mqtt2iDiamant
|
||||||
from time import sleep
|
from time import sleep
|
||||||
|
|
||||||
iDiamant.getToken()
|
iDiamant.getToken()
|
||||||
iDiamant.initDiscovery()
|
iDiamant.initDiscovery()
|
||||||
|
|
||||||
# Temps entre chaque pull >= 2
|
# Temps entre chaque pull >= 2
|
||||||
pullTime = Constantes.idiamantPullStatus
|
# pullTime = Constantes.idiamantPullStatus
|
||||||
if pullTime < 2:
|
# if pullTime < 2:
|
||||||
pullTime = 2
|
# pullTime = 2
|
||||||
|
|
||||||
# Envoie des ordres à iDiamant
|
# Envoie des ordres à iDiamant
|
||||||
# mqtt2idiamant = Mqtt2iDiamant()
|
mqtt2idiamant = Mqtt2iDiamant()
|
||||||
# mqtt2idiamant.start()
|
mqtt2idiamant.start()
|
||||||
Reference in New Issue
Block a user