homebridge-nest = function (homebridge) {
Service = homebridge.hap.Service;
Characteristic = homebridge.hap.Characteristic;
Accessory = homebridge.hap.Accessory;
uuid = homebridge.hap.uuid;
/**
* Characteristic "Away"
*/
Away = function () {
Characteristic.call(this, 'Away', 'D6D47D29-4639-4F44-B53C-D84015DAEBDB');
this.setProps({
format: Characteristic.Formats.BOOL,
perms: [Characteristic.Perms.READ, Characteristic.Perms.WRITE, Characteristic.Perms.NOTIFY]
});
this.value = this.getDefaultValue();
};
inherits(Away, Characteristic);
Away.HOME = 0;
Away.AWAY = 1;
var exportedTypes = {
Accessory: Accessory,
Service: Service,
Characteristic: Characteristic,
uuid: uuid,
Away: Away
};
DeviceAccessory = require('./lib/nest-device-accessory.js')(exportedTypes);
ThermostatAccessory = require('./lib/nest-thermostat-accessory.js')(exportedTypes);
ProtectAccessory = require('./lib/nest-protect-accessory.js')(exportedTypes);
CamAccessory = require('./lib/nest-cam-accessory.js')(exportedTypes);
var acc = NestThermostatAccessory.prototype;
inherits(NestThermostatAccessory, Accessory);
NestThermostatAccessory.prototype.parent = Accessory.prototype;
for (var mn in acc) {
NestThermostatAccessory.prototype[mn] = acc[mn];
}
homebridge.registerPlatform("homebridge-nest", "Nest", NestPlatform);
}
n/a
function Connection(token, log) { this.token = token; this.log = function(info) { log(logPrefix + info); }; this.debug = function(info) { log.debug(logPrefix + info); }; this.error = function(info) { log.error(logPrefix + info); }; }
n/a
function Connection(token, log) { this.token = token; this.log = function(info) { log(logPrefix + info); }; this.debug = function(info) { log.debug(logPrefix + info); }; this.error = function(info) { log.error(logPrefix + info); }; }
n/a
auth = function (clientId, clientSecret, code) { return rp({ method: 'POST', uri: 'https://api.home.nest.com/oauth2/access_token', form: { client_id: clientId, client_secret: clientSecret, code: code, grant_type: "authorization_code" } }) .then(function (parsedBody) { var body = JSON.parse(parsedBody); this.token = body.access_token; return this.token; }.bind(this)); }
...
return;
}
var conn = new NestConnection(token, log);
if (token) {
resolve(conn)
} else {
conn.auth(clientId, clientSecret, code)
.then(function(token) {
if (log) log.warn("CODE IS ONLY VALID ONCE! Update config to use {'token':'" + token + "'
;} instead.");
resolve(conn);
})
.catch(function(err){
reject(err);
if (log) log.warn("Auth failed which likely means the code is no longer valid. Should be able to generate a new one at
" + authURL);
...
isOpen = function () { return this.conn ? true : false; }
n/a
open = function () { if (!this.token) { return Promise.reject(new Error("You must provide a token or auth before you can open a connection.")); } this.conn = new Firebase('wss://developer-api.nest.com', new Firebase.Context()); return authAsync.call(this) .then(function() { // Register the callback to be fired every time auth state changes this.conn.onAuth(authDataCallback.bind(this)); return this; }.bind(this)); }
...
var handleUpdates = function(data){
updateAccessories(data, that.accessoryLookup);
};
setupConnection(this.config, this.log)
.then(function(conn){
that.conn = conn;
return that.conn.open();
})
.then(function(){
return that.conn.subscribe(handleUpdates);
})
.then(function(data) {
that.accessoryLookup = generateAccessories(data);
if (callback) {
...
subscribe = function (handler) { var self = this; return new Promise(function (resolve, reject) { if (!handler){ reject(new Error("You must specify a handler")) } else { var notify = resolve || handler; this.conn.on('value', function (snapshot) { var data = snapshot.val(); if (data) { notify(data); notify = handler; } else { self.log("Disconnect Detected"); } }); } }.bind(this)); }
...
};
setupConnection(this.config, this.log)
.then(function(conn){
that.conn = conn;
return that.conn.open();
})
.then(function(){
return that.conn.subscribe(handleUpdates);
})
.then(function(data) {
that.accessoryLookup = generateAccessories(data);
if (callback) {
var copy = that.accessoryLookup.map(function (a) { return a; });
callback(copy);
}
...
update = function (path, data) { var child = this.conn.child(path); return Promise.fromCallback(child.set.bind(child, data)); }
...
return 'devices/' + this.deviceGroup + '/' + this.deviceId + '/' + property;
};
NestDeviceAccessory.prototype.updateDevicePropertyAsync = function(property, value, propertyDescription, valueDescription) {
propertyDescription = propertyDescription || property;
valueDescription = valueDescription || value;
this.log("Setting " + propertyDescription + " for " + this.name + " to: " + valueDescription);
return this.conn.update(this.getDevicePropertyPath(property), value)
.return(value);
};
NestDeviceAccessory.prototype.getStructurePropertyPath = function(property) {
return 'structures/' + this.structureId + '/' + property;
};
...