homebridge-melcloud-control 4.0.0-beta.285 → 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.
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.285",
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,44 +312,42 @@ 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
316
 
316
- // Form_post pattern
317
- const formCodeMatch = html.match(/name="code"\s+value="([^"]+)"/);
318
- const formStateMatch = html.match(/name="state"\s+value="([^"]+)"/);
319
- 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="([^"]+)"/);
320
321
 
321
322
  if (formCodeMatch && formStateMatch && formActionMatch) {
322
- console.log('Authorization code found in form_post HTML');
323
- // Wyślij POST do action
324
- const actionUrl = formActionMatch[1];
325
- const formData = new URLSearchParams();
326
- formData.append('code', formCodeMatch[1]);
327
- formData.append('state', formStateMatch[1]);
328
-
329
- const response = await page.evaluate(async (url, body) => {
330
- const res = await fetch(url, {
331
- method: 'POST',
332
- body,
333
- credentials: 'include'
334
- });
335
- return res.text();
336
- }, actionUrl, formData);
337
-
338
- // Spróbuj znaleźć code w odpowiedzi POST
339
- const postCodeMatch = response.match(/code=([^&"']+)/);
340
- 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;
341
328
  }
342
329
 
343
- // 3️⃣ Jeśli nie w form_post, sprawdź JS melcloudhome://
344
- const jsCodeMatch = html.match(/melcloudhome:\/\/[^"'\s]*[?&]code=([^&"'\s]+)/);
345
- if (jsCodeMatch) {
346
- console.log('Authorization code found in JS redirect');
347
- 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;
348
336
  }
349
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
+
350
348
  // 5. Wymień code na access_token
351
349
  const tokenResponse = await axios.post(TOKEN_ENDPOINT, new URLSearchParams({
352
- client_id: clientId,
350
+ client_id: 'homemobile',
353
351
  grant_type: 'authorization_code',
354
352
  code: authCode,
355
353
  redirect_uri: 'melcloudhome://'
@@ -30,17 +30,14 @@ class MelCloudHomeToken {
30
30
  }
31
31
 
32
32
  async getLoginPage(authUrl) {
33
- console.log('Getting login page...');
34
33
  const res = await this.client.get(authUrl, { maxRedirects: 10 });
35
34
  return { url: res.request.res.responseUrl };
36
35
  }
37
36
 
38
37
  async getUrl() {
39
38
  try {
40
- console.log('Starting OAuth flow...');
41
39
  const pkce = this.generatePKCE();
42
40
  const state = this.generateState();
43
-
44
41
  const authUrl = new URL(AUTHORIZE_ENDPOINT);
45
42
  authUrl.searchParams.set('client_id', CLIENT_ID);
46
43
  authUrl.searchParams.set('redirect_uri', REDIRECT_URI);