forked from mmende/homebridge-samsungtv-control
-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
152 lines (120 loc) · 5.48 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
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
const samsungAPI = require('./lib/samsungAPI').SamsungAPI;
let Service, Characteristic;
module.exports = function (homebridge) {
Service = homebridge.hap.Service;
Characteristic = homebridge.hap.Characteristic;
homebridge.registerAccessory("homebridge-samsung-tv", "samsungTv", SamsungTvAccessory);
};
function SamsungTvAccessory(log, config) {
this.log = log;
this.name = config.name;
this.ipAddress = config.ip_address;
this.polling = config.polling | true;
this.pollingInterval = config.pollingInterval | 1;
this.isOn;
this.api = new samsungAPI(this.log, this.ipAddress);
this.services = [];
this.tvService = new Service.Television(this.name, 'Television')
.setCharacteristic(Characteristic.ConfiguredName, this.name)
.setCharacteristic(Characteristic.SleepDiscoveryMode, Characteristic.SleepDiscoveryMode.NOT_DISCOVERABLE)
.setCharacteristic(Characteristic.PowerModeSelection, Characteristic.PowerModeSelection.SHOW);
this.tvService
.getCharacteristic(Characteristic.Active)
.on('set', this.setPowerState.bind(this))
.on('get', this.getPowerState.bind(this));
this.tvService.setCharacteristic(Characteristic.ActiveIdentifier, 1);
this.tvService
.getCharacteristic(Characteristic.ActiveIdentifier)
.on('set', this.setInput.bind(this));
this.tvService
.getCharacteristic(Characteristic.RemoteKey)
.on('set', this.sendRemoteKey.bind(this));
this.speakerService = new Service.TelevisionSpeaker(this.name + ' volume', 'volumeService')
.setCharacteristic(Characteristic.Active, Characteristic.Active.ACTIVE)
.setCharacteristic(Characteristic.VolumeControlType, Characteristic.VolumeControlType.ABSOLUTE);
this.speakerService
.getCharacteristic(Characteristic.VolumeSelector)
.on('set', this.setVolume.bind(this));
this.speakerService
.getCharacteristic(Characteristic.Mute)
.on('set', this.setMute.bind(this));
let index = 1;
this.tvInputService = new Service.InputSource(null, 'TV')
.setCharacteristic(Characteristic.Name, 'TV')
.setCharacteristic(Characteristic.Identifier, index++)
.setCharacteristic(Characteristic.ConfiguredName, 'TV')
.setCharacteristic(Characteristic.CurrentVisibilityState, Characteristic.CurrentVisibilityState.SHOWN)
.setCharacteristic(Characteristic.IsConfigured, Characteristic.IsConfigured.CONFIGURED)
.setCharacteristic(Characteristic.InputSourceType, Characteristic.InputSourceType.TUNER)
.setCharacteristic(Characteristic.InputDeviceType, Characteristic.InputDeviceType.TV);
this.tvService.addLinkedService(this.speakerService);
this.tvService.addLinkedService(this.tvInputService);
this.services.push(this.tvService, this.speakerService, this.tvInputService);
this.inputServices = {
1: this.tvInputService
};
config.enabledInputs
.forEach(input => {
let serviceIndex = index++;
let inputService = this.createInputService(serviceIndex, input);
this.tvService.addLinkedService(inputService);
this.services.push(inputService);
this.inputServices[serviceIndex] = inputService;
});
this.timer;
this.updateTimer();
}
SamsungTvAccessory.prototype = {
getServices() {
return this.services;
},
setPowerState(state, callback) {
this.log.debug('State received:', state);
this.api.setState(state, callback);
},
getPowerState(callback) {
this.log.debug('Get state called');
this.api.isOn(callback);
},
sendRemoteKey(key, callback) {
this.log.debug('Key received:', key);
this.api.sendKey(key, callback);
},
setVolume(volume, callback) {
this.log.debug('Volume received:', volume);
this.api.changeVolume(volume,callback);
},
setMute(mute, callback) {
this.log.debug('Mute received:', mute);
this.api.setMute(callback);
},
setInput(input, callback) {
this.log.debug('Input received:', input);
this.api.setInput(input, callback);
},
updateTimer() {
if (this.polling) {
clearTimeout(this.timer);
this.timer = setTimeout(function () {
this.getPowerState(function (err, isOn) {
if (err == null && isOn !== this.isOn) {
this.log("State changed: %s", isOn ? "on" : "off");
this.isOn = isOn;
this.tvService.getCharacteristic(Characteristic.Active).updateValue(isOn);
}
}.bind(this));
this.updateTimer();
}.bind(this), this.pollingInterval * 1000);
}
},
createInputService(index, input) {
this.log.debug('Creating %s with id %s', input.id, index);
return new Service.InputSource(null, input.id)
.setCharacteristic(Characteristic.Name, input.id)
.setCharacteristic(Characteristic.Identifier, index)
.setCharacteristic(Characteristic.ConfiguredName, input.id)
.setCharacteristic(Characteristic.IsConfigured, input.configured ? Characteristic.IsConfigured.CONFIGURED : Characteristic.IsConfigured.NOT_CONFIGURED)
.setCharacteristic(Characteristic.InputSourceType, Characteristic.InputSourceType.HDMI)
.setCharacteristic(Characteristic.InputDeviceType, input.deviceType | Characteristic.InputDeviceType.OTHER);
}
};