From 2c429b22db20ff8453822afa174ea71f2fbff4e6 Mon Sep 17 00:00:00 2001 From: Nicolas Dujardin Date: Sun, 24 Mar 2019 19:18:02 +0100 Subject: [PATCH] registerPlatformAccessories --- harmonyAsSwitches.js | 22 +++------- harmonyAsTVPlatform.js | 18 +++------ harmonyBase.js | 85 ++++++++++++--------------------------- harmonyPlatform.js | 91 ++++++++++++++++++++++++++++++++++++++++++ index.js | 22 +--------- package.json | 2 +- 6 files changed, 130 insertions(+), 110 deletions(-) create mode 100644 harmonyPlatform.js diff --git a/harmonyAsSwitches.js b/harmonyAsSwitches.js index 1f15518..ace82f9 100644 --- a/harmonyAsSwitches.js +++ b/harmonyAsSwitches.js @@ -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( @@ -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); } } @@ -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); } @@ -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(); diff --git a/harmonyAsTVPlatform.js b/harmonyAsTVPlatform.js index 4d6bc2f..7893511 100644 --- a/harmonyAsTVPlatform.js +++ b/harmonyAsTVPlatform.js @@ -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( @@ -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); } @@ -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) { diff --git a/harmonyBase.js b/harmonyBase.js index c90f281..f898722 100644 --- a/harmonyBase.js +++ b/harmonyBase.js @@ -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']; @@ -51,8 +53,6 @@ HarmonyBase.prototype = { true ); - harmonyPlatform.cleanCache = config['cleanCache']; - harmonyPlatform.showCommandsAtStartup = config['showCommandsAtStartup']; harmonyPlatform._currentActivity = -9999; @@ -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( @@ -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); @@ -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; @@ -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); } @@ -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); } }, @@ -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); @@ -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) { @@ -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); } @@ -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); @@ -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); @@ -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); @@ -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; }, @@ -766,6 +732,7 @@ HarmonyBase.prototype = { harmonyPlatform._foundAccessories, accessoriesToAdd ); + harmonyPlatform.api.registerPlatformAccessories( 'homebridge-harmonyHub', 'HarmonyHubWebSocket', diff --git a/harmonyPlatform.js b/harmonyPlatform.js new file mode 100644 index 0000000..0c13b56 --- /dev/null +++ b/harmonyPlatform.js @@ -0,0 +1,91 @@ +const HarmonyPlatformAsSwitches = require('./harmonyAsSwitches') + .HarmonyPlatformAsSwitches; +const HarmonyPlatformAsTVPlatform = require('./harmonyAsTVPlatform') + .HarmonyPlatformAsTVPlatform; + +module.exports = { + HarmonyPlatform: HarmonyPlatform, +}; + +function HarmonyPlatform(log, config, api) { + log('HarmonyPlatform Init'); + + this.log = log; + this.plaformsConfigs = config['subPlatform']; + this.cleanCache = config['cleanCache']; + this.platforms = []; + this._foundAccessories = []; + + for (let i = 0, len = this.plaformsConfigs.length; i < len; i++) { + let platformConfig = this.plaformsConfigs[i]; + let TVPlatformMode = platformConfig['TVPlatformMode']; + + if (TVPlatformMode) { + this.platforms.push( + new HarmonyPlatformAsTVPlatform(log, platformConfig, api) + ); + } else { + this.platforms.push( + new HarmonyPlatformAsSwitches(log, platformConfig, api) + ); + } + } + + if (api) { + // Save the API object as plugin needs to register new accessory via this object + this.api = api; + + this.api + .on( + 'shutdown', + function() { + this.log('INFO - shutdown'); + for (let i = 0, len = this.platforms.length; i < len; i++) { + let platform = this.platforms[i]; + platform.harmonyBase.harmony.removeAllListeners(); + platform.harmonyBase.harmony.end(); + } + }.bind(this) + ) + .on( + 'didFinishLaunching', + function() { + this.log('DidFinishLaunching'); + + if (this.cleanCache) { + this.log('WARNING - Removing Accessories'); + this.api.unregisterPlatformAccessories( + 'homebridge-harmonyHub', + 'HarmonyHubWebSocket', + this._foundAccessories + ); + this._foundAccessories = []; + + for (let i = 0, len = this.platforms.length; i < len; i++) { + let platform = this.platforms[i]; + platform._foundAccessories = []; + } + } + + for (let i = 0, len = this.platforms.length; i < len; i++) { + let platform = this.platforms[i]; + platform.harmonyBase.configureAccessories(platform); + } + }.bind(this) + ); + } +} + +HarmonyPlatform.prototype = { + configureAccessory: function(accessory) { + let platformName = accessory.context.subPlatformName; + let platform = this.platforms.find(x => x.name == platformName); + + this.log.debug( + accessory.displayName, + 'Got cached Accessory ' + accessory.UUID + ' for ' + platform.name + ); + this._foundAccessories.push(accessory); + platform._foundAccessories.push(accessory); + }, +}; diff --git a/index.js b/index.js index 5d971fd..0e55ab0 100755 --- a/index.js +++ b/index.js @@ -1,7 +1,4 @@ -const HarmonyPlatformAsSwitches = require('./harmonyAsSwitches') - .HarmonyPlatformAsSwitches; -const HarmonyPlatformAsTVPlatform = require('./harmonyAsTVPlatform') - .HarmonyPlatformAsTVPlatform; +const HarmonyPlatform = require('./harmonyPlatform').HarmonyPlatform; module.exports = function(homebridge) { homebridge.registerPlatform( @@ -11,20 +8,3 @@ module.exports = function(homebridge) { true ); }; - -function HarmonyPlatform(log, config, api) { - log('HarmonyPlatform Init'); - - this.log = log; - this.TVPlatformMode = config['TVPlatformMode']; - - if (this.TVPlatformMode == undefined) this.TVPlatformMode = false; - - this.log('INFO - TVPlatformMode : ' + this.TVPlatformMode); - - if (this.TVPlatformMode) { - return new HarmonyPlatformAsTVPlatform(log, config, api); - } else { - return new HarmonyPlatformAsSwitches(log, config, api); - } -} diff --git a/package.json b/package.json index 9cdea4a..0f7c68f 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "homebridge-harmony", - "version": "1.0.0-beta4", + "version": "1.0.0-beta5", "author": "Nicolas Dujardin", "description": "Publish your harmony activities as homekit accessories", "main": "index.js",