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.
- package/package.json +1 -1
- 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.
|
|
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
|
-
//
|
|
295
|
-
|
|
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
|
-
|
|
307
|
-
await
|
|
308
|
-
|
|
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
|
-
//
|
|
313
|
-
await page.
|
|
314
|
-
await page.type('input[name="
|
|
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
|
-
|
|
319
|
-
await Promise.all([
|
|
320
|
-
submitBtn.click(),
|
|
321
|
-
page.waitForNavigation({ waitUntil: 'networkidle2' })
|
|
322
|
-
]);
|
|
302
|
+
await submitBtn?.click();
|
|
323
303
|
|
|
324
|
-
//
|
|
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
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
//
|
|
331
|
-
await
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
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 {
|