homebridge-melcloud-control 3.9.8-beta.1 → 3.9.8-beta.2

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 +55 -80
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "displayName": "MELCloud Control",
3
3
  "name": "homebridge-melcloud-control",
4
- "version": "3.9.8-beta.1",
4
+ "version": "3.9.8-beta.2",
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
@@ -17,49 +17,48 @@ class MelCloud extends EventEmitter {
17
17
  this.contextKey = '';
18
18
  this.functions = new Functions();
19
19
 
20
- this.options = {
21
- data: {
22
- Email: user,
23
- Password: passwd,
24
- Language: language,
25
- AppVersion: '1.34.12',
26
- CaptchaChallenge: '',
27
- CaptchaResponse: '',
28
- Persist: true
29
- }
20
+ this.loginData = {
21
+ Email: user,
22
+ Password: passwd,
23
+ Language: language,
24
+ AppVersion: '1.34.12',
25
+ CaptchaChallenge: '',
26
+ CaptchaResponse: '',
27
+ Persist: true
28
+ };
29
+
30
+ this.axiosDefaults = {
31
+ timeout: 10000,
32
+ maxContentLength: 100000000,
33
+ maxBodyLength: 1000000000,
34
+ httpsAgent: new Agent({
35
+ keepAlive: false,
36
+ rejectUnauthorized: false
37
+ })
30
38
  };
31
39
 
32
40
  if (!requestConfig) {
33
41
  this.impulseGenerator = new ImpulseGenerator()
34
42
  .on('checkDevicesList', async () => {
35
43
  try {
36
- await this.chackDevicesList(this.contextKey);
44
+ await this.checkDevicesList(this.contextKey);
37
45
  } catch (error) {
38
46
  this.emit('error', `Impulse generator error: ${error}`);
39
- };
40
- }).on('state', (state) => {
47
+ }
48
+ })
49
+ .on('state', (state) => {
41
50
  this.emit('success', `Impulse generator ${state ? 'started' : 'stopped'}.`);
42
51
  });
43
- };
44
- };
52
+ }
53
+ }
45
54
 
46
- async chackDevicesList(contextKey) {
55
+ async checkDevicesList(contextKey) {
47
56
  try {
48
- //create axios instance get
49
57
  const axiosInstanceGet = axios.create({
50
58
  method: 'GET',
51
59
  baseURL: ApiUrls.BaseURL,
52
- timeout: 10000,
53
- headers: {
54
- 'X-MitsContextKey': contextKey
55
- },
56
- maxContentLength: 100000000,
57
- maxBodyLength: 1000000000,
58
- withCredentials: true,
59
- httpsAgent: new Agent({
60
- keepAlive: false,
61
- rejectUnauthorized: false
62
- })
60
+ headers: { 'X-MitsContextKey': contextKey },
61
+ ...this.axiosDefaults
63
62
  });
64
63
 
65
64
  if (this.enableDebugMode) this.emit('debug', `Scanning for devices`);
@@ -72,23 +71,20 @@ class MelCloud extends EventEmitter {
72
71
  return null;
73
72
  }
74
73
 
75
- //save buildings to the file
76
74
  await this.functions.saveData(this.buildingsFile, buildingsList);
77
75
  if (this.enableDebugMode) this.emit('debug', `Buildings list saved`);
78
76
 
79
- //read buildings structure and get the devices
80
77
  const devices = [];
81
78
  for (const building of buildingsList) {
82
79
  const buildingStructure = building.Structure;
83
-
84
- //get all devices from the building structure
85
80
  const allDevices = [
86
- ...buildingStructure.Floors.flatMap(floor => [...floor.Areas.flatMap(area => area.Devices), ...floor.Devices]),
81
+ ...buildingStructure.Floors.flatMap(floor => [
82
+ ...floor.Areas.flatMap(area => area.Devices),
83
+ ...floor.Devices
84
+ ]),
87
85
  ...buildingStructure.Areas.flatMap(area => area.Devices),
88
86
  ...buildingStructure.Devices
89
87
  ];
90
-
91
- //add all devices to the devices array
92
88
  devices.push(...allDevices);
93
89
  }
94
90
 
@@ -98,14 +94,13 @@ class MelCloud extends EventEmitter {
98
94
  return null;
99
95
  }
100
96
 
101
- //save buildings to the file
102
97
  await this.functions.saveData(this.devicesFile, devices);
103
98
  if (this.enableDebugMode) this.emit('debug', `${devicesCount} devices saved`);
104
99
 
105
100
  return devices;
106
101
  } catch (error) {
107
- throw new Error(`Check devices list error: ${error}`);
108
- };
102
+ throw new Error(`Check devices list error: ${error.message}`);
103
+ }
109
104
  }
