-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
executable file
·67 lines (58 loc) · 1.99 KB
/
index.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
const AVReceiver = require('marantz-avr-fix')
let Service, Characteristic
module.exports = function (homebridge) {
Service = homebridge.hap.Service
Characteristic = homebridge.hap.Characteristic
homebridge.registerAccessory("homebridge-denon-switch", "denon-switch", Denon)
}
class Denon {
constructor(log, config) {
// Setup configuration
this.log = log
this.name = config['name'] || 'Scenario'
if (!config['ip']) {
this.log.error('No ip define for', this.name)
return
}
this.ip = config['ip']
this.defaultVolume = config['defaultVolume'] || 35
this.defaultInput = config['defaultInput'] || 'DVD'
this.receiver = new AVReceiver(this.ip)
// Setup services
this.switchService = new Service.Switch(this.name)
this.switchService.getCharacteristic(Characteristic.On)
.on('get', this.getState.bind(this))
.on('set', this.setState.bind(this))
}
async getState(callback) {
this.log('Getting current state..')
try {
const state = await this.receiver.getState()
const isScenarioActive = state.power === true && state.input === this.defaultInput
this.log(`Scenario is ${isScenarioActive}`)
callback(null, isScenarioActive)
} catch (e) {
this.log.error(e)
callback(e)
}
}
async setState(state, callback) {
this.log(`Set state to ${state}`)
try {
if (state) {
await this.receiver.setPowerState(true)
await this.receiver.setVolumeLevel(this.defaultVolume)
await this.receiver.setInputSource(this.defaultInput)
} else {
await this.receiver.setPowerState(false)
}
callback(null)
} catch (e) {
this.log.error(e)
callback(e)
}
}
getServices() {
return [this.switchService]
}
}