From 77727dc166f9108abc9ef97c1a4f190fcafb941f Mon Sep 17 00:00:00 2001 From: LeClubber Date: Sun, 13 Mar 2022 11:15:21 +0100 Subject: [PATCH] It works ! --- idiamant/idiamant.py | 34 +++++++++---------- idiamant/mqtt2idiamant.py | 69 +++++++++++++++++++++++++++++++++++++++ idiamant/server.py | 12 +++---- 3 files changed, 92 insertions(+), 23 deletions(-) create mode 100644 idiamant/mqtt2idiamant.py diff --git a/idiamant/idiamant.py b/idiamant/idiamant.py index 78ef9cd..dd8e6e4 100644 --- a/idiamant/idiamant.py +++ b/idiamant/idiamant.py @@ -12,7 +12,7 @@ class iDiamant(): access_token = "" refresh_token = "" - liste_home_id = list() + volets = {} @staticmethod def getToken(): @@ -42,7 +42,15 @@ class iDiamant(): jsonStatus = json.loads(response.text) homes = jsonStatus['body']['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 @@ -62,18 +70,10 @@ class iDiamant(): @staticmethod def initDiscovery(): """ Publication des config pour discovery """ - for home_id in iDiamant.liste_home_id: - url = "https://api.netatmo.com/api/homestatus?home_id=" + home_id - headers = {"Authorization": "Bearer " + iDiamant.access_token} - response = requests.get(url, headers=headers) - jsonStatus = json.loads(response.text) - modules = jsonStatus['body']['home']['modules'] - for module in modules: - 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) + for volet in iDiamant.volets: + topic = Constantes.mqttTopic + "/cover/" + volet + "/config" + payload = '{' + payload += '"unique_id": "' + volet + '",' + payload += '"command_topic": "' + Constantes.mqttTopic + '/cover/' + volet + '/set"' + payload += '}' + iDiamant.publish(topic, payload) diff --git a/idiamant/mqtt2idiamant.py b/idiamant/mqtt2idiamant.py new file mode 100644 index 0000000..164850e --- /dev/null +++ b/idiamant/mqtt2idiamant.py @@ -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() + + diff --git a/idiamant/server.py b/idiamant/server.py index 7d7fe58..937da2b 100644 --- a/idiamant/server.py +++ b/idiamant/server.py @@ -4,17 +4,17 @@ from const import Constantes from idiamant import iDiamant -from idiamant2mqtt import iDiamant2Mqtt +from mqtt2idiamant import Mqtt2iDiamant from time import sleep iDiamant.getToken() iDiamant.initDiscovery() # Temps entre chaque pull >= 2 -pullTime = Constantes.idiamantPullStatus -if pullTime < 2: - pullTime = 2 +# pullTime = Constantes.idiamantPullStatus +# if pullTime < 2: +# pullTime = 2 # Envoie des ordres à iDiamant -# mqtt2idiamant = Mqtt2iDiamant() -# mqtt2idiamant.start() \ No newline at end of file +mqtt2idiamant = Mqtt2iDiamant() +mqtt2idiamant.start() \ No newline at end of file