homebridge-melcloud-control 4.0.0-beta.260 → 4.0.0-beta.261

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 +76 -1
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.260",
4
+ "version": "4.0.0-beta.261",
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
@@ -7,6 +7,8 @@ import MelCloudHomeToken from './melcloudhometoken.js';
7
7
  import ImpulseGenerator from './impulsegenerator.js';
8
8
  import Functions from './functions.js';
9
9
  import { ApiUrls, ApiUrlsHome } from './constants.js';
10
+ import fetch from 'node-fetch';
11
+ import { URLSearchParams } from 'url';
10
12
 
11
13
  class MelCloud extends EventEmitter {
12
14
  constructor(accountType, user, passwd, language, accountFile, buildingsFile, devicesFile, logWarn, logDebug, requestConfig) {
@@ -175,7 +177,7 @@ class MelCloud extends EventEmitter {
175
177
  }
176
178
 
177
179
  // MELCloud Home
178
- async connectToMelCloudHome() {
180
+ async connectToMelCloudHome2() {
179
181
  const oauth = new MelCloudHomeToken();
180
182
  this.tokens = await oauth.getTokensWithCredentials(this.user, this.passwd);
181
183
  this.emit('warn', `Token ${JSON.stringify(this.tokens, null, 2)}`);
@@ -286,6 +288,79 @@ class MelCloud extends EventEmitter {
286
288
  }
287
289
  }
288
290
 
291
+ async connectToMelCloudHome() {
292
+ let browser;
293
+ try {
294
+ browser = await puppeteer.launch({
295
+ headless: true,
296
+ args: ['--no-sandbox', '--disable-setuid-sandbox']
297
+ });
298
+
299
+ const page = await browser.newPage();
300
+ await page.goto(ApiUrlsHome.BaseURL, { waitUntil: 'networkidle2' });
301
+
302
+ // Kliknięcie przycisku logowania
303
+ const loginBtn = await page.$x("//button[contains(text(),'Log In') or contains(text(),'Zaloguj')]");
304
+ if (loginBtn.length === 0) throw new Error('Login button not found');
305
+ await Promise.all([
306
+ loginBtn[0].click(),
307
+ page.waitForNavigation({ waitUntil: 'networkidle2' })
308
+ ]);
309
+
310
+ // Wpisanie loginu i hasła
311
+ await page.type('input[name="username"]', this.user, { delay: 50 });
312
+ await page.type('input[name="password"]', this.passwd, { delay: 50 });
313
+
314
+ // Kliknięcie submit
315
+ const submitBtn = await page.$('input[type="submit"]');
316
+ if (!submitBtn) throw new Error('Submit button not found');
317
+ await Promise.all([
318
+ submitBtn.click(),
319
+ page.waitForNavigation({ waitUntil: 'networkidle2' })
320
+ ]);
321
+
322
+ // Wyłapanie kodu z URL
323
+ const currentUrl = page.url();
324
+ const codeMatch = currentUrl.match(/[?&]code=([^&]+)/);
325
+ if (!codeMatch) throw new Error('Authorization code not found in URL');
326
+ const authorizationCode = codeMatch[1];
327
+
328
+ // Zamknięcie przeglądarki
329
+ await browser.close();
330
+
331
+ // Pobranie tokenów
332
+ const params = new URLSearchParams();
333
+ params.append('grant_type', 'authorization_code');
334
+ params.append('code', authorizationCode);
335
+ params.append('client_id', 'melCloudWeb');
336
+ params.append('redirect_uri', 'melcloudhome://');
337
+
338
+ const tokenResp = await fetch('https://auth.melcloudhome.com/connect/token', {
339
+ method: 'POST',
340
+ headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
341
+ body: params.toString()
342
+ });
343
+
344
+ if (!tokenResp.ok) {
345
+ const errText = await tokenResp.text();
346
+ throw new Error(`Token request failed: ${errText}`);
347
+ }
348
+
349
+ const tokenData = await tokenResp.json();
350
+ this.emit('debug', `Token: ${JSON.stringify(tokenData, null, 2)}`);
351
+ return {
352
+ accessToken: tokenData.access_token,
353
+ refreshToken: tokenData.refresh_token,
354
+ expiresIn: tokenData.expires_in
355
+ };
356
+
357
+ } catch (error) {
358
+ if (browser) await browser.close().catch(() => { });
359
+ throw error;
360
+ }
361
+ }
362
+
363
+
289
364
  async connectToMelCloudHome1(refresh = false) {
290
365
  if (this.logDebug) this.emit('debug', `Connecting to MELCloud Home`);
291
366