Skip to content

Commit

Permalink
registerPlatformAccessories
Browse files Browse the repository at this point in the history
  • Loading branch information
nicoduj committed Mar 24, 2019
1 parent 5d1dc57 commit 2c429b2
Show file tree
Hide file tree
Showing 6 changed files with 130 additions and 110 deletions.
22 changes: 6 additions & 16 deletions harmonyAsSwitches.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ module.exports = {
function HarmonyPlatformAsSwitches(log, config, api) {
Characteristic = api.hap.Characteristic;

this.api = api;
this.harmonyBase = new HarmonyBase(api);
this.harmonyBase.configCommonProperties(log, config, api, this);
this.harmonyBase.configCommonProperties(log, config, this);

this.showTurnOffActivity = config['showTurnOffActivity'];
this.publishActivitiesAsIndividualAccessories = HarmonyTools.checkParameter(
Expand All @@ -37,9 +38,9 @@ HarmonyPlatformAsSwitches.prototype = {
var myHarmonyAccessory;

if (!this.publishActivitiesAsIndividualAccessories) {
myHarmonyAccessory = this.harmonyBase.checkAccessory(this, this.name);
myHarmonyAccessory = this.harmonyBase.checkAccessory(this, '');
if (!myHarmonyAccessory) {
myHarmonyAccessory = this.harmonyBase.createAccessory(this, this.name);
myHarmonyAccessory = this.harmonyBase.createAccessory(this, '');
accessoriesToAdd.push(myHarmonyAccessory);
}
}
Expand All @@ -50,17 +51,15 @@ HarmonyPlatformAsSwitches.prototype = {
? 'DEV' + activities[i].label
: activities[i].label;

let accessoryName = this.name + '-' + activities[i].label;

if (this.publishActivitiesAsIndividualAccessories) {
myHarmonyAccessory = this.harmonyBase.checkAccessory(
this,
accessoryName
switchName
);
if (!myHarmonyAccessory) {
myHarmonyAccessory = this.harmonyBase.createAccessory(
this,
accessoryName
switchName
);
accessoriesToAdd.push(myHarmonyAccessory);
}
Expand Down Expand Up @@ -90,15 +89,6 @@ HarmonyPlatformAsSwitches.prototype = {
);
},

//Cache call method
configureAccessory: function(accessory) {
this.log(
accessory.displayName,
'Got cached Accessory For SwitchMode ' + accessory.UUID
);
this._foundAccessories.push(accessory);
},

refreshCurrentActivity: function(response) {
this._currentActivity = response;
this._currentActivityLastUpdate = Date.now();
Expand Down
18 changes: 5 additions & 13 deletions harmonyAsTVPlatform.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@ function HarmonyPlatformAsTVPlatform(log, config, api) {
Service = api.hap.Service;
Characteristic = api.hap.Characteristic;

this.api = api;
this.harmonyBase = new HarmonyBase(api);
this.harmonyBase.configCommonProperties(log, config, api, this);
this.harmonyBase.configCommonProperties(log, config, this);

this.mainActivity = config['mainActivity'];
this.playPauseBehavior = HarmonyTools.checkParameter(
Expand Down Expand Up @@ -201,12 +202,12 @@ HarmonyPlatformAsTVPlatform.prototype = {

readAccessories: function(data, homedata) {
let activities = data.data.activity;

let accessoriesToAdd = [];
let name = this.devMode ? 'DEV' : '';

myHarmonyAccessory = this.harmonyBase.checkAccessory(this, this.name);
myHarmonyAccessory = this.harmonyBase.checkAccessory(this, name);
if (!myHarmonyAccessory) {
myHarmonyAccessory = this.harmonyBase.createAccessory(this, this.name);
myHarmonyAccessory = this.harmonyBase.createAccessory(this, name);
accessoriesToAdd.push(myHarmonyAccessory);
}

Expand Down Expand Up @@ -261,15 +262,6 @@ HarmonyPlatformAsTVPlatform.prototype = {
);
},

//Cache call method
configureAccessory: function(accessory) {
this.log.debug(
accessory.displayName,
'Got cached Accessory For TVMode ' + accessory.UUID
);
this._foundAccessories.push(accessory);
},

///REFRESHING TOOLS

handleRefreshOfCharacteristic(activity) {
Expand Down
85 changes: 26 additions & 59 deletions harmonyBase.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,17 @@ function HarmonyBase(api) {
}

HarmonyBase.prototype = {
configCommonProperties: function(log, config, api, harmonyPlatform) {
configCommonProperties: function(log, config, harmonyPlatform) {
harmonyPlatform.log = log;
harmonyPlatform.hubIP = config['hubIP'];

harmonyPlatform.name = config['name'];
harmonyPlatform.devMode = config['DEVMODE'];

harmonyPlatform.addAllActivitiesToSkippedIfSameStateActivitiesList =
config['addAllActivitiesToSkippedIfSameStateActivitiesList'];
harmonyPlatform.addAllActivitiesToSkippedIfSameStateActivitiesList = HarmonyTools.checkParameter(
config['addAllActivitiesToSkippedIfSameStateActivitiesList'],
false
);
harmonyPlatform.skippedIfSameStateActivities =
config['skippedIfSameStateActivities'];

Expand All @@ -51,8 +53,6 @@ HarmonyBase.prototype = {
true
);

harmonyPlatform.cleanCache = config['cleanCache'];

harmonyPlatform.showCommandsAtStartup = config['showCommandsAtStartup'];

harmonyPlatform._currentActivity = -9999;
Expand All @@ -65,40 +65,10 @@ HarmonyBase.prototype = {
'INFO : following activites controls will be ignored if they are in the same state : ' +
(harmonyPlatform.addAllActivitiesToSkippedIfSameStateActivitiesList
? 'ALL'
: harmonyPlatform.skippedIfSameStateActivities)
: harmonyPlatform.skippedIfSameStateActivities
? harmonyPlatform.skippedIfSameStateActivities
: 'NONE')
);

if (api) {
// Save the API object as plugin needs to register new accessory via this object
harmonyPlatform.api = api;

harmonyPlatform.api
.on(
'shutdown',
function() {
harmonyPlatform.log('INFO - shutdown');
this.harmony.removeAllListeners();
this.harmony.end();
}.bind(this)
)
.on(
'didFinishLaunching',
function() {
harmonyPlatform.log('DidFinishLaunching');

if (harmonyPlatform.cleanCache) {
harmonyPlatform.log('WARNING - Removing Accessories');
harmonyPlatform.api.unregisterPlatformAccessories(
'homebridge-harmonyHub',
'HarmonyHubWebSocket',
harmonyPlatform._foundAccessories
);
harmonyPlatform._foundAccessories = [];
}
this.configureAccessories(harmonyPlatform);
}.bind(this)
);
}
},

handleCharacteristicUpdate: function(
Expand Down Expand Up @@ -328,13 +298,12 @@ HarmonyBase.prototype = {
);

let homeControls = data.data;
let services = [];

var accessoriesToAdd = [];
var myHarmonyAccessory;

if (!harmonyPlatform.publishHomeControlsAsIndividualAccessories) {
let name = harmonyPlatform.name + '-HomeControls';
let name = 'HomeControls';
myHarmonyAccessory = this.checkAccessory(harmonyPlatform, name);
if (!myHarmonyAccessory) {
myHarmonyAccessory = this.createAccessory(harmonyPlatform, name);
Expand All @@ -344,7 +313,6 @@ HarmonyBase.prototype = {

for (var key in homeControls) {
let switchName = key;
let accessoryName = harmonyPlatform.name + '-' + switchName;

if (harmonyPlatform.devMode) {
switchName = 'DEV' + switchName;
Expand All @@ -353,14 +321,11 @@ HarmonyBase.prototype = {
harmonyPlatform.log('INFO - Discovered Home Control : ' + switchName);

if (harmonyPlatform.publishHomeControlsAsIndividualAccessories) {
myHarmonyAccessory = this.checkAccessory(
harmonyPlatform,
accessoryName
);
myHarmonyAccessory = this.checkAccessory(harmonyPlatform, switchName);
if (!myHarmonyAccessory) {
myHarmonyAccessory = this.createAccessory(
harmonyPlatform,
accessoryName
switchName
);
accessoriesToAdd.push(myHarmonyAccessory);
}
Expand Down Expand Up @@ -395,7 +360,6 @@ HarmonyBase.prototype = {
' {"cmd":"harmony.automation?getstate","code":200,"id":"0.11199321450018873","msg":"OK","data":{"hue-light.harmony_virtual_button_3":{"color":{"mode":"xy","xy":{"y":0,"x":0},"temp":300,"hueSat":{"hue":0,"sat":0}},"brightness":254,"on":true,"status":0},"hue-light.harmony_virtual_button_4":{"color":{"mode":"xy","xy":{"y":0,"x":0},"temp":300,"hueSat":{"hue":0,"sat":0}},"brightness":254,"on":false,"status":0},"hue-light.harmony_virtual_button_1":{"color":{"mode":"xy","xy":{"y":0,"x":0},"temp":300,"hueSat":{"hue":0,"sat":0}},"brightness":254,"on":false,"status":0},"hue-light.harmony_virtual_button_2":{"color":{"mode":"xy","xy":{"y":0,"x":0},"temp":300,"hueSat":{"hue":0,"sat":0}},"brightness":254,"on":false,"status":0}}}'
);
*/

return Promise.resolve(responseHome);
}
},
Expand All @@ -412,7 +376,7 @@ HarmonyBase.prototype = {
var myHarmonyAccessory;

if (!harmonyPlatform.publishSequencesAsIndividualAccessories) {
let name = harmonyPlatform.name + '-Sequences';
let name = 'Sequences';
myHarmonyAccessory = this.checkAccessory(harmonyPlatform, name);
if (!myHarmonyAccessory) {
myHarmonyAccessory = this.createAccessory(harmonyPlatform, name);
Expand All @@ -430,7 +394,6 @@ HarmonyBase.prototype = {

for (let i = 0, len = sequences.length; i < len; i++) {
if (sequences[i].name === sequence) {
let accessoryName = harmonyPlatform.name + '-' + sequence;
let switchName = sequence;

if (harmonyPlatform.devMode) {
Expand All @@ -442,12 +405,12 @@ HarmonyBase.prototype = {
if (harmonyPlatform.publishSequencesAsIndividualAccessories) {
myHarmonyAccessory = this.checkAccessory(
harmonyPlatform,
accessoryName
switchName
);
if (!myHarmonyAccessory) {
myHarmonyAccessory = this.createAccessory(
harmonyPlatform,
accessoryName
switchName
);
accessoriesToAdd.push(myHarmonyAccessory);
}
Expand Down Expand Up @@ -569,7 +532,7 @@ HarmonyBase.prototype = {
!foundToggle
) {
if (harmonyPlatform.publishDevicesAsIndividualAccessories) {
let name = accessoryName + '-' + commandFunctions[j].key;
let name = switchName + '-' + commandFunctions[j].key;
myHarmonyAccessory = this.checkAccessory(harmonyPlatform, name);
if (!myHarmonyAccessory) {
myHarmonyAccessory = this.createAccessory(harmonyPlatform, name);
Expand Down Expand Up @@ -644,7 +607,7 @@ HarmonyBase.prototype = {
harmonyPlatform.log('Error - No function list found for ' + switchName);
} else {
if (harmonyPlatform.publishDevicesAsIndividualAccessories) {
let name = accessoryName + '-' + functionsKey;
let name = switchName + '-' + functionsKey;
myHarmonyAccessory = this.checkAccessory(harmonyPlatform, name);
if (!myHarmonyAccessory) {
myHarmonyAccessory = this.createAccessory(harmonyPlatform, name);
Expand Down Expand Up @@ -685,7 +648,7 @@ HarmonyBase.prototype = {
harmonyPlatform.log('INFO - Loading devices...');

if (!harmonyPlatform.publishDevicesAsIndividualAccessories) {
let name = harmonyPlatform.name + '-Devices';
let name = 'Devices';
myHarmonyAccessory = this.checkAccessory(harmonyPlatform, name);
if (!myHarmonyAccessory) {
myHarmonyAccessory = this.createAccessory(harmonyPlatform, name);
Expand Down Expand Up @@ -744,19 +707,22 @@ HarmonyBase.prototype = {
},

checkAccessory(harmonyPlatform, name) {
let uuid = UUIDGen.generate(name);
let fullName = harmonyPlatform.name + '-' + name;
let uuid = UUIDGen.generate(fullName);
return harmonyPlatform._foundAccessories.find(x => x.UUID == uuid);
},

createAccessory(harmonyPlatform, name) {
harmonyPlatform.log('INFO - Adding Accessory : ' + name);
let uuid = UUIDGen.generate(name);
let myHarmonyAccessory = new Accessory(name, uuid);
let fullName = harmonyPlatform.name + '-' + name;
harmonyPlatform.log('INFO - Adding Accessory : ' + fullName);
let uuid = UUIDGen.generate(fullName);
let myHarmonyAccessory = new Accessory(fullName, uuid);

myHarmonyAccessory.name = name;
myHarmonyAccessory.name = fullName;
myHarmonyAccessory.model = harmonyPlatform.name;
myHarmonyAccessory.manufacturer = 'Harmony';
myHarmonyAccessory.serialNumber = harmonyPlatform.hubIP;
myHarmonyAccessory.context.subPlatformName = harmonyPlatform.name;

return myHarmonyAccessory;
},
Expand All @@ -766,6 +732,7 @@ HarmonyBase.prototype = {
harmonyPlatform._foundAccessories,
accessoriesToAdd
);

harmonyPlatform.api.registerPlatformAccessories(
'homebridge-harmonyHub',
'HarmonyHubWebSocket',
Expand Down
Loading

0 comments on commit 2c429b2

Please sign in to comment.