-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
129 lines (108 loc) · 4.61 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
"use strict";
var Wunderground = require('wundergroundnode');
var inherits = require('util').inherits;
var Service, Characteristic;
var weatherStationService;
var WeatherConditionValue
var WeatherCondition
var WeatherStation
module.exports = function (homebridge) {
Service = homebridge.hap.Service;
Characteristic = homebridge.hap.Characteristic;
homebridge.registerAccessory("homebridge-wunderground", "WUWeatherStation", WUWeatherStation);
WeatherConditionValue = function() {
Characteristic.call(this, 'Weather Condition Value', 'cd65a9ab-85ad-494a-b2bd-2f380084134c');
this.setProps({
format: Characteristic.Formats.UINT8,
maxValue: 2,
minValue: 0,
minStep: 1,
perms: [Characteristic.Perms.READ, Characteristic.Perms.NOTIFY]
});
this.value = this.getDefaultValue();
};
inherits(WeatherConditionValue, Characteristic);
WeatherCondition = function() {
Characteristic.call(this, 'Weather Condition', 'cd65a9ab-85ad-494a-b2bd-2f380084134d');
this.setProps({
format: Characteristic.Formats.STRING,
perms: [Characteristic.Perms.READ]
});
this.value = this.getDefaultValue();
};
inherits(WeatherCondition, Characteristic);
WeatherStation = function(displayName, subtype) {
Service.call(this, displayName, 'debf1b79-312e-47f7-bf82-993d9950f3a2', subtype);
// Required Characteristics
this.addCharacteristic(WeatherCondition);
this.addCharacteristic(WeatherConditionValue);
this.addCharacteristic(Characteristic.CurrentRelativeHumidity);
this.addCharacteristic(Characteristic.CurrentTemperature);
// Optional Characteristics
this.addOptionalCharacteristic(Characteristic.StatusActive);
this.addOptionalCharacteristic(Characteristic.Name);
};
inherits(WeatherStation, Service);
}
function WUWeatherStation(log, config) {
this.log = log;
this.wunderground = new Wunderground(config['key']);
this.name = config['name'];
this.location = config['location'];
this.timestampOfLastUpdate = 0;
this.informationService = new Service.AccessoryInformation();
this.informationService
.setCharacteristic(Characteristic.Manufacturer, "HomeBridge")
.setCharacteristic(Characteristic.Model, "Weather Underground")
.setCharacteristic(Characteristic.SerialNumber, this.location);
this.weatherStationService = new WeatherStation(this.name)
this.updateWeatherConditions();
}
WUWeatherStation.prototype = {
identify: function (callback) {
this.log("Identify requested!");
callback(); // success
},
getServices: function () {
return [this.informationService, this.weatherStationService];
},
updateWeatherConditions: function() {
var that = this
that.wunderground.conditions().request(that.location, function(err, response){
if (!err && response['current_observation'] && response['current_observation']['temp_c']) {
that.timestampOfLastUpdate = Date.now() / 1000 | 0;
that.temperature = response['current_observation']['temp_c'];
let conditionIcon = response['current_observation']['icon']
that.condition = response['current_observation']['weather']
switch (conditionIcon) {
case "rain":
case "tstorm":
case "tstorms":
that.conditionValue = 1
break;
case "snow":
case "sleet":
case "flurries":
that.conditionValue = 2
break;
default:
that.conditionValue = 0
break;
}
that.humidity = parseInt(response['current_observation']['relative_humidity'].substr(0, response['current_observation']['relative_humidity'].length-1));
let humidityString = response['current_observation']['relative_humidity']
let temperatureString = response['current_observation']['temperature_string']
let uv = response['current_observation']['UV']
that.log("Current Weather Conditions: " + that.condition + ", " + temperatureString + ", " + humidityString + " humidity, UV: " + uv)
that.weatherStationService.setCharacteristic(WeatherConditionValue,that.conditionValue);
that.weatherStationService.setCharacteristic(WeatherCondition,that.condition);
that.weatherStationService.setCharacteristic(Characteristic.CurrentTemperature, that.temperature);
that.weatherStationService.setCharacteristic(Characteristic.CurrentRelativeHumidity, that.humidity);
} else {
that.log("Error retrieving the weather conditions")
}
});
// wunderground limits to 500 api calls a day. Making a call every 4 minutes == 360 calls
setTimeout(this.updateWeatherConditions.bind(this), 4 * 60 * 1000);
}
};