-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGarageDoorAccessory.js
99 lines (72 loc) · 3.63 KB
/
GarageDoorAccessory.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
const BaseAccessory = require('./BaseAccessory');
class GarageDoorAccessory extends BaseAccessory {
static getCategory(Categories) {
return Categories.GARAGE_DOOR_OPENER;
}
constructor(...props) {
super(...props);
}
_registerPlatformAccessory() {
const {Service} = this.hap;
this.accessory.addService(Service.GarageDoorOpener, this.device.context.name);
super._registerPlatformAccessory();
}
_registerCharacteristics(dps) {
const {Service, Characteristic} = this.hap;
const service = this.accessory.getService(Service.GarageDoorOpener);
this._checkServiceName(service, this.device.context.name);
this.dpAction = this._getCustomDP(this.device.context.dpAction) || '1';
this.dpStatus = this._getCustomDP(this.device.context.dpStatus) || '101';
this.currentOpen = Characteristic.CurrentDoorState.OPEN;
this.currentOpening = Characteristic.CurrentDoorState.OPENING;
this.currentClosing = Characteristic.CurrentDoorState.CLOSING;
this.currentClosed = Characteristic.CurrentDoorState.CLOSED;
this.targetOpen = Characteristic.TargetDoorState.OPEN;
this.targetClosed = Characteristic.TargetDoorState.CLOSED;
if (!!this.device.context.flipState) {
this.currentOpen = Characteristic.CurrentDoorState.CLOSED;
this.currentOpening = Characteristic.CurrentDoorState.CLOSING;
this.currentClosing = Characteristic.CurrentDoorState.OPENING;
this.currentClosed = Characteristic.CurrentDoorState.OPEN;
this.targetOpen = Characteristic.TargetDoorState.CLOSED;
this.targetClosed = Characteristic.TargetDoorState.OPEN;
}
const characteristicTargetDoorState = service.getCharacteristic(Characteristic.TargetDoorState)
.updateValue(this._getTargetDoorState(dps[this.dpStatus]))
.on('get', this.getTargetDoorState.bind(this))
.on('set', this.setTargetDoorState.bind(this));
const characteristicCurrentDoorState = service.getCharacteristic(Characteristic.CurrentDoorState)
.updateValue(this._getCurrentDoorState(dps[this.dpStatus]))
.on('get', this.getCurrentDoorState.bind(this));
this.device.on('change', (changes, state) => {
console.log('[TuyaAccessory] GarageDoor changed: ' + JSON.stringify(state));
if (changes.hasOwnProperty(this.dpStatus)) {
const newCurrentDoorState = this._getCurrentDoorState(state);
if (characteristicCurrentDoorState.value !== newCurrentDoorState) characteristicCurrentDoorState.updateValue(newCurrentDoorState);
}
});
}
getTargetDoorState(callback) {
this.getState(this.dpStatus, (err, dp) => {
if (err) return callback(err);
callback(null, this._getTargetDoorState(dp));
});
}
_getTargetDoorState(dp) {
return dp ? this.targetOpen : this.targetClosed;
}
setTargetDoorState(value, callback) {
this.setState([this.dpAction], value === this.targetOpen, callback);
}
getCurrentDoorState(callback) {
this.getState([this.dpStatus], (err, dps) => {
if (err) return callback(err);
callback(null, this._getCurrentDoorState(dps));
});
}
_getCurrentDoorState(dps) {
// ToDo: Check other `dps` for opening and closing states
return dps[this.dpStatus] ? this.currentOpen : this.currentClosed;
}
}
module.exports = GarageDoorAccessory;