homebridge-melcloud-control 4.1.2-beta.10 → 4.1.2-beta.11

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 +50 -30
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "displayName": "MELCloud Control",
3
3
  "name": "homebridge-melcloud-control",
4
- "version": "4.1.2-beta.10",
4
+ "version": "4.1.2-beta.11",
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
@@ -235,67 +235,87 @@ class MelCloud extends EventEmitter {
235
235
  if (this.logDebug) this.emit('debug', `Buildings list saved`);
236
236
 
237
237
  const devices = buildingsList.flatMap(building => {
238
- // Rekurencyjna funkcja kapitalizująca klucze w całym obiekcie
238
+ // Rekurencyjna kapitalizacja kluczy w całym obiekcie
239
239
  const capitalizeKeysDeep = obj => {
240
240
  if (Array.isArray(obj)) {
241
241
  return obj.map(item => capitalizeKeysDeep(item));
242
242
  } else if (obj && typeof obj === 'object' && obj.constructor === Object) {
243
243
  return Object.fromEntries(
244
244
  Object.entries(obj).map(([key, value]) => {
245
- const safeKey =
245
+ const capitalizedKey =
246
246
  typeof key === 'string'
247
247
  ? key.charAt(0).toUpperCase() + key.slice(1)
248
248
  : key;
249
- return [safeKey, capitalizeKeysDeep(value)];
249
+ return [capitalizedKey, capitalizeKeysDeep(value)];
250
250
  })
251
251
  );
252
252
  }
253
253
  return obj;
254
254
  };
255
255
 
256
- // Funkcja tworząca finalny obiekt Device
257
- const createDevice = (device, type) => {
258
- const settingsArray = device.Settings || [];
259
-
260
- const settingsObject = Object.fromEntries(
261
- settingsArray.map(({ name, value }) => {
262
- let parsedValue = value;
263
- if (value === "True") parsedValue = true;
264
- else if (value === "False") parsedValue = false;
265
- else if (!isNaN(value) && value !== "") parsedValue = Number(value);
266
-
267
- const key = name.charAt(0).toUpperCase() + name.slice(1);
268
- return [key, parsedValue];
269
- }))
270
-
256
+ // Funkcja budująca pojedyncze urządzenie
257
+ const createDevice = (device, type, headers = {}) => {
258
+ if (!device || typeof device !== 'object') return null;
259
+
260
+ // Parsowanie tablicy settings → obiekt
261
+ const parsedSettings = Object.fromEntries(
262
+ (device.settings || [])
263
+ .filter(s => s && typeof s.name === 'string')
264
+ .map(({ name, value }) => {
265
+ let parsedValue = value;
266
+ if (value === 'True') parsedValue = true;
267
+ else if (value === 'False') parsedValue = false;
268
+ else if (
269
+ typeof value === 'string' &&
270
+ value.trim() !== '' &&
271
+ !isNaN(Number(value))
272
+ )
273
+ parsedValue = Number(value);
274
+
275
+ return [name.charAt(0).toUpperCase() + name.slice(1), parsedValue];
276
+ })
277
+ );
271
278
 
272
- // Połącz Capabilities + Settings + DeviceType
279
+ // Tworzenie obiektu Device z capabilities + settings
273
280
  const deviceObject = {
274
- ...capitalizeKeysDeep(device.Capabilities || {}),
275
- ...settingsObject,
281
+ ...capitalizeKeysDeep(device.capabilities || {}),
282
+ ...parsedSettings,
276
283
  DeviceType: type
277
284
  };
278
285
 
279
- // Usuń pola, które nie mają trafić do finalnego obiektu
280
- const { Settings, Capabilities, Id, GivenDisplayName, ...rest } = device;
286
+ // Usuwamy niepotrzebne pola z oryginału
287
+ const {
288
+ settings,
289
+ capabilities,
290
+ id,
291
+ givenDisplayName,
292
+ ...rest
293
+ } = device;
281
294
 
282
- // Zwróć gotowy obiekt urządzenia
295
+ // Finalny obiekt
283
296
  return {
284
297
  ...capitalizeKeysDeep(rest),
285
298
  Type: type,
286
- DeviceID: Id,
287
- DeviceName: GivenDisplayName,
299
+ DeviceID: id,
300
+ DeviceName: givenDisplayName,
288
301
  Device: deviceObject,
289
302
  Headers: headers
290
303
  };
291
304
  };
292
305
 
293
- // Mapowanie urządzeń w budynku
306
+ // Przykład użycia:
294
307
  return [
295
- ...(building.airToAirUnits || []).map(d => createDevice(capitalizeKeysDeep(d), 0)),
296
- ...(building.airToWaterUnits || []).map(d => createDevice(capitalizeKeysDeep(d), 1)),
297
- ...(building.airToVentilationUnits || []).map(d => createDevice(capitalizeKeysDeep(d), 3))
308
+ ...(building.airToAirUnits || []).map(d =>
309
+ createDevice(capitalizeKeysDeep(d), 0, headers)
310
+ ),
311
+ ...(building.airToWaterUnits || []).map(d =>
312
+ createDevice(capitalizeKeysDeep(d), 1, headers)
313
+ ),
314
+ ...(building.airToVentilationUnits || []).map(d =>
315
+ createDevice(capitalizeKeysDeep(d), 3, headers)
316
+ )
298
317
  ];
318
+
299
319
  });
300
320
 
301
321
  const devicesCount = devices.length;