From f5cc6cf6fb8128736506b8749ef69e66dd96f3d5 Mon Sep 17 00:00:00 2001 From: Jon Maddox Date: Sun, 13 Sep 2015 22:24:39 -0400 Subject: [PATCH 1/9] add home assistant shim --- platforms/HomeAssistant.js | 301 +++++++++++++++++++++++++++++++++++++ 1 file changed, 301 insertions(+) create mode 100644 platforms/HomeAssistant.js diff --git a/platforms/HomeAssistant.js b/platforms/HomeAssistant.js new file mode 100644 index 0000000..f2b5cd1 --- /dev/null +++ b/platforms/HomeAssistant.js @@ -0,0 +1,301 @@ +// Home Assistant +// +// Current Support: lights +// +// This is a shim to publish lights maintained by Home Assistant. +// Home Assistant is an open-source home automation platform. +// URL: http://home-assistant.io +// GitHub: https://github.com/balloob/home-assistant +// +// Remember to add platform to config.json. Example: +// "platforms": [ +// { +// "platform": "HomeAssistant", +// "name": "HomeAssistant", +// "host": "http://192.168.1.50:8123", +// "password": "xxx" +// } +// ] +// +// When you attempt to add a device, it will ask for a "PIN code". +// The default code for all HomeBridge accessories is 031-45-154. + +var types = require("HAP-NodeJS/accessories/types.js"); +var url = require('url') +var request = require("request"); + +function HomeAssistantPlatform(log, config){ + + // auth info + this.host = config["host"]; + this.password = config["password"]; + + this.log = log; +} + +HomeAssistantPlatform.prototype = { + _request: function(method, path, options, callback) { + var self = this + var requestURL = this.host + '/api' + path + options = options || {} + options.query = options.query || {} + + var reqOpts = { + url: url.parse(requestURL), + method: method || 'GET', + qs: options.query, + body: JSON.stringify(options.body), + headers: { + 'Accept': 'application/json', + 'Content-Type': 'application/json', + 'x-ha-access': this.password + } + } + + request(reqOpts, function onResponse(error, response, body) { + if (error) { + callback(error, response) + return + } + + if (response.statusCode === 401) { + callback(new Error('You are not authenticated'), response) + return + } + + json = JSON.parse(body) + callback(error, response, json) + }) + + }, + accessories: function(callback) { + this.log("Fetching HomeAssistant devices."); + + var that = this; + var foundAccessories = []; + var lightsRE = /^light\./i + + + this._request('GET', '/states', {}, function(error, response, data){ + + for (var i = 0; i < data.length; i++) { + entity = data[i] + + if (entity.entity_id.match(lightsRE)) { + accessory = new HomeAssistantAccessory(that.log, entity, that) + foundAccessories.push(accessory) + } + } + + callback(foundAccessories) + }) + + } +} + +function HomeAssistantAccessory(log, data, client) { + // device info + this.data = data + this.entity_id = data.entity_id + this.name = data.attributes.friendly_name + + this.client = client + this.log = log; +} + +HomeAssistantAccessory.prototype = { + _callService: function(service, service_data, callback){ + var options = {} + options.body = service_data + + this.client._request('POST', '/services/light/' + service, options, function(error, response, data){ + if (error) { + callback(null) + }else{ + callback(data) + } + }) + }, + _fetchState: function(callback){ + this.client._request('GET', '/states/' + this.entity_id, {}, function(error, response, data){ + if (error) { + callback(null) + }else{ + callback(data) + } + }) + }, + getPowerState: function(callback){ + this.log("fetching power state for: " + this.name); + this._fetchState(function(data){ + if (data) { + powerState = data.state == 'on' + callback(powerState) + }else{ + callback(null) + } + }) + }, + getBrightness: function(callback){ + this.log("fetching brightness for: " + this.name); + that = this + this._fetchState(function(data){ + if (data && data.attributes) { + that.log('returned brightness: ' + data.attributes.brightness) + brightness = ((data.attributes.brightness || 0) / 255)*100 + callback(brightness) + }else{ + callback(null) + } + }) + }, + setPowerState: function(powerOn) { + 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._callService('turn_on', service_data, function(data){ + if (data) { + that.log("Successfully set power state on the '"+that.name+"' to on"); + } + }) + }else{ + this.log("Setting power state on the '"+this.name+"' to off"); + + this._callService('turn_off', service_data, function(data){ + if (data) { + that.log("Successfully set power state on the '"+that.name+"' to off"); + } + }) + } + }, + setBrightness: function(level) { + var that = this; + var service_data = {} + service_data.entity_id = this.entity_id + + service_data.brightness = 255*(level/100.0) + + this.log("Setting brightness on the '"+this.name+"' to " + level); + + this._callService('turn_on', service_data, function(data){ + if (data) { + that.log("Successfully set brightness on the '"+that.name+"' to " + level); + } + }) + }, + getServices: function() { + var that = this; + return [{ + sType: types.ACCESSORY_INFORMATION_STYPE, + characteristics: [{ + cType: types.NAME_CTYPE, + onUpdate: null, + perms: ["pr"], + format: "string", + initialValue: this.name, + supportEvents: false, + supportBonjour: false, + manfDescription: "Name of the accessory", + designedMaxLength: 255 + },{ + cType: types.MANUFACTURER_CTYPE, + onUpdate: null, + perms: ["pr"], + format: "string", + initialValue: "HomeAssistant", + supportEvents: false, + supportBonjour: false, + manfDescription: "Manufacturer", + designedMaxLength: 255 + },{ + cType: types.MODEL_CTYPE, + onUpdate: null, + perms: ["pr"], + format: "string", + initialValue: "Rev-1", + supportEvents: false, + supportBonjour: false, + manfDescription: "Model", + designedMaxLength: 255 + },{ + cType: types.SERIAL_NUMBER_CTYPE, + onUpdate: null, + perms: ["pr"], + format: "string", + initialValue: "A1S2NASF88EW", + supportEvents: false, + supportBonjour: false, + manfDescription: "SN", + designedMaxLength: 255 + },{ + cType: types.IDENTIFY_CTYPE, + onUpdate: null, + perms: ["pw"], + format: "bool", + initialValue: false, + supportEvents: false, + supportBonjour: false, + manfDescription: "Identify Accessory", + designedMaxLength: 1 + }] + },{ + sType: types.LIGHTBULB_STYPE, + characteristics: [{ + cType: types.NAME_CTYPE, + onUpdate: null, + perms: ["pr"], + format: "string", + initialValue: this.name, + supportEvents: true, + supportBonjour: false, + manfDescription: "Name of service", + designedMaxLength: 255 + },{ + cType: types.POWER_STATE_CTYPE, + onUpdate: function(value) { + that.setPowerState(value); + }, + onRead: function(callback) { + that.getPowerState(function(powerState){ + callback(powerState); + }); + }, + perms: ["pw","pr","ev"], + format: "bool", + initialValue: 0, + supportEvents: true, + supportBonjour: false, + manfDescription: "Change the power state of the Bulb", + designedMaxLength: 1 + },{ + cType: types.BRIGHTNESS_CTYPE, + onUpdate: function(value) { + that.setBrightness(value); + }, + onRead: function(callback) { + that.getBrightness(function(level){ + callback(level); + }); + }, + perms: ["pw","pr","ev"], + format: "int", + initialValue: 0, + supportEvents: true, + supportBonjour: false, + manfDescription: "Adjust Brightness of Light", + designedMinValue: 0, + designedMaxValue: 255, + designedMinStep: 1, + unit: "%" + }] + }]; + } + +} + +module.exports.accessory = HomeAssistantAccessory; +module.exports.platform = HomeAssistantPlatform; From ec550d1638d17326723aa687e7551b1010596396 Mon Sep 17 00:00:00 2001 From: Jon Maddox Date: Sun, 13 Sep 2015 22:24:54 -0400 Subject: [PATCH 2/9] add sample config for home assistant --- config-sample.json | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/config-sample.json b/config-sample.json index afb2893..58a1f4b 100644 --- a/config-sample.json +++ b/config-sample.json @@ -89,6 +89,12 @@ "delay": 30, "repeat": 3, "zones":["Kitchen Lamp","Bedroom Lamp","Living Room Lamp","Hallway Lamp"] + }, + { + "platform": "HomeAssistant", + "name": "HomeAssistant", + "host": "http://192.168.1.10:8123", + "password": "XXXXX" } ], From a6d61cc93afab8b86cde6c60534881e7f450c9dc Mon Sep 17 00:00:00 2001 From: Jon Maddox Date: Sun, 13 Sep 2015 22:30:47 -0400 Subject: [PATCH 3/9] add link to HA in readme --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index f11dd75..641e32f 100644 --- a/README.md +++ b/README.md @@ -11,7 +11,7 @@ Since Siri supports devices added through HomeKit, this means that with Homebrid * _Siri, turn off the Speakers._ ([Sonos](http://www.sonos.com)) * _Siri, turn on the Dehumidifier._ ([WeMo](http://www.belkin.com/us/Products/home-automation/c/wemo-home-automation/)) * _Siri, turn on Away Mode._ ([Xfinity Home](http://www.comcast.com/home-security.html)) - * _Siri, turn on the living room lights._ ([Wink](http://www.wink.com), [SmartThings](http://www.smartthings.com), [X10](http://github.com/edc1591/rest-mochad), [Philips Hue](http://meethue.com), [LimitlessLED/MiLight/Easybulb](http://www.limitlessled.com/), [LIFx](http://www.lifx.com/)) + * _Siri, turn on the living room lights._ ([Wink](http://www.wink.com), [SmartThings](http://www.smartthings.com), [X10](http://github.com/edc1591/rest-mochad), [Philips Hue](http://meethue.com), [Home Assistant](http://home-assistant.io) [LimitlessLED/MiLight/Easybulb](http://www.limitlessled.com/), [LIFx](http://www.lifx.com/)) * _Siri, set the movie scene._ ([Logitech Harmony](http://myharmony.com/)) If you would like to support any other devices, please write a shim and create a pull request and I'd be happy to add it to this official list. From 167a983068000b9f26cab30f726285777fe04b1f Mon Sep 17 00:00:00 2001 From: Jon Maddox Date: Mon, 14 Sep 2015 00:14:02 -0400 Subject: [PATCH 4/9] handle missing friendly name --- platforms/HomeAssistant.js | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/platforms/HomeAssistant.js b/platforms/HomeAssistant.js index f2b5cd1..ce7fc3d 100644 --- a/platforms/HomeAssistant.js +++ b/platforms/HomeAssistant.js @@ -97,7 +97,11 @@ function HomeAssistantAccessory(log, data, client) { // device info this.data = data this.entity_id = data.entity_id - this.name = data.attributes.friendly_name + 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; From 544124fbabdc803ab5e245f4d2c8a52fca2a1c94 Mon Sep 17 00:00:00 2001 From: Jon Maddox Date: Mon, 14 Sep 2015 00:20:28 -0400 Subject: [PATCH 5/9] clean it up and get on that modern tip --- platforms/HomeAssistant.js | 116 ++++--------------------------------- 1 file changed, 11 insertions(+), 105 deletions(-) diff --git a/platforms/HomeAssistant.js b/platforms/HomeAssistant.js index ce7fc3d..727cc77 100644 --- a/platforms/HomeAssistant.js +++ b/platforms/HomeAssistant.js @@ -192,111 +192,17 @@ HomeAssistantAccessory.prototype = { }) }, getServices: function() { - var that = this; - return [{ - sType: types.ACCESSORY_INFORMATION_STYPE, - characteristics: [{ - cType: types.NAME_CTYPE, - onUpdate: null, - perms: ["pr"], - format: "string", - initialValue: this.name, - supportEvents: false, - supportBonjour: false, - manfDescription: "Name of the accessory", - designedMaxLength: 255 - },{ - cType: types.MANUFACTURER_CTYPE, - onUpdate: null, - perms: ["pr"], - format: "string", - initialValue: "HomeAssistant", - supportEvents: false, - supportBonjour: false, - manfDescription: "Manufacturer", - designedMaxLength: 255 - },{ - cType: types.MODEL_CTYPE, - onUpdate: null, - perms: ["pr"], - format: "string", - initialValue: "Rev-1", - supportEvents: false, - supportBonjour: false, - manfDescription: "Model", - designedMaxLength: 255 - },{ - cType: types.SERIAL_NUMBER_CTYPE, - onUpdate: null, - perms: ["pr"], - format: "string", - initialValue: "A1S2NASF88EW", - supportEvents: false, - supportBonjour: false, - manfDescription: "SN", - designedMaxLength: 255 - },{ - cType: types.IDENTIFY_CTYPE, - onUpdate: null, - perms: ["pw"], - format: "bool", - initialValue: false, - supportEvents: false, - supportBonjour: false, - manfDescription: "Identify Accessory", - designedMaxLength: 1 - }] - },{ - sType: types.LIGHTBULB_STYPE, - characteristics: [{ - cType: types.NAME_CTYPE, - onUpdate: null, - perms: ["pr"], - format: "string", - initialValue: this.name, - supportEvents: true, - supportBonjour: false, - manfDescription: "Name of service", - designedMaxLength: 255 - },{ - cType: types.POWER_STATE_CTYPE, - onUpdate: function(value) { - that.setPowerState(value); - }, - onRead: function(callback) { - that.getPowerState(function(powerState){ - callback(powerState); - }); - }, - perms: ["pw","pr","ev"], - format: "bool", - initialValue: 0, - supportEvents: true, - supportBonjour: false, - manfDescription: "Change the power state of the Bulb", - designedMaxLength: 1 - },{ - cType: types.BRIGHTNESS_CTYPE, - onUpdate: function(value) { - that.setBrightness(value); - }, - onRead: function(callback) { - that.getBrightness(function(level){ - callback(level); - }); - }, - perms: ["pw","pr","ev"], - format: "int", - initialValue: 0, - supportEvents: true, - supportBonjour: false, - manfDescription: "Adjust Brightness of Light", - designedMinValue: 0, - designedMaxValue: 255, - designedMinStep: 1, - unit: "%" - }] - }]; + var lightbulbService = new Service.Lightbulb(); + + lightbulbService + .getCharacteristic(Characteristic.On) + .on('set', this.setPowerState.bind(this)); + + lightbulbService + .addCharacteristic(new Characteristic.Brightness()) + .on('set', this.setBrightness.bind(this)); + + return [lightbulbService]; } } From 025bca7a4357c1f516305fe77fc96e30090198e4 Mon Sep 17 00:00:00 2001 From: Jon Maddox Date: Mon, 14 Sep 2015 01:16:34 -0400 Subject: [PATCH 6/9] factor things out of the accessory and make it a Light --- platforms/HomeAssistant.js | 91 ++++++++++++++++++++++---------------- 1 file changed, 52 insertions(+), 39 deletions(-) diff --git a/platforms/HomeAssistant.js b/platforms/HomeAssistant.js index 727cc77..a081adf 100644 --- a/platforms/HomeAssistant.js +++ b/platforms/HomeAssistant.js @@ -20,7 +20,8 @@ // When you attempt to add a device, it will ask for a "PIN code". // The default code for all HomeBridge accessories is 031-45-154. -var types = require("HAP-NodeJS/accessories/types.js"); +var Service = require("HAP-NodeJS").Service; +var Characteristic = require("HAP-NodeJS").Characteristic; var url = require('url') var request = require("request"); @@ -68,6 +69,27 @@ HomeAssistantPlatform.prototype = { }) }, + fetchState: function(entity_id, callback){ + this._request('GET', '/states/' + entity_id, {}, function(error, response, data){ + if (error) { + callback(null) + }else{ + callback(data) + } + }) + }, + callService: function(domain, service, service_data, callback){ + var options = {} + options.body = service_data + + this._request('POST', '/services/' + domain + '/' + service, options, function(error, response, data){ + if (error) { + callback(null) + }else{ + callback(data) + } + }) + }, accessories: function(callback) { this.log("Fetching HomeAssistant devices."); @@ -82,7 +104,7 @@ HomeAssistantPlatform.prototype = { entity = data[i] if (entity.entity_id.match(lightsRE)) { - accessory = new HomeAssistantAccessory(that.log, entity, that) + accessory = new HomeAssistantLight(that.log, entity, that) foundAccessories.push(accessory) } } @@ -93,8 +115,9 @@ HomeAssistantPlatform.prototype = { } } -function HomeAssistantAccessory(log, data, client) { +function HomeAssistantLight(log, data, client) { // device info + this.domain = "light" this.data = data this.entity_id = data.entity_id if (data.attributes && data.attributes.friendly_name) { @@ -107,43 +130,22 @@ function HomeAssistantAccessory(log, data, client) { this.log = log; } -HomeAssistantAccessory.prototype = { - _callService: function(service, service_data, callback){ - var options = {} - options.body = service_data - - this.client._request('POST', '/services/light/' + service, options, function(error, response, data){ - if (error) { - callback(null) - }else{ - callback(data) - } - }) - }, - _fetchState: function(callback){ - this.client._request('GET', '/states/' + this.entity_id, {}, function(error, response, data){ - if (error) { - callback(null) - }else{ - callback(data) - } - }) - }, +HomeAssistantLight.prototype = { getPowerState: function(callback){ this.log("fetching power state for: " + this.name); - this._fetchState(function(data){ + this.client.fetchState(this.entity_id, function(data){ if (data) { powerState = data.state == 'on' callback(powerState) }else{ callback(null) } - }) + }.bind(this)) }, getBrightness: function(callback){ this.log("fetching brightness for: " + this.name); that = this - this._fetchState(function(data){ + this.client.fetchState(this.entity_id, function(data){ if (data && data.attributes) { that.log('returned brightness: ' + data.attributes.brightness) brightness = ((data.attributes.brightness || 0) / 255)*100 @@ -151,9 +153,9 @@ HomeAssistantAccessory.prototype = { }else{ callback(null) } - }) + }.bind(this)) }, - setPowerState: function(powerOn) { + setPowerState: function(powerOn, callback) { var that = this; var service_data = {} service_data.entity_id = this.entity_id @@ -161,22 +163,28 @@ HomeAssistantAccessory.prototype = { if (powerOn) { this.log("Setting power state on the '"+this.name+"' to on"); - this._callService('turn_on', service_data, function(data){ + 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(new Error('Can not communicate with Home Assistant.')) } - }) + }.bind(this)) }else{ this.log("Setting power state on the '"+this.name+"' to off"); - this._callService('turn_off', service_data, function(data){ + 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(new Error('Can not communicate with Home Assistant.')) } - }) + }.bind(this)) } }, - setBrightness: function(level) { + setBrightness: function(level, callback) { var that = this; var service_data = {} service_data.entity_id = this.entity_id @@ -185,21 +193,26 @@ HomeAssistantAccessory.prototype = { this.log("Setting brightness on the '"+this.name+"' to " + level); - this._callService('turn_on', service_data, function(data){ + this.client.callService(this.domain, 'turn_on', service_data, function(data){ if (data) { that.log("Successfully set brightness on the '"+that.name+"' to " + level); + callback() + }else{ + callback(new Error('Can not communicate with Home Assistant.')) } - }) + }.bind(this)) }, getServices: function() { var lightbulbService = new Service.Lightbulb(); lightbulbService .getCharacteristic(Characteristic.On) + .on('get', this.getPowerState.bind(this)) .on('set', this.setPowerState.bind(this)); lightbulbService - .addCharacteristic(new Characteristic.Brightness()) + .addCharacteristic(Characteristic.Brightness) + .on('get', this.getBrightness.bind(this)) .on('set', this.setBrightness.bind(this)); return [lightbulbService]; @@ -207,5 +220,5 @@ HomeAssistantAccessory.prototype = { } -module.exports.accessory = HomeAssistantAccessory; +module.exports.accessory = HomeAssistantLight; module.exports.platform = HomeAssistantPlatform; From 488456c1081d688e55220337baaf8570ae9369c2 Mon Sep 17 00:00:00 2001 From: Jon Maddox Date: Mon, 14 Sep 2015 01:30:55 -0400 Subject: [PATCH 7/9] ohhhhhhh the callback signature --- platforms/HomeAssistant.js | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/platforms/HomeAssistant.js b/platforms/HomeAssistant.js index a081adf..f08ddfb 100644 --- a/platforms/HomeAssistant.js +++ b/platforms/HomeAssistant.js @@ -25,6 +25,8 @@ var Characteristic = require("HAP-NodeJS").Characteristic; var url = require('url') var request = require("request"); +var communicationError = new Error('Can not communicate with Home Assistant.') + function HomeAssistantPlatform(log, config){ // auth info @@ -133,25 +135,25 @@ function HomeAssistantLight(log, data, client) { HomeAssistantLight.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(powerState) + callback(null, powerState) }else{ - callback(null) + callback(communicationError) } }.bind(this)) }, getBrightness: function(callback){ this.log("fetching brightness for: " + this.name); - that = this + this.client.fetchState(this.entity_id, function(data){ if (data && data.attributes) { - that.log('returned brightness: ' + data.attributes.brightness) brightness = ((data.attributes.brightness || 0) / 255)*100 - callback(brightness) + callback(null, brightness) }else{ - callback(null) + callback(communicationError) } }.bind(this)) }, @@ -168,7 +170,7 @@ HomeAssistantLight.prototype = { that.log("Successfully set power state on the '"+that.name+"' to on"); callback() }else{ - callback(new Error('Can not communicate with Home Assistant.')) + callback(communicationError) } }.bind(this)) }else{ @@ -179,7 +181,7 @@ HomeAssistantLight.prototype = { that.log("Successfully set power state on the '"+that.name+"' to off"); callback() }else{ - callback(new Error('Can not communicate with Home Assistant.')) + callback(communicationError) } }.bind(this)) } @@ -198,7 +200,7 @@ HomeAssistantLight.prototype = { that.log("Successfully set brightness on the '"+that.name+"' to " + level); callback() }else{ - callback(new Error('Can not communicate with Home Assistant.')) + callback(communicationError) } }.bind(this)) }, From 69d948e0fae82393a409427370d991b3bb941d7c Mon Sep 17 00:00:00 2001 From: Jon Maddox Date: Mon, 14 Sep 2015 01:40:03 -0400 Subject: [PATCH 8/9] add switch --- platforms/HomeAssistant.js | 71 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) diff --git a/platforms/HomeAssistant.js b/platforms/HomeAssistant.js index f08ddfb..6440728 100644 --- a/platforms/HomeAssistant.js +++ b/platforms/HomeAssistant.js @@ -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; From c1e3d45fa1755c838545ba8db409a4f6f1f18a81 Mon Sep 17 00:00:00 2001 From: Jon Maddox Date: Mon, 14 Sep 2015 01:40:09 -0400 Subject: [PATCH 9/9] scan in switches --- platforms/HomeAssistant.js | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/platforms/HomeAssistant.js b/platforms/HomeAssistant.js index 6440728..d9984c8 100644 --- a/platforms/HomeAssistant.js +++ b/platforms/HomeAssistant.js @@ -98,15 +98,22 @@ HomeAssistantPlatform.prototype = { var that = this; var foundAccessories = []; var lightsRE = /^light\./i + var switchRE = /^switch\./i this._request('GET', '/states', {}, function(error, response, data){ for (var i = 0; i < data.length; i++) { entity = data[i] + var accessory = null if (entity.entity_id.match(lightsRE)) { accessory = new HomeAssistantLight(that.log, entity, that) + }else if (entity.entity_id.match(switchRE)){ + accessory = new HomeAssistantSwitch(that.log, entity, that) + } + + if (accessory) { foundAccessories.push(accessory) } }