Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue When Sending Siri Commands To Nest Thermostat Through Homebridge-Nest #132 #136

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 39 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
var nest = require('unofficial-nest-api');
var NestConnection = require('./lib/nest-connection.js');
var inherits = require('util').inherits;
var fs = require('fs');
var readline = require('readline');
let CONFIG_PATH = process.env.HOME +'lastMode.conf'
var previousMode;

var Service, Characteristic, Accessory, uuid, Away;
var DeviceAccessory, ThermostatAccessory, ProtectAccessory, CamAccessory;
Expand Down Expand Up @@ -301,15 +305,35 @@ function NestThermostatAccessory(log, name, device, deviceId, initialData, struc
.on('get', function (callback) {
var targetTemp = this.getTargetTemperature();
this.log("Target temperature for " + this.name + " is: " + targetTemp);
var targetHeatingCooling = this.getTargetHeatingCooling();
this.log("Target heating for " + this.name + " is: " + targetHeatingCooling);
if (targetHeatingCooling != 'Off') {
fs.writeFile(CONFIG_PATH, targetHeatingCooling, encoding='utf8', function(err) {
if(err) {
return this.log(err);
}
this.previousMode = targetHeatingCooling;
this.log("config was saved!");
});
}
if (callback) callback(null, targetTemp);
}.bind(this))
.on('set', this.setTargetTemperature.bind(this));
.on('set', this.setTargetTemperature.bind(this));

this.getService(Service.Thermostat)
.getCharacteristic(Characteristic.TargetHeatingCoolingState)
.on('get', function (callback) {
var targetHeatingCooling = this.getTargetHeatingCooling();
this.log("Target heating for " + this.name + " is: " + targetHeatingCooling);
if (targetHeatingCooling != 'Off') {
fs.writeFile(CONFIG_PATH, targetHeatingCooling, encoding='utf8', function(err) {
if(err) {
return this.log(err);
}
this.previousMode = targetHeatingCooling;
this.log("config was saved!");
});
}
if (callback) callback(null, targetHeatingCooling);
}.bind(this))
.on('set', this.setTargetHeatingCooling.bind(this));
Expand Down Expand Up @@ -427,8 +451,21 @@ NestThermostatAccessory.prototype.setTargetHeatingCooling = function (targetHeat
}

this.log("Setting target heating cooling for " + this.name + " to: " + targetTemperatureType);
nest.setTargetTemperatureType(this.deviceId, targetTemperatureType);
try {
var data = fs.readFileSync(CONFIG_PATH, 'utf8');
this.previousMode = data;
this.log(data);
} catch(e) {
this.log('Error:', e.stack);
}

if (this.getCurrentHeatingCooling() == this.previousMode){
nest.setTargetTemperatureType(this.deviceId, this.getTargetHeatingCooling());
}
else {
nest.setTargetTemperatureType(this.deviceId, this.previousMode);
}

if (callback) callback(null, targetTemperatureType);
};

Expand Down
14 changes: 12 additions & 2 deletions lib/nest-thermostat-accessory.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
* Created by kraigm on 12/15/15.
*/

var Promise = require('bluebird');
var debounce = require('lodash.debounce');
var inherits = require('util').inherits;
var Accessory, Service, Characteristic, uuid;
var NestDeviceAccessory = require('./nest-device-accessory')();
Expand Down Expand Up @@ -179,7 +181,15 @@ NestThermostatAccessory.prototype.setTargetHeatingCooling = function (targetHeat
.asCallback(callback);
};

NestThermostatAccessory.prototype.setTargetTemperature = function (targetTemperature, callback) {
NestThermostatAccessory.prototype.setTargetTemperature = function(targetTemperature, callback) {
this.log('Trying to set temperature ' + targetTemperature);
this.setTargetTemperatureDebounced(targetTemperature, function() {
this.log('Temperature set to ' + targetTemperature);
}.bind(this));
return Promise.resolve().asCallback(callback);
};

NestThermostatAccessory.prototype.setTargetTemperatureDebounced = debounce(function (targetTemperature, callback) {
var usesFahrenheit = this.usesFahrenheit();
if (usesFahrenheit) {
// Convert to Fahrenheit and round to nearest integer
Expand Down Expand Up @@ -213,7 +223,7 @@ NestThermostatAccessory.prototype.setTargetTemperature = function (targetTempera
return this.cancelAutoAway()
.then(this.updateDevicePropertyAsync.bind(this, key, targetTemperature, prop + "target temperature"))
.asCallback(callback);
};
}, 5000);

NestThermostatAccessory.prototype.usesFahrenheit = function () {
return this.getTemperatureUnits() == Characteristic.TemperatureDisplayUnits.FAHRENHEIT;
Expand Down
11 changes: 11 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@
"homebridge": ">=0.2.5"
},
"dependencies": {
"firebase": "^2.3.2",
"bluebird": "^3.2.2",
"firebase": "^2.3.2",
"lodash.debounce": "^4.0.8",
"request-promise": "^1.0.2",
"unofficial-nest-api": "git+https://github.com/kraigm/unofficial_nodejs_nest.git#3cbd337adc32fab3b481659b38d86f9fcd6a9c02"
}
Expand Down
Loading