Merge branch 'nfarina/master' into filedate-sensor

This commit is contained in:
S'pht'Kr
2015-09-14 19:39:38 +02:00
7 changed files with 475 additions and 186 deletions

View File

@@ -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.

View File

@@ -1,196 +1,73 @@
var types = require("HAP-NodeJS/accessories/types.js");
var Service = require('HAP-NodeJS').Service;
var Characteristic = require('HAP-NodeJS').Characteristic;
var request = require("request");
module.exports = {
accessory: LockitronAccessory
}
function LockitronAccessory(log, config) {
this.log = log;
this.name = config["name"];
this.lockID = config["lock_id"];
this.accessToken = config["api_token"];
this.lockID = config["lock_id"];
}
LockitronAccessory.prototype = {
getState: function(callback) {
this.log("Getting current state...");
var that = this;
var query = {
access_token: this.accessToken
};
request.get({
url: "https://api.lockitron.com/v2/locks/"+this.lockID,
qs: query
}, function(err, response, body) {
if (!err && response.statusCode == 200) {
var json = JSON.parse(body);
var state = json.state; // "lock" or "unlock"
var locked = state == "lock"
callback(locked);
}
else {
that.log("Error getting state (status code "+response.statusCode+"): " + err)
callback(undefined);
}
});
},
LockitronAccessory.prototype.getState = function(callback) {
this.log("Getting current state...");
setState: function(state) {
this.log("Set state to " + state);
request.get({
url: "https://api.lockitron.com/v2/locks/"+this.lockID,
qs: { access_token: this.accessToken }
}, function(err, response, body) {
if (!err && response.statusCode == 200) {
var json = JSON.parse(body);
var state = json.state; // "lock" or "unlock"
this.log("Lock state is %s", state);
var locked = state == "lock"
callback(null, locked); // success
}
else {
this.log("Error getting state (status code %s): %s", response.statusCode, err);
callback(err);
}
}.bind(this));
}
LockitronAccessory.prototype.setState = function(state, callback) {
var lockitronState = (state == 1) ? "lock" : "unlock";
var lockitronState = (state == 1) ? "lock" : "unlock";
var that = this;
this.log("Set state to %s", lockitronState);
var query = {
access_token: this.accessToken,
state: lockitronState
};
request.put({
url: "https://api.lockitron.com/v2/locks/"+this.lockID,
qs: { access_token: this.accessToken, state: lockitronState }
}, function(err, response, body) {
request.put({
url: "https://api.lockitron.com/v2/locks/"+this.lockID,
qs: query
}, function(err, response, body) {
if (!err && response.statusCode == 200) {
this.log("State change complete.");
callback(null); // success
}
else {
this.log("Error '%s' setting lock state. Response: %s", err, body);
callback(err || new Error("Error setting lock state."));
}
}.bind(this));
},
if (!err && response.statusCode == 200) {
that.log("State change complete.");
}
else {
that.log("Error '"+err+"' setting lock state: " + body);
}
});
},
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: "Apigee",
supportEvents: false,
supportBonjour: false,
manfDescription: "Manufacturer",
designedMaxLength: 255
},{
cType: types.MODEL_CTYPE,
onUpdate: null,
perms: ["pr"],
format: "string",
initialValue: "Rev-2",
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.LOCK_MECHANISM_STYPE,
characteristics: [{
cType: types.NAME_CTYPE,
onUpdate: null,
perms: ["pr"],
format: "string",
initialValue: "Lock Mechanism",
supportEvents: false,
supportBonjour: false,
manfDescription: "Name of service",
designedMaxLength: 255
},{
cType: types.CURRENT_LOCK_MECHANISM_STATE_CTYPE,
onRead: function(callback) { that.getState(callback); },
onUpdate: function(value) { that.log("Update current state to " + value); },
perms: ["pr","ev"],
format: "int",
initialValue: 0,
supportEvents: false,
supportBonjour: false,
manfDescription: "BlaBla",
designedMinValue: 0,
designedMaxValue: 3,
designedMinStep: 1,
designedMaxLength: 1
},{
cType: types.TARGET_LOCK_MECHANISM_STATE_CTYPE,
onUpdate: function(value) { that.setState(value); },
perms: ["pr","pw","ev"],
format: "int",
initialValue: 0,
supportEvents: false,
supportBonjour: false,
manfDescription: "BlaBla",
designedMinValue: 0,
designedMaxValue: 1,
designedMinStep: 1,
designedMaxLength: 1
}]
},{
sType: types.LOCK_MANAGEMENT_STYPE,
characteristics: [{
cType: types.NAME_CTYPE,
onUpdate: null,
perms: ["pr"],
format: "string",
initialValue: "Lock Management",
supportEvents: false,
supportBonjour: false,
manfDescription: "Name of service",
designedMaxLength: 255
},{
cType: types.LOCK_MANAGEMENT_CONTROL_POINT_CTYPE,
onUpdate: function(value) { that.log("Update control point to " + value); },
perms: ["pw"],
format: "data",
initialValue: 0,
supportEvents: false,
supportBonjour: false,
manfDescription: "BlaBla",
designedMaxLength: 255
},{
cType: types.VERSION_CTYPE,
onUpdate: function(value) { that.log("Update version to " + value); },
perms: ["pr"],
format: "string",
initialValue: "1.0",
supportEvents: false,
supportBonjour: false,
manfDescription: "BlaBla",
designedMaxLength: 255
}]
}];
}
};
module.exports.accessory = LockitronAccessory;
LockitronAccessory.prototype.getServices = function() {
var service = new Service.LockMechanism(this.name);
service
.getCharacteristic(Characteristic.LockCurrentState)
.on('get', this.getState.bind(this));
service
.getCharacteristic(Characteristic.LockTargetState)
.on('get', this.getState.bind(this))
.on('set', this.setState.bind(this));
return [service];
}

