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

Use home status #8

Open
blakey87 opened this issue Aug 17, 2019 · 0 comments
Open

Use home status #8

blakey87 opened this issue Aug 17, 2019 · 0 comments

Comments

@blakey87
Copy link

blakey87 commented Aug 17, 2019

`var Service, Characteristic;
const { getAccessToken, getStatus, setStatus } = require('yalealarmsystem');

module.exports = function(homebridge) {
Service = homebridge.hap.Service;
Characteristic = homebridge.hap.Characteristic;

homebridge.registerAccessory("homebridge-yalealarmsystem", "YaleAlarm", YaleAlarm);

}

function YaleAlarm(log, config) {
this.log = log;
this.name = config["name"];
this.config = config;

this.service = new Service.SecuritySystem(this.name);

console.log(this.service);
this.service
.getCharacteristic(Characteristic.SecuritySystemCurrentState)
.on('get', this.getState.bind(this));

this.service
.getCharacteristic(Characteristic.SecuritySystemTargetState)
.on('get', this.getState.bind(this))
.on('set', this.setState.bind(this));

}

YaleAlarm.prototype.getState = function(callback) {
this.log("Getting current state...");

getAccessToken(
    this.config.username, 
    this.config.password
).then(getStatus).then((response) => {
    console.log(response);
    callback(null, response === 'arm');
}).catch(console.log);

}

YaleAlarm.prototype.setState = function(state, callback) {
var alarmState = null;

console.log(`Incoming Alarm state is ${state}`);	
switch(state){
	case Characteristic.SecuritySystemTargetState.STAY_ARM:
	case Characteristic.SecuritySystemTargetState.NIGHT_ARM:
		var alarmState = "home";
		break;
	case Characteristic.SecuritySystemTargetState.AWAY_ARM:
		var alarmState = "arm";
		break;
	case Characteristic.SecuritySystemTargetState.DISARM:
	default:
		var alarmState = "disarm";
		break;
	
}	
console.log(`Set Alarm state to ${alarmState}`);

getAccessToken(
    this.config.username, 
    this.config.password
).then((accessToken) => {
    setStatus(accessToken, alarmState).then((response) => { 

		var currentState = "";
		switch(state){
			case Characteristic.SecuritySystemTargetState.STAY_ARM:
				currentState = Characteristic.SecuritySystemCurrentState.STAY_ARM
				break;
			case Characteristic.SecuritySystemTargetState.NIGHT_ARM:
				currentState = Characteristic.SecuritySystemCurrentState.NIGHT_ARM
				break;
			case Characteristic.SecuritySystemTargetState.AWAY_ARM:
				currentState = Characteristic.SecuritySystemCurrentState.AWAY_ARM
				break;
			case Characteristic.SecuritySystemTargetState.DISARM:
				currentState = Characteristic.SecuritySystemCurrentState.DISARMED
				break;
			
		}			
		console.log(Characteristic.SecuritySystemCurrentState);
        this.service.setCharacteristic(Characteristic.SecuritySystemCurrentState, currentState);
    
        callback(null); // success
    }).catch((response) => {
        console.log('catch')
        console.log(response)
    });
});

}

YaleAlarm.prototype.getServices = function() {
return [this.service];
}`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant