homebridge-melcloud-control 4.0.0-beta.263 → 4.0.0-beta.265

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 +25 -55
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.263",
4
+ "version": "4.0.0-beta.265",
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
@@ -283,71 +283,41 @@ class MelCloud extends EventEmitter {
283
283
  async connectToMelCloudHome() {
284
284
  let browser;
285
285
  try {
286
- browser = await puppeteer.launch({
287
- headless: true,
288
- args: ['--no-sandbox', '--disable-setuid-sandbox']
289
- });
290
-
286
+ browser = await puppeteer.launch({ headless: true, args: ['--no-sandbox', '--disable-setuid-sandbox'] });
291
287
  const page = await browser.newPage();
292
- await page.goto(ApiUrlsHome.BaseURL, { waitUntil: 'networkidle2' });
293
288
 
294
- // Kliknięcie przycisku logowania
295
- const buttons = await page.$$('button.btn--blue');
296
- let loginBtn = null;
297
-
298
- for (const btn of buttons) {
299
- const text = await page.evaluate(el => el.textContent, btn);
300
- if (text.trim() === 'Zaloguj' || text.trim() === 'Log In') {
301
- loginBtn = btn;
302
- break;
303
- }
304
- }
289
+ // 1. Otwórz stronę logowania
290
+ await page.goto('https://app.melcloud.com/Mitsubishi.Wifi.Client/Login', { waitUntil: 'networkidle2' });
305
291
 
306
- if (!loginBtn) throw new Error('Login button not found');
307
- await Promise.all([
308
- loginBtn.click(),
309
- page.waitForNavigation({ waitUntil: 'networkidle2' })
310
- ]);
292
+ // 2. Kliknij login (jeżeli strona główna ma przycisk)
293
+ const loginBtn = await page.$x("//button[contains(text(), 'Log In') or contains(text(), 'Zaloguj')]");
294
+ if (loginBtn.length) await loginBtn[0].click();
311
295
 
312
- // Wpisanie loginu i hasła
313
- await page.type('input[name="username"]', this.user, { delay: 50 });
314
- await page.type('input[name="password"]', this.passwd, { delay: 50 });
296
+ // 3. Wypełnij login i hasło
297
+ await page.waitForSelector('input[name="username"]', { timeout: 5000 });
298
+ await page.type('input[name="username"]', user, { delay: 50 });
299
+ await page.type('input[name="password"]', passwd, { delay: 50 });
315
300
 
316
- // Kliknięcie submit
317
301
  const submitBtn = await page.$('input[type="submit"]');
318
- if (!submitBtn) throw new Error('Submit button not found');
319
- await Promise.all([
320
- submitBtn.click(),
321
- page.waitForNavigation({ waitUntil: 'networkidle2' })
322
- ]);
302
+ await submitBtn?.click();
323
303
 
324
- // Wyłapanie kodu z URL
304
+ // 4. Czekamy na przekierowanie z 'code' w URL
305
+ await page.waitForFunction(() => window.location.href.includes('code'), { timeout: 15000 });
325
306
  const currentUrl = page.url();
326
- const codeMatch = currentUrl.match(/[?&]code=([^&]+)/);
327
- if (!codeMatch) throw new Error('Authorization code not found in URL');
328
- const authorizationCode = codeMatch[1];
329
-
330
- // Zamknięcie przeglądarki
331
- await browser.close();
332
-
333
- // Pobranie tokenów
334
- const params = new URLSearchParams();
335
- params.append('grant_type', 'authorization_code');
336
- params.append('code', authorizationCode);
337
- params.append('client_id', 'melCloudWeb');
338
- params.append('redirect_uri', 'melcloudhome://');
339
-
340
- const tokenResp = await fetch('https://auth.melcloudhome.com/connect/token', {
341
- method: 'POST',
342
- headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
343
- body: params.toString()
307
+ const authCode = new URL(currentUrl).searchParams.get('code');
308
+
309
+ if (!authCode) throw new Error('Authorization code not found in URL');
310
+
311
+ // 5. Wymień code na access_token
312
+ const tokenResponse = await axios.post('https://app.melcloud.com/Mitsubishi.Wifi.Client/OAuth/Token', new URLSearchParams({
313
+ client_id: clientId,
314
+ grant_type: 'authorization_code',
315
+ code: authCode,
316
+ redirect_uri: 'melcloudhome://'
317
+ }).toString(), {
318
+ headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
344
319
  });
345
320
 
346
- if (!tokenResp.ok) {
347
- const errText = await tokenResp.text();
348
- throw new Error(`Token request failed: ${errText}`);
349
- }
350
-
351
321
  const tokenData = await tokenResp.json();
352
322
  this.emit('debug', `Token: ${JSON.stringify(tokenData, null, 2)}`);
353
323
  return {