homebridge-melcloud-control 4.0.0-beta.323 → 4.0.0-beta.325

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.
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.323",
4
+ "version": "4.0.0-beta.325",
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
@@ -296,39 +296,12 @@ class MelCloud extends EventEmitter {
296
296
  .on('error', error => this.emit('error', error));
297
297
 
298
298
  const data = await melCloudHomeToken.buildAuthorizeUrl();
299
- const code = await melCloudHomeToken.loginToMelCloudHome(data.url);
300
- const token = await melCloudHomeToken.getTokens(code, data.codeVerifier);
299
+ const cookies = await melCloudHomeToken.loginToMelCloudHome(data.url);
301
300
 
302
- return false
303
-
304
- browser = await puppeteer.launch({
305
- headless: true,
306
- args: ['--no-sandbox', '--disable-setuid-sandbox']
307
- });
308
-
309
- const page = await browser.newPage();
310
- await page.goto(data.url, { waitUntil: 'networkidle2' });
311
-
312
- await page.waitForSelector('input[name="username"]', { timeout: 5000 });
313
- await page.type('input[name="username"]', this.user, { delay: 50 });
314
- await page.type('input[name="password"]', this.passwd, { delay: 50 });
315
- const button1 = await page.$('input[type="submit"]');
316
- await Promise.all([button1.click(), page.waitForNavigation({ waitUntil: 'networkidle2', timeout: 10000 })]);
317
- const html = await page.content();
318
- const code1 = await melCloudHomeToken.extractCodeFromHtml(html);
319
- this.emit('warn', code);
320
-
321
- // 5. Wymień code na access_token
322
- const tokenResponse = await melCloudHomeToken.getTokens(code, data.codeVerifier);
323
- const tokenData = await tokenResponse.json();
324
- this.emit('debug', `Token: ${JSON.stringify(tokenData, null, 2)}`);
325
- const accountInfo = {
326
- accessToken: tokenData.access_token,
327
- refreshToken: tokenData.refresh_token,
328
- expiresIn: tokenData.expires_in
329
- };
301
+ const accountInfo = { ContextKey: cookies, UseFahrenheit: false };
302
+ this.contextKey = contextKey;
330
303
 
331
- return accountInfo;
304
+ return accountInfo
332
305
  } catch (error) {
333
306
  if (browser) await browser.close().catch(() => { });
334
307
  throw error;
@@ -93,7 +93,8 @@ class MelCloudHomeToken extends EventEmitter {
93
93
  code_verifier: codeVerifier,
94
94
  }).toString(), {
95
95
  headers: {
96
- 'Content-Type': 'application/x-www-form-urlencoded'
96
+ 'Content-Type': 'application/x-www-form-urlencoded',
97
+ 'Authorization': 'Basic aG9tZW1vYmlsZTo='
97
98
  }
98
99
  });
99
100
 
@@ -105,7 +106,6 @@ class MelCloudHomeToken extends EventEmitter {
105
106
  }
106
107
  }
107
108
 
108
-
109
109
  async loginToMelCloudHome(url) {
110
110
  try {
111
111
  // Step 1: Pobierz stronę logowania, żeby uzyskać sesję + token _csrf
@@ -119,25 +119,6 @@ class MelCloudHomeToken extends EventEmitter {
119
119
 
120
120
  const cookies = getResp.headers['set-cookie'] || [];
121
121
  const dom = new JSDOM(getResp.data);
122
- const formAction = dom.window.document.querySelector('form[action]')?.getAttribute('action');
123
- if (!formAction) {
124
- if (this.logWarn) this.emit('warn', `Login form not found in HTML`);
125
- return null;
126
- }
127
-
128
- const formUrl = new URL(formAction, 'https://auth.melcloudhome.com'); // absolutny URL
129
- const clientId = formUrl.searchParams.get('client_id');
130
- const redirectUri = formUrl.searchParams.get('redirect_uri');
131
-
132
- if (!clientId || !redirectUri) {
133
- if (this.logWarn) this.emit('warn', `client_id or redirect_uri missing in login form`);
134
- return null;
135
- }
136
-
137
- // Zapisz do this.* żeby użyć później w exchangeCodeForToken()
138
- this.clientId = clientId;
139
- this.redirectUri = redirectUri;
140
-
141
122
  const csrf = dom.window.document.querySelector('input[name="_csrf"]')?.value;
142
123
  if (!csrf) {
143
124
  if (this.logWarn) this.emit('warn', `CSRF token not found`);
@@ -164,6 +145,12 @@ class MelCloudHomeToken extends EventEmitter {
164
145
  validateStatus: status => [200, 302, 400].includes(status),
165
146
  });
166
147
 
148
+ const cookie = response.headers['set-cookie'];
149
+ if (cookie) {
150
+ if (this.logWarn) this.emit('warn', `Cookie returned from login. Response headers: ${JSON.stringify(response.headers)}`);
151
+ return cookie;
152
+ }
153
+
167
154
  if (response.status === 302 && response.headers.location) {
168
155
  // Login success — mamy redirect z "code="
169
156
  const redirectUrl = response.headers.location;