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

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 +64 -57
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.13",
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,89 +235,96 @@ 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 => {
238
+ const capitalizeKeys = obj =>
239
+ Object.fromEntries(
240
+ Object.entries(obj).map(([key, value]) => [
241
+ key.charAt(0).toUpperCase() + key.slice(1),
242
+ value
243
+ ])
244
+ );
245
+
246
+ // Rekurencyjna kapitalizacja kluczy tylko dla wybranych pól
247
+ const capitalizeNestedDeep = obj => {
240
248
  if (Array.isArray(obj)) {
241
- return obj.map(item => capitalizeKeysDeep(item));
249
+ return obj.map(capitalizeNestedDeep);
242
250
  } else if (obj && typeof obj === 'object' && obj.constructor === Object) {
243
251
  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
- })
252
+ Object.entries(obj).map(([k, v]) => [
253
+ k.charAt(0).toUpperCase() + k.slice(1),
254
+ capitalizeNestedDeep(v)
255
+ ])
251
256
  );
252
257
  }
253
258
  return obj;
254
259
  };
255
260
 
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
- })
261
+ const capitalizeNestedFields = device => {
262
+ const copy = { ...device };
263
+
264
+ if (copy.frostProtection) {
265
+ copy.FrostProtection = capitalizeNestedDeep(copy.frostProtection);
266
+ delete copy.frostProtection;
267
+ }
268
+
269
+ if (copy.overheatProtection) {
270
+ copy.OverheatProtection = capitalizeNestedDeep(copy.overheatProtection);
271
+ delete copy.overheatProtection;
272
+ }
273
+
274
+ if (copy.holidayMode) {
275
+ copy.HolidayMode = capitalizeNestedDeep(copy.holidayMode);
276
+ delete copy.holidayMode;
277
+ }
278
+
279
+ if (Array.isArray(copy.schedule)) {
280
+ copy.Schedule = copy.schedule.map(capitalizeNestedDeep);
281
+ delete copy.schedule;
282
+ }
283
+
284
+ return copy;
285
+ };
286
+
287
+ const createDevice = (device, type) => {
288
+ const settingsArray = device.Settings || [];
289
+
290
+ const settingsObject = Object.fromEntries(
291
+ settingsArray.map(({ name, value }) => {
292
+ let parsedValue = value;
293
+ if (value === "True") parsedValue = true;
294
+ else if (value === "False") parsedValue = false;
295
+ else if (!isNaN(value) && value !== "") parsedValue = Number(value);
296
+
297
+ const key = name.charAt(0).toUpperCase() + name.slice(1);
298
+ return [key, parsedValue];
299
+ })
277
300
  );
278
301
 
279
- // Tworzenie obiektu Device z capabilities + settings
280
302
  const deviceObject = {
281
- ...capitalizeKeysDeep(device.capabilities || {}),
282
- ...parsedSettings,
303
+ ...capitalizeKeys(device.Capabilities || {}),
304
+ ...settingsObject,
283
305
  DeviceType: type
284
306
  };
285
307
 
286
- // Usuwamy niepotrzebne pola z oryginału
287
- const {
288
- settings,
289
- capabilities,
290
- id,
291
- givenDisplayName,
292
- ...rest
293
- } = device;
308
+ const { Settings, Capabilities, Id, GivenDisplayName, ...rest } = device;
294
309
 
295
- // Finalny obiekt
296
310
  return {
297
- ...capitalizeKeysDeep(rest),
311
+ ...capitalizeNestedFields(rest),
298
312
  Type: type,
299
- DeviceID: id,
300
- DeviceName: givenDisplayName,
313
+ DeviceID: Id,
314
+ DeviceName: GivenDisplayName,
301
315
  Device: deviceObject,
302
316
  Headers: headers
303
317
  };
304
318
  };
305
319
 
306
- // Przykład użycia:
307
320
  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
- )
321
+ ...(building.airToAirUnits || []).map(d => createDevice(capitalizeKeys(d), 0)),
322
+ ...(building.airToWaterUnits || []).map(d => createDevice(capitalizeKeys(d), 1)),
323
+ ...(building.airToVentilationUnits || []).map(d => createDevice(capitalizeKeys(d), 3))
317
324
  ];
318
-
319
325
  });
320
326
 
327
+
321
328
  const devicesCount = devices.length;
322
329
  if (devicesCount === 0) {
323
330
  devicesList.Info = 'No devices found'