homebridge-melcloud-control 4.0.0-beta.11 → 4.0.0-beta.13

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 +48 -22
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.11",
4
+ "version": "4.0.0-beta.13",
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,64 @@ 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"]', thispasswd);
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) {
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.');
263
+ }
264
+
265
+ // Opcjonalnie obsługa "Stay signed in?"
266
+ const staySignedInSelector = 'input[name="rememberDevice"], input[type="submit"]';
267
+ if (await page.$(staySignedInSelector)) {
268
+ console.log('Handling "Stay signed in?" screen...');
249
269
  await Promise.all([
250
- staySignedInBtn.click(),
270
+ page.click(staySignedInSelector),
251
271
  page.waitForNavigation({ waitUntil: 'networkidle2', timeout: 20000 })
252
272
  ]);
253
273
  }
254
274
 
255
275
  // Monitorowanie requestów i oczekiwanie na cookies C1 i C2
256
276
  let c1 = null, c2 = null;
257
- const start = Date.now();
277
+ page.on('response', async response => {
278
+ const url = response.url();
279
+ if (url.includes('GetDevices') || url.includes('Dashboard')) {
280
+ const cookies = await page.cookies();
281
+ c1 = cookies.find(c => c.name === '__Secure-monitorandcontrolC1')?.value || c1;
282
+ c2 = cookies.find(c => c.name === '__Secure-monitorandcontrolC2')?.value || c2;
283
+ }
284
+ });
258
285
 
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));
286
+ // Retry loop max 15s
287
+ const start = Date.now();
288
+ while ((!c1 || !c2) && Date.now() - start < 15000) {
289
+ await new Promise(resolve => setTimeout(resolve, 500));
264
290
  }
265
291
 
266
- if (!c1 || !c2) throw new Error('Cookies C1/C2 not found');
292
+ if (!c1 || !c2) throw new Error('Cookies C1/C2 not found after login');
267
293
 
268
294
  const data = { C1: c1, C2: c2, date: new Date().toISOString() };
269
295
  await this.functions.saveData(this.cookiesFile, data);