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