@@ -79,19 +79,43 @@ script:
7979 lambda : ' return network::is_connected();'
8080 then :
8181 - http_request.post :
82- url : " https://api.enphaseenergy.com/api/v2/login"
82+ url : " https://enlighten.enphaseenergy.com/login/login.json?"
83+ capture_response : true
8384 headers :
84- Content-Type : " application/json"
85- body : ' {"username": "${enphase_username}", "password": "${enphase_password}"}'
85+ Content-Type : application/x-www-form-urlencoded
86+ body : >
87+ user[email]=${enphase_username}
88+ &user[password]=${enphase_password}
89+ &secured_user=true
90+ &commit=Sign+In
8691 on_response :
8792 then :
93+ - logger.log :
94+ format : " Réponse HTTP %d : %s"
95+ args :
96+ - response->status_code
97+ - body.c_str()
8898 - lambda : |-
8999 if (response->status_code != 200) {
90100 ESP_LOGW("custom", "HTTP Request failed with status: %d", response->status_code);
101+ ESP_LOGW("custom", "Body: %s", body.c_str());
102+ bool parse_success = json::parse_json(body, [](JsonObject root) -> bool {
103+ if (root.containsKey("message")) {
104+ id(enphase_token)=root["message"].as<std::string>();
105+ return true;
106+ }
107+ ESP_LOGW("custom", "Invalid JSON structure");
108+ id(enphase_token) = "Can't find token in json";
109+ return false;
110+ });
111+ if (!parse_success) {
112+ ESP_LOGW("custom", "JSON Parsing failed");
113+ id(enphase_token) = "Invalid JSON structure";
114+ }
91115 id(enphase_token) = "Server access error";
92116 } else {
93117 bool parse_success = json::parse_json(body, [](JsonObject root) -> bool {
94- if (! root.containsKey("access_token")) {
118+ if (root.containsKey("access_token")) {
95119 id(enphase_token)=root["access_token"].as<std::string>();
96120 return true;
97121 }
0 commit comments