Login et discovery MQTT

This commit is contained in:
LeClubber
2022-03-12 10:19:05 +01:00
parent 5d619e8535
commit 39f61f0b34
9 changed files with 859 additions and 0 deletions

13
idiamant/Dockerfile Normal file
View File

@@ -0,0 +1,13 @@
FROM python:3-alpine
ENV MQTT_PORT 1883
ENV MQTT_HOST mqtt
ENV MQTT_TOPIC homeassistant
ENV IDIAMANT_PULL_STATUS 5
WORKDIR /usr/src/idiamant
COPY requirements.txt requirements.txt
RUN pip install -r requirements.txt
COPY . .
CMD ["python", "-u", "server.py"]

19
idiamant/const.py Normal file
View File

@@ -0,0 +1,19 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Author: Jérémy BRAUD
import os
class Constantes():
# Recuperation des variables d'environnement
mqttPort = int(os.getenv('MQTT_PORT', 1883))
mqttHost = os.getenv('MQTT_HOST', "localhost")
mqttTopic = os.getenv('MQTT_TOPIC', "homeassistant")
mqttUser = os.getenv('MQTT_USER')
mqttPassword = os.getenv('MQTT_PASSWORD')
idiamantUser = os.getenv("IDIAMANT_USER")
idiamantPassword = os.getenv("IDIAMANT_PASSWORD")
idiamantClientId = os.getenv("IDIAMANT_CLIENT_ID")
idiamantClientSecret = os.getenv("IDIAMANT_CLIENT_SECRET")
idiamantPullStatus = int(os.getenv("IDIAMANT_PULL_STATUS", 5))

79
idiamant/idiamant.py Normal file
View File

@@ -0,0 +1,79 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Author: Jérémy BRAUD
import json
import requests
import paho.mqtt.client as mqtt
from const import Constantes
from time import sleep
class iDiamant():
access_token = ""
refresh_token = ""
liste_home_id = list()
@staticmethod
def getToken():
""" Récupération du token sepuis Netatmo """
url = "https://api.netatmo.com/oauth2/token"
data = {'grant_type': 'password',
'username': Constantes.idiamantUser,
'password': Constantes.idiamantPassword,
'client_id': Constantes.idiamantClientId,
'client_secret': Constantes.idiamantClientSecret,
'scope': 'read_bubendorff write_bubendorff'
}
response = requests.post(url, data)
while 200 != response.status_code:
attente = 20
print("Problème d'accès au token : attente de " + attente + " secondes")
sleep(attente)
response = requests.post(url, data)
jsonStatus = json.loads(response.text)
iDiamant.access_token = jsonStatus['access_token']
iDiamant.refresh_token = jsonStatus['refresh_token']
url = "https://api.netatmo.com/api/homesdata"
headers = {"Authorization": "Bearer " + iDiamant.access_token}
response = requests.get(url, headers=headers)
jsonStatus = json.loads(response.text)
homes = jsonStatus['body']['homes']
for home in homes:
iDiamant.liste_home_id.append(home['id'])
@staticmethod
def updateToken():
""" Update d'un token en fin de vie """
@staticmethod
def publish(topic, playload, retain=True):
""" Publication des messages MQTT """
client = mqtt.Client()
if Constantes.mqttUser:
client.username_pw_set(Constantes.mqttUser, Constantes.mqttPassword)
client.connect(Constantes.mqttHost, Constantes.mqttPort, 60)
client.publish(topic, playload, retain=retain)
client.disconnect()
@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)

View File

@@ -0,0 +1,2 @@
paho-mqtt
requests

20
idiamant/server.py Normal file
View File

@@ -0,0 +1,20 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Author: Jérémy BRAUD
from const import Constantes
from idiamant import iDiamant
from idiamant2mqtt import iDiamant2Mqtt
from time import sleep
iDiamant.getToken()
iDiamant.initDiscovery()
# Temps entre chaque pull >= 2
pullTime = Constantes.idiamantPullStatus
if pullTime < 2:
pullTime = 2
# Envoie des ordres à iDiamant
# mqtt2idiamant = Mqtt2iDiamant()
# mqtt2idiamant.start()