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

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 +41 -25
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.12",
4
+ "version": "4.0.0-beta.14",
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
@@ -232,38 +232,54 @@ class MelCloud extends EventEmitter {
232
232
  this.emit('warn', 'Opening login page...');
233
233
  await page.goto(loginUrl.toString(), { waitUntil: 'networkidle2' });
234
234
 
235
- // Wpisz login i hasło
236
- await page.type('input[name="username"]', this.user);
237
- await page.type('input[name="password"]', this.passwd);
238
-
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
- // Opcjonalnie obsłuż "Stay signed in?" jeśli występuje
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
- ]);
235
+ this.emit('warn', 'Typing credentials...');
236
+ await page.type('input[name="username"]', this.user, { delay: 50 });
237
+ await page.type('input[name="password"]', this.passwd, { delay: 50 });
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.');
253
263
  }
254
264
 
255
265
  // Monitorowanie requestów i oczekiwanie na cookies C1 i C2
256
266
  let c1 = null, c2 = null;
257
- const start = Date.now();
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
+ });
258
275
 
259
- while ((!c1 || !c2) && Date.now() - start < 20000) { // max 20s
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));
276
+ // Retry loop max 15s
277
+ const start = Date.now();
278
+ while ((!c1 || !c2) && Date.now() - start < 15000) {
279
+ await new Promise(resolve => setTimeout(resolve, 500));
264
280
  }
265
281
 
266
- if (!c1 || !c2) throw new Error('Cookies C1/C2 not found');
282
+ if (!c1 || !c2) throw new Error('Cookies C1/C2 not found after login');
267
283
 
268
284
  const data = { C1: c1, C2: c2, date: new Date().toISOString() };
269
285
  await this.functions.saveData(this.cookiesFile, data);