forked from ToddGreenfield/homebridge-directv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
227 lines (200 loc) · 7.37 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
'use strict';
// Directv Platform for HomeBridge
//
// Remember to add platform to config.json. Example:
// "platforms": [
// {
// "platform": "Directv", // required
// "name": "DTV", // Optional - defaults to DTV
// "ip_address": "IP of Primary STB", // required
// "exclude_geni": true, // Optional - defaults to false
// "min_channel": 1, // Optional - defaults to 1
// "max_channel": 575 // Optional - defaults to 575
// }
// ],
//
//
var inherits = require('util').inherits;
var DirecTV = require('directv-remote');
var titleCase = require('title-case');
var remote, min_ch, max_ch;
function DirectvPlatform(log, config){
this.config = config;
this.ip_address = config["ip_address"];
this.name = config["name"] || 'DTV';
this.excludeGeni = config["exclude_geni"] || false;
min_ch = config["min_channel"] || 1;
max_ch = config["max_channel"] || 575;
this.log = log;
if (!this.ip_address) throw new Error("DTV - You must provide a config value for 'ip_address'.");
remote = new DirecTV.Remote(this.ip_address);
}
DirectvPlatform.prototype = {
accessories: function(callback) {
this.log("Fetching DTV locations.");
var that = this;
var foundAccessories = [];
remote.getLocations('1', function(err, response) {
if (!err) {
that.log( "Finding DTV Locations (accessories)...");
for(var i=0; i <response.locations.length; ++i){
if ((!that.excludeGeni) || (response.locations[i].clientAddr == '0')) {
that.log( "Found DTV Location %s", response.locations[i].locationName);
var accessory = new DirectvSTBAccessory(that.log, response.locations[i], that.config);
foundAccessories.push(accessory);
} else {
that.log( "Found DTV Location %s, but EXCLUDING per configuration setting.", response.locations[i].locationName);
}
}
callback(foundAccessories);
} else {
that.log( "Failed to find any DTV Locations (accessories) - Check IP address in config.json!");
callback(null);
}
});
}
}
function DirectvSTBAccessory(log, location, config) {
// STB basic device info
this.log = log;
this.config = config;
this.location = titleCase(location.locationName);
this.config_name = config["name"] || 'DTV';
this.name = this.location + ' ' + this.config_name;
this.ip_address = config["ip_address"];
this.clientAddr = location.clientAddr.toUpperCase();
}
DirectvSTBAccessory.prototype = {
getRemote: function(type, callback){
var that = this;
switch(type) {
case "power":
remote.getMode(that.clientAddr, function (err, response) {
if (err || response.mode == "1") {
that.log.debug('DTV location %s power state is currently OFF or UNREACHABLE', that.location);
callback(null, false);
return;
} else if (response.mode == "0") {
that.log.debug('DTV location %s power state is currently ON', that.location);
callback(null, true);
}
});
break;
case "channel":
remote.getMode(that.clientAddr, function (err, response) {
if (err || response.mode == "1") {
that.log.debug('Unable to call for current channel at DTV location %s.', that.location);
callback(null, parseInt("0"));
} else if (response.mode == "0") {
remote.getTuned(that.clientAddr, function (err, response) {
if (!err) {
that.log.debug('DTV location %s current channel is %d', that.location, parseInt(response.major));
try{
callback(null, parseInt(response.major));
} catch (e) {
}
} else {
that.log.debug('Unable to call for current channel at DTV location %s.', that.location);
callback(null, parseInt("0"));
}
});
}
});
break;
}
},
setRemote: function(type, value, callback){
var that = this;
switch(type) {
case "power":
remote.getMode(that.clientAddr, function (err, response) {
var responseMode = response.mode;
if (err) { responseMode = "1"}
if (parseInt(responseMode) === parseInt(value ? 1 : 0)) {
remote.processKey('power', that.clientAddr, function(err, response) {
if (err) {
that.log.debug('Unable to change DTV location %s power state!', that.location);
callback(new Error("STB Process Power Error."), false);
return;
} else {
that.log.debug('DTV location %s power state is now: %s', that.location, (value) ? 'ON' : 'OFF');
callback();
}
});
} else {
callback();
}
});
break;
case "channel":
remote.getMode(that.clientAddr, function (err, response) {
if (err || response.mode == "1") {
that.log.debug('Unable to set channel at DTV location %s.', that.location);
callback();
} else if (response.mode == "0") {
remote.tune(value, that.clientAddr, function(err, response) {
if (err) {
that.log.debug('Unable to set channel at DTV location %s', that.location);
callback();
} else {
that.log.debug('DTV location %s Channel is now: %s', that.location, value);
callback();
}
});
}
});
break;
}
},
getServices: function() {
var that = this;
var services = []
if (this.clientAddr == '0') {
this.service = new Service.Switch(this.name);
this.service.getCharacteristic(Characteristic.On)
.on('get', function(callback) { that.getRemote("power", callback);})
.on('set', function(value, callback) {that.setRemote("power", value, callback);});
} else {
this.service = new Service.MotionSensor(this.name);
this.service.getCharacteristic(Characteristic.MotionDetected)
.on('get', function(callback) { that.getRemote("power", callback);});
}
this.service.addCharacteristic(ChannelCharacteristic)
.on('get', function(callback) { that.getRemote("channel", callback);})
.on('set', function(value, callback) {that.setRemote("channel", value, callback);});
services.push(this.service);
var service = new Service.AccessoryInformation();
service.setCharacteristic(Characteristic.Manufacturer, "Directv")
.setCharacteristic(Characteristic.Name, this.name)
.setCharacteristic(Characteristic.SerialNumber, this.clientAddr)
.setCharacteristic(Characteristic.Model, this.ip_address);
services.push(service);
return services;
}
}
module.exports.accessory = DirectvSTBAccessory;
module.exports.platform = DirectvPlatform;
var Service, Characteristic, ChannelCharacteristic;
module.exports = function(homebridge) {
Service = homebridge.hap.Service;
Characteristic = homebridge.hap.Characteristic;
// we can only do this after we receive the homebridge API object
makeChannelCharacteristic();
homebridge.registerAccessory("homebridge-directv-location", "DirectvSTB", DirectvSTBAccessory);
homebridge.registerPlatform("homebridge-directv", "Directv", DirectvPlatform);
};
function makeChannelCharacteristic() {
ChannelCharacteristic = function () {
Characteristic.call(this, 'Channel', '212131F4-2E14-4FF4-AE13-C97C3232499D');
this.setProps({
format: Characteristic.Formats.INT,
unit: Characteristic.Units.NONE,
maxValue: max_ch,
minValue: min_ch,
minStep: 1,
perms: [Characteristic.Perms.READ, Characteristic.Perms.WRITE, Characteristic.Perms.NOTIFY]
});
this.value = this.getDefaultValue();
};
inherits(ChannelCharacteristic, Characteristic);
}