homebridge-melcloud-control 4.0.0-beta.15 → 4.0.0-beta.17

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 +32 -52
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.15",
4
+ "version": "4.0.0-beta.17",
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
@@ -214,71 +214,51 @@ class MelCloud extends EventEmitter {
214
214
  }
215
215
 
216
216
  async connectHomeCookies() {
217
- const loginUrl = new URL('https://live-melcloudhome.auth.eu-west-1.amazoncognito.com/login');
218
- loginUrl.searchParams.set('client_id', '3g4d5l5kivuqi7oia68gib7uso');
219
- loginUrl.searchParams.set('redirect_uri', 'https://auth.melcloudhome.com/signin-oidc-meu');
220
- loginUrl.searchParams.set('response_type', 'code');
221
- loginUrl.searchParams.set('scope', 'openid profile');
222
- loginUrl.searchParams.set('response_mode', 'form_post');
223
-
224
- const browser = await puppeteer.launch({
225
- headless: true,
226
- args: ['--no-sandbox', '--disable-setuid-sandbox']
227
- });
228
-
229
- const page = await browser.newPage();
230
-
231
217
  try {
218
+ const loginUrl = 'https://melcloudhome.com';
219
+ const browser = await puppeteer.launch({ headless: true, args: ['--no-sandbox', '--disable-setuid-sandbox'] });
220
+ const page = await browser.newPage();
221
+
232
222
  this.emit('warn', 'Opening login page...');
233
- await page.goto(loginUrl.toString(), { waitUntil: 'networkidle2' });
223
+ await page.goto(loginUrl, { waitUntil: 'networkidle2' });
234
224
 
235
225
  this.emit('warn', 'Typing credentials...');
236
226
  await page.type('input[name="username"]', this.user, { delay: 50 });
237
227
  await page.type('input[name="password"]', this.passwd, { delay: 50 });
238
228
 
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.');
229
+ // Kliknij przycisk logowania
230
+ const button = await page.$('input[type="submit"]');
231
+ await Promise.all([
232
+ button.click(),
233
+ page.waitForNavigation({ waitUntil: 'networkidle2', timeout: 20000 })
234
+ ]);
235
+
236
+ // Obsługa ewentualnego "Stay signed in?"
237
+ const staySignedInBtn = await page.$('input[type="submit"]');
238
+ if (staySignedInBtn) {
239
+ await Promise.all([
240
+ staySignedInBtn.click(),
241
+ page.waitForNavigation({ waitUntil: 'networkidle2', timeout: 20000 })
242
+ ]);
263
243
  }
264
244
 
265
- // Monitorowanie requestów i oczekiwanie na cookies C1 i C2
266
245
  let c1 = null, c2 = null;
267
- page.on('response', async response => {
268
- const url = response.url();
269
- if (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
246
  const start = Date.now();
278
- while ((!c1 || !c2) && Date.now() - start < 15000) {
279
- await new Promise(resolve => setTimeout(resolve, 500));
247
+
248
+ // Loop max 20s, czekamy aż pojawią się cookies
249
+ while ((!c1 || !c2) && Date.now() - start < 20000) {
250
+ const cookies = await page.cookies();
251
+ c1 = cookies.find(c => c.name === '__Secure-monitorandcontrolC1')?.value || c1;
252
+ c2 = cookies.find(c => c.name === '__Secure-monitorandcontrolC2')?.value || c2;
253
+ if (!c1 || !c2) await new Promise(r => setTimeout(r, 500));
254
+ }
255
+
256
+ if (!c1 || !c2) {
257
+ await browser.close();
258
+ throw new Error('❌ Cookies C1/C2 not found after login');
280
259
  }
281
260
 
261
+ // Zapis do pliku
282
262
  const data = { C1: c1, C2: c2, date: new Date().toISOString() };
283
263
  await this.functions.saveData(this.cookiesFile, data);
284
264