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

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.284",
4
+ "version": "4.0.0-beta.286",
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
@@ -299,31 +299,58 @@ class MelCloud extends EventEmitter {
299
299
  await page.waitForSelector('input[name="username"]', { timeout: 5000 });
300
300
  await page.type('input[name="username"]', this.user, { delay: 50 });
301
301
  await page.type('input[name="password"]', this.passwd, { delay: 50 });
302
- const button1 = await page.$('input[type="Submit"]');
302
+ const button1 = await page.$('input[type="submit"]');
303
303
  await Promise.all([button1.click(), page.waitForNavigation({ waitUntil: 'networkidle2', timeout: 10000 })]);
304
304
 
305
- let authCode = null;
306
- const start = Date.now();
307
- const timeout = 30000; // 30s
308
-
309
- while (!authCode && Date.now() - start < timeout) {
310
- authCode = await page.evaluate(() => {
311
- const m = window.location.href.match(/[?&]code=([^&]+)/);
312
- return m ? m[1] : null;
313
- });
314
- if (!authCode) await new Promise(r => setTimeout(r, 200));
305
+ // 1️⃣ Sprawdzenie, czy code jest w URL
306
+ const url1 = page.url();
307
+ const urlMatch = url1.match(/[?&]code=([^&]+)/);
308
+ if (urlMatch) {
309
+ console.log('Authorization code found in URL');
310
+ return urlMatch[1];
315
311
  }
316
- console.log('Authorization code:', page);
317
- console.log('Authorization code1:', window.location.href);
318
- if (!authCode) {
319
- throw new Error('Authorization code not found in URL');
312
+
313
+ // 2️⃣ Jeśli nie w URL, sprawdź body HTML (form_post)
314
+ const html = await page.content();
315
+ console.log(html);
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="([^"]+)"/);
321
+
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];
320
342
  }
321
343
 
322
- console.log('Authorization code:', authCode);
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];
349
+ }
323
350
 
324
351
  // 5. Wymień code na access_token
325
352
  const tokenResponse = await axios.post(TOKEN_ENDPOINT, new URLSearchParams({
326
- client_id: clientId,
353
+ client_id: 'homemobile',
327
354
  grant_type: 'authorization_code',
328
355
  code: authCode,
329
356
  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);