homebridge-melcloud-control 4.0.0-beta.324 → 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.324",
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;
@@ -106,7 +106,6 @@ class MelCloudHomeToken extends EventEmitter {
106
106
  }
107
107
  }
108
108
 
109
-
110
109
  async loginToMelCloudHome(url) {
111
110
  try {
112
111
  // Step 1: Pobierz stronę logowania, żeby uzyskać sesję + token _csrf
@@ -120,25 +119,6 @@ class MelCloudHomeToken extends EventEmitter {
120
119
 
121
120
  const cookies = getResp.headers['set-cookie'] || [];
122
121
  const dom = new JSDOM(getResp.data);
123
- const formAction = dom.window.document.querySelector('form[action]')?.getAttribute('action');
124
- if (!formAction) {
125
- if (this.logWarn) this.emit('warn', `Login form not found in HTML`);
126
- return null;
127
- }
128
-
129
- const formUrl = new URL(formAction, 'https://auth.melcloudhome.com'); // absolutny URL
130
- const clientId = formUrl.searchParams.get('client_id');
131
- const redirectUri = formUrl.searchParams.get('redirect_uri');
132
-
133
- if (!clientId || !redirectUri) {
134
- if (this.logWarn) this.emit('warn', `client_id or redirect_uri missing in login form`);
135
- return null;
136
- }
137
-
138
- // Zapisz do this.* żeby użyć później w exchangeCodeForToken()
139
- this.clientId = clientId;
140
- this.redirectUri = redirectUri;
141
-
142
122
  const csrf = dom.window.document.querySelector('input[name="_csrf"]')?.value;
143
123
  if (!csrf) {
144
124
  if (this.logWarn) this.emit('warn', `CSRF token not found`);
@@ -165,6 +145,12 @@ class MelCloudHomeToken extends EventEmitter {
165
145
  validateStatus: status => [200, 302, 400].includes(status),
166
146
  });
167
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
+
168
154
  if (response.status === 302 && response.headers.location) {
169
155
  // Login success — mamy redirect z "code="
170
156
  const redirectUrl = response.headers.location;