homebridge-melcloud-control 4.0.0-beta.20 → 4.0.0-beta.22

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 +54 -24
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.20",
4
+ "version": "4.0.0-beta.22",
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
@@ -213,20 +213,64 @@ class MelCloud extends EventEmitter {
213
213
  }
214
214
  }
215
215
 
216
- async connectHomeCookies() {
216
+ async loginToMelcloudHome() {
217
217
  try {
218
- const loginUrl = 'https://melcloudhome.com';
219
- const browser = await puppeteer.launch({ headless: true, args: ['--no-sandbox', '--disable-setuid-sandbox'] });
220
- const page = await browser.newPage();
218
+ const form = new URLSearchParams();
219
+ form.append('user[email]', this.user);
220
+ form.append('user[password]', this.passwd);
221
+
222
+ const response = await axios({
223
+ method: 'POST',
224
+ baseURL: EnphaseUrls.BaseUrl,
225
+ url: EnphaseUrls.Login,
226
+ headers: {
227
+ 'Content-Type': 'application/x-www-form-urlencoded'
228
+ },
229
+ data: form,
230
+ timeout: 10000
231
+ });
232
+
233
+ if (response.status !== 200) {
234
+ if (this.logError) this.emit('error', `Login failed with status code: ${response.status}`);
235
+ return null;
236
+ }
221
237
 
238
+ const cookie = response.headers['set-cookie'];
239
+ if (!cookie) {
240
+ if (this.logWarn) this.emit('warn', `No cookie returned from login. Response headers: ${JSON.stringify(response.headers)}`);
241
+ return null;
242
+ }
243
+
244
+ return cookie;
245
+ } catch (error) {
246
+ throw new Error(`Login to Enlighten error: ${error}`);
247
+ }
248
+ }
249
+
250
+ async connectHomeCookies() {
251
+ const loginUrl = 'https://melcloudhome.com';
252
+ const browser = await puppeteer.launch({ headless: true, args: ['--no-sandbox', '--disable-setuid-sandbox'] });
253
+ const page = await browser.newPage();
254
+
255
+ try {
222
256
  this.emit('warn', 'Opening login page...');
223
257
  await page.goto(loginUrl, { waitUntil: 'networkidle2' });
224
258
 
225
259
  // Kliknij przycisk logowania
226
- const loginBtn = await page.$x("//button[contains(text(), 'Zaloguj')]");
227
- if (!loginBtn || loginBtn.length === 0) throw new Error('Login button not found on homepage');
260
+ const buttons = await page.$$('button.btn--blue');
261
+ let loginBtn = null;
262
+ for (const btn of buttons) {
263
+ const text = await page.evaluate(el => el.textContent, btn);
264
+ if (text.trim() === 'Zaloguj') {
265
+ loginBtn = btn;
266
+ break;
267
+ }
268
+ }
269
+
270
+ if (!loginBtn) throw new Error('Login button not found on homepage');
271
+
228
272
  await Promise.all([
229
- loginBtn[0].click(),
273
+ loginBtn.click(),
230
274
  page.waitForNavigation({ waitUntil: 'networkidle2', timeout: 20000 })
231
275
  ]);
232
276
 
@@ -244,25 +288,11 @@ class MelCloud extends EventEmitter {
244
288
  page.waitForNavigation({ waitUntil: 'networkidle2', timeout: 20000 })
245
289
  ]);
246
290
 
247
- let c1 = null, c2 = null;
248
- const start = Date.now();
249
-
250
- // Loop max 20s, czekamy aż pojawią się cookies
251
- while ((!c1 || !c2) && Date.now() - start < 20000) {
252
- const cookies = await page.cookies();
253
- c1 = cookies.find(c => c.name === '__Secure-monitorandcontrolC1')?.value || c1;
254
- c2 = cookies.find(c => c.name === '__Secure-monitorandcontrolC2')?.value || c2;
255
- if (!c1 || !c2) await new Promise(r => setTimeout(r, 500));
256
- }
257
-
258
- if (!c1 || !c2) {
259
- await browser.close();
260
- throw new Error('❌ Cookies C1/C2 not found after login');
261
- }
291
+ const cookies = await page.cookies();
292
+ this.emit('warn', cookies);
262
293
 
263
294
  // Zapis do pliku
264
- const data = { C1: c1, C2: c2, date: new Date().toISOString() };
265
- await this.functions.saveData(this.cookiesFile, data);
295
+ await this.functions.saveData(this.cookiesFile, cookies);
266
296
 
267
297
  this.emit('warn', 'Login successful.');
268
298
  return data;