homebridge-melcloud-control 4.0.0-beta.353 → 4.0.0-beta.355

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.353",
4
+ "version": "4.0.0-beta.355",
5
5
  "description": "Homebridge plugin to control Mitsubishi Air Conditioner, Heat Pump and Energy Recovery Ventilation.",
6
6
  "license": "MIT",
7
7
  "author": "grzegorz914",
@@ -111,79 +111,50 @@ class MelCloudHomeToken extends EventEmitter {
111
111
 
112
112
  async loginToMelCloudHome(url) {
113
113
  try {
114
- // Step 1: Pobierz stronę logowania, żeby uzyskać sesję + token _csrf
115
- const getResp = await axios.get(url, {
116
- headers: {
117
- 'User-Agent': MOBILE_USER_AGENT,
118
- 'Accept': 'text/html',
119
- },
120
- withCredentials: true,
114
+
115
+ // --- Krok 1: GET strony logowania ---
116
+ const getResp = await this.client.get(url, {
117
+ headers: { 'User-Agent': MOBILE_USER_AGENT }
121
118
  });
122
119
 
123
- const cookies = getResp.headers['set-cookie'] || [];
124
120
  const dom = new JSDOM(getResp.data);
125
- const csrf = dom.window.document.querySelector('input[name="_csrf"]')?.value;
126
- if (!csrf) {
127
- if (this.logWarn) this.emit('warn', `CSRF token not found`);
128
- return null;
129
- }
121
+ const csrf = dom.window.document.querySelector('input[name="_csrf"]')?.getAttribute('value');
122
+ if (!csrf) throw new Error('CSRF token not found');
130
123
 
131
- // Step 2: Przygotuj dane logowania
124
+ // --- Krok 2: POST login ---
132
125
  const formData = new URLSearchParams({
133
126
  _csrf: csrf,
134
127
  username: this.user,
135
- password: this.passwd,
136
- });
137
-
138
- if (this.logWarn) this.emit('warn', `Redirect URL: ${url}`);
128
+ password: this.passwd
129
+ }).toString();
139
130
 
140
- // Step 3: Wyślij POST z danymi logowania
141
- const response = await axios.post(url, formData.toString(), {
131
+ const postResp = await this.client.post(url, formData, {
142
132
  headers: {
143
133
  'User-Agent': MOBILE_USER_AGENT,
144
- 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
145
- 'Accept-Language': 'en-US,en;q=0.9',
146
134
  'Content-Type': 'application/x-www-form-urlencoded',
147
- 'Content-Length': formData.toString().length,
148
- 'Cookie': cookies,
149
135
  'Origin': 'https://live-melcloudhome.auth.eu-west-1.amazoncognito.com',
150
136
  'Referer': url,
151
137
  },
152
138
  maxRedirects: 0,
153
- validateStatus: status => [200, 302, 400].includes(status),
139
+ validateStatus: status => [200, 302, 400].includes(status)
154
140
  });
155
141
 
156
- if (this.logWarn) this.emit('warn', `Response: ${response}`);
157
-
158
- if (response.status === 302 && response.headers.location) {
159
- // Login success — mamy redirect z "code="
160
- const redirectUrl = response.headers.location;
161
- if (this.logWarn) this.emit('warn', `Redirect URL: ${redirectUrl}`);
162
-
163
- const getResp = await axios.get('https://melcloudhome.com/dashboard', {
164
- headers: {
165
- 'User-Agent': MOBILE_USER_AGENT,
166
- 'Accept': 'text/html',
167
- },
168
- withCredentials: true,
169
- });
170
-
171
- if (this.logWarn) this.emit('warn', `ResponseL: ${getResp.data}, ${getResp.headers}`);
172
-
173
- const match = redirectUrl.match(/[?&]code=([^&]+)/);
174
- if (match) {
175
- const code = match[1];
176
- if (this.logWarn) this.emit('warn', `Redirect URL found code: ${code}`);
177
- return code;
178
- } else {
179
- if (this.logWarn) this.emit('warn', `Redirect URL found, but no "code=" param: ${redirectUrl}`);
180
- return null;
181
- }
182
- }
142
+ if (postResp.status === 400) throw new Error('Login failed');
183
143
 
184
- if (response.status === 400) if (this.logWarn) this.emit('warn', `Login failed (bad request or invalid credentials: ${response.data}`);
144
+ // --- Krok 3: GET dashboard po zalogowaniu ---
145
+ const dashboardResp = await this.client.get('https://melcloudhome.com/dashboard', {
146
+ headers: { 'User-Agent': MOBILE_USER_AGENT }
147
+ });
148
+
149
+ // --- Krok 4: Pobierz cookies z dashboard ---
150
+ const dashboardCookies = await jar.getCookies('https://melcloudhome.com/dashboard');
151
+ // Możemy zwrócić jako string do użycia w nagłówku:
152
+ const cookieHeader = dashboardCookies.map(c => `${c.key}=${c.value}`).join('; ');
153
+
154
+ this.emit('debug', `Cookies ${cookieHeader}`);
155
+
156
+ return dashboardCookies.map(c => `${c.key}=${c.value}`);
185
157
 
186
- return null;
187
158
  } catch (error) {
188
159
  throw new Error(`Login to MELCloud Home error: ${error}`);
189
160
  }