homebridge-melcloud-control 4.2.4 → 4.2.5-beta.10

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.
@@ -52,11 +52,15 @@ class MelCloudAtw extends EventEmitter {
52
52
  try {
53
53
  //read device info from file
54
54
  const devicesData = await this.functions.readData(this.devicesFile, true);
55
- const scenes = devicesData.Scenes ?? [];
56
- const deviceData = devicesData.Devices.find(device => device.DeviceID === this.deviceId);
55
+ this.axiosInstance = axios.create({
56
+ baseURL: this.baseURL,
57
+ timeout: 30000,
58
+ headers: devicesData.Headers
59
+ });
57
60
 
61
+ const deviceData = devicesData.Devices.find(device => device.DeviceID === this.deviceId);
58
62
  if (this.accountType === 'melcloudhome') {
59
- deviceData.Scenes = scenes;
63
+ deviceData.Scenes = devicesData.Scenes ?? [];
60
64
  }
61
65
 
62
66
  const safeConfig = {
@@ -167,13 +171,7 @@ class MelCloudAtw extends EventEmitter {
167
171
  }
168
172
 
169
173
  if (this.logDebug) this.emit('debug', `Send Data: ${JSON.stringify(payload, null, 2)}`);
170
- await axios(ApiUrls.SetAtw, {
171
- method: 'POST',
172
- baseURL: ApiUrls.BaseURL,
173
- timeout: 10000,
174
- headers: deviceData.Headers,
175
- data: payload
176
- });
174
+ await this.axiosInstance(path, { method: 'POST', data: payload });
177
175
  this.updateData(deviceData);
178
176
  return true;
179
177
  case "melcloudhome":
@@ -187,19 +185,19 @@ class MelCloudAtw extends EventEmitter {
187
185
  };
188
186
  method = 'POST';
189
187
  path = ApiUrlsHome.PostHolidayMode;
190
- deviceData.Headers.Referer = ApiUrlsHome.Referers.PostHolidayMode.replace('deviceid', deviceData.DeviceID);
188
+ this.headers.Referer = ApiUrlsHome.Referers.PostHolidayMode.replace('deviceid', deviceData.DeviceID);
191
189
  break;
192
190
  case 'schedule':
193
191
  payload = { enabled: deviceData.ScheduleEnabled };
194
192
  method = 'PUT';
195
193
  path = ApiUrlsHome.PutScheduleEnabled.replace('deviceid', deviceData.DeviceID);
196
- deviceData.Headers.Referer = ApiUrlsHome.Referers.PutScheduleEnabled.replace('deviceid', deviceData.DeviceID);
194
+ this.headers.Referer = ApiUrlsHome.Referers.PutScheduleEnabled.replace('deviceid', deviceData.DeviceID);
197
195
  break;
198
196
  case 'scene':
199
197
  method = 'PUT';
200
198
  const state = flagData.Enabled ? 'Enable' : 'Disable';
201
199
  path = ApiUrlsHome.PutScene[state].replace('sceneid', flagData.Id);
202
- deviceData.Headers.Referer = ApiUrlsHome.Referers.GetPutScenes;
200
+ this.headers.Referer = ApiUrlsHome.Referers.GetPutScenes;
203
201
  break;
204
202
  default:
205
203
  payload = {
@@ -219,20 +217,14 @@ class MelCloudAtw extends EventEmitter {
219
217
  };
220
218
  method = 'PUT';
221
219
  path = ApiUrlsHome.PutAtw.replace('deviceid', deviceData.DeviceID);
222
- deviceData.Headers.Referer = ApiUrlsHome.Referers.PutDeviceSettings
220
+ this.headers.Referer = ApiUrlsHome.Referers.PutDeviceSettings;
223
221
  break
224
222
  }
225
223
 
226
- deviceData.Headers['Content-Type'] = 'application/json; charset=utf-8';
227
- deviceData.Headers.Origin = ApiUrlsHome.Origin;
228
- if (this.logDebug) this.emit('debug', `Send Data: ${JSON.stringify(payload, null, 2)}, Headers: ${JSON.stringify(deviceData.Headers, null, 2)}`);
229
- await axios(path, {
230
- method: method,
231
- baseURL: ApiUrlsHome.BaseURL,
232
- timeout: 10000,
233
- headers: deviceData.Headers,
234
- data: payload
235
- });
224
+ this.headers['Content-Type'] = 'application/json; charset=utf-8';
225
+ this.headers.Origin = ApiUrlsHome.Origin;
226
+ if (this.logDebug) this.emit('debug', `Send Data: ${JSON.stringify(payload, null, 2)}, Headers: ${JSON.stringify(this.headers, null, 2)}`);
227
+ await this.axiosInstance(path, { method: method, data: payload });
236
228
  this.updateData(deviceData);
237
229
  return true;
238
230
  default:
@@ -52,11 +52,15 @@ class MelCloudErv extends EventEmitter {
52
52
  try {
53
53
  //read device info from file
54
54
  const devicesData = await this.functions.readData(this.devicesFile, true);
55
- const scenes = devicesData.Scenes ?? [];
56
- const deviceData = devicesData.Devices.find(device => device.DeviceID === this.deviceId);
55
+ this.axiosInstance = axios.create({
56
+ baseURL: this.baseURL,
57
+ timeout: 30000,
58
+ headers: devicesData.Headers
59
+ });
57
60
 
61
+ const deviceData = devicesData.Devices.find(device => device.DeviceID === this.deviceId);
58
62
  if (this.accountType === 'melcloudhome') {
59
- deviceData.Scenes = scenes;
63
+ deviceData.Scenes = devicesData.Scenes ?? [];
60
64
 
61
65
  //read default temps
62
66
  const temps = await this.functions.readData(this.defaultTempsFile, true);
@@ -170,13 +174,7 @@ class MelCloudErv extends EventEmitter {
170
174
  }
171
175
 
172
176
  if (this.logDebug) this.emit('debug', `Send Data: ${JSON.stringify(payload, null, 2)}`);
173
- await axios(ApiUrls.SetErv, {
174
- method: 'POST',
175
- baseURL: ApiUrls.BaseURL,
176
- timeout: 10000,
177
- headers: deviceData.Headers,
178
- data: payload
179
- });
177
+ await this.axiosInstance(path, { method: 'POST', data: payload });
180
178
  this.updateData(deviceData);
181
179
  return true;
182
180
  case "melcloudhome":
@@ -202,19 +200,19 @@ class MelCloudErv extends EventEmitter {
202
200
  };
203
201
  method = 'POST';
204
202
  path = ApiUrlsHome.PostHolidayMode;
205
- deviceData.Headers.Referer = ApiUrlsHome.Referers.PostHolidayMode.replace('deviceid', deviceData.DeviceID);
203
+ this.headers.Referer = ApiUrlsHome.Referers.PostHolidayMode.replace('deviceid', deviceData.DeviceID);
206
204
  break;
207
205
  case 'schedule':
208
206
  payload = { enabled: deviceData.ScheduleEnabled };
209
207
  method = 'PUT';
210
208
  path = ApiUrlsHome.PutScheduleEnabled.replace('deviceid', deviceData.DeviceID);
211
- deviceData.Headers.Referer = ApiUrlsHome.Referers.PutScheduleEnabled.replace('deviceid', deviceData.DeviceID);
209
+ this.headers.Referer = ApiUrlsHome.Referers.PutScheduleEnabled.replace('deviceid', deviceData.DeviceID);
212
210
  break;
213
211
  case 'scene':
214
212
  method = 'PUT';
215
213
  const state = flagData.Enabled ? 'Enable' : 'Disable';
216
214
  path = ApiUrlsHome.PutScene[state].replace('sceneid', flagData.Id);
217
- deviceData.Headers.Referer = ApiUrlsHome.Referers.GetPutScenes;
215
+ this.headers.Referer = ApiUrlsHome.Referers.GetPutScenes;
218
216
  break;
219
217
  default:
220
218
  payload = {
@@ -226,20 +224,14 @@ class MelCloudErv extends EventEmitter {
226
224
  };
227
225
  method = 'PUT';
228
226
  path = ApiUrlsHome.PutErv.replace('deviceid', deviceData.DeviceID);
229
- deviceData.Headers.Referer = ApiUrlsHome.Referers.PutDeviceSettings
227
+ this.headers.Referer = ApiUrlsHome.Referers.PutDeviceSettings;
230
228
  break
231
229
  }
232
230
 
233
- deviceData.Headers['Content-Type'] = 'application/json; charset=utf-8';
234
- deviceData.Headers.Origin = ApiUrlsHome.Origin;
235
- if (this.logDebug) this.emit('debug', `Send Data: ${JSON.stringify(payload, null, 2)}, Headers: ${JSON.stringify(deviceData.Headers, null, 2)}`);
236
- await axios(path, {
237
- method: method,
238
- baseURL: ApiUrlsHome.BaseURL,
239
- timeout: 10000,
240
- headers: deviceData.Headers,
241
- data: payload
242
- });
231
+ this.headers['Content-Type'] = 'application/json; charset=utf-8';
232
+ this.headers.Origin = ApiUrlsHome.Origin;
233
+ if (this.logDebug) this.emit('debug', `Send Data: ${JSON.stringify(payload, null, 2)}, Headers: ${JSON.stringify(this.headers, null, 2)}`);
234
+ await this.axiosInstance(path, { method: method, data: payload });
243
235
  this.updateData(deviceData);
244
236
  return true;
245
237
  default:
@@ -65,12 +65,7 @@ class MelCloud extends EventEmitter {
65
65
  async checkScenesList() {
66
66
  try {
67
67
  if (this.logDebug) this.emit('debug', `Scanning for scenes`);
68
- const listScenesData = await axios(ApiUrlsHome.GetUserScenes, {
69
- method: 'GET',
70
- baseURL: ApiUrlsHome.BaseURL,
71
- timeout: 25000,
72
- headers: this.headers
73
- });
68
+ const listScenesData = await this.axiosInstance(ApiUrlsHome.GetUserScenes, { method: 'GET', });
74
69
 
75
70
  const scenesList = listScenesData.data;
76
71
  if (this.logDebug) this.emit('debug', `Scenes: ${JSON.stringify(scenesList, null, 2)}`);
@@ -102,12 +97,7 @@ class MelCloud extends EventEmitter {
102
97
  try {
103
98
  const devicesList = { State: false, Info: null, Devices: [], Scenes: [] }
104
99
  if (this.logDebug) this.emit('debug', `Scanning for devices`);
105
- const listDevicesData = await axios(ApiUrlsHome.GetUserContext, {
106
- method: 'GET',
107
- baseURL: ApiUrlsHome.BaseURL,
108
- timeout: 25000,
109
- headers: this.headers
110
- });
100
+ const listDevicesData = await this.axiosInstance(ApiUrlsHome.GetUserContext, { method: 'GET' });
111
101
 
112
102
  const userContext = listDevicesData.data;
113
103
  const buildings = userContext.buildings ?? [];
@@ -189,7 +179,6 @@ class MelCloud extends EventEmitter {
189
179
  DeviceName: GivenDisplayName,
190
180
  SerialNumber: Id,
191
181
  Device: deviceObject,
192
- Headers: this.headers
193
182
  };
194
183
  };
195
184
 
@@ -212,6 +201,7 @@ class MelCloud extends EventEmitter {
212
201
  devicesList.Info = `Found ${devicesCount} devices`;
213
202
  devicesList.Devices = devices;
214
203
  devicesList.Scenes = scenes;
204
+ devicesList.Headers = this.headers;
215
205
 
216
206
  await this.functions.saveData(this.devicesFile, devicesList);
217
207
  if (this.logDebug) this.emit('debug', `${devicesCount} devices saved`);
@@ -330,7 +320,7 @@ class MelCloud extends EventEmitter {
330
320
  return accountInfo;
331
321
  }
332
322
  await Promise.race([Promise.all([submitButton.click(), page.waitForNavigation({ waitUntil: ['domcontentloaded', 'networkidle2'], timeout: GLOBAL_TIMEOUT / 4 })]), new Promise(r => setTimeout(r, GLOBAL_TIMEOUT / 3))]);
333
-
323
+ this.emit('warn', `Cookies: ${await browser.cookies()}`);
334
324
  // Extract cookies
335
325
  let c1 = null, c2 = null;
336
326
  const start = Date.now();
@@ -352,7 +342,8 @@ class MelCloud extends EventEmitter {
352
342
  `__Secure-monitorandcontrolC2=${c2}`
353
343
  ].join('; ');
354
344
 
355
- this.headers = {
345
+ const userAgent = await page.evaluate(() => navigator.userAgent);
346
+ const headers = {
356
347
  'Accept': '*/*',
357
348
  'Accept-Encoding': 'gzip, deflate, br',
358
349
  'Accept-Language': LanguageLocaleMap[this.language],
@@ -362,12 +353,18 @@ class MelCloud extends EventEmitter {
362
353
  'Sec-Fetch-Dest': 'empty',
363
354
  'Sec-Fetch-Mode': 'cors',
364
355
  'Sec-Fetch-Site': 'same-origin',
356
+ 'User-Agent': userAgent,
365
357
  'x-csrf': '1'
366
358
  };
359
+ this.headers = headers;
360
+ this.axiosInstance = axios.create({
361
+ baseURL: ApiUrlsHome.BaseURL,
362
+ timeout: 30000,
363
+ headers: headers
364
+ })
367
365
 
368
366
  accountInfo.State = true;
369
367
  accountInfo.Info = 'Connect to MELCloud Home Success';
370
- accountInfo.Headers = this.headers;
371
368
  await this.functions.saveData(this.accountFile, accountInfo);
372
369
 
373
370
  return accountInfo;
@@ -385,13 +382,8 @@ class MelCloud extends EventEmitter {
385
382
 
386
383
  async send(accountInfo) {
387
384
  try {
388
- await axios(ApiUrlsHome.UpdateApplicationOptions, {
389
- method: 'POST',
390
- baseURL: ApiUrlsHome.BaseURL,
391
- timeout: 15000,
392
- headers: accountInfo.Headers
393
- });
394
- await this.functions.saveData(this.accountFile, accountInfo);
385
+ //await this.axiosInstance(ApiUrlsHome.UpdateApplicationOptions, { method: 'POST' });
386
+ //await this.functions.saveData(this.accountFile, accountInfo);
395
387
  return true;
396
388
  } catch (error) {
397
389
  throw new Error(`Send data error: ${error.message}`);