homebridge-melcloud-control 4.0.0-beta.277 → 4.0.0-beta.279

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 +15 -11
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.277",
4
+ "version": "4.0.0-beta.279",
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,25 +299,29 @@ 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
-
303
302
  const button1 = await page.$('input[type="Submit"]');
304
303
  await Promise.all([button1.click(), page.waitForNavigation({ waitUntil: 'networkidle2', timeout: 10000 })]);
305
304
 
305
+ let authCode = null;
306
+ const maxWait = 15000; // 15 sekund
307
+ const start = Date.now();
306
308
 
307
- await page.waitForTimeout(2000); // 1-2s może wystarczyć
308
-
309
- // pobierz aktualny URL Puppeteera
310
- const currentUrl = page.url();
311
-
312
- // wyciągnij parametr 'code'
313
- const urlObj = new URL(currentUrl);
314
- const code = urlObj.searchParams.get('code');
309
+ // pollowanie URL co 200ms
310
+ while (!authCode && Date.now() - start < maxWait) {
311
+ const url = await page.evaluate(() => window.location.href);
312
+ const match = url.match(/[?&]code=([^&]+)/);
313
+ if (match) {
314
+ authCode = match[1];
315
+ break;
316
+ }
317
+ await new Promise(r => setTimeout(r, 200));
318
+ }
315
319
 
316
- if (!code) {
320
+ if (!authCode) {
317
321
  throw new Error('Authorization code not found in URL');
318
322
  }
319
323
 
320
- console.log('Authorization code:', code); console.log('Authorization code:', code);
324
+ console.log('Authorization code:', authCode);
321
325
 
322
326
  // 5. Wymień code na access_token
323
327
  const tokenResponse = await axios.post(TOKEN_ENDPOINT, new URLSearchParams({