From 6c0bea5a80d38a731b8a8effb31c064db7e22a2f Mon Sep 17 00:00:00 2001 From: LeClubber Date: Sun, 13 Mar 2022 18:23:44 +0100 Subject: [PATCH] Refresh du token --- README.md | 2 +- idiamant/idiamant.py | 14 ++++++++++++++ idiamant/server.py | 12 ++++++------ 3 files changed, 21 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 8ef2645..e47d822 100644 --- a/README.md +++ b/README.md @@ -121,7 +121,7 @@ Vous pourrez alors : - [x] Documentation - [x] Utilisation d'un login/mot de passe pour le broker MQTT - [x] Publier image Docker en multiple arch -- [ ] Renouvellement du token +- [x] Renouvellement du token - [ ] Attente du serveur MQTT si non disponible - [ ] Tester les paramètres et gestion d'erreur - [ ] Utilisation de certificat pour le broker MQTT diff --git a/idiamant/idiamant.py b/idiamant/idiamant.py index e8f6030..fc41cab 100644 --- a/idiamant/idiamant.py +++ b/idiamant/idiamant.py @@ -12,6 +12,7 @@ class iDiamant(): access_token = "" refresh_token = "" + expire_token = 0 volets = {} @staticmethod @@ -35,6 +36,7 @@ class iDiamant(): jsonStatus = json.loads(response.text) iDiamant.access_token = jsonStatus['access_token'] iDiamant.refresh_token = jsonStatus['refresh_token'] + iDiamant.expire_token = int(jsonStatus['expires_in']) url = "https://api.netatmo.com/api/homesdata" headers = {"Authorization": "Bearer " + iDiamant.access_token} @@ -56,6 +58,18 @@ class iDiamant(): @staticmethod def updateToken(): """ Update d'un token en fin de vie """ + url = "https://api.netatmo.com/oauth2/token" + data = {'grant_type': 'refresh_token', + 'refresh_token': iDiamant.refresh_token, + 'client_id': Constantes.idiamantClientId, + 'client_secret': Constantes.idiamantClientSecret + } + response = requests.post(url, data) + while 200 != response.status_code: + attente = 20 + print("Problème d'accès au renouvellement de token : attente de " + attente + " secondes") + sleep(attente) + response = requests.post(url, data) @staticmethod def publish(topic, playload, retain=True): diff --git a/idiamant/server.py b/idiamant/server.py index 937da2b..d1eca96 100644 --- a/idiamant/server.py +++ b/idiamant/server.py @@ -10,11 +10,11 @@ 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() \ No newline at end of file +mqtt2idiamant.start() + +# Refresh du token +while True: + sleep(iDiamant.expire_token / 2) + iDiamant.updateToken()