add switch

This commit is contained in:
Jon Maddox
2015-09-14 01:40:03 -04:00
parent 488456c108
commit 69d948e0fa

View File

@@ -222,5 +222,76 @@ HomeAssistantLight.prototype = {
}
function HomeAssistantSwitch(log, data, client) {
// device info
this.domain = "switch"
this.data = data
this.entity_id = data.entity_id
if (data.attributes && data.attributes.friendly_name) {
this.name = data.attributes.friendly_name
}else{
this.name = data.entity_id.split('.').pop().replace(/_/g, ' ')
}
this.client = client
this.log = log;
}
HomeAssistantSwitch.prototype = {
getPowerState: function(callback){
this.log("fetching power state for: " + this.name);
this.client.fetchState(this.entity_id, function(data){
if (data) {
powerState = data.state == 'on'
callback(null, powerState)
}else{
callback(communicationError)
}
}.bind(this))
},
setPowerState: function(powerOn, callback) {
var that = this;
var service_data = {}
service_data.entity_id = this.entity_id
if (powerOn) {
this.log("Setting power state on the '"+this.name+"' to on");
this.client.callService(this.domain, 'turn_on', service_data, function(data){
if (data) {
that.log("Successfully set power state on the '"+that.name+"' to on");
callback()
}else{
callback(communicationError)
}
}.bind(this))
}else{
this.log("Setting power state on the '"+this.name+"' to off");
this.client.callService(this.domain, 'turn_off', service_data, function(data){
if (data) {
that.log("Successfully set power state on the '"+that.name+"' to off");
callback()
}else{
callback(communicationError)
}
}.bind(this))
}
},
getServices: function() {
var switchService = new Service.Switch();
switchService
.getCharacteristic(Characteristic.On)
.on('get', this.getPowerState.bind(this))
.on('set', this.setPowerState.bind(this));
return [switchService];
}
}
module.exports.accessory = HomeAssistantLight;
module.exports.accessory = HomeAssistantSwitch;
module.exports.platform = HomeAssistantPlatform;