homebridge-melcloud-control 4.0.0-beta.14 → 4.0.0-beta.16

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 -38
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.14",
4
+ "version": "4.0.0-beta.16",
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
@@ -236,51 +236,39 @@ class MelCloud extends EventEmitter {
236
236
  await page.type('input[name="username"]', this.user, { delay: 50 });
237
237
  await page.type('input[name="password"]', this.passwd, { delay: 50 });
238
238
 
239
- // Wyszukiwanie przycisku logowania
240
- const buttonSelectors = [
241
- 'button[type="submit"]',
242
- 'input[type="submit"]',
243
- 'button[name="signIn"]',
244
- 'button.btn-primary'
245
- ];
246
-
247
- let buttonFound = false;
248
- for (const selector of buttonSelectors) {
249
- const button = await page.$(selector);
250
- if (button) {
251
- this.emit('warn', `Found submit button: ${selector}`);
252
- await Promise.all([
253
- button.click(),
254
- page.waitForNavigation({ waitUntil: 'networkidle2', timeout: 20000 }) // czekamy na redirect
255
- ]);
256
- buttonFound = true;
257
- break;
258
- }
259
- }
260
-
261
- if (!buttonFound) {
262
- throw new Error('❌ Could not find login button on the page.');
239
+ // Kliknij przycisk logowania
240
+ const button = await page.$('input[type="submit"]');
241
+ await Promise.all([
242
+ button.click(),
243
+ page.waitForNavigation({ waitUntil: 'networkidle2', timeout: 20000 })
244
+ ]);
245
+
246
+ // Obsługa ewentualnego "Stay signed in?"
247
+ const staySignedInBtn = await page.$('input[type="submit"]');
248
+ if (staySignedInBtn) {
249
+ await Promise.all([
250
+ staySignedInBtn.click(),
251
+ page.waitForNavigation({ waitUntil: 'networkidle2', timeout: 20000 })
252
+ ]);
263
253
  }
264
254
 
265
- // Monitorowanie requestów i oczekiwanie na cookies C1 i C2
266
255
  let c1 = null, c2 = null;
267
- page.on('response', async response => {
268
- const url = response.url();
269
- if (url.includes('GetDevices') || url.includes('Dashboard')) {
270
- const cookies = await page.cookies();
271
- c1 = cookies.find(c => c.name === '__Secure-monitorandcontrolC1')?.value || c1;
272
- c2 = cookies.find(c => c.name === '__Secure-monitorandcontrolC2')?.value || c2;
273
- }
274
- });
275
-
276
- // Retry loop max 15s
277
256
  const start = Date.now();
278
- while ((!c1 || !c2) && Date.now() - start < 15000) {
279
- await new Promise(resolve => setTimeout(resolve, 500));
257
+
258
+ // Loop max 20s, czekamy aż pojawią się cookies
259
+ while ((!c1 || !c2) && Date.now() - start < 20000) {
260
+ const cookies = await page.cookies();
261
+ c1 = cookies.find(c => c.name === '__Secure-monitorandcontrolC1')?.value || c1;
262
+ c2 = cookies.find(c => c.name === '__Secure-monitorandcontrolC2')?.value || c2;
263
+ if (!c1 || !c2) await new Promise(r => setTimeout(r, 500));
280
264
  }
281
265
 
282
- if (!c1 || !c2) throw new Error('Cookies C1/C2 not found after login');
266
+ if (!c1 || !c2) {
267
+ await browser.close();
268
+ throw new Error('❌ Cookies C1/C2 not found after login');
269
+ }
283
270
 
271
+ // Zapis do pliku
284
272
  const data = { C1: c1, C2: c2, date: new Date().toISOString() };
285
273
  await this.functions.saveData(this.cookiesFile, data);
286
274