Page 1 sur 1

dzVents: Problème de lecture données Netatmo format JSON

Publié : 18 déc. 2018, 16:46
par didier3164
Bonjour,
Las de la multitudes de recherches effectuées sur internet je m'en remets à vous :)
Je voudrais récupérer les données de mon thermostat Netatmo.
Le script python fourni par Netatmo focntionne à merveille mais je ne sais pas comment en récupérer les données dans Domoticz...
Le script python:

Code : Tout sélectionner

import requests

payload = {'grant_type': 'password',
           'username': "xxxxxxxxxxxxxxx@hotmail.com",
           'password': "xxxxxxxxx",
           'client_id':"client_id",
           'client_secret': "client_secret",
           'scope': 'read_thermostat write_thermostat'}
try:
    response = requests.post("https://api.netatmo.com/oauth2/token", data=payload)
    response.raise_for_status()
    access_token=response.json()["access_token"]
    refresh_token=response.json()["refresh_token"]
    scope=response.json()["scope"]
    print("Your access token is:", access_token)
    print("Your refresh token is:", refresh_token)
    print("Your scopes are:", scope)
except requests.exceptions.HTTPError as error:
    print(error.response.status_code, error.response.text)
La chaîne retournée au format JSON est la suivante:

Code : Tout sélectionner

{u'access_token': u'5bf33acbae47630a008c8790|0eaaf9f5c946????????????????????', u'scope': [u'read_thermostat', u'write_thermostat'], u'expires_in': 10800, u'expire_in': 10800, u'refresh_token': u'5bf33acbae47630a008c8790|3140?????????????????????'}
Ma tentative de code dzVents

Code : Tout sélectionner

return {
	on = {timer = { 'every 1 minutes'},	httpResponses = { 'token', 'getthermostatsdata', 'homesdata'	} },
	data = {access_token = { initial = '' }},
	execute = function(domoticz, item)

		if (item.isTimer) then
		    print ("Demande token");
		    domoticz.openURL({
                url = 'https://api.netatmo.com/oauth2/token',
                method = 'POST',
                callback = 'token',
                postData = {
                    grant_type = 'password',
                    username = 'monUserNname',
                    password = 'monPassword',
                    client_id = 'monClientId',
                    client_secret = 'monClientSecret',
                    scope = 'read_thermostat'
                }
            })
		end

		if (item.isHTTPResponse) then
			if (item.ok) then
				if (item.isJSON) then
                    if (item.callback == 'token') then
						print (item.data)
						print (item.headers)
						print(domoticz.utils.fromJSON(item.data))
                    elseif (item.callback == 'homesdata') then
						...
                    elseif (item.callback == 'getthermostatsdata') then
						...
                    end
                    
				end
			else
				domoticz.log('Il y a eu un problème avec la requête.', domoticz.LOG_ERROR)
				domoticz.log(item, domoticz.LOG_ERROR)
			end

		end

	end
}
J'ai donc ces erreurs dans les logs

2018-12-17 18:27:01.654 Status: dzVents: item.data
2018-12-17 18:27:01.654 Status: dzVents: Info: {"error":"invalid_request","error_description":"Invalid grant_type parameter or parameter missing"}
2018-12-17 18:27:01.654 Status: dzVents: item.headers
2018-12-17 18:27:01.654 Status: dzVents: Info: {["Expires"]="0", ["Server"]="nginx", ["Connection"]="keep-alive", ["Access-Control-Allow-Origin"]="*", ["X-Powered-By"]="Netatmo", ["Strict-Transport-Security"]="max-age=31536000; includeSubDomains", ["Content-Type"]="application/json", ["Cache-Control"]="no-cache, must-revalidate", ["Date"]="Mon, 17 Dec 2018 17:27:01 GMT", ["X-XSS-Protection"]="1; mode=block", ["Transfer-Encoding"]="chunked"}
2018-12-17 18:27:01.654 Status: dzVents: fromJson: item.data
2018-12-17 18:27:01.654 Status: dzVents: Info: {["error_description"]="Invalid grant_type parameter or parameter missing", ["error"]="invalid_request"}

D'après ce que j'ai pu trouver, les u minuscules sont là pour dire que c'est de l'unicode.
Quelqu'un aurait-il une idée pour dépatouiller un pauvre débutant? ;)
Merci d'avance

Re: dzVents: Problème de lecture données Netatmo format JSON

Publié : 23 déc. 2018, 19:19
par didier3164
Bonsoir,
Même pas une petite idée? ;)