homebridge-xbox-tv 2.11.1 → 2.11.3

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/xboxdevice.js +39 -50
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "displayName": "Xbox TV",
3
3
  "name": "homebridge-xbox-tv",
4
- "version": "2.11.1",
4
+ "version": "2.11.3",
5
5
  "description": "Homebridge plugin to control Xbox game consoles.",
6
6
  "license": "MIT",
7
7
  "author": "grzegorz914",
package/src/xboxdevice.js CHANGED
@@ -63,15 +63,14 @@ class XboxDevice extends EventEmitter {
63
63
  //add configured inputs to the default inputs
64
64
  this.inputs = [...CONSTANTS.DefaultInputs, ...this.inputs];
65
65
  this.inputsConfigured = [];
66
- this.displayOrder = [];
66
+ this.sensorInputs = [];
67
67
 
68
68
  //setup variables
69
69
  this.restFulConnected = false;
70
70
  this.mqttConnected = false;
71
71
 
72
72
  this.allServices = [];
73
- this.sensorInputsServices = [];
74
- this.sensorInputs = [];
73
+ this.sensorsInputsServices = [];
75
74
  this.buttonsServices = [];
76
75
 
77
76
  this.power = false;
@@ -103,7 +102,7 @@ class XboxDevice extends EventEmitter {
103
102
 
104
103
  files.forEach((file) => {
105
104
  if (!fs.existsSync(file)) {
106
- fs.writeFileSync(file, ' ');
105
+ fs.writeFileSync(file, '');
107
106
  }
108
107
  });
109
108
  } catch (error) {
@@ -274,7 +273,8 @@ class XboxDevice extends EventEmitter {
274
273
  }
275
274
  })
