mirror of
https://github.com/mtan93/homebridge.git
synced 2026-03-08 05:31:55 +00:00
Merge pull request #253 from stevetrease/master
Added readonly thermometer and hygrometer accessories
This commit is contained in:
71
accessories/HttpHygrometer.js
Normal file
71
accessories/HttpHygrometer.js
Normal file
@@ -0,0 +1,71 @@
|
||||
var Service = require("HAP-NodeJS").Service;
|
||||
var Characteristic = require("HAP-NodeJS").Characteristic;
|
||||
var request = require("request");
|
||||
|
||||
module.exports = {
|
||||
accessory: HygrometerAccessory
|
||||
}
|
||||
|
||||
function HygrometerAccessory(log, config) {
|
||||
this.log = log;
|
||||
|
||||
// url info
|
||||
this.url = config["url"];
|
||||
this.http_method = config["http_method"];
|
||||
}
|
||||
|
||||
|
||||
HygrometerAccessory.prototype = {
|
||||
|
||||
httpRequest: function(url, method, callback) {
|
||||
request({
|
||||
url: url,
|
||||
method: method
|
||||
},
|
||||
function (error, response, body) {
|
||||
callback(error, response, body)
|
||||
})
|
||||
},
|
||||
|
||||
|
||||
identify: function(callback) {
|
||||
this.log("Identify requested!");
|
||||
callback(); // success
|
||||
},
|
||||
|
||||
getCurrentRelativeHumidity: function (callback) {
|
||||
var that = this;
|
||||
that.log ("getting CurrentCurrentRelativeHumidity");
|
||||
|
||||
this.httpRequest(this.url, this.http_method, function(error, response, body) {
|
||||
if (error) {
|
||||
this.log('HTTP function failed: %s', error);
|
||||
callback(error);
|
||||
}
|
||||
else {
|
||||
this.log('HTTP function succeeded - %s', body);
|
||||
callback(null, Number(body));
|
||||
}
|
||||
}.bind(this));
|
||||
},
|
||||
|
||||
getServices: function() {
|
||||
|
||||
// you can OPTIONALLY create an information service if you wish to override
|
||||
// the default values for things like serial number, model, etc.
|
||||
var informationService = new Service.AccessoryInformation();
|
||||
|
||||
informationService
|
||||
.setCharacteristic(Characteristic.Manufacturer, "HTTP Manufacturer")
|
||||
.setCharacteristic(Characteristic.Model, "HTTP Hygrometer")
|
||||
.setCharacteristic(Characteristic.SerialNumber, "HTTP Serial Number");
|
||||
|
||||
var humidityService = new Service.HumiditySensor();
|
||||
|
||||
humidityService
|
||||
.getCharacteristic(Characteristic.CurrentRelativeHumidity)
|
||||
.on('get', this.getCurrentRelativeHumidity.bind(this));
|
||||
|
||||
return [informationService, humidityService];
|
||||
}
|
||||
};
|
||||
79
accessories/HttpThermometer.js
Normal file
79
accessories/HttpThermometer.js
Normal file
@@ -0,0 +1,79 @@
|
||||
var Service = require("HAP-NodeJS").Service;
|
||||
var Characteristic = require("HAP-NodeJS").Characteristic;
|
||||
var request = require("request");
|
||||
|
||||
module.exports = {
|
||||
accessory: ThermometerAccessory
|
||||
}
|
||||
|
||||
function ThermometerAccessory(log, config) {
|
||||
this.log = log;
|
||||
|
||||
// url info
|
||||
this.url = config["url"];
|
||||
this.http_method = config["http_method"];
|
||||
}
|
||||
|
||||
|
||||
ThermometerAccessory.prototype = {
|
||||
|
||||
httpRequest: function(url, method, callback) {
|
||||
request({
|
||||
url: url,
|
||||
method: method
|
||||
},
|
||||
function (error, response, body) {
|
||||
callback(error, response, body)
|
||||
})
|
||||
},
|
||||
|
||||
|
||||
identify: function(callback) {
|
||||
this.log("Identify requested!");
|
||||
callback(); // success
|
||||
},
|
||||
|
||||
getCurrentTemperature: function (callback) {
|
||||
var that = this;
|
||||
that.log ("getting CurrentTemperature");
|
||||
|
||||
|
||||
this.httpRequest(this.url, this.http_method, function(error, response, body) {
|
||||
if (error) {
|
||||
this.log('HTTP function failed: %s', error);
|
||||
callback(error);
|
||||
}
|
||||
else {
|
||||
this.log('HTTP function succeeded - %s', body);
|
||||
callback(null, Number(body));
|
||||
}
|
||||
}.bind(this));
|
||||
},
|
||||
|
||||
getTemperatureUnits: function (callback) {
|
||||
var that = this;
|
||||
that.log ("getTemperature Units");
|
||||
// 1 = F and 0 = C
|
||||
callback (null, 0);
|
||||
},
|
||||
|
||||
getServices: function() {
|
||||
|
||||
// you can OPTIONALLY create an information service if you wish to override
|
||||
// the default values for things like serial number, model, etc.
|
||||
var informationService = new Service.AccessoryInformation();
|
||||
|
||||
informationService
|
||||
.setCharacteristic(Characteristic.Manufacturer, "HTTP Manufacturer")
|
||||
.setCharacteristic(Characteristic.Model, "HTTP Thermometer")
|
||||
.setCharacteristic(Characteristic.SerialNumber, "HTTP Serial Number");
|
||||
|
||||
var temperatureService = new Service.TemperatureSensor();
|
||||
|
||||
temperatureService
|
||||
.getCharacteristic(Characteristic.CurrentTemperature)
|
||||
.on('get', this.getCurrentTemperature.bind(this));
|
||||
|
||||
return [informationService, temperatureService];
|
||||
}
|
||||
};
|
||||
@@ -164,7 +164,20 @@
|
||||
"off_url": "https://192.168.1.22:3030/devices/23222/off",
|
||||
"brightness_url": "https://192.168.1.22:3030/devices/23222/brightness/%b",
|
||||
"http_method": "POST"
|
||||
},{
|
||||
},
|
||||
{
|
||||
"accessory": "HttpHygrometer",
|
||||
"name": "Kitchen",
|
||||
"url": "http://host/URL",
|
||||
"http_method": "GET"
|
||||
},
|
||||
{
|
||||
"accessory": "HttpThermometer",
|
||||
"name": "Garage",
|
||||
"url": "http://home/URL",
|
||||
"http_method": "GET"
|
||||
},
|
||||
{
|
||||
"accessory": "ELKM1",
|
||||
"name": "Security System",
|
||||
"description": "Allows basic control of Elk M1 security system. You can use 1 of 3 arm modes: Away, Stay, Night. If you need to access all 3, create 3 accessories with different names.",
|
||||
|
||||
Reference in New Issue
Block a user