-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHomeKitHMDevice.js
39 lines (33 loc) · 1.27 KB
/
HomeKitHMDevice.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
class HomeKitHMDevice {
constructor (accessory, device) {
this.hmDevice = device
this.accessory = accessory
}
applyMappings (mappings) {
for (const [hmCharacteristic, mapping] of Object.entries(mappings)) {
const service = this.accessory.getService(mapping.service) ||
this.accessory.addService(mapping.service)
const characteristic = service.getCharacteristic(mapping.characteristic) ||
service.addCharacteristic(mapping.characteristic)
characteristic
.on('get', callback => {
const value = this.hmDevice.getValue(hmCharacteristic) || mapping.defaultValue
callback(null, value)
})
.on('set', (value, callback) => {
this.hmDevice.setValue(hmCharacteristic, value, callback)
})
}
this.hmDevice.on('update', (hmCharacteristic, value) => {
const mapping = mappings[hmCharacteristic]
if (!mapping) return
const service = this.accessory.getService(mapping.service)
const characteristic = service.getCharacteristic(mapping.characteristic)
if (mapping.valueConversion) {
value = mapping.valueConversion(value)
}
characteristic.updateValue(value)
})
}
}
module.exports = HomeKitHMDevice