homebridge-melcloud-control 4.0.0-beta.286 → 4.0.0-beta.287

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/src/melcloud.js +26 -29
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "displayName": "MELCloud Control",
3
3
  "name": "homebridge-melcloud-control",
4
- "version": "4.0.0-beta.286",
4
+ "version": "4.0.0-beta.287",
5
5
  "description": "Homebridge plugin to control Mitsubishi Air Conditioner, Heat Pump and Energy Recovery Ventilation.",
6
6
  "license": "MIT",
7
7
  "author": "grzegorz914",
package/src/melcloud.js CHANGED
@@ -312,42 +312,39 @@ class MelCloud extends EventEmitter {
312
312
 
313
313
  // 2️⃣ Jeśli nie w URL, sprawdź body HTML (form_post)
314
314
  const html = await page.content();
315
- console.log(html);
315
+ console.log(html);
316
316
 
317
- // Form_post pattern
318
- const formCodeMatch = html.match(/name="code"\s+value="([^"]+)"/);
319
- const formStateMatch = html.match(/name="state"\s+value="([^"]+)"/);
320
- const formActionMatch = html.match(/action="([^"]+)"/);
317
+ // Check for form_post response (HTML with hidden form)
318
+ const formCodeMatch = data.match(/name="code"\s+value="([^"]+)"/);
319
+ const formStateMatch = data.match(/name="state"\s+value="([^"]+)"/);
320
+ const formActionMatch = data.match(/action="([^"]+)"/);
321
321
 
322
322
  if (formCodeMatch && formStateMatch && formActionMatch) {
323
- console.log('Authorization code found in form_post HTML');
324
- // Wyślij POST do action
325
- const actionUrl = formActionMatch[1];
326
- const formData = new URLSearchParams();
327
- formData.append('code', formCodeMatch[1]);
328
- formData.append('state', formStateMatch[1]);
329
-
330
- const response = await page.evaluate(async (url, body) => {
331
- const res = await fetch(url, {
332
- method: 'POST',
333
- body,
334
- credentials: 'include'
335
- });
336
- return res.text();
337
- }, actionUrl, formData);
338
-
339
- // Spróbuj znaleźć code w odpowiedzi POST
340
- const postCodeMatch = response.match(/code=([^&"']+)/);
341
- if (postCodeMatch) return postCodeMatch[1];
323
+ console.log('Found form_post response, submitting to callback endpoint...');
324
+ this.submitFormPost(formActionMatch[1], formCodeMatch[1], formStateMatch[1], cookies)
325
+ .then(code => resolve(code))
326
+ .catch(err => reject(err));
327
+ return;
342
328
  }
343
329
 
344
- // 3️⃣ Jeśli nie w form_post, sprawdź JS melcloudhome://
345
- const jsCodeMatch = html.match(/melcloudhome:\/\/[^"'\s]*[?&]code=([^&"'\s]+)/);
346
- if (jsCodeMatch) {
347
- console.log('Authorization code found in JS redirect');
348
- return jsCodeMatch[1];
330
+ // Check for melcloudhome:// redirect in JS
331
+ const bodyCodeMatch = data.match(/melcloudhome:\/\/[^"'\s]*[?&]code=([^&"'\s]+)/);
332
+ if (bodyCodeMatch) {
333
+ console.log('Found authorization code in response body (JS)');
334
+ resolve(bodyCodeMatch[1]);
335
+ return;
349
336
  }
350
337
 
338
+ // 🆕 NEW: Check for data-url with code parameter (like in 500 error pages)
339
+ const dataUrlMatch = data.match(/data-url="[^"]*code=([^&"']+)/);
340
+ if (dataUrlMatch) {
341
+ console.log('Found authorization code in data-url attribute');
342
+ resolve(dataUrlMatch[1]);
343
+ return;
344
+ }
345
+
346
+ reject(new Error('Authorization code not found in URL or HTML'));
347
+
351
348
  // 5. Wymień code na access_token
352
349
  const tokenResponse = await axios.post(TOKEN_ENDPOINT, new URLSearchParams({
353
350
  client_id: 'homemobile',