homebridge-melcloud-control 4.0.0-beta.411 → 4.0.0-beta.412

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 +53 -31
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.411",
4
+ "version": "4.0.0-beta.412",
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
@@ -62,20 +62,28 @@ class MelCloud extends EventEmitter {
62
62
  // MELCloud
63
63
  async checkMelcloudDevicesList(contextKey) {
64
64
  try {
65
- const axiosInstanceGet = axios.create({
66
- method: 'GET',
65
+ const axiosInstance = axios.create({
67
66
  baseURL: ApiUrls.BaseURL,
68
67
  timeout: 15000,
69
68
  headers: { 'X-MitsContextKey': contextKey }
70
69
  });
71
70
 
72
- if (this.logDebug) this.emit('debug', `Scanning for devices`);
73
- const listDevicesData = await axiosInstanceGet(ApiUrls.ListDevices);
71
+ if (this.logDebug) this.emit('debug', `Scanning for devices...`);
72
+
73
+ const listDevicesData = await axiosInstance.get(ApiUrls.ListDevices);
74
+
75
+ if (!listDevicesData || !listDevicesData.data) {
76
+ this.emit('warn', `Invalid or empty response from MELCloud API`);
77
+ return null;
78
+ }
79
+
74
80
  const buildingsList = listDevicesData.data;
75
- if (this.logDebug) this.emit('debug', `Buildings: ${JSON.stringify(buildingsList, null, 2)}`);
76
81
 
77
- if (!buildingsList) {
78
- if (this.logWarn) this.emit('warn', `No building found`);
82
+ if (this.logDebug)
83
+ this.emit('debug', `Buildings: ${JSON.stringify(buildingsList, null, 2)}`);
84
+
85
+ if (!Array.isArray(buildingsList) || buildingsList.length === 0) {
86
+ this.emit('warn', `No buildings found in MELCloud account`);
79
87
  return null;
80
88
  }
81
89
 
@@ -83,42 +91,59 @@ class MelCloud extends EventEmitter {
83
91
  if (this.logDebug) this.emit('debug', `Buildings list saved`);
84
92
 
85
93
  const devices = [];
94
+
86
95
  for (const building of buildingsList) {
87
- const buildingStructure = building.Structure;
88
- const allDevices = [
89
- ...buildingStructure.Floors.flatMap(floor => [
90
- ...floor.Areas.flatMap(area => area.Devices),
91
- ...floor.Devices
92
- ]),
93
- ...buildingStructure.Areas.flatMap(area => area.Devices),
94
- ...buildingStructure.Devices
95
- ];
96
+ if (!building.Structure) {
97
+ this.emit(
98
+ 'warn',
99
+ `Building missing structure: ${building.BuildingName || 'Unnamed'}`
100
+ );
101
+ continue;
102
+ }
96
103
 
97
- // Zamiana DeviceID na string
104
+ const { Structure } = building;
105
+
106
+ const allDevices = [
107
+ ...(Structure.Floors?.flatMap(floor => [
108
+ ...(floor.Areas?.flatMap(area => area.Devices || []) || []),
109
+ ...(floor.Devices || [])
110
+ ]) || []),
111
+ ...(Structure.Areas?.flatMap(area => area.Devices || []) || []),
112
+ ...(Structure.Devices || [])
113
+ ].filter(d => d != null);
114
+
115
+ // Zamiana ID na string
98
116
  allDevices.forEach(device => {
99
- if (device.DeviceID !== undefined && device.DeviceID !== null) {
100
- device.DeviceID = device.DeviceID.toString();
101
- }
117
+ if (device.DeviceID != null) device.DeviceID = String(device.DeviceID);
102
118
  });
103
119
 
120
+ if (this.logDebug) {
121
+ const count = allDevices.length;
122
+ this.emit(
123
+ 'debug',
124
+ `Found ${count} devices in building: ${building.BuildingName || 'Unnamed'}`
125
+ );
126
+ }
127
+
104
128
  devices.push(...allDevices);
105
129
  }
106
130
 
107
- const devicesCount = devices.length;
108
- if (devicesCount === 0) {
109
- if (this.logWarn) this.emit('warn', `No devices found`);
131
+ if (devices.length === 0) {
132
+ this.emit('warn', `No devices found in any building`);
110
133
  return null;
111
134
  }
112
135
 
113
136
  await this.functions.saveData(this.devicesFile, devices);
114
- if (this.logDebug) this.emit('debug', `${devicesCount} devices saved`);
137
+ if (this.logDebug) this.emit('debug', `${devices.length} devices saved`);
115
138
 
116
139
  return devices;
117
140
  } catch (error) {
118
- throw new Error(`Check devices list error: ${error.message}`);
141
+ const msg = error.response ? `HTTP ${error.response.status}: ${error.response.statusText}` : error.message;
142
+ throw new Error(`Check devices list error: ${msg}`);
119
143
  }
120
144
  }
121
145
 
146
+
122
147
  async connectToMelCloud() {
123
148
  if (this.logDebug) this.emit('debug', `Connecting to MELCloud`);
124
149
 
@@ -166,7 +191,7 @@ class MelCloud extends EventEmitter {
166
191
 
167
192
  return accountInfo
168
193
  } catch (error) {
169
- throw new Error(`Connect to MELCloud error: ${error.message}`);
194
+ throw new Error(`Connect error: ${error.message}`);
170
195
  }
171
196
  }
172
197
 
@@ -270,7 +295,7 @@ class MelCloud extends EventEmitter {
270
295
 
271
296
  return devices;
272
297
  } catch (error) {
273
- throw new Error(`Connect to MELCloud Home error: ${error.message}`);
298
+ throw new Error(`Check devices list error: ${error.message}`);
274
299
  }
275
300
  }
276
301
 
@@ -394,7 +419,6 @@ class MelCloud extends EventEmitter {
394
419
  if (!refresh) this.emit('success', `Connect to MELCloud Home Success`);
395
420
 
396
421
  return accountInfo;
397
-
398
422
  } catch (error) {
399
423
  // Handle Puppeteer system dependency errors
400
424
  if (error.message.includes('libnspr4.so') || error.message.includes('Failed to launch the browser process')) {
@@ -422,8 +446,7 @@ class MelCloud extends EventEmitter {
422
446
  }
423
447
 
424
448
  // Generic Puppeteer error
425
- throw new Error(`MELCloud connection failed: ${error.message}`);
426
-
449
+ throw new Error(`Connect error: ${error.message}`);
427
450
  } finally {
428
451
  if (browser) {
429
452
  try {
@@ -435,7 +458,6 @@ class MelCloud extends EventEmitter {
435
458
  }
436
459
  }
437
460
 
438
-
439
461
  async checkDevicesList(contextKey) {
440
462
  let devices = [];
441
463
  switch (this.accountType) {