-
Notifications
You must be signed in to change notification settings - Fork 187
/
Copy pathsmokesensor_accessory.js
95 lines (86 loc) · 2.5 KB
/
smokesensor_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
const BaseAccessory = require('./base_accessory')
let Accessory;
let Service;
let Characteristic;
let UUIDGen;
class SmokeSensorAccessory extends BaseAccessory {
constructor(platform, homebridgeAccessory, deviceConfig) {
({ Accessory, Characteristic, Service } = platform.api.hap);
super(
platform,
homebridgeAccessory,
deviceConfig,
Accessory.Categories.SENSOR,
Service.SmokeSensor
);
this.statusArr = deviceConfig.status;
this.refreshAccessoryServiceIfNeed(this.statusArr, false);
}
//init Or refresh AccessoryService
refreshAccessoryServiceIfNeed(statusArr, isRefresh) {
this.isRefresh = isRefresh
for (var statusMap of statusArr) {
if (statusMap.code === 'smoke_sensor_status') {
this.sensorStatus = statusMap
var rawStatus = this.sensorStatus.value
let status
switch (rawStatus) {
case 'alarm':
status = '1'
break;
case 'normal':
status = '0'
break;
default:
break;
}
this.setCachedState(Characteristic.SmokeDetected, status);
if (this.isRefresh) {
this.service
.getCharacteristic(Characteristic.SmokeDetected)
.updateValue(status);
} else {
this.getAccessoryCharacteristic(Characteristic.SmokeDetected);
}
}
if (statusMap.code === 'battery_state') {
this.batteryStatus = statusMap
var rawStatus = this.batteryStatus.value
let status
switch (rawStatus) {
case 'low':
status = '1'
break;
case 'middle':
case 'high':
status = '0'
break;
default:
break;
}
this.setCachedState(Characteristic.StatusLowBattery, status);
if (this.isRefresh) {
this.service
.getCharacteristic(Characteristic.StatusLowBattery)
.updateValue(status);
} else {
this.getAccessoryCharacteristic(Characteristic.StatusLowBattery);
}
}
}
}
getAccessoryCharacteristic(name) {
//set Accessory service Characteristic
this.service.getCharacteristic(name)
.on('get', callback => {
if (this.hasValidCache()) {
callback(null, this.getCachedState(name));
}
});
}
//update device status
updateState(data) {
this.refreshAccessoryServiceIfNeed(data.status, true);
}
}
module.exports = SmokeSensorAccessory;