From 6ac719bab6ee5273f6a0e0ad46ecf66809b5ec65 Mon Sep 17 00:00:00 2001 From: Nick Parfene Date: Fri, 27 Jan 2017 22:25:21 +1300 Subject: [PATCH 1/4] Add support for a default_input configuration option --- README.md | 11 ++++++++++- index.js | 8 +++++++- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 9f72046..e185228 100644 --- a/README.md +++ b/README.md @@ -16,6 +16,7 @@ Next step: Check what happens if the AVR is reset or is temporary not available. # Configuration Example accessory config (needs to be added to the homebridge config.json): + ``` "accessories": [ { @@ -23,7 +24,15 @@ Example accessory config (needs to be added to the homebridge config.json): "name": "My Onkyo", "ip_address": "10.0.1.23", "model" : "TX-NR609", - "poll_status_interval": "900" + "poll_status_interval": "900", + "default_input": "sat" } ] ``` + + `default_input` is optional. If it not supplied, the previous input will be persisted. If a valid option is supplied, a request to set the input to the specified source will be made when the unit is turned on. The valid options depend on the unit. As a rule of thumb try to use the values as they are printed on the remote. + Examples of values: + - VCR + - SAT + - DVD + diff --git a/index.js b/index.js index 2982ce8..52c4c89 100644 --- a/index.js +++ b/index.js @@ -29,6 +29,7 @@ function HttpStatusAccessory(log, config) this.name = config["name"]; this.model = config["model"]; this.poll_status_interval = config["poll_status_interval"] || "0"; + this.defaultInput = config["default_input"]; this.state = false; this.interval = parseInt( this.poll_status_interval); @@ -43,7 +44,6 @@ function HttpStatusAccessory(log, config) this.eiscp.on('debug', this.eventDebug.bind(this)); this.eiscp.on('error', this.eventError.bind(this)); this.eiscp.on('connect', this.eventConnect.bind(this)); - this.eiscp.on('connect', this.eventConnect.bind(this)); this.eiscp.on('system-power', this.eventSystemPower.bind(this)); this.eiscp.on('volume', this.eventVolume.bind(this)); this.eiscp.on('close', this.eventClose.bind(this)); @@ -103,6 +103,12 @@ eventSystemPower: function( response) if (this.switchService ) { this.switchService.getCharacteristic(Characteristic.On).setValue(this.state, null, "statuspoll"); } + + // If the AVR has just been turned on, apply the Input default + if (this.state && this.defaultInput) { + this.log("Attempting to set the input selector to sat"); + this.eiscp.command("input-selector="+this.defaultInput); + } }, eventVolume: function( response) From 45595552b7748e22b3bbe88c5d01957046ecac16 Mon Sep 17 00:00:00 2001 From: Nick Parfene Date: Sat, 28 Jan 2017 00:58:48 +1300 Subject: [PATCH 2/4] Fix resetting of Input after the Poll event --- index.js | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/index.js b/index.js index 52c4c89..7662262 100644 --- a/index.js +++ b/index.js @@ -103,12 +103,6 @@ eventSystemPower: function( response) if (this.switchService ) { this.switchService.getCharacteristic(Characteristic.On).setValue(this.state, null, "statuspoll"); } - - // If the AVR has just been turned on, apply the Input default - if (this.state && this.defaultInput) { - this.log("Attempting to set the input selector to sat"); - this.eiscp.command("input-selector="+this.defaultInput); - } }, eventVolume: function( response) @@ -152,6 +146,13 @@ setPowerState: function(powerOn, callback, context) { that.switchService.getCharacteristic(Characteristic.On).setValue(powerOn, null, "statuspoll"); } } + else { + // If the AVR has just been turned on, apply the Input default + if (this.state && this.defaultInput) { + this.log("Attempting to set the input selector to sat"); + this.eiscp.command("input-selector="+this.defaultInput); + } + } }.bind(this) ); } else { this.log("setPowerState - actual mode, power state: %s, switching to OFF", that.state); From 4ba423e0679568fb2d6f3a133fa677236e8589e6 Mon Sep 17 00:00:00 2001 From: Nick Parfene Date: Sat, 28 Jan 2017 08:51:22 +1300 Subject: [PATCH 3/4] Fix crash on startup. powerOn undefined. --- index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.js b/index.js index 7662262..2bf0c3f 100644 --- a/index.js +++ b/index.js @@ -162,7 +162,7 @@ setPowerState: function(powerOn, callback, context) { that.state = false; that.log( "setPowerState - PWR OFF: ERROR - current state: %s", that.state); if (that.switchService ) { - that.switchService.getCharacteristic(Characteristic.On).setValue(powerOn, null, "statuspoll"); + that.switchService.getCharacteristic(Characteristic.On).setValue(that.state, null, "statuspoll"); } } }.bind(this) ); From 1c97c9c89ae933c4837e587ce9c1c4451d08f4a2 Mon Sep 17 00:00:00 2001 From: Nick Parfene Date: Sat, 28 Jan 2017 13:41:30 +1300 Subject: [PATCH 4/4] Prevent input selection from happening more than once. Fix crash when setting the switch state and powerOn was undefined. --- index.js | 33 ++++++++++++++++++++++++++++----- 1 file changed, 28 insertions(+), 5 deletions(-) diff --git a/index.js b/index.js index 2bf0c3f..e650a58 100644 --- a/index.js +++ b/index.js @@ -29,6 +29,8 @@ function HttpStatusAccessory(log, config) this.name = config["name"]; this.model = config["model"]; this.poll_status_interval = config["poll_status_interval"] || "0"; + + this.defaultInputNeedsSetting = false; this.defaultInput = config["default_input"]; this.state = false; @@ -103,6 +105,25 @@ eventSystemPower: function( response) if (this.switchService ) { this.switchService.getCharacteristic(Characteristic.On).setValue(this.state, null, "statuspoll"); } + + // If the AVR has just been turned on, apply the Input default + if (this.defaultInputNeedsSetting) { + this.log("Attempting to set the input selector to "+this.defaultInput); + if (this.state && this.defaultInput) { + this.log("Setting input selector to "+this.defaultInput); + var that = this; + this.eiscp.command("input-selector="+this.defaultInput, function(error, response) { + if (error) { + that.log( "Error while setting input: %s", error); + } + }); + } + + this.defaultInputNeedsSetting = false; + } + else { + this.log("Default input doesn't need setting"); + } }, eventVolume: function( response) @@ -147,13 +168,12 @@ setPowerState: function(powerOn, callback, context) { } } else { - // If the AVR has just been turned on, apply the Input default - if (this.state && this.defaultInput) { - this.log("Attempting to set the input selector to sat"); - this.eiscp.command("input-selector="+this.defaultInput); + if (that.defaultInput) { + that.defaultInputNeedsSetting = true; } } }.bind(this) ); + } else { this.log("setPowerState - actual mode, power state: %s, switching to OFF", that.state); this.eiscp.command("system-power=standby", function(error, response) { @@ -165,6 +185,9 @@ setPowerState: function(powerOn, callback, context) { that.switchService.getCharacteristic(Characteristic.On).setValue(that.state, null, "statuspoll"); } } + + that.defaultInputNeedsSetting = false; + }.bind(this) ); } }, @@ -195,7 +218,7 @@ getPowerState: function(callback, context) { that.state = false; that.log( "getPowerState - PWR QRY: ERROR - current state: %s", that.state); if (that.switchService ) { - that.switchService.getCharacteristic(Characteristic.On).setValue(powerOn, null, "statuspoll"); + that.switchService.getCharacteristic(Characteristic.On).setValue(that.state, null, "statuspoll"); } } }.bind(this) );