mirror of
https://github.com/mtan93/homebridge.git
synced 2026-04-22 22:05:42 +01:00
Upgrade Lockitron accessory to new API
This commit is contained in:
@@ -1,196 +1,72 @@
|
|||||||
var types = require("HAP-NodeJS/accessories/types.js");
|
var Service = require('HAP-NodeJS').Service;
|
||||||
|
var Characteristic = require('HAP-NodeJS').Characteristic;
|
||||||
var request = require("request");
|
var request = require("request");
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
accessory: LockitronAccessory
|
||||||
|
}
|
||||||
|
|
||||||
function LockitronAccessory(log, config) {
|
function LockitronAccessory(log, config) {
|
||||||
this.log = log;
|
this.log = log;
|
||||||
this.name = config["name"];
|
|
||||||
this.lockID = config["lock_id"];
|
|
||||||
this.accessToken = config["api_token"];
|
this.accessToken = config["api_token"];
|
||||||
|
this.lockID = config["lock_id"];
|
||||||
}
|
}
|
||||||
|
|
||||||
LockitronAccessory.prototype = {
|
LockitronAccessory.prototype.getState = function(callback) {
|
||||||
getState: function(callback) {
|
this.log("Getting current state...");
|
||||||
this.log("Getting current state...");
|
|
||||||
|
|
||||||
var that = this;
|
request.get({
|
||||||
|
url: "https://api.lockitron.com/v2/locks/"+this.lockID,
|
||||||
|
qs: { access_token: this.accessToken }
|
||||||
|
}, function(err, response, body) {
|
||||||
|
|
||||||
var query = {
|
if (!err && response.statusCode == 200) {
|
||||||
access_token: this.accessToken
|
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));
|
||||||
|
}
|
||||||
|
|
||||||
request.get({
|
LockitronAccessory.prototype.setState = function(state, callback) {
|
||||||
url: "https://api.lockitron.com/v2/locks/"+this.lockID,
|
var lockitronState = (state == 1) ? "lock" : "unlock";
|
||||||
qs: query
|
|
||||||
}, function(err, response, body) {
|
|
||||||
|
|
||||||
if (!err && response.statusCode == 200) {
|
this.log("Set state to %s", lockitronState);
|
||||||
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);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
},
|
|
||||||
|
|
||||||
setState: function(state) {
|
request.put({
|
||||||
this.log("Set state to " + state);
|
url: "https://api.lockitron.com/v2/locks/"+this.lockID,
|
||||||
|
qs: { access_token: this.accessToken, state: lockitronState }
|
||||||
|
}, function(err, response, body) {
|
||||||
|
|
||||||
var lockitronState = (state == 1) ? "lock" : "unlock";
|
if (!err && response.statusCode == 200) {
|
||||||
var that = this;
|
this.log("State change complete.");
|
||||||
|
callback(null); // success
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
this.log("Error '%s' setting lock state. Response: %s", err.message, body);
|
||||||
|
callback(err);
|
||||||
|
}
|
||||||
|
}.bind(this));
|
||||||
|
},
|
||||||
|
|
||||||
var query = {
|
LockitronAccessory.prototype.getServices = function() {
|
||||||
access_token: this.accessToken,
|
|
||||||
state: lockitronState
|
|
||||||
};
|
|
||||||
|
|
||||||
request.put({
|
var service = new Service.LockMechanism(this.name);
|
||||||
url: "https://api.lockitron.com/v2/locks/"+this.lockID,
|
|
||||||
qs: query
|
|
||||||
}, function(err, response, body) {
|
|
||||||
|
|
||||||
if (!err && response.statusCode == 200) {
|
service
|
||||||
that.log("State change complete.");
|
.getCharacteristic(Characteristic.LockCurrentState)
|
||||||
}
|
.on('get', this.getState.bind(this));
|
||||||
else {
|
|
||||||
that.log("Error '"+err+"' setting lock state: " + body);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
},
|
|
||||||
|
|
||||||
getServices: function() {
|
service
|
||||||
var that = this;
|
.getCharacteristic(Characteristic.LockTargetState)
|
||||||
return [{
|
.on('get', this.getState.bind(this))
|
||||||
sType: types.ACCESSORY_INFORMATION_STYPE,
|
.on('set', this.setState.bind(this));
|
||||||
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;
|
return [service];
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user