homebridge-melcloud-control 4.0.0-beta.85 → 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);
@@ -223,13 +232,9 @@
223
232
  const removedAtw = removeStaleDevices(acc.atwDevices, devicesByType.atw);
224
233
  const removedErv = removeStaleDevices(acc.ervDevices, devicesByType.erv);
225
234
 
226
- // Function to handle device & presets
227
- // Function to handle devices & presets safely (deep cloning)
228
235
  const handleDevices = (devicesInCloud, devicesInConfig, typeString, newArr, newPresets) => {
229
236
  devicesInCloud.forEach(device => {
230
237
  const { DeviceID: id, Type: type, DeviceName: name, Presets: presets = [] } = device;
231
-
232
- // Create a cloned device object
233
238
  const devObj = structuredClone({
234
239
  id,
235
240
  type,
@@ -240,13 +245,11 @@
240
245
  buttonsSensors: []
241
246
  });
242
247
 
243
- // Add device if not exists
244
248
  if (!devicesInConfig.some(d => d.id === id)) {
245
249
  devicesInConfig.push(devObj);
246
- newArr.push(structuredClone(devObj)); // also clone into new devices list
250
+ newArr.push(structuredClone(devObj));
247
251
  }
248
252
 
249
- // Add presets safely
250
253
  presets.forEach(preset => {
251
254
  const p = structuredClone({
252
255
  id: preset.ID,
@@ -264,12 +267,10 @@
264
267
  });
265
268
  };
266
269
 
267
- // Call for each device type
268
270
  handleDevices(devicesByType.ata, acc.ataDevices, "Air Conditioner", newDevices.ata, newDevices.ataPresets);
269
271
  handleDevices(devicesByType.atw, acc.atwDevices, "Heat Pump", newDevices.atw, newDevices.atwPresets);
270
272
  handleDevices(devicesByType.erv, acc.ervDevices, "Energy Recovery Ventilation", newDevices.erv, newDevices.ervPresets);
271
273
 
272
- // Summary info
273
274
  const newDevicesCount = newDevices.ata.length + newDevices.atw.length + newDevices.erv.length;
274
275
  const newPresetsCount = newDevices.ataPresets.length + newDevices.atwPresets.length + newDevices.ervPresets.length;
275
276
  const removedDevicesCount = removedAta.length + removedAtw.length + removedErv.length;
@@ -285,16 +286,10 @@
285
286
  updateInfo('info2', `Removed devices: ATA: ${removedAta.length}, ATW: ${removedAtw.length}, ERV: ${removedErv.length}.`, 'orange');
286
287
  }
287
288
 
288
- console.log('Account 0:', pluginConfig[0].accounts[0].ataDevices);
289
- console.log('Account 1:', pluginConfig[0].accounts[1].ataDevices);
290
- console.log('Same ref?', pluginConfig[0].accounts[0].ataDevices === pluginConfig[0].accounts[1].ataDevices);
291
-
292
- // Correct save (must be array)
293
289
  await homebridge.updatePluginConfig(pluginConfig);
294
290
  await homebridge.savePluginConfig(pluginConfig);
295
291
  document.getElementById('logIn').className = "btn btn-secondary";
296
292
 
297
-
298
293
  } catch (error) {
299
294
  updateInfo('info', 'Check Your credentials data and try again.', 'yellow');
300
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.85",
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",