110
105
 
111
106
  async connect() {
@@ -115,23 +110,16 @@ class MelCloud extends EventEmitter {
115
110
  const axiosInstanceLogin = axios.create({
116
111
  method: 'POST',
117
112
  baseURL: ApiUrls.BaseURL,
118
- timeout: 10000,
119
- withCredentials: true,
120
- maxContentLength: 100000000,
121
- maxBodyLength: 1000000000,
122
- httpsAgent: new Agent({
123
- keepAlive: false,
124
- rejectUnauthorized: false
125
- })
113
+ ...this.axiosDefaults
126
114
  });
127
- const accountData = await axiosInstanceLogin(ApiUrls.ClientLogin, this.options);
115
+
116
+ const accountData = await axiosInstanceLogin(ApiUrls.ClientLogin, this.loginData);
128
117
  const account = accountData.data;
129
118
  const accountInfo = account.LoginData;
130
119
  const contextKey = accountInfo.ContextKey;
131
120
  const useFahrenheit = accountInfo.UseFahrenheit ?? false;
132
121
  this.contextKey = contextKey;
133
122
 
134
- //remove sensitive data
135
123
  const debugData = {
136
124
  ...accountInfo,
137
125
  ContextKey: 'removed',
@@ -143,59 +131,46 @@ class MelCloud extends EventEmitter {
143
131
  };
144
132
  if (this.enableDebugMode) this.emit('debug', `MELCloud Info: ${JSON.stringify(debugData, null, 2)}`);
145
133
 
146
- if (contextKey === undefined || contextKey === null) {
147
- this.emit('warn', `Context key: ${contextKey}, missing`)
134
+ if (!contextKey) {
135
+ this.emit('warn', `Context key missing`);
148
136
  return null;
149
- };
137
+ }
150
138
 
151
- //create axios instance post
152
139
  this.axiosInstancePost = axios.create({
153
140
  method: 'POST',
154
141
  baseURL: ApiUrls.BaseURL,
155
- timeout: 10000,
156
142
  headers: {
157
143
  'X-MitsContextKey': contextKey,
158
144
  'content-type': 'application/json'
159
145
  },
160
- maxContentLength: 100000000,
161
- maxBodyLength: 1000000000,
162
- withCredentials: true,
163
- httpsAgent: new Agent({
164
- keepAlive: false,
165
- rejectUnauthorized: false
166
- })
146
+ ...this.axiosDefaults
167
147
  });
168
148
 
169
- //save melcloud info to the file
170
149
  await this.functions.saveData(this.accountFile, accountInfo);
171
150
 
172
- //emit connect success
173
- this.emit('success', `Connect to MELCloud Success`)
151
+ this.emit('success', `Connect to MELCloud Success`);
174
152
 
175
- const obj = {
176
- accountInfo: accountInfo,
177
- contextKey: contextKey,
178
- useFahrenheit: useFahrenheit
179
- }
180
-
181
- return obj;
153
+ return {
154
+ accountInfo,
155
+ contextKey,
156
+ useFahrenheit
157
+ };
182
158
  } catch (error) {
183
- throw new Error(`Connect to MELCloud error: ${error}`);
184
- };
159
+ throw new Error(`Connect to MELCloud error: ${error.message}`);
160
+ }
185
161
  }
186
162
 
187
163
  async send(accountInfo) {
188
164
  try {
189
- const options = {
190
- data: accountInfo
191
- };
192
-
165
+ const options = { data: accountInfo };
193
166
  await this.axiosInstancePost(ApiUrls.UpdateApplicationOptions, options);
194
167
  await this.functions.saveData(this.accountFile, accountInfo);
195
168
  return true;
196
169
  } catch (error) {
197
- throw new Error(`Send data error: ${error}`);
198
- };
199
- };
200
- };
170
+ throw new Error(`Send data error: ${error.message}`);
171
+ }
172
+ }
173
+ }
174
+
201
175
  export default MelCloud;
176
+