homebridge-melcloud-control 4.0.0-beta.86 → 4.0.0-beta.87

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.
@@ -97,15 +97,19 @@
97
97
  <script>
98
98
  (async () => {
99
99
  const pluginConfig = await homebridge.getPluginConfig();
100
+
101
+ // Ensure proper structure on load
100
102
  if (!pluginConfig.length) {
101
- pluginConfig.push({});
103
+ pluginConfig.push({ accounts: [] });
102
104
  await homebridge.updatePluginConfig(pluginConfig);
103
105
  homebridge.showSchemaForm();
104
106
  return;
105
107
  }
108
+ pluginConfig[0].accounts ??= [];
106
109
 
107
110
  this.accountIndex = 0;
108
111
  const accountsCount = pluginConfig[0].accounts.length;
112
+
109
113
  for (let i = 0; i < accountsCount; i++) {
110
114
  const acc = pluginConfig[0].accounts[i];
111
115
  const button = document.createElement("button");
@@ -131,20 +135,25 @@
131
135
  document.getElementById('logIn').disabled = !(acc.name && acc.user && acc.passwd && acc.language && acc.displayType);
132
136
  });
133
137
 
134
- if (i === accountsCount - 1) document.getElementById(`button0`).click();
138
+ if (i === accountsCount - 1 && accountsCount > 0)
139
+ document.getElementById(`button0`).click();
140
+
135
141
  this.accountIndex = i;
136
142
  }
137
143
 
138
144
  document.getElementById('melCloudAccount').style.display = 'block';
145
+
146
+ // Safe update
139
147
  document.getElementById('configForm').addEventListener('input', async () => {
140
148
  const acc = pluginConfig[0].accounts[this.accountIndex];
149
+ if (!acc) return;
141
150
  acc.name = document.querySelector('#name').value;
142
151
  acc.user = document.querySelector('#user').value;
143
152
  acc.passwd = document.querySelector('#passwd').value;
144
153
  acc.language = document.querySelector('#language').value;
145
154
  acc.displayType = document.querySelector('#displayType').value;
146
155
 
147
- document.getElementById('logIn').disabled = !(acc.name && acc.user && acc.passwd && acc.languaged && acc.displayType);
156
+ document.getElementById('logIn').disabled = !(acc.name && acc.user && acc.passwd && acc.language && acc.displayType);
148
157
 
149
158
  await homebridge.updatePluginConfig(pluginConfig);
150
159
  await homebridge.savePluginConfig(pluginConfig);
@@ -200,16 +209,6 @@
200
209
  document.getElementById(`logIn`).className = "btn btn-primary";
201
210
  updateInfo('info', 'Connecting...', 'yellow');
202
211
 
203
- // Ensure each account has independent device arrays (avoid shared references)
204
- if (Array.isArray(pluginConfig?.[0]?.accounts)) {
205
- pluginConfig[0].accounts = pluginConfig[0].accounts.map(acc => ({
206
- ...acc,
207
- ataDevices: Array.isArray(acc.ataDevices) ? structuredClone(acc.ataDevices) : [],
208
- atwDevices: Array.isArray(acc.atwDevices) ? structuredClone(acc.atwDevices) : [],
209
- ervDevices: Array.isArray(acc.ervDevices) ? structuredClone(acc.ervDevices) : []
210
- }));
211
- }
212
-
213
212
  try {
214
213
  const acc = pluginConfig[0].accounts[this.accountIndex];
215
214
  const payload = { accountName: acc.name, user: acc.user, passwd: acc.passwd, language: acc.language, displayType: acc.displayType };
@@ -233,13 +232,9 @@
233
232
  const removedAtw = removeStaleDevices(acc.atwDevices, devicesByType.atw);
234
233
  const removedErv = removeStaleDevices(acc.ervDevices, devicesByType.erv);
235
234
 
236
- // Function to handle device & presets
237
- // Function to handle devices & presets safely (deep cloning)
238
235
  const handleDevices = (devicesInCloud, devicesInConfig, typeString, newArr, newPresets) => {
239
236
  devicesInCloud.forEach(device => {
240
237
  const { DeviceID: id, Type: type, DeviceName: name, Presets: presets = [] } = device;
241
-
242
- // Create a cloned device object
243
238
  const devObj = structuredClone({
244
239
  id,
245
240
  type,
@@ -250,13 +245,11 @@
250
245
  buttonsSensors: []
251
246
  });
252
247
 
253
- // Add device if not exists
254
248
  if (!devicesInConfig.some(d => d.id === id)) {
255
249
  devicesInConfig.push(devObj);
256
- newArr.push(structuredClone(devObj)); // also clone into new devices list
250
+ newArr.push(structuredClone(devObj));
257
251
  }
258
252
 
259
- // Add presets safely
260
253
  presets.forEach(preset => {
261
254
  const p = structuredClone({
262
255
  id: preset.ID,
@@ -274,12 +267,10 @@
274
267
  });
275
268
  };
276
269
 
277
- // Call for each device type
278
270
  handleDevices(devicesByType.ata, acc.ataDevices, "Air Conditioner", newDevices.ata, newDevices.ataPresets);
279
271
  handleDevices(devicesByType.atw, acc.atwDevices, "Heat Pump", newDevices.atw, newDevices.atwPresets);
280
272
  handleDevices(devicesByType.erv, acc.ervDevices, "Energy Recovery Ventilation", newDevices.erv, newDevices.ervPresets);
281
273
 
282
- // Summary info
283
274
  const newDevicesCount = newDevices.ata.length + newDevices.atw.length + newDevices.erv.length;
284
275
  const newPresetsCount = newDevices.ataPresets.length + newDevices.atwPresets.length + newDevices.ervPresets.length;
285
276
  const removedDevicesCount = removedAta.length + removedAtw.length + removedErv.length;
@@ -295,12 +286,10 @@
295
286
  updateInfo('info2', `Removed devices: ATA: ${removedAta.length}, ATW: ${removedAtw.length}, ERV: ${removedErv.length}.`, 'orange');
296
287
  }
297
288
 
298
- // Correct save (must be array)
299
289
  await homebridge.updatePluginConfig(pluginConfig);
300
290
  await homebridge.savePluginConfig(pluginConfig);
301
291
  document.getElementById('logIn').className = "btn btn-secondary";
302
292
 
303
-
304
293
  } catch (error) {
305
294
  updateInfo('info', 'Check Your credentials data and try again.', 'yellow');
306
295
  updateInfo('info1', `Error: ${JSON.stringify(error)}`, 'red');
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.86",
4
+ "version": "4.0.0-beta.87",
5
5
  "description": "Homebridge plugin to control Mitsubishi Air Conditioner, Heat Pump and Energy Recovery Ventilation.",
6
6
  "license": "MIT",
7
7
  "author": "grzegorz914",