homebridge-melcloud-control 4.0.0-beta.262 → 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 +26 -46
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.262",
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,61 +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 loginBtn = await page.$x("//button[contains(text(),'Log In') or contains(text(),'Zaloguj')]");
296
- if (loginBtn.length === 0) throw new Error('Login button not found');
297
- await Promise.all([
298
- loginBtn[0].click(),
299
- page.waitForNavigation({ waitUntil: 'networkidle2' })
300
- ]);
289
+ // 1. Otwórz stronę logowania
290
+ await page.goto('https://app.melcloud.com/Mitsubishi.Wifi.Client/Login', { waitUntil: 'networkidle2' });
301
291
 
302
- // Wpisanie loginu i hasła
303
- await page.type('input[name="username"]', this.user, { delay: 50 });
304
- await page.type('input[name="password"]', this.passwd, { delay: 50 });
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();
295
+
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 });
305
300
 
306
- // Kliknięcie submit
307
301
  const submitBtn = await page.$('input[type="submit"]');
308
- if (!submitBtn) throw new Error('Submit button not found');
309
- await Promise.all([
310
- submitBtn.click(),
311
- page.waitForNavigation({ waitUntil: 'networkidle2' })
312
- ]);
302
+ await submitBtn?.click();
313
303
 
314
- // 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 });
315
306
  const currentUrl = page.url();
316
- const codeMatch = currentUrl.match(/[?&]code=([^&]+)/);
317
- if (!codeMatch) throw new Error('Authorization code not found in URL');
318
- const authorizationCode = codeMatch[1];
319
-
320
- // Zamknięcie przeglądarki
321
- await browser.close();
322
-
323
- // Pobranie tokenów
324
- const params = new URLSearchParams();
325
- params.append('grant_type', 'authorization_code');
326
- params.append('code', authorizationCode);
327
- params.append('client_id', 'melCloudWeb');
328
- params.append('redirect_uri', 'melcloudhome://');
329
-
330
- const tokenResp = await fetch('https://auth.melcloudhome.com/connect/token', {
331
- method: 'POST',
332
- headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
333
- 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' }
334
319
  });
335
320
 
336
- if (!tokenResp.ok) {
337
- const errText = await tokenResp.text();
338
- throw new Error(`Token request failed: ${errText}`);
339
- }
340
-
341
321
  const tokenData = await tokenResp.json();
342
322
  this.emit('debug', `Token: ${JSON.stringify(tokenData, null, 2)}`);
343
323
  return {