forked from naofireblade/homebridge-weather-plus
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
345 lines (299 loc) · 13.4 KB
/
Copy pathindex.js
File metadata and controls
345 lines (299 loc) · 13.4 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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
/*jshint esversion: 6,node: true,-W041: false */
"use strict";
const darksky = require('./api/darksky').DarkSkyAPI,
weatherunderground = require('./api/weatherunderground').WundergroundAPI,
openweathermap = require('./api/openweathermap').OpenWeatherMapAPI,
yahoo = require('./api/yahoo').YahooAPI,
debug = require('debug')('homebridge-weather-plus'),
version = require('./package.json').version;
var Service,
Characteristic,
CustomCharacteristic,
CustomService,
FakeGatoHistoryService;
module.exports = function (homebridge) {
Service = homebridge.hap.Service;
// Homekit Characteristics
Characteristic = homebridge.hap.Characteristic;
// History Service
FakeGatoHistoryService = require('fakegato-history')(homebridge);
homebridge.registerPlatform("homebridge-weather-plus", "WeatherPlus", WeatherStationPlatform);
};
// ============
// = Platform =
// ============
function WeatherStationPlatform(log, config, api) {
debug("Init WeatherStationPlus platform");
this.log = log;
this.debug = debug;
this.units = config.units || 'si';
this.config = config;
this.interval = ('interval' in config ? parseInt(config.interval) : 4);
this.interval = (typeof this.interval !== 'number' || (this.interval % 1) !== 0 || this.interval < 0) ? 4 : this.interval;
this.apis = [];
this.accessoriesList = [];
// Custom Characteristics
CustomCharacteristic = require('./util/characteristics')(api, this.units);
// Custom Services
CustomService = require('./util/services')(api);
//set default settings and provides backward compatibility with old config file
if (!('stations' in config)) {
this.config.stations = [{}];
this.config.stations[0].displayName = config.displayName || "Now";
this.config.stations[0].displayNameForecast = config.displayNameForecast;
this.config.stations[0].service = config.service;
this.config.stations[0].key = config.key;
this.config.stations[0].units = this.units;
this.config.stations[0].location = config.location;
this.config.stations[0].locationGeo = config.locationGeo;
this.config.stations[0].locationCity = config.locationCity;
this.config.stations[0].forecast = ('forecast' in config ? config.forecast : []);
this.config.stations[0].language = ('language' in config ? config.language : 'en');
this.config.stations[0].currentObservationsMode = ('currentObservations' in config ? config.currentObservations : 'normal');
this.hiddenServices = '';
this.hiddenServices = ('disabled' in config ? config['disabled'] : this.hiddenServices);
this.hiddenServices = ('hidden' in config ? config['hidden'] : this.hiddenServices);
this.config.stations[0].fakegatoParameters = ('fakegatoParameters' in config ? config.fakegatoParameters : { storage: 'fs' });
this.config.stations[0].serial = config.serial || config.location || 999;
this.stationsNumber = 1;
}
else {
this.stationsNumber = this.config.stations.length;
this.config.stations.forEach(function (station, stationIndex) {
if (this.stationsNumber > 1) {
station.displayName = station.displayName || ("Now" + " - " + (stationIndex + 1));
station.serial = station.serial || station.location || ("999 - " + (stationIndex + 1));
}
else {
station.displayName = station.displayName || ("Now");
station.serial = station.serial || station.location || ("999");
}
station.units = this.units;
station.forecast = ('forecast' in station ? station.forecast : []);
station.language = ('language' in station ? station.language : 'en');
station.compatibility = 'mix';
station.compatibility = ('currentObservations' in station ? station.currentObservations : station.compatibility); // old config key word
station.compatibility = ('compatibility' in station ? station.currentObservations : station.compatibility);
station.fakegatoParameters = ('fakegatoParameters' in station ? station.fakegatoParameters : { storage: 'fs' });
}.bind(this));
}
//create accessories
this.config.stations.forEach(function (station, index) {
let service = station.service.toLowerCase().replace(/\s/g, '');
if (service === 'darksky') {
debug("Using service dark sky");
// TODO adapt unit of characteristics
if (station.location) {
station.locationGeo = station.location;
}
this.apis.push(new darksky(station.key, station.language, station.locationGeo, this.log, this.debug));
}
else if (service === 'weatherunderground') {
debug("Using service weather underground");
this.apis.push(new weatherunderground(station.key, station.location, log, debug));
}
else if (service === 'openweathermap') {
debug("Using service OpenWeatherMap");
this.apis.push(new openweathermap(station.key, station.language, station.location, station.locationGeo, station.locationCity, this.log, this.debug));
}
else if (service === 'yahoo') {
debug("Using service Yahoo");
this.apis.push(new yahoo(this.location, log, debug));
}
this.accessoriesList.push(new CurrentConditionsWeatherAccessory(this, index));
// Add all configured forecast days
for (let i = 0; i < station.forecast.length; i++) {
const day = station.forecast[i];
if (typeof day === 'number' && (day % 1) === 0 && day >= 1 && day <= this.apis[index].forecastDays) {
this.accessoriesList.push(new ForecastWeatherAccessory(this, index, day - 1));
}
else {
debug("Ignoring forecast day: " + day);
}
}
}.bind(this));
//start updating
this.updateWeather();
}
WeatherStationPlatform.prototype = {
// Get the current condition accessory and all forecast accessories
accessories: function (callback) {
this.debug("WeatherStationPlus: accessoriesList readed");
callback(this.accessoriesList);
},
// Update the weather for all accessories
updateWeather: function () {
this.apis.forEach(function (station, stationIndex) {
station.update(function (error, weather) {
if (!error) {
for (var i = 0; i < this.accessoriesList.length; i++) {
// Add current weather conditions
if (this.accessoriesList[i].currentConditionsService !== undefined && weather.report !== undefined && this.accessoriesList[i].stationIndex == stationIndex) {
try {
let service = this.accessoriesList[i].currentConditionsService;
let data = weather.report;
for (let i = 0; i < this.apis[stationIndex].reportCharacteristics.length; i++) {
const name = this.apis[stationIndex].reportCharacteristics[i];
this.saveCharacteristic(service, name, data[name]);
}
debug("Saving history entry");
this.accessoriesList[i].historyService.addEntry({
time: new Date().getTime() / 1000,
temp: this.accessoriesList[i].currentConditionsService.getCharacteristic(Characteristic.CurrentTemperature).value,
pressure: this.accessoriesList[i].currentConditionsService.getCharacteristic(CustomCharacteristic.AirPressure).value,
humidity: this.accessoriesList[i].currentConditionsService.getCharacteristic(Characteristic.CurrentRelativeHumidity).value
});
}
catch (error) {
this.log.error("Exception while parsing weather report: " + error);
this.log.error("Report: " + weather.report);
}
}
// Add a weather forecast for the given day
else if (this.accessoriesList[i].forecastService !== undefined && weather.forecasts[this.accessoriesList[i].day] !== undefined && this.accessoriesList[i].stationIndex == stationIndex) {
try {
let service = this.accessoriesList[i].forecastService;
let data = weather.forecasts[this.accessoriesList[i].day];
for (let i = 0; i < this.apis[stationIndex].forecastCharacteristics.length; i++) {
const name = this.apis[stationIndex].forecastCharacteristics[i];
this.saveCharacteristic(service, name, data[name]);
}
}
catch (error) {
this.log.error("Exception while parsing weather forecast: " + error);
this.log.error("Forecast: " + weather.forecast);
}
}
}
}
}.bind(this), this.config.stations[stationIndex].forecast.length);
}.bind(this));
setTimeout(this.updateWeather.bind(this), (this.interval) * 60 * 1000);
},
// Save changes from update in characteristics
saveCharacteristic: function (service, name, value) {
// humidity not a custom but a general apple home kit characteristic
if (name === 'Humidity') {
debug("Characteristic:" + name + ":" + value);
service.setCharacteristic(Characteristic.CurrentRelativeHumidity, value);
}
// temperature not a custom but a general apple home kit characteristic
else if (name === 'Temperature') {
debug("Characteristic:" + name + ":" + value);
service.setCharacteristic(Characteristic.CurrentTemperature, value);
}
// all other custom characteristics
else {
if (CustomCharacteristic[name]._unitvalue) value = CustomCharacteristic[name]._unitvalue(value);
debug("CustomCharacteristic:" + name + ":" + value);
service.setCharacteristic(CustomCharacteristic[name], value);
}
},
};
// ===============================
// = Current Condition Accessory =
// ===============================
function CurrentConditionsWeatherAccessory(platform, stationIndex) {
this.platform = platform;
this.log = platform.log;
this.config = platform.config.stations[stationIndex];
this.name = this.config.displayName;
this.displayName = this.name; //needed by fakegato for proper logging and file naming
this.stationIndex = stationIndex;
// Create temperature sensor or Eve Weather service that includes temperature characteristic
if (this.config.compatibility !== 'eve')
this.currentConditionsService = new Service.TemperatureSensor(this.name);
else
this.currentConditionsService = new CustomService.EveWeatherService(this.name);
// Fix negative temperatures not supported by homekit
this.currentConditionsService.getCharacteristic(Characteristic.CurrentTemperature).props.minValue = -50;
// Add additional characteristics to temperature sensor that are supported by the selected api
for (let i = 0; i < this.platform.apis[stationIndex].reportCharacteristics.length; i++) {
const name = this.platform.apis[stationIndex].reportCharacteristics[i];
debug("Characteristic-name:" + name);
// humidity not a custom but a general apple home kit characteristic
if (name === 'Humidity') {
this.currentConditionsService.addCharacteristic(Characteristic.CurrentRelativeHumidity);
}
// temperature is already in the service
else if (name !== 'Temperature') {
this.currentConditionsService.addCharacteristic(CustomCharacteristic[name]);
}
}
// Create information service
this.informationService = new Service.AccessoryInformation();
this.informationService
.setCharacteristic(Characteristic.Manufacturer, "github.com naofireblade")
.setCharacteristic(Characteristic.Model, this.platform.apis[stationIndex].attribution)
.setCharacteristic(Characteristic.SerialNumber, this.config.serial)
.setCharacteristic(Characteristic.FirmwareRevision, version);
// Create history service
this.historyService = new FakeGatoHistoryService("weather", this, this.config.fakegatoParameters);
// Start the weather update process
}
CurrentConditionsWeatherAccessory.prototype = {
identify: function (callback) {
callback();
},
getServices: function () {
return [this.informationService, this.currentConditionsService, this.historyService];
}
};
// ======================
// = Forecast Accessory =
// ======================
function ForecastWeatherAccessory(platform, stationIndex, day) {
this.platform = platform;
this.log = platform.log;
this.config = platform.config.stations[stationIndex];
this.stationIndex = stationIndex;
this.serial = this.config.serial + " - Day " + day;
switch (day) {
case 0:
this.name = "Today";
break;
case 1:
this.name = "In 1 Day";
break;
default:
this.name = "In " + day + " Days";
break;
}
if (this.config.displayNameForecast)
this.name = this.config.displayNameForecast + ' ' + this.name;
else
if (platform.stationsNumber > 1)
this.name = this.name + " - " + (stationIndex + 1);
this.day = day;
// Create temperature sensor service that includes temperature characteristic
this.forecastService = new Service.TemperatureSensor(this.name);
// Fix negative temperatures not supported by homekit
this.forecastService.getCharacteristic(Characteristic.CurrentTemperature).props.minValue = -50;
// Add additional characteristics to temperature sensor that are supported by the selected api
for (let i = 0; i < this.platform.apis[stationIndex].forecastCharacteristics.length; i++) {
const name = this.platform.apis[stationIndex].forecastCharacteristics[i];
// humidity not a custom but a general apple home kit characteristic
if (name === 'Humidity') {
this.forecastService.addCharacteristic(Characteristic.CurrentRelativeHumidity);
}
// temperature is already in the service
else if (name !== 'Temperature') {
this.forecastService.addCharacteristic(CustomCharacteristic[name]);
}
}
// Create information service
this.informationService = new Service.AccessoryInformation();
this.informationService
.setCharacteristic(Characteristic.Manufacturer, "github.com naofireblade")
.setCharacteristic(Characteristic.Model, this.platform.apis[stationIndex].attribution)
.setCharacteristic(Characteristic.SerialNumber, this.serial)
.setCharacteristic(Characteristic.FirmwareRevision, version);
}
ForecastWeatherAccessory.prototype = {
identify: function (callback) {
callback();
},
getServices: function () {
return [this.informationService, this.forecastService];
}
};