homebridge-melcloud-control 4.0.0-beta.278 → 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 -8
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.278",
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
@@ -302,19 +302,26 @@ class MelCloud extends EventEmitter {
302
302
  const button1 = await page.$('input[type="Submit"]');
303
303
  await Promise.all([button1.click(), page.waitForNavigation({ waitUntil: 'networkidle2', timeout: 10000 })]);
304
304
 
305
- await new Promise(resolve => setTimeout(resolve, 1000));
306
- // pobierz aktualny URL Puppeteera
307
- const currentUrl = page.url();
305
+ let authCode = null;
306
+ const maxWait = 15000; // 15 sekund
307
+ const start = Date.now();
308
308
 
309
- // wyciągnij parametr 'code'
310
- const urlObj = new URL(currentUrl);
311
- 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
+ }
312
319
 
313
- if (!code) {
320
+ if (!authCode) {
314
321
  throw new Error('Authorization code not found in URL');
315
322
  }
316
323
 
317
- console.log('Authorization code:', code); console.log('Authorization code:', code);
324
+ console.log('Authorization code:', authCode);
318
325
 
319
326
  // 5. Wymień code na access_token
320
327
  const tokenResponse = await axios.post(TOKEN_ENDPOINT, new URLSearchParams({