276
275
  .on('stateChanged', (power, volume, mute, mediaState, titleId, reference) => {
277
- const inputIdentifier = this.inputsConfigured.findIndex(index => index.reference === reference) + 1 ?? this.inputsConfigured.findIndex(index => index.titleId === titleId) + 1;
276
+ const inputIdentifierR = this.inputsConfigured.findIndex(index => index.reference === reference) ?? 0;
277
+ const inputIdentifier = inputIdentifierR > 0 ? inputIdentifierR : this.inputsConfigured.findIndex(index => index.titleId === titleId) + 1;
278
278
 
279
279
  //update characteristics
280
280
  if (this.televisionService) {
@@ -282,9 +282,9 @@ class XboxDevice extends EventEmitter {
282
282
  .updateCharacteristic(Characteristic.Active, power)
283
283
  };
284
284
 
285
- if (this.televisionService && inputIdentifier > 0) {
285
+ if (this.televisionService && inputIdentifier !== -1) {
286
286
  this.televisionService
287
- .updateCharacteristic(Characteristic.ActiveIdentifier, inputIdentifier);
287
+ .updateCharacteristic(Characteristic.ActiveIdentifier, inputIdentifier + 1);
288
288
  };
289
289
 
290
290
  if (this.speakerService) {
@@ -308,35 +308,36 @@ class XboxDevice extends EventEmitter {
308
308
  .updateCharacteristic(Characteristic.ContactSensorState, power)
309
309
  }
310
310
 
311
- if (this.sensorInputService && inputIdentifier > 0) {
311
+ if (this.sensorInputService && inputIdentifier !== -1) {
312
312
  const state = power ? (this.inputIdentifier !== inputIdentifier) : false;
313
313
  this.sensorInputService
314
314
  .updateCharacteristic(Characteristic.ContactSensorState, state)
315
315
  this.sensorInputState = state;
316
316
  }
317
317
 
318
- if (this.sensorScreenSaverService) {
319
- const state = power ? (reference === 'Xbox.IdleScreen_8wekyb3d8bbwe!Xbox.IdleScreen.Application') : false;
320
- this.sensorScreenSaverService
321
- .updateCharacteristic(Characteristic.ContactSensorState, state)
322
- this.sensorScreenSaverState = state;
323
- }
324
-
325
318
  if (reference !== undefined) {
326
319
  this.reference = reference;
327
- if (this.sensorInputsServices) {
328
- const servicesCount = this.sensorInputsServices.length;
320
+
321
+ if (this.sensorScreenSaverService) {
322
+ const state = power ? (reference === 'Xbox.IdleScreen_8wekyb3d8bbwe!Xbox.IdleScreen.Application') : false;
323
+ this.sensorScreenSaverService
324
+ .updateCharacteristic(Characteristic.ContactSensorState, state)
325
+ this.sensorScreenSaverState = state;
326
+ }
327
+
328
+ if (this.sensorsInputsServices) {
329
+ const servicesCount = this.sensorsInputsServices.length;
329
330
  for (let i = 0; i < servicesCount; i++) {
330
331
  const state = power ? (this.sensorInputs[i].reference === reference) : false;
331
332
  const displayType = this.sensorInputs[i].displayType;
332
333
  const characteristicType = [Characteristic.MotionDetected, Characteristic.OccupancyDetected, Characteristic.ContactSensorState][displayType];
333
- this.sensorInputsServices[i]
334
+ this.sensorsInputsServices[i]
334
335
  .updateCharacteristic(characteristicType, state);
335
336
  }
336
337
  }
337
338
  }
338
339
 
339
- this.inputIdentifier = inputIdentifier > 0 ? inputIdentifier : this.inputIdentifier;
340
+ this.inputIdentifier = inputIdentifier !== -1 ? inputIdentifier : this.inputIdentifier;
340
341
  this.power = power;
341
342
  this.volume = volume;
342
343
  this.mute = mute;
@@ -359,7 +360,7 @@ class XboxDevice extends EventEmitter {
359
360
  //read dev info from file
360
361
  try {
361
362
  const data = await fsPromises.readFile(this.devInfoFile);
362
- this.savedInfo = data.length > 5 ? JSON.parse(data) : {};
363
+ this.savedInfo = data.length > 0 ? JSON.parse(data) : {};
363
364
  const debug = this.enableDebugMode ? this.emit('debug', `Read saved Info: ${JSON.stringify(this.savedInfo, null, 2)}`) : false;
364
365
  } catch (error) {
365
366
  this.emit('error', `read saved Info error: ${error}`);
@@ -368,7 +369,7 @@ class XboxDevice extends EventEmitter {
368
369
  //read inputs file
369
370
  try {
370
371
  const data = await fsPromises.readFile(this.inputsFile);
371
- this.savedInputs = data.length > 5 ? JSON.parse(data) : this.inputs;
372
+ this.savedInputs = data.length > 0 ? JSON.parse(data) : this.inputs;
372
373
  const debug = this.enableDebugMode ? this.emit('debug', `Read saved Inputs: ${JSON.stringify(this.savedInputs, null, 2)}`) : false;
373
374
  } catch (error) {
374
375
  this.emit('error', `read saved Inputs error: ${error}`);
@@ -377,7 +378,7 @@ class XboxDevice extends EventEmitter {
377
378
  //read inputs names from file
378
379
  try {
379
380
  const data = await fsPromises.readFile(this.inputsNamesFile);
380
- this.savedInputsNames = data.length > 5 ? JSON.parse(data) : {};
381
+ this.savedInputsNames = data.length > 0 ? JSON.parse(data) : {};
381
382
  const debug = this.enableDebugMode ? this.emit('debug', `Read saved Inputs Names: ${JSON.stringify(this.savedInputsNames, null, 2)}`) : false;
382
383
  } catch (error) {
383
384
  this.emit('error', `read saved Inputs/Channels Names error: ${error}`);
@@ -386,7 +387,7 @@ class XboxDevice extends EventEmitter {
386
387
  //read inputs visibility from file
387
388
  try {
388
389
  const data = await fsPromises.readFile(this.inputsTargetVisibilityFile);
389
- this.savedInputsTargetVisibility = data.length > 5 ? JSON.parse(data) : {};
390
+ this.savedInputsTargetVisibility = data.length > 0 ? JSON.parse(data) : {};
390
391
  const debug = this.enableDebugMode ? this.emit('debug', `Read saved Inputs Target Visibility: ${JSON.stringify(this.savedInputsTargetVisibility, null, 2)}`) : false;
391
392
  } catch (error) {
392
393
  this.emit('error', `read saved Inputs/Channels Target Visibility error: ${error}`);
@@ -440,8 +441,8 @@ class XboxDevice extends EventEmitter {
440
441
  //Prepare television service
441
442
  const debug1 = !this.enableDebugMode ? false : this.emit('debug', `Prepare television service`);
442
443
  this.televisionService = new Service.Television(`${accessoryName} Television`, 'Television');
443
- this.televisionService.setCharacteristic(Characteristic.ConfiguredName, accessoryName)
444
- .setCharacteristic(Characteristic.SleepDiscoveryMode, 1);
444
+ this.televisionService.setCharacteristic(Characteristic.ConfiguredName, accessoryName);
445
+ this.televisionService.setCharacteristic(Characteristic.SleepDiscoveryMode, 1);
445
446
 
446
447
  this.televisionService.getCharacteristic(Characteristic.Active)
447
448
  .onGet(async () => {
@@ -497,7 +498,7 @@ class XboxDevice extends EventEmitter {
497
498
  const inputReference = this.inputsConfigured[inputIdentifier].reference;
498
499
  const inputName = this.inputsConfigured[inputIdentifier].name;
499
500
  const logInfo = this.disableLogInfo ? false : this.emit('message', `Input: ${inputName}, Reference: ${inputReference}, Product Id: ${inputOneStoreProductId}`);
500
- return inputIdentifier;
501
+ return inputIdentifier + 1;
501
502
  })
502
503
  .onSet(async (inputIdentifier) => {
503
504
  try {
@@ -784,6 +785,9 @@ class XboxDevice extends EventEmitter {
784
785
  //get input
785
786
  const input = inputs[i];
786
787
 
788
+ //get index
789
+ const index = i + 1;
790
+
787
791
  //get input oneStoreProductId
788
792
  const inputOneStoreProductId = input.oneStoreProductId;
789
793
 
@@ -805,9 +809,9 @@ class XboxDevice extends EventEmitter {
805
809
  const targetVisibility = currentVisibility;
806
810
 
807
811
  if (inputReference && inputName) {
808
- const inputService = new Service.InputSource(`${inputName} ${i}`, `Input ${i}`);
812
+ const inputService = new Service.InputSource(`${inputName} ${index}`, `Input ${index}`);
809
813
  inputService
810
- .setCharacteristic(Characteristic.Identifier, i + 1)
814
+ .setCharacteristic(Characteristic.Identifier, index)
811
815
  .setCharacteristic(Characteristic.Name, inputName)
812
816
  .setCharacteristic(Characteristic.InputSourceType, inputType)
813
817
  .setCharacteristic(Characteristic.IsConfigured, isConfigured)
@@ -818,19 +822,11 @@ class XboxDevice extends EventEmitter {
818
822
  return inputName;
819
823
  })
820
824
  .onSet(async (value) => {
821
- const valueExist = inputName;
822
- if (valueExist) {
823
- return;
824
- }
825
-
826
825
  try {
827
826
  this.savedInputsNames[inputReference] = value;
828
- const newCustomName = JSON.stringify(this.savedInputsNames, null, 2);
829
-
830
- await fsPromises.writeFile(this.inputsNamesFile, newCustomName);
827
+ await fsPromises.writeFile(this.inputsNamesFile, JSON.stringify(this.savedInputsNames, null, 2));
831
828
  const debug = this.enableDebugMode ? this.emit('debug', `Saved Input Name: ${value}, Reference: ${nameIdentifier}.`) : false;
832
829
  inputService.updateCharacteristic(Characteristic.Name, value);
833
- inputService.updateCharacteristic(Characteristic.ConfiguredName, value);
834
830
  } catch (error) {
835
831
  this.emit('error', `save Input Name error: ${error}`);
836
832
  }
@@ -842,25 +838,17 @@ class XboxDevice extends EventEmitter {
842
838
  return targetVisibility;
843
839
  })
844
840
  .onSet(async (state) => {
845
- const stateExist = state === currentVisibility;
846
- if (stateExist) {
847
- return;
848
- };
849
-
850
841
  try {
851
842
  this.savedInputsTargetVisibility[inputReference] = state;
852
- const newTargetVisibility = JSON.stringify(this.savedInputsTargetVisibility, null, 2);
853
-
854
- await fsPromises.writeFile(this.inputsTargetVisibilityFile, newTargetVisibility);
843
+ await fsPromises.writeFile(this.inputsTargetVisibilityFile, JSON.stringify(this.savedInputsTargetVisibility, null, 2));
855
844
  const debug = this.enableDebugMode ? this.emit('debug', `Saved Input: ${inputName} Target Visibility: ${state ? 'HIDEN' : 'SHOWN'}`) : false;
856
845
  inputService.updateCharacteristic(Characteristic.CurrentVisibilityState, state);
857
- inputService.updateCharacteristic(Characteristic.TargetVisibilityState, state);
858
846
  } catch (error) {
859
847
  this.emit('error', `save Target Visibility error: ${error}`);
860
848
  }
861
849
  });
862
850
 
863
- this.displayOrder.push(i + 1);
851
+ input.identifier = index;
864
852
  this.inputsConfigured.push(input);
865
853
 
866
854
  this.televisionService.addLinkedService(inputService);
@@ -871,7 +859,8 @@ class XboxDevice extends EventEmitter {
871
859
 
872
860
  };
873
861
  }
874
- this.televisionService.setCharacteristic(Characteristic.DisplayOrder, Encode(1, this.displayOrder).toString('base64'));
862
+ const displayOrder = this.inputsConfigured.map(input => input.identifier);
863
+ this.televisionService.setCharacteristic(Characteristic.DisplayOrder, Encode(1, displayOrder).toString('base64'));
875
864
 
876
865
  //Prepare volume service
877
866
  if (this.volumeControl >= 0) {
@@ -1007,9 +996,9 @@ class XboxDevice extends EventEmitter {
1007
996
  });
1008
997
 
1009
998
  this.sensorInputs.push(sensorInput);
1010
- this.sensorInputsServices.push(sensorInputService);
999
+ this.sensorsInputsServices.push(sensorInputService);
1011
1000
  this.allServices.push(sensorInputService);
1012
- accessory.addService(this.sensorInputsServices[i]);
1001
+ accessory.addService(this.sensorsInputsServices[i]);
1013
1002
  } else {
1014
1003
  this.emit('message', `Sensor Name: ${sensorInputName ? sensorInputName : 'Missing'}, Reference: ${sensorInputReference ? sensorInputReference : 'Missing'}.`);
1015
1004
  };