homebridge-weather
Advanced tools
Comparing version 1.1.1 to 1.2.0
61
index.js
@@ -5,2 +5,3 @@ "use strict"; | ||
var temperatureService; | ||
var humidityService; | ||
var request = require("request"); | ||
@@ -12,3 +13,3 @@ | ||
homebridge.registerAccessory("homebridge-weather", "Weather", WeatherAccessory); | ||
} | ||
}; | ||
@@ -23,4 +24,6 @@ function WeatherAccessory(log, config) { | ||
this.locationByZip = config["locationByZip"]; | ||
this.showHumidity = config["showHumidity"] || true; | ||
this.lastupdate = 0; | ||
this.temperature = 0; | ||
this.humidity = 0; | ||
} | ||
@@ -30,6 +33,7 @@ | ||
{ | ||
getState: function (callback) { | ||
getStateTemp: function (callback) { | ||
// Only fetch new data once per hour | ||
if (this.lastupdate + (60 * 60) < (Date.now() / 1000 | 0)) { | ||
var url = "http://api.openweathermap.org/data/2.5/weather?APPID=" + this.apikey + "&"; | ||
if (this.locationByCity) { | ||
@@ -44,3 +48,3 @@ url += "q=" + this.locationByCity; | ||
} | ||
this.httpRequest(url, function (error, response, responseBody) { | ||
@@ -61,3 +65,3 @@ if (error) { | ||
} else { | ||
this.log("Returning cached data", this.temperature); | ||
this.log("Returning cached data Temperature", this.temperature); | ||
temperatureService.setCharacteristic(Characteristic.CurrentTemperature, this.temperature); | ||
@@ -68,2 +72,37 @@ callback(null, this.temperature); | ||
getStateHum: function (callback) { | ||
// Only fetch new data once per hour | ||
if (this.lastupdate + (60 * 60) < (Date.now() / 1000 | 0)) { | ||
var url = "http://api.openweathermap.org/data/2.5/weather?APPID=" + this.apikey + "&"; | ||
if (this.locationByCity) { | ||
url += "q=" + this.locationByCity; | ||
} else if (this.locationById) { | ||
url += "id=" + this.locationById; | ||
} else if (this.locationByCoordinates) { | ||
url += this.locationByCoordinates; | ||
} else if (this.locationByZip) { | ||
url += "zip=" + this.locationByZip; | ||
} | ||
this.httpRequest(url, function (error, response, responseBody) { | ||
if (error) { | ||
this.log("HTTP get weather function failed: %s", error.message); | ||
callback(error); | ||
} else { | ||
this.log("HTTP Response", responseBody); | ||
var weatherObj = JSON.parse(responseBody); | ||
var humidity = parseFloat(weatherObj.main.humidity); | ||
this.humidity = humidity; | ||
this.lastupdate = (Date.now() / 1000); | ||
callback(null, humidity); | ||
} | ||
}.bind(this)); | ||
} else { | ||
this.log("Returning cached data Humidity", this.humidity); | ||
temperatureService.setCharacteristic(Characteristic.CurrentRelativeHumidity, this.humidity); | ||
callback(null, this.humidity); | ||
} | ||
}, | ||
identify: function (callback) { | ||
@@ -85,3 +124,3 @@ this.log("Identify requested"); | ||
.getCharacteristic(Characteristic.CurrentTemperature) | ||
.on("get", this.getState.bind(this)); | ||
.on("get", this.getStateTemp.bind(this)); | ||
@@ -96,3 +135,13 @@ temperatureService | ||
return [informationService, temperatureService]; | ||
if (this.showHumidity) { | ||
humidityService = new Service.HumiditySensor(this.name); | ||
humidityService | ||
.getCharacteristic(Characteristic.CurrentRelativeHumidity) | ||
.on("get", this.getStateHum.bind(this)); | ||
return [informationService, temperatureService, humidityService]; | ||
} else { | ||
return [informationService, temperatureService]; | ||
} | ||
}, | ||
@@ -99,0 +148,0 @@ |
{ | ||
"name": "homebridge-weather", | ||
"version": "1.1.1", | ||
"version": "1.2.0", | ||
"description": "A homebridge temperature sensor for displaying the weather at your current location.", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
# homebridge-weather | ||
A homebridge temperature sensor for displaying the weather at your current location. | ||
A homebridge temperature sensor for displaying the weather (and optional the humidity) at your current location. | ||
@@ -57,2 +57,2 @@ # Installation | ||
* `name` is the name of the published accessory (required). | ||
* `showHumidity` weather or not show the humidity (optional). |
10299
137