-
Notifications
You must be signed in to change notification settings - Fork 187
/
Copy pathgaragedoor_accessory.js
122 lines (112 loc) · 3.21 KB
/
garagedoor_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
120
121
122
const BaseAccessory = require('./base_accessory')
let Accessory;
let Service;
let Characteristic;
let UUIDGen;
class GarageDoorAccessory extends BaseAccessory {
constructor(platform, homebridgeAccessory, deviceConfig) {
({ Accessory, Characteristic, Service } = platform.api.hap);
super(
platform,
homebridgeAccessory,
deviceConfig,
Accessory.Categories.GARAGE_DOOR_OPENER,
Service.GarageDoorOpener
);
this.statusArr = deviceConfig.status;
this.refreshAccessoryServiceIfNeed(this.statusArr, false);
}
//init Or refresh AccessoryService
refreshAccessoryServiceIfNeed(statusArr, isRefresh) {
this.isRefresh = isRefresh;
for (var statusMap of statusArr) {
var rawStatus = statusMap.value
let characteristicName
let value
switch (statusMap.code) {
case 'switch_1':
if (rawStatus) {
value = '0'
} else {
value = '1'
}
characteristicName = Characteristic.TargetDoorState
this.initAccessoryCharacteristic(characteristicName, value)
break;
case 'doorcontact_state':
if (rawStatus) {
value = '0'
} else {
value = '1'
}
characteristicName = Characteristic.CurrentDoorState
this.initAccessoryCharacteristic(characteristicName, value)
break;
case 'countdown_alarm':
value = false
characteristicName = Characteristic.ObstructionDetected
this.initAccessoryCharacteristic(characteristicName, value)
break;
default:
break;
}
}
}
initAccessoryCharacteristic(characteristicName,value){
this.setCachedState(characteristicName, value);
if (this.isRefresh) {
this.service
.getCharacteristic(characteristicName)
.updateValue(value);
} else {
this.getAccessoryCharacteristic(characteristicName);
}
}
getAccessoryCharacteristic(name) {
//set Accessory service Characteristic
this.service.getCharacteristic(name)
.on('get', callback => {
if (this.hasValidCache()) {
callback(null, this.getCachedState(name));
}
})
.on('set', (value, callback) => {
var param = this.getSendParam(name, value)
this.platform.tuyaOpenApi.sendCommand(this.deviceId, param).then(() => {
this.setCachedState(name, value);
callback();
}).catch((error) => {
this.log.error('[SET][%s] Characteristic.Brightness Error: %s', this.homebridgeAccessory.displayName, error);
this.invalidateCache();
callback(error);
});
});
}
//get Command SendData
getSendParam(name, value) {
var code;
var value;
switch (name) {
case Characteristic.TargetDoorState:
const isOn = value == '1' ? false : true;
code = "switch_1";
value = isOn;
break;
default:
break;
}
return {
"commands": [
{
"code": code,
"value": value
}
]
};
}
//update device status
updateState(data) {
this.refreshAccessoryServiceIfNeed(data.status, true);
}
}
module.exports = GarageDoorAccessory;