89
accessories/mpdclient.js Normal file
View File

@@ -0,0 +1,89 @@
var Service = require("HAP-NodeJS").Service;
var Characteristic = require("HAP-NodeJS").Characteristic;
var request = require("request");
var komponist = require('komponist')
module.exports = {
accessory: MpdClient
}
function MpdClient(log, config) {
this.log = log;
this.host = config["host"] || 'localhost';
this.port = config["port"] || 6600;
}
MpdClient.prototype = {
setPowerState: function(powerOn, callback) {
var log = this.log;
komponist.createConnection(this.port, this.host, function(error, client) {
if (error) {
return callback(error);
}
if (powerOn) {
client.play(function(error) {
log("start playing");
client.destroy();
callback(error);
});
} else {
client.stop(function(error) {
log("stop playing");
client.destroy();
callback(error);
});
}
});
},
getPowerState: function(callback) {
komponist.createConnection(this.port, this.host, function(error, client) {
if (error) {
return callback(error);
}
client.status(function(error, status) {
client.destroy();
if (status['state'] == 'play') {
callback(error, 1);
} else {
callback(error, 0);
}
});
});
},
identify: function(callback) {
this.log("Identify requested!");
callback(); // success
},
getServices: function() {
var informationService = new Service.AccessoryInformation();
informationService
.setCharacteristic(Characteristic.Manufacturer, "MPD")
.setCharacteristic(Characteristic.Model, "MPD Client")
.setCharacteristic(Characteristic.SerialNumber, "81536334");
var switchService = new Service.Switch();
switchService.getCharacteristic(Characteristic.On)
.on('get', this.getPowerState.bind(this))
.on('set', this.setPowerState.bind(this));
return [informationService, switchService];
}
};

View File

@@ -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"
}
],
@@ -183,6 +189,13 @@
"description": "Control the Hyperion TV backlight server. https://github.com/tvdzwan/hyperion",
"host": "localhost",
"port": "19444"
},
{
"accessory": "mpdclient",
"name" : "mpd",
"host" : "localhost",
"port" : 6600,
"description": "Allows some control of an MPD server"
}
]
}

View File

@@ -20,7 +20,7 @@
"harmonyhubjs-client": "^1.1.4",
"harmonyhubjs-discover": "git+https://github.com/swissmanu/harmonyhubjs-discover.git",
"lifx-api": "^1.0.1",
"lifx": "https://github.com/magicmonkey/lifxjs.git",
"lifx": "git+https://github.com/magicmonkey/lifxjs.git",
"mdns": "^2.2.4",
"node-hue-api": "^1.0.5",
"node-icontrol": "^0.1.4",
@@ -37,6 +37,7 @@
"wink-js": "0.0.5",
"xml2js": "0.4.x",
"xmldoc": "0.1.x",
"komponist" : "0.1.0",
"yamaha-nodejs": "0.4.x",
"debug": "^2.2.0"
}

304
platforms/HomeAssistant.js Normal file
View File

@@ -0,0 +1,304 @@
// 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 Service = require("HAP-NodeJS").Service;
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
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)
})
},
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.");
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)
}
}
callback(foundAccessories)
})
}
}
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) {
this.name = data.attributes.friendly_name
}else{
this.name = data.entity_id.split('.').pop().replace(/_/g, ' ')
}
this.client = client
this.log = log;
}
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(null, powerState)
}else{
callback(communicationError)
}
}.bind(this))
},
getBrightness: function(callback){
this.log("fetching brightness for: " + this.name);
this.client.fetchState(this.entity_id, function(data){
if (data && data.attributes) {
brightness = ((data.attributes.brightness || 0) / 255)*100
callback(null, brightness)
}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))
}
},
setBrightness: function(level, callback) {
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.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(communicationError)
}
}.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(Characteristic.Brightness)
.on('get', this.getBrightness.bind(this))
.on('set', this.setBrightness.bind(this));
return [lightbulbService];
}
}
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;

View File

@@ -458,7 +458,12 @@ ZWayServerAccessory.prototype = {
debug("Getting value for " + vdev.metrics.title + ", characteristic \"" + cx.displayName + "\"...");
callback(false, Characteristic.TargetHeatingCoolingState.HEAT);
});
cx.writable = false;
// Hmm... apparently if this is not setable, we can't add a thermostat change to a scene. So, make it writable but a no-op.
cx.writable = true;
cx.on('set', function(newValue, callback){
debug("WARN: Set of TargetHeatingCoolingState not yet implemented, resetting to HEAT!")
callback(undefined, Characteristic.TargetHeatingCoolingState.HEAT);
}.bind(this));
return cx;
}