homebridge-melcloud-control 2.5.0-beta.95 → 2.5.0-beta.96

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.
@@ -26,7 +26,9 @@ class PluginUiServer extends HomebridgePluginUiServer {
26
26
  try {
27
27
  let data = {};
28
28
  const melCloud = new MelCloud(user, passwd, language, accountInfoFile, deviceInfoFile, buildingsFile, false, 0, true);
29
- const contextKey = await melCloud.connect();
29
+ const response = await melCloud.connect();
30
+ const accountInfo = response.accountInfo;
31
+ const contextKey = response.contextKey;
30
32
  const devices = await melCloud.chackDevicesList(contextKey);
31
33
  return data = {
32
34
  info: '',
package/index.js CHANGED
@@ -2,6 +2,7 @@
2
2
  const path = require('path');
3
3
  const fs = require('fs');
4
4
  const MelCloud = require('./src/melcloud.js')
5
+ const MelCloudDevices = require('./src/melclouddevices.js')
5
6
  const DeviceAta = require('./src/deviceata.js')
6
7
  const DeviceAtw = require('./src/deviceatw.js')
7
8
  const DeviceErv = require('./src/deviceerv.js')
@@ -17,7 +18,7 @@ class MelCloudPlatform {
17
18
  this.accessories = [];
18
19
  const accountsName = [];
19
20
 
20
- api.on('didFinishLaunching', () => {
21
+ api.on('didFinishLaunching', async () => {
21
22
  //loop through accounts
22
23
  for (const account of config.accounts) {
23
24
  const accountName = account.name;
@@ -73,8 +74,12 @@ class MelCloudPlatform {
73
74
  const deviceRefreshInterval = account.deviceRefreshInterval * 1000 || 5000;
74
75
 
75
76
  //melcloud account
76
- const melCloud = new MelCloud(user, passwd, language, accountInfoFile, buildingsFile, deviceFile, enableDebugMode, refreshInterval, false);
77
- melCloud.on('checkDevicesListComplete', (accountInfo, contextKey) => {
77
+ try {
78
+ const melCloud = new MelCloud(user, passwd, language, accountInfoFile, buildingsFile, deviceFile, enableDebugMode, refreshInterval, false);
79
+ const response = await melCloud.connect();
80
+ const accountInfo = response.accountInfo;
81
+ const contextKey = response.contextKey;
82
+ const devices = await melCloud.chackDevicesList(contextKey);
78
83
 
79
84
  //Air Conditioner
80
85
  for (const device of account.ataDevices) {
@@ -189,24 +194,24 @@ class MelCloudPlatform {
189
194
  log.error(`${deviceTypeText}, ${deviceName}, ${error}`);
190
195
  });
191
196
  };
192
- })
193
- .on('success', (message) => {
197
+ melCloud.on('success', (message) => {
194
198
  log.success(`Account ${accountName}, ${message}`);
195
199
  })
196
- .on('message', (message) => {
197
- log.info(`Account ${accountName}, ${message}`);
198
- })
199
- .on('debug', (debug) => {
200
- log.info(`Account ${accountName}, debug: ${debug}`);
201
- })
202
- .on('warn', (warn) => {
203
- log.warn(`${deviceTypeText}, ${deviceName}, ${warn}`);
204
- })
205
- .on('error', async (error) => {
206
- log.error(`Account ${accountName}, ${error}, check again in: ${refreshInterval / 1000}s.`);
207
- await new Promise(resolve => setTimeout(resolve, refreshInterval));
208
- await melCloud.connect();
209
- });
200
+ .on('message', (message) => {
201
+ log.info(`Account ${accountName}, ${message}`);
202
+ })
203
+ .on('debug', (debug) => {
204
+ log.info(`Account ${accountName}, debug: ${debug}`);
205
+ })
206
+ .on('warn', (warn) => {
207
+ log.warn(`Account ${accountName}, ${warn}`);
208
+ })
209
+ .on('error', async (error) => {
210
+ log.error(`Account ${accountName}, ${error}.`);
211
+ });
212
+ } catch (error) {
213
+ log.error(`Account: ${accountName}, MELCloud error: ${error.message ?? error}`);
214
+ }
210
215
  };
211
216
  });
212
217
  };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "displayName": "MELCloud Control",
3
3
  "name": "homebridge-melcloud-control",
4
- "version": "2.5.0-beta.95",
4
+ "version": "2.5.0-beta.96",
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
@@ -16,7 +16,6 @@ class MelCloud extends EventEmitter {
16
16
  this.enableDebugMode = enableDebugMode;
17
17
  this.refreshInterval = refreshInterval;
18
18
  this.requestConfig = requestConfig;
19
- this.emitData = true;
20
19
  this.contextKey = '';
21
20
  this.devicesId = [];
22
21
 
@@ -36,7 +35,7 @@ class MelCloud extends EventEmitter {
36
35
  this.impulseGenerator = new ImpulseGenerator();
37
36
  this.impulseGenerator.on('checkDevicesList', async () => {
38
37
  try {
39
- await this.chackDevicesList(this.contextKey);
38
+ const devices = await this.chackDevicesList(this.contextKey);
40
39
  } catch (error) {
41
40
  this.emit('error', `Check devices list: ${error}.`);
42
41
  };
@@ -66,6 +65,7 @@ class MelCloud extends EventEmitter {
66
65
  const account = accountData.data;
67
66
  const accountInfo = account.LoginData;
68
67
  const contextKey = accountInfo.ContextKey;
68
+ this.contextKey = contextKey;
69
69
 
70
70
  //remove sensitive data
71
71
  const debugData = {
@@ -83,30 +83,21 @@ class MelCloud extends EventEmitter {
83
83
  this.emit('warn', `Context key: ${contextKey}, missing.`)
84
84
  return;
85
85
  };
86
- this.accountInfo = accountInfo;
87
- this.contextKey = contextKey;
88
86
 
89
87
  //save melcloud info to the file
90
88
  await this.saveData(this.accountInfoFile, accountInfo);
91
89
 
92
- //return if connect for config
93
- if (this.requestConfig) {
94
- return contextKey;
95
- };
96
-
97
- //check devices list
98
- await this.chackDevicesList(contextKey);
99
-
100
90
  //emit connect success
101
91
  this.emit('success', `Connect to MELCloud Success.`)
102
92
 
103
- //start impulse generator
104
- const timers = [{ name: 'checkDevicesList', sampling: this.refreshInterval }];
105
- this.impulseGenerator.start(timers);
93
+ const obj = {
94
+ accountInfo: accountInfo,
95
+ contextKey: contextKey
96
+ }
106
97
 
107
- return true;
98
+ return obj;
108
99
  } catch (error) {
109
- this.emit('error', `Connect to MELCloud error: ${error.message ?? error}.`);
100
+ throw new Error(`Connect to MELCloud error: ${error.message ?? error}.`);
110
101
  };
111
102
  }
112
103
 
@@ -177,20 +168,15 @@ class MelCloud extends EventEmitter {
177
168
  const debug = this.enableDebugMode ? this.emit('debug', `Device: ${deviceName} info saved.`) : false;
178
169
  };
179
170
 
180
- //return devices list for config
181
- if (this.requestConfig) {
182
- return devices;
183
- };
184
-
185
- //emit check devices list complete
186
- if (!this.requestConfig && this.emitData) {
187
- this.emit('checkDevicesListComplete', this.accountInfo, contextKey)
188
- this.emitData = false;
189
- };
171
+ if (!this.requestConfig) {
172
+ //start impulse generator
173
+ const timers = [{ name: 'checkDevicesList', sampling: this.refreshInterval }];
174
+ this.impulseGenerator.start(timers);
175
+ }
190
176
 
191
- return true;
177
+ return devices;
192
178
  } catch (error) {
193
- const stop = connectForConfig ? false : this.impulseGenerator.stop();
179
+ const stop = !this.requestConfig ? false : this.impulseGenerator.stop();
194
180
  throw new Error(`Scanning for devices error: ${error.message ?? error}.`);
195
181
  };
196
182
  }