This repository has been archived by the owner on Oct 18, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 36
/
chamberlain-accessory.js
119 lines (99 loc) · 3.37 KB
/
chamberlain-accessory.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
const _ = require('underscore');
const Api = require('./api');
const instance = require('./instance');
const ACTIVE_DELAY = 1000 * 2;
const IDLE_DELAY = 1000 * 10;
module.exports = class {
constructor(log, {deviceId, name, password, username}) {
this.log = log;
this.api = new Api({MyQDeviceId: deviceId, password, username});
const {Service, Characteristic} = instance.homebridge.hap;
const {CurrentDoorState, TargetDoorState} = Characteristic;
this.apiToHap = {
'open': CurrentDoorState.OPEN,
'closed': CurrentDoorState.CLOSED,
};
this.hapToApi = {
[TargetDoorState.OPEN]: 'open',
[TargetDoorState.CLOSED]: 'close'
};
this.hapToEnglish = {
[CurrentDoorState.OPEN]: 'open',
[CurrentDoorState.CLOSED]: 'closed',
[CurrentDoorState.OPENING]: 'opening',
[CurrentDoorState.CLOSING]: 'closing'
};
this.currentToTarget = {
[CurrentDoorState.OPEN]: TargetDoorState.OPEN,
[CurrentDoorState.CLOSED]: TargetDoorState.CLOSED,
[CurrentDoorState.OPENING]: TargetDoorState.OPEN,
[CurrentDoorState.CLOSING]: TargetDoorState.CLOSED
};
const service = this.service = new Service.GarageDoorOpener(name);
this.states = {
doorstate:
service
.getCharacteristic(Characteristic.CurrentDoorState)
.on('get', this.getCurrentDoorState.bind(this))
.on('change', this.logChange.bind(this, 'doorstate')),
desireddoorstate:
service
.getCharacteristic(Characteristic.TargetDoorState)
.on('set', this.setTargetDoorState.bind(this))
.on('change', this.logChange.bind(this, 'desireddoorstate'))
};
this.states.doorstate.value = CurrentDoorState.CLOSED;
this.states.desireddoorstate.value = TargetDoorState.CLOSED;
(this.poll = this.poll.bind(this))();
}
poll() {
clearTimeout(this.pollTimeoutId);
const {doorstate, desireddoorstate} = this.states;
return new Promise((resolve, reject) =>
doorstate.getValue(er => er ? reject(er) : resolve())
).then(() =>
doorstate.value !== desireddoorstate.value ? ACTIVE_DELAY : IDLE_DELAY
).catch(_.noop).then((delay = IDLE_DELAY) => {
clearTimeout(this.pollTimeoutId);
this.pollTimeoutId = setTimeout(this.poll, delay);
});
}
logChange(name, {oldValue, newValue}) {
const from = this.hapToEnglish[oldValue];
const to = this.hapToEnglish[newValue];
this.log.info(`${name} changed from ${from} to ${to}`);
if (name === 'doorstate') {
this.reactiveSetTargetDoorState = true;
this.states.desireddoorstate.setValue(this.currentToTarget[newValue]);
delete this.reactiveSetTargetDoorState;
}
}
getErrorHandler(cb) {
return er => {
this.log.error(er);
cb(er);
};
}
getCurrentDoorState(cb) {
return this.api.getDeviceAttribute({name: 'door_state'})
.then(value =>{
cb(null, this.apiToHap[value])
})
.catch(this.getErrorHandler(cb));
}
setTargetDoorState(value, cb) {
if (this.reactiveSetTargetDoorState) return cb();
const action_type = this.hapToApi[value];
this.targetDoorState = value;
return this.api.actOnDevice({action_type})
.then(() => {
this.poll();
this.targetDoorState = null;
cb();
})
.catch(this.getErrorHandler(cb));
}
getServices() {
return [this.service];
}
};