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

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 +59 -62
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.11",
4
+ "version": "4.1.2-beta.12",
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,87 +235,84 @@ 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 kapitalizacja kluczy w całym obiekcie
239
- const capitalizeKeysDeep = obj => {
240
- if (Array.isArray(obj)) {
241
- return obj.map(item => capitalizeKeysDeep(item));
242
- } else if (obj && typeof obj === 'object' && obj.constructor === Object) {
243
- return Object.fromEntries(
244
- Object.entries(obj).map(([key, value]) => {
245
- const capitalizedKey =
246
- typeof key === 'string'
247
- ? key.charAt(0).toUpperCase() + key.slice(1)
248
- : key;
249
- return [capitalizedKey, capitalizeKeysDeep(value)];
250
- })
251
- );
238
+ // Funkcja kapitalizująca klucze obiektu
239
+ const capitalizeKeys = obj =>
240
+ Object.fromEntries(
241
+ Object.entries(obj).map(([key, value]) => [
242
+ key.charAt(0).toUpperCase() + key.slice(1),
243
+ value
244
+ ])
245
+ );
246
+
247
+ // Funkcja kapitalizująca tylko wybrane zagnieżdżone obiekty/tablice
248
+ const capitalizeNestedFields = device => {
249
+ const copy = { ...device };
250
+
251
+ if (copy.frostProtection && typeof copy.frostProtection === 'object') {
252
+ copy.FrostProtection = capitalizeKeys(copy.frostProtection);
253
+ delete copy.frostProtection;
252
254
  }
253
- return obj;
255
+
256
+ if (copy.overheatProtection && typeof copy.overheatProtection === 'object') {
257
+ copy.OverheatProtection = capitalizeKeys(copy.overheatProtection);
258
+ delete copy.overheatProtection;
259
+ }
260
+
261
+ if (copy.holidayMode && typeof copy.holidayMode === 'object') {
262
+ copy.HolidayMode = capitalizeKeys(copy.holidayMode);
263
+ delete copy.holidayMode;
264
+ }
265
+
266
+ if (Array.isArray(copy.schedule)) {
267
+ copy.Schedule = copy.schedule.map(item => capitalizeKeys(item));
268
+ delete copy.schedule;
269
+ }
270
+
271
+ return copy;
254
272
  };
255
273
 
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
- })
274
+ // Funkcja tworząca finalny obiekt Device
275
+ const createDevice = (device, type) => {
276
+ // Settings już kapitalizowane w nazwach
277
+ const settingsArray = device.Settings || [];
278
+
279
+ const settingsObject = Object.fromEntries(
280
+ settingsArray.map(({ name, value }) => {
281
+ let parsedValue = value;
282
+ if (value === "True") parsedValue = true;
283
+ else if (value === "False") parsedValue = false;
284
+ else if (!isNaN(value) && value !== "") parsedValue = Number(value);
285
+
286
+ const key = name.charAt(0).toUpperCase() + name.slice(1);
287
+ return [key, parsedValue];
288
+ })
277
289
  );
278
290
 
279
- // Tworzenie obiektu Device z capabilities + settings
291
+ // Scal Capabilities + Settings + DeviceType w Device
280
292
  const deviceObject = {
281
- ...capitalizeKeysDeep(device.capabilities || {}),
282
- ...parsedSettings,
293
+ ...capitalizeKeys(device.Capabilities || {}),
294
+ ...settingsObject,
283
295
  DeviceType: type
284
296
  };
285
297
 
286
- // Usuwamy niepotrzebne pola z oryginału
287
- const {
288
- settings,
289
- capabilities,
290
- id,
291
- givenDisplayName,
292
- ...rest
293
- } = device;
298
+ // Usuń stare pola Settings i Capabilities
299
+ const { Settings, Capabilities, Id, GivenDisplayName, ...rest } = device;
294
300
 
295
- // Finalny obiekt
296
301
  return {
297
- ...capitalizeKeysDeep(rest),
302
+ ...capitalizeNestedFields(rest),
298
303
  Type: type,
299
- DeviceID: id,
300
- DeviceName: givenDisplayName,
304
+ DeviceID: Id,
305
+ DeviceName: GivenDisplayName,
301
306
  Device: deviceObject,
302
307
  Headers: headers
303
308
  };
304
309
  };
305
310
 
306
- // Przykład użycia:
307
311
  return [
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
- )
312
+ ...(building.airToAirUnits || []).map(d => createDevice(capitalizeKeys(d), 0)),
313
+ ...(building.airToWaterUnits || []).map(d => createDevice(capitalizeKeys(d), 1)),
314
+ ...(building.airToVentilationUnits || []).map(d => createDevice(capitalizeKeys(d), 3))
317
315
  ];
318
-
319
316
  });
320
317
 
321
318
  const devicesCount = devices.length;