-
Notifications
You must be signed in to change notification settings - Fork 34
/
index.js
153 lines (123 loc) · 4.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
var SamsungRemote = require('samsung-remote');
var inherits = require('util').inherits;
var Service, Characteristic, VolumeCharacteristic, 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
makeVolumeCharacteristic();
makeChannelCharacteristic();
homebridge.registerAccessory("homebridge-samsungtv", "SamsungTV", SamsungTvAccessory);
};
//
// SoundTouch Accessory
//
function SamsungTvAccessory(log, config) {
this.log = log;
this.config = config;
this.name = config["name"];
this.ip_address = config["ip_address"];
if (!this.ip_address) throw new Error("You must provide a config value for 'ip_address'.");
this.remote = new SamsungRemote({
ip: this.ip_address // required: IP address of your Samsung Smart TV
});
this.service = new Service.Switch(this.name);
this.service
.getCharacteristic(Characteristic.On)
.on('get', this._getOn.bind(this))
.on('set', this._setOn.bind(this));
this.service
.addCharacteristic(VolumeCharacteristic)
.on('get', this._getVolume.bind(this))
.on('set', this._setVolume.bind(this));
this.service
.addCharacteristic(ChannelCharacteristic)
.on('get', this._getChannel.bind(this))
.on('set', this._setChannel.bind(this));
}
SamsungTvAccessory.prototype.getInformationService = function() {
var informationService = new Service.AccessoryInformation();
informationService
.setCharacteristic(Characteristic.Name, this.name)
.setCharacteristic(Characteristic.Manufacturer, 'Samsung TV')
.setCharacteristic(Characteristic.Model, '1.0.0')
.setCharacteristic(Characteristic.SerialNumber, this.ip_address);
return informationService;
};
SamsungTvAccessory.prototype.getServices = function() {
return [this.service, this.getInformationService()];
};
SamsungTvAccessory.prototype._getOn = function(callback) {
var accessory = this;
this.remote.isAlive(function(err) {
if (err) {
callback(new Error('TV is offline'));
} else {
accessory.log.debug('TV is ALIVE!');
callback(null, true);
}
});
};
SamsungTvAccessory.prototype._setOn = function(on, callback) {
if (on) {
callback(new Error('Could not turn on TV'));
} else {
this.remote.send('KEY_POWER', function(err) {
if (err) {
callback(new Error(err));
} else {
// command has been successfully transmitted to your tv
callback(null);
}
});
}
};
SamsungTvAccessory.prototype._getVolume = function(callback) {
var accessory = this;
callback(null, 25);
};
SamsungTvAccessory.prototype._setVolume = function(volume, callback) {
var accessory = this;
callback(null);
};
SamsungTvAccessory.prototype._getChannel = function(callback) {
var accessory = this;
callback(null, 2);
};
SamsungTvAccessory.prototype._setChannel = function(volume, callback) {
var accessory = this;
callback(null);
};
//
// Custom Characteristic for Volume
//
function makeVolumeCharacteristic() {
VolumeCharacteristic = function() {
Characteristic.call(this, 'Volume', '91288267-5678-49B2-8D22-F57BE995AA00');
this.setProps({
format: Characteristic.Formats.INT,
unit: Characteristic.Units.PERCENTAGE,
maxValue: 100,
minValue: 0,
minStep: 1,
perms: [Characteristic.Perms.READ, Characteristic.Perms.WRITE, Characteristic.Perms.NOTIFY]
});
this.value = this.getDefaultValue();
};
inherits(VolumeCharacteristic, Characteristic);
}
function makeChannelCharacteristic() {
ChannelCharacteristic = function () {
Characteristic.call(this, 'Channel', '212131F4-2E14-4FF4-AE13-C97C3232499D');
this.setProps({
format: Characteristic.Formats.INT,
unit: Characteristic.Units.NONE,
maxValue: 100,
minValue: 0,
minStep: 1,
perms: [Characteristic.Perms.READ, Characteristic.Perms.WRITE, Characteristic.Perms.NOTIFY]
});
this.value = this.getDefaultValue();
};
inherits(ChannelCharacteristic, Characteristic);
}