Repository navigation
Expand file tree
/
Copy pathindex.js
More file actions
132 lines (122 loc) · 4.54 KB
/
Copy pathindex.js
File metadata and controls
132 lines (122 loc) · 4.54 KB
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
var Service, Characteristic;
var request = require('request');
var colorsys = require( 'colorsys' );
module.exports = function(homebridge) {
Service = homebridge.hap.Service;
Characteristic = homebridge.hap.Characteristic;
homebridge.registerAccessory("homebridge-sonoff-tasmota-http-h801", "SonoffTasmotaHTTPH801", SonoffTasmotaHTTPH801Accessory);
}
function SonoffTasmotaHTTPH801Accessory(log, config) {
this.log = log;
this.config = config;
this.name = config["name"]
this.hostname = config["hostname"] || "sonoff";
this.hsv = {
h: 0,
s: 0,
v: 0
};
}
SonoffTasmotaHTTPH801Accessory.prototype.setColor = function(hsv) {
var color = colorsys.hsv_to_hex( hsv );
color = color.substring(1, color.length);
var that = this
request("http://" + this.hostname + "/cm?cmnd=Color " + color, function(error, response, body) {
if (error) return callback(error);
var sonoff_reply = JSON.parse(body); // {"Color":"FF9245"}
that.log("Sonoff H801: " + that.hostname + " Set Color: " + sonoff_reply.Color);
})
};
SonoffTasmotaHTTPH801Accessory.prototype.getColor = function(callback) {
var that = this
request("http://" + this.hostname + "/cm?cmnd=Color", function(error, response, body) {
if (error) return callback(error);
var sonoff_reply = JSON.parse(body); // {"Color":"FF9245"}
var hsvcolor = colorsys.hex_to_hsv('#'+sonoff_reply.Color);
that.log("Sonoff H801: " + that.hostname + " Get Color: " + sonoff_reply.Color + '. Parsed color is {h: ' + hsvcolor.h + ', s: ' + hsvcolor.s + ', v: ' + hsvcolor.v + '}');
callback(error, hsvcolor)
})
};
SonoffTasmotaHTTPH801Accessory.prototype.getServices = function() {
var informationService = new Service.AccessoryInformation();
informationService
.setCharacteristic(Characteristic.Manufacturer, 'Sonoff Tasmota')
.setCharacteristic(Characteristic.Model, 'homebridge-sonoff-tasmota-http-h801')
.setCharacteristic(Characteristic.SerialNumber, 'HTTP Serial Number')
var bulb = this
this.service = new Service.Lightbulb(this.name);
this.service
.getCharacteristic(Characteristic.On)
.on( 'get', function( callback ) {
request("http://" + bulb.hostname + "/cm?cmnd=Power", function(error, response, body) {
if (error) return callback(error);
var sonoff_reply = JSON.parse(body); // {"POWER":"ON"}
bulb.log("Sonoff H801: " + bulb.hostname + ", Get State: " + JSON.stringify(sonoff_reply));
switch (sonoff_reply["POWER"]) {
case "ON":
callback(null, 1); break;
case "OFF":
callback(null, 0); break;
}
})
} )
.on( 'set', function( value, callback ) {
var newstate = "%20Off"
if (value) newstate = "%20On"
request("http://" + bulb.hostname + "/cm?cmnd=Power" + newstate, function(error, response, body) {
if (error) return callback(error);
var sonoff_reply = JSON.parse(body); // {"POWER":"ON"}
bulb.log("Sonoff HTTP: " + bulb.hostname + ", Set State: " + JSON.stringify(sonoff_reply));
switch (sonoff_reply["POWER"]) {
case "ON":
callback(); break;
case "OFF":
callback(); break;
}
})
} );
this.service
.addCharacteristic(new Characteristic.Brightness())
.on( 'get', function( callback ) {
bulb.getColor((err, hsv) => {
if (err) return callback(err)
callback( err, hsv.v );
});
} )
.on( 'set', function( value, callback ) {
bulb.log('Set Characteristic.Brightness to ' + value);
bulb.hsv.v = value;
bulb.setColor(bulb.hsv);
callback();
} );
this.service
.addCharacteristic(new Characteristic.Hue())
.on( 'get', function( callback ) {
bulb.getColor((err, hsv) => {
if (err) return callback(err)
callback( err, hsv.h );
});
} )
.on( 'set', function( value, callback ) {
bulb.log('Set Characteristic.Hue to ' + value);
bulb.hsv.h = value;
bulb.setColor(bulb.hsv);
callback();
} );
this.service
.addCharacteristic(new Characteristic.Saturation())
.on( 'get', function( callback ) {
bulb.getColor((err, hsv) => {
if (err) return callback(err)
callback( err, hsv.s );
});
} )
.on( 'set', function( value, callback ) {
bulb.log('Set Characteristic.Saturation to ' + value);
bulb.hsv.s = value;
bulb.setColor(bulb.hsv)
callback();
});
this.log("Sonoff Tasmota HTTP H801 Initialized")
return [this.service];
}