-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
122 lines (108 loc) · 4.47 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
var Service, Characteristic;
var request = require('request');
module.exports = function(homebridge) {
Service = homebridge.hap.Service;
Characteristic = homebridge.hap.Characteristic;
homebridge.registerAccessory("homebridge-sonoff-tasmota-http-led", "SonoffTasmotaHTTPLED", SonoffTasmotaHTTPLEDAccessory);
}
function SonoffTasmotaHTTPLEDAccessory(log, config) {
this.log = log;
this.config = config;
this.name = config["name"]
this.hostname = config["hostname"] || "sonoff";
var informationService = new Service.AccessoryInformation();
informationService
.setCharacteristic(Characteristic.Manufacturer, 'Sonoff Tasmota')
.setCharacteristic(Characteristic.Model, 'homebridge-sonoff-tasmota-http-led')
.setCharacteristic(Characteristic.SerialNumber, 'HTTP Serial Number')
this.service = new Service.Lightbulb(this.name);
this.service
.getCharacteristic(Characteristic.On)
.on('get', this.getState.bind(this))
.on('set', this.setState.bind(this));
this.service
.addCharacteristic(new Characteristic.Brightness())
.on('get', this.getBrightness.bind(this))
.on('set', this.setBrightness.bind(this))
this.service
.addCharacteristic(new Characteristic.ColorTemperature())
.on('get', this.getColorTemperature.bind(this))
.on('set', this.setColorTemperature.bind(this))
this.log("Sonoff Tasmota HTTP LED Initialized")
}
SonoffTasmotaHTTPLEDAccessory.prototype.getState = function(callback) {
var that = this
request("http://" + this.hostname + "/cm?cmnd=Power", function(error, response, body) {
if (error) return callback(error);
var lines = body.split("\n");
that.log("Sonoff LED: " + that.hostname + " Get State: " + lines[1]);
if (lines[1] == "POWER = OFF") callback(null, 0)
else if (lines[1] == "POWER = ON") callback(null, 1)
})
}
SonoffTasmotaHTTPLEDAccessory.prototype.setState = function(toggle, callback) {
var newstate = "%20Off"
if (toggle) newstate = "%20On"
var that = this
request("http://" + this.hostname + "/cm?cmnd=Power" + newstate, function(error, response, body) {
if (error) return callback(error);
var lines = body.split("\n");
that.log("Sonoff LED: " + that.hostname + " Set State to: " + lines[1]);
if (lines[1] == "POWER = OFF") callback()
else if (lines[1] == "POWER = ON") callback()
})
}
SonoffTasmotaHTTPLEDAccessory.prototype.getBrightness = function(callback) {
var that = this
request("http://" + this.hostname + "/cm?cmnd=Dimmer", function(error, response, body) {
if (error) return callback(error);
var lines = body.split("=");
var jsonreply = JSON.parse(lines[1])
that.log("Sonoff LED: " + that.hostname + " Get Brightness: " + jsonreply.Dimmer);
callback(null, jsonreply.Dimmer)
})
}
SonoffTasmotaHTTPLEDAccessory.prototype.setBrightness = function(brightness, callback) {
var that = this
request("http://" + this.hostname + "/cm?cmnd=Dimmer%20" + brightness, function(error, response, body) {
if (error) return callback(error);
var lines = body.split("=");
var jsonreply = JSON.parse(lines[1])
that.log("Sonoff LED: " + that.hostname + " Set Brightness to: " + jsonreply.Dimmer);
if (jsonreply.Dimmer == brightness) callback()
else {
that.log("Sonoff LED: " + that.hostname + " ERROR Setting Brightness to: " + brightness)
callback()
}
})
}
SonoffTasmotaHTTPLEDAccessory.prototype.getColorTemperature = function(callback) {
var that = this
request("http://" + this.hostname + "/cm?cmnd=CT", function(error, response, body) {
if (error) return callback(error);
var lines = body.split("=");
var jsonreply = JSON.parse(lines[1])
that.log("Sonoff LED: " + that.hostname + " Get Color Temperature: " + jsonreply.CT);
callback(null, jsonreply.CT)
})
}
SonoffTasmotaHTTPLEDAccessory.prototype.setColorTemperature = function(CT, callback) {
var that = this
if (CT < 153) CT = 153
if (CT > 499) CT = 499
request("http://" + this.hostname + "/cm?cmnd=CT%20" + CT, function(error, response, body) {
if (error) return callback(error);
that.service.setCharacteristic(Characteristic.On, 1)
var lines = body.split("=");
var jsonreply = JSON.parse(lines[1])
that.log("Sonoff LED: " + that.hostname + " Set Color Temperature to: " + CT);
if (jsonreply.Color != undefined) callback()
else {
that.log("Sonoff LED: " + that.hostname + " ERROR Setting Color Temperature to: " + CT)
callback()
}
})
}
SonoffTasmotaHTTPLEDAccessory.prototype.getServices = function() {
return [this.service];
}