-
Notifications
You must be signed in to change notification settings - Fork 187
/
Copy pathoutlet_accessory.js
108 lines (95 loc) · 2.7 KB
/
outlet_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
const BaseAccessory = require('./base_accessory')
let PlatformAccessory;
let Accessory;
let Service;
let Characteristic;
let UUIDGen;
class OutletAccessory extends BaseAccessory {
constructor(platform, homebridgeAccessory, deviceConfig, deviceData) {
({ Accessory, Characteristic, Service } = platform.api.hap);
super(
platform,
homebridgeAccessory,
deviceConfig,
Accessory.Categories.OUTLET,
Service.Outlet,
deviceData.subType
)
this.statusArr = deviceConfig.status;
this.subTypeArr = deviceData.subType;
this.refreshAccessoryServiceIfNeed(this.statusArr, false);
}
//init Or refresh AccessoryService
refreshAccessoryServiceIfNeed(statusArr, isRefresh) {
this.isRefresh = isRefresh;
if (!statusArr) {
return;
}
for (var subType of this.subTypeArr) {
var status = statusArr.find(item => item.code === subType)
if (!status) {
continue;
}
var value = status.value
let service
if (this.subTypeArr.length == 1) {
service = this.service;
this.switchValue = status;
}else{
service = this.homebridgeAccessory.getService(subType);
}
this.setCachedState(service.displayName, value);
if (this.isRefresh) {
service
.getCharacteristic(Characteristic.On)
.updateValue(value);
} else {
this.getAccessoryCharacteristic(service, Characteristic.On);
}
}
}
getAccessoryCharacteristic(service, name) {
//set Accessory service Characteristic
service.getCharacteristic(name)
.on('get', callback => {
if (this.hasValidCache()) {
callback(null, this.getCachedState(service.displayName));
}
})
.on('set', (value, callback) => {
var param = this.getSendParam(service.displayName, value)
this.platform.tuyaOpenApi.sendCommand(this.deviceId, param).then(() => {
this.setCachedState(service.displayName, 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;
const isOn = value ? true : false;
if (this.subTypeArr.length == 1) {
code = this.switchValue.code;
}else{
code = name;
}
value = isOn;
return {
"commands": [
{
"code": code,
"value": value
}
]
};
}
updateState(data) {
this.refreshAccessoryServiceIfNeed(data.status, true);
}
}
module.exports = OutletAccessory;