-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
288 lines (248 loc) · 10.1 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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
const LoeweAPI = require('./lib/LoeweAPI.js');
let Service, Characteristic, Homebridge, Accessory;
const PLUGIN_NAME = 'homebridge-loewetv';
const PLATFORM_NAME = 'LoeweTV';
module.exports = (api) => {
api.registerPlatform(PLATFORM_NAME, HomebridgeLoewetvPlugin);
}
class HomebridgeLoewetvPlugin {
constructor(log, config, api) {
this.log = log;
this.config = config;
this.api = api;
this.loewe = new LoeweAPI(config.ip)
this.Service = api.hap.Service;
this.Characteristic = api.hap.Characteristic;
// get the name
const tvName = this.config.name || 'Loewe TV';
// generate a UUID
const uuid = this.api.hap.uuid.generate('homebridge:loewetv' + tvName);
// create the accessory
this.tvAccessory = new api.platformAccessory(tvName, uuid);
// set the accessory category
this.tvAccessory.category = this.api.hap.Categories.TELEVISION;
// add the tv service
const tvService = this.tvAccessory.addService(this.Service.Television);
// set the tv name
tvService.setCharacteristic(this.Characteristic.ConfiguredName, tvName);
// set sleep discovery characteristic
tvService.setCharacteristic(this.Characteristic.SleepDiscoveryMode, this.Characteristic.SleepDiscoveryMode.ALWAYS_DISCOVERABLE);
// handle on / off events using the Active characteristic
tvService.getCharacteristic(this.Characteristic.Active)
.onSet((newValue) => {
this.log.info('set Active => setNewValue: ' + newValue);
let that = this;
if(newValue === 1){
this.loewe.auth(function (clientID){
that.loewe.injectRCKey(clientID, "22")
})
}
if(newValue === 0){
this.loewe.auth(function (clientID){
that.loewe.injectRCKey(clientID, "25")
})
}
tvService.updateCharacteristic(this.Characteristic.Active, 1);
});
tvService.setCharacteristic(this.Characteristic.ActiveIdentifier, 1);
// handle input source changes
tvService.getCharacteristic(this.Characteristic.ActiveIdentifier)
.onSet((newValue) => {
// the value will be the value you set for the Identifier Characteristic
// on the Input Source service that was selected - see input sources below.
if(newValue === 1){
// HDMI 1
let that = this;
this.loewe.auth(function (clientID){
that.loewe.injectRCKey(clientID, "119")
})
}
if(newValue === 2){
// HDMI 2
let that = this;
this.loewe.auth(function (clientID){
that.loewe.injectRCKey(clientID, "121")
})
}
if(newValue === 3){
// TV
let that = this;
this.loewe.auth(function (clientID){
that.loewe.injectRCKey(clientID, "72")
})
}
this.log.info('set Active Identifier => setNewValue: ' + newValue);
});
// handle remote control input
tvService.getCharacteristic(this.Characteristic.RemoteKey)
.onSet((newValue) => {
switch(newValue) {
case this.Characteristic.RemoteKey.REWIND: {
this.log.info('set Remote Key Pressed: REWIND');
break;
}
case this.Characteristic.RemoteKey.FAST_FORWARD: {
this.log.info('set Remote Key Pressed: FAST_FORWARD');
break;
}
case this.Characteristic.RemoteKey.NEXT_TRACK: {
this.log.info('set Remote Key Pressed: NEXT_TRACK');
break;
}
case this.Characteristic.RemoteKey.PREVIOUS_TRACK: {
this.log.info('set Remote Key Pressed: PREVIOUS_TRACK');
break;
}
case this.Characteristic.RemoteKey.ARROW_UP: {
this.log.info('set Remote Key Pressed: ARROW_UP');
let that = this;
this.loewe.auth(function (clientID){
that.loewe.injectRCKey(clientID, "32")
})
break;
}
case this.Characteristic.RemoteKey.ARROW_DOWN: {
this.log.info('set Remote Key Pressed: ARROW_DOWN');
let that = this;
this.loewe.auth(function (clientID){
that.loewe.injectRCKey(clientID, "33")
})
break;
}
case this.Characteristic.RemoteKey.ARROW_LEFT: {
this.log.info('set Remote Key Pressed: ARROW_LEFT');
let that = this;
this.loewe.auth(function (clientID){
that.loewe.injectRCKey(clientID, "17")
})
break;
}
case this.Characteristic.RemoteKey.ARROW_RIGHT: {
this.log.info('set Remote Key Pressed: ARROW_RIGHT');
let that = this;
this.loewe.auth(function (clientID){
that.loewe.injectRCKey(clientID, "16")
})
break;
}
case this.Characteristic.RemoteKey.SELECT: {
this.log.info('set Remote Key Pressed: SELECT');
let that = this;
this.loewe.auth(function (clientID){
that.loewe.injectRCKey(clientID, "38")
})
break;
}
case this.Characteristic.RemoteKey.BACK: {
this.log.info('set Remote Key Pressed: BACK');
let that = this;
this.loewe.auth(function (clientID){
that.loewe.injectRCKey(clientID, "65")
})
break;
}
case this.Characteristic.RemoteKey.EXIT: {
this.log.info('set Remote Key Pressed: EXIT');
let that = this;
this.loewe.auth(function (clientID){
that.loewe.injectRCKey(clientID, "63")
})
break;
}
/*case this.Characteristic.RemoteKey.PLAY_PAUSE: {
this.log.info('set Remote Key Pressed: PLAY_PAUSE');
break;
}*/
case this.Characteristic.RemoteKey.INFORMATION: {
this.log.info('set Remote Key Pressed: INFORMATION');
let that = this;
this.loewe.auth(function (clientID){
that.loewe.injectRCKey(clientID, "49")
})
break;
}
}
});
/**
* Create a speaker service to allow volume control
*/
const speakerService = this.tvAccessory.addService(this.Service.TelevisionSpeaker);
speakerService
.setCharacteristic(this.Characteristic.Active, this.Characteristic.Active.ACTIVE)
.setCharacteristic(this.Characteristic.VolumeControlType, this.Characteristic.VolumeControlType.ABSOLUTE);
// handle volume control
speakerService.getCharacteristic(this.Characteristic.VolumeSelector)
.onSet((newValue) => {
if(newValue === 0){
// lauter
let that = this;
this.loewe.auth(function (clientID){
that.loewe.injectRCKey(clientID, "21")
})
}
if(newValue === 1){
// leiser
let that = this;
this.loewe.auth(function (clientID){
that.loewe.injectRCKey(clientID, "20")
})
}
});
/**
* Create TV Input Source Services
* These are the inputs the user can select from.
* When a user selected an input the corresponding Identifier Characteristic
* is sent to the TV Service ActiveIdentifier Characteristic handler.
*/
// HDMI 1 Input Source
const hdmi1InputService = this.tvAccessory.addService(this.Service.InputSource, 'hdmi1', 'HDMI 1');
hdmi1InputService
.setCharacteristic(this.Characteristic.Identifier, 1)
.setCharacteristic(this.Characteristic.ConfiguredName, 'HDMI 1')
.setCharacteristic(this.Characteristic.IsConfigured, this.Characteristic.IsConfigured.CONFIGURED)
.setCharacteristic(this.Characteristic.InputSourceType, this.Characteristic.InputSourceType.HDMI);
tvService.addLinkedService(hdmi1InputService); // link to tv service
// HDMI 2 Input Source
const hdmi2InputService = this.tvAccessory.addService(this.Service.InputSource, 'hdmi2', 'HDMI 2');
hdmi2InputService
.setCharacteristic(this.Characteristic.Identifier, 2)
.setCharacteristic(this.Characteristic.ConfiguredName, 'HDMI 2')
.setCharacteristic(this.Characteristic.IsConfigured, this.Characteristic.IsConfigured.CONFIGURED)
.setCharacteristic(this.Characteristic.InputSourceType, this.Characteristic.InputSourceType.HDMI);
tvService.addLinkedService(hdmi2InputService); // link to tv service
// TV Input Source
const tvInputService = this.tvAccessory.addService(this.Service.InputSource, 'tv', 'TV');
tvInputService
.setCharacteristic(this.Characteristic.Identifier, 3)
.setCharacteristic(this.Characteristic.ConfiguredName, 'TV')
.setCharacteristic(this.Characteristic.IsConfigured, this.Characteristic.IsConfigured.CONFIGURED)
.setCharacteristic(this.Characteristic.InputSourceType, this.Characteristic.InputSourceType.HDMI);
tvService.addLinkedService(tvInputService); // link to tv service
// Seconds
var that = this;
var active = false;
this.loewe.fetchCurrentState(function (data){
if(data["m:Power"] === "idle"){
// ist aus
console.log("TV ist aus.")
active = false
//tvService.setCharacteristic(this.Characteristic.Active, 0)
}
if(data["m:Power"] === "tv"){
// ist an
console.log("TV ist an.")
active = true
//tvService.setCharacteristic(this.Characteristic.Active, 1)
}
console.log(data)
})
//tvService.getCharacteristic(this.Characteristic.Active).updateValue(Characteristic.Active.ACTIVE);
//
/**
* Publish as external accessory
* Only one TV can exist per bridge, to bypass this limitation, you should
* publish your TV as an external accessory.
*/
this.api.publishExternalAccessories(PLUGIN_NAME, [this.tvAccessory]);
}
}