diff --git a/CHANGELOG.md b/CHANGELOG.md index 547899a..74d8007 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -117,3 +117,9 @@ * Added option to rename forecastAccessory * Added plugin version to Accessory Info * Updated debug dependency + +## 2.6.0 + +* Added option to set serial number +* Changed history timer to make use of advantages in newer fakegato version +* Fixed precipitation calculation in dark sky api \ No newline at end of file diff --git a/README.md b/README.md index b2728d2..ef2d4ff 100644 --- a/README.md +++ b/README.md @@ -116,6 +116,8 @@ The **currentObservations** parameter is *optional* and sets how the 3 current o The **fakegatoParameters** parameter is *optional*. By default, history is persisted on filesystem. You can pass your own parameters to *fakegato-history* module using this paramter, in order to change the location of the persisted file or use GoogleDrive persistance. See https://github.com/simont77/fakegato-history#file-system and https://github.com/simont77/fakegato-history#google-drive for more info. **IMPORTANT NOTE:** Do not modify the parameter for the fakegato internal timer. +The **serial** parameter is *optional* and sets the Serial Number of the accessory. If it's not provided the serial number will be set to the **location** if present, or to 999 if not. Note that for proper operation of fakegato when multiple fakegato-enabled weather accessories are present in your system, the serial number must be unique. + ### Dark Sky diff --git a/api/darksky.js b/api/darksky.js index 1e1ad2a..f63532b 100644 --- a/api/darksky.js +++ b/api/darksky.js @@ -85,24 +85,24 @@ var init = function (apiKey, language, locationGeo, l, d) { }); }; -var update = function (callback) { +var update = function (callback, requestedForecastDays) { let weather = {}; weather.forecasts = []; let that = this; - updateCache(this.darkskyTimeMachine, function () { + updateCache(this.darkskyTimeMachine, requestedForecastDays, function () { debug("Updating weather with dark sky"); that.darksky.get() .then(function (response) { // Current weather report - response['currently']['rainDay'] = cache.report.rainDay; // Load rainDay from cache - weather.report = parseReport(response['currently'], response['timezone']); + response.currently.rainDay = cache.report.rainDay; // Load rainDay from cache + weather.report = parseReport(response.currently, response.timezone); // Forecasts for today and next 6 days for (let i = 0; i <= 6; i++) { - response['daily']['data'][i]['rainDay'] = cache.forecast['day' + i].rainDay; // Load rainDay from cache - weather.forecasts.push(parseForecast(response['daily']['data'][i], response['timezone'])); + response.daily.data[i].rainDay = cache.forecast['day' + i].rainDay; // Load rainDay from cache + weather.forecasts.push(parseForecast(response.daily.data[i], response.timezone)); } callback(null, weather); @@ -118,22 +118,22 @@ var update = function (callback) { var parseReport = function (values, timezone) { let report = {}; - report.AirPressure = parseInt(values['pressure']); - report.CloudCover = parseInt(values['cloudCover'] * 100); - report.Condition = values['summary']; - report.ConditionCategory = converter.getConditionCategory(values['icon']); - report.DewPoint = parseInt(values['dewPoint']); - report.Humidity = parseInt(values['humidity'] * 100); - report.ObservationTime = moment.unix(values['time']).tz(timezone).format('HH:mm:ss'); - report.Ozone = parseInt(values['ozone']); - report.Rain1h = isNaN(parseInt(values['precipIntensity'])) ? 0 : parseInt(values['precipIntensity']); - report.RainDay = values['rainDay'] - report.Temperature = values['temperature']; - report.UVIndex = isNaN(parseInt(values['uvIndex'])) ? 0 : parseInt(values['uvIndex']); - report.Visibility = isNaN(parseInt(values['visibility'])) ? 0 : parseInt(values['visibility']); - report.WindDirection = converter.getWindDirection(values['windBearing']); - report.WindSpeed = parseFloat(values['windSpeed']); - report.WindSpeedMax = parseFloat(values['windGust']); + report.AirPressure = parseInt(values.pressure); + report.CloudCover = parseInt(values.cloudCover * 100); + report.Condition = values.summary; + report.ConditionCategory = converter.getConditionCategory(values.icon); + report.DewPoint = parseInt(values.dewPoint); + report.Humidity = parseInt(values.humidity * 100); + report.ObservationTime = moment.unix(values.time).tz(timezone).format('HH:mm:ss'); + report.Ozone = parseInt(values.ozone); + report.Rain1h = isNaN(parseInt(values.precipIntensity)) ? 0 : parseInt(values.precipIntensity); + report.RainDay = values.rainDay; + report.Temperature = values.temperature; + report.UVIndex = isNaN(parseInt(values.uvIndex)) ? 0 : parseInt(values.uvIndex); + report.Visibility = isNaN(parseInt(values.visibility)) ? 0 : parseInt(values.visibility); + report.WindDirection = converter.getWindDirection(values.windBearing); + report.WindSpeed = parseFloat(values.windSpeed); + report.WindSpeedMax = parseFloat(values.windGust); return report; }; @@ -141,34 +141,34 @@ var parseReport = function (values, timezone) { var parseForecast = function (values, timezone, i) { let forecast = {}; - forecast.AirPressure = parseInt(values['pressure']); - forecast.CloudCover = parseInt(values['cloudCover'] * 100); - forecast.Condition = values['summary']; - forecast.ConditionCategory = converter.getConditionCategory(values['icon']); - forecast.DewPoint = parseInt(values['dewPoint']); - forecast.ForecastDay = moment.unix(values['time']).tz(timezone).format('dddd'); - forecast.Humidity = parseInt(values['humidity'] * 100); - forecast.Ozone = parseInt(values['ozone']); - forecast.RainChance = parseInt(values['precipProbability'] * 100); - forecast.RainDay = values['rainDay'] - forecast.Temperature = values['temperatureHigh']; - forecast.TemperatureMin = values['temperatureLow']; - forecast.UVIndex = isNaN(parseInt(values['uvIndex'])) ? 0 : parseInt(values['uvIndex']); - forecast.Visibility = isNaN(parseInt(values['visibility'])) ? 0 : parseInt(values['visibility']); - forecast.WindDirection = converter.getWindDirection(values['windBearing']); - forecast.WindSpeed = parseFloat(values['windSpeed']); - forecast.WindSpeedMax = parseFloat(values['windGust']); + forecast.AirPressure = parseInt(values.pressure); + forecast.CloudCover = parseInt(values.cloudCover * 100); + forecast.Condition = values.summary; + forecast.ConditionCategory = converter.getConditionCategory(values.icon); + forecast.DewPoint = parseInt(values.dewPoint); + forecast.ForecastDay = moment.unix(values.time).tz(timezone).format('dddd'); + forecast.Humidity = parseInt(values.humidity * 100); + forecast.Ozone = parseInt(values.ozone); + forecast.RainChance = parseInt(values.precipProbability * 100); + forecast.RainDay = values.rainDay; + forecast.Temperature = values.temperatureHigh; + forecast.TemperatureMin = values.temperatureLow; + forecast.UVIndex = isNaN(parseInt(values.uvIndex)) ? 0 : parseInt(values.uvIndex); + forecast.Visibility = isNaN(parseInt(values.visibility)) ? 0 : parseInt(values.visibility); + forecast.WindDirection = converter.getWindDirection(values.windBearing); + forecast.WindSpeed = parseFloat(values.windSpeed); + forecast.WindSpeedMax = parseFloat(values.windGust); return forecast; }; -var updateCache = function (api, callback) { +var updateCache = function (api, requestedForecastDays, callback) { if (typeof cache.lastUpdate === 'undefined' || new Date() - cache.lastUpdate > 3600000) { debug("Called hourly update of rain data"); cache.lastUpdate = new Date(); let now = moment(); - let callbacks = 8; + let callbacks = requestedForecastDays + 1; doTimeMachineRequest(api, now, function (result) { cache.report.rainDay = result; @@ -178,7 +178,7 @@ var updateCache = function (api, callback) { } }, true); - for (let i = 0; i <= 6; i++) { + for (let i = 0; i < requestedForecastDays; i++) { doTimeMachineRequest(api, now.clone().add(i, 'd'), function (result) { cache.forecast['day' + i].rainDay = result; callbacks--; @@ -201,12 +201,12 @@ var doTimeMachineRequest = function (api, now, callback, limit = false) { // Current hour in time zone of weather location let hour = 24; if (limit) { - hour = now.tz(response['timezone']).format('H'); + hour = now.tz(response.timezone).format('H'); } // Sum all values for the requested day - debug("Accumulate rain for " + now.tz(response['timezone']).format('dddd') + (limit ? (' until ' + hour + ':00') : '')); - let result = converter.getRainAccumulated(response['hourly']['data'].slice(0, hour), 'precipIntensity'); + debug("Accumulate rain for " + now.tz(response.timezone).format('dddd') + (limit ? (' until ' + hour + ':00') : '')); + let result = converter.getRainAccumulated(response.hourly.data.slice(0, hour), 'precipIntensity'); debug("Accumulated rain: " + result); callback(result); }) diff --git a/api/openweathermap.js b/api/openweathermap.js index 38005f1..65cd872 100644 --- a/api/openweathermap.js +++ b/api/openweathermap.js @@ -57,7 +57,7 @@ var update = function (callback) { let weather = {}; Openweathermap.getAllWeather(function (err, jsonObj) { - if (!err && jsonObj['coord']) { + if (!err && jsonObj.coord) { // Current weather report parseReport(weather, jsonObj, callback); } @@ -69,7 +69,7 @@ var update = function (callback) { }); Openweathermap.getWeatherForecast(function (err, jsonObj) { - if (!err && jsonObj['city']) { + if (!err && jsonObj.city) { parseForecasts(weather, jsonObj, callback); } else { @@ -82,17 +82,17 @@ var update = function (callback) { var parseReport = function (weather, values, callback) { let report = weather.report || {}; - const timezone = geoTz(values['coord']['lat'], values['coord']['lon']); - - report.AirPressure = parseInt(values['main']['pressure']); - report.CloudCover = parseInt(values['clouds']['all']); - report.Condition = values['weather'][0]['description']; - report.ConditionCategory = converter.getConditionCategoryOwm(values['weather'][0]['id']); - report.Humidity = parseInt(values['main']['humidity']); - report.ObservationTime = moment.unix(values['dt']).tz(timezone).format('HH:mm:ss'); - report.Temperature = values['main']['temp']; - report.WindDirection = converter.getWindDirection(values['wind']['deg']); - report.WindSpeed = values['wind']['speed']; + const timezone = geoTz(values.coord.lat, values.coord.lon); + + report.AirPressure = parseInt(values.main.pressure); + report.CloudCover = parseInt(values.clouds.all); + report.Condition = values.weather[0].description; + report.ConditionCategory = converter.getConditionCategoryOwm(values.weather[0].id); + report.Humidity = parseInt(values.main.humidity); + report.ObservationTime = moment.unix(values.dt).tz(timezone).format('HH:mm:ss'); + report.Temperature = values.main.temp; + report.WindDirection = converter.getWindDirection(values.wind.deg); + report.WindSpeed = values.wind.speed; weather.report = report; if (weather.forecasts) { @@ -101,11 +101,11 @@ var parseReport = function (weather, values, callback) { }; var parseForecasts = function (weather, values, callback) { - const timezone = geoTz(values['city']['coord']['lat'], values['city']['coord']['lon']); + const timezone = geoTz(values.city.coord.lat, values.city.coord.lon); let forecasts = []; // We get a forecast for 5 days with values each 3 hours. - const itemsFiltered = prepareForecasts(values['list'], timezone); + const itemsFiltered = prepareForecasts(values.list, timezone); for (let i = 0; i < itemsFiltered.length; i++) { forecasts[forecasts.length] = parseForecast(itemsFiltered[i], timezone); } @@ -119,16 +119,16 @@ var parseForecasts = function (weather, values, callback) { var parseForecast = function (values, timezone) { let forecast = {}; - forecast.AirPressure = parseInt(values['main']['pressure']); - forecast.CloudCover = parseInt(values['clouds']['all']); - forecast.Condition = values['weather'][0]['description']; - forecast.ConditionCategory = converter.getConditionCategoryOwm(values['weather'][0]['id']); - forecast.ForecastDay = moment.unix(values['dt']).tz(timezone).format('dddd'); - forecast.Humidity = parseInt(values['main']['humidity']); - forecast.Temperature = values['main']['temp_max']; - forecast.TemperatureMin = values['main']['temp_min']; - forecast.WindDirection = converter.getWindDirection(values['wind']['deg']); - forecast.WindSpeed = values['wind']['speed']; + forecast.AirPressure = parseInt(values.main.pressure); + forecast.CloudCover = parseInt(values.clouds.all); + forecast.Condition = values.weather[0].description; + forecast.ConditionCategory = converter.getConditionCategoryOwm(values.weather[0].id); + forecast.ForecastDay = moment.unix(values.dt).tz(timezone).format('dddd'); + forecast.Humidity = parseInt(values.main.humidity); + forecast.Temperature = values.main.temp_max; + forecast.TemperatureMin = values.main.temp_min; + forecast.WindDirection = converter.getWindDirection(values.wind.deg); + forecast.WindSpeed = values.wind.speed; return forecast; }; @@ -150,8 +150,8 @@ var prepareForecasts = function (forecasts, timezone) { // Find one for today for (let i = 0; i < forecasts.length; i++) { const listItem = forecasts[i]; - if (listItem['dt'] <= endOfToday) { - const hourOfDay = moment.unix(listItem['dt']).tz(timezone).format("H"); + if (listItem.dt <= endOfToday) { + const hourOfDay = moment.unix(listItem.dt).tz(timezone).format("H"); if (parseInt(hourOfDay) >= 10 && parseInt(hourOfDay) <= 13) { forecastsFiltered[forecastsFiltered.length] = forecasts[i]; } @@ -165,8 +165,8 @@ var prepareForecasts = function (forecasts, timezone) { // For all following days find exactly one value (best: noon) for (let i = 0; i < forecasts.length; i++) { const listItem = forecasts[i]; - if (listItem['dt'] > endOfToday) { - const hourOfDay = moment.unix(listItem['dt']).tz(timezone).format("H"); + if (listItem.dt > endOfToday) { + const hourOfDay = moment.unix(listItem.dt).tz(timezone).format("H"); if (parseInt(hourOfDay) >= 10 && parseInt(hourOfDay) <= 13) { forecastsFiltered[forecastsFiltered.length] = forecasts[i]; } diff --git a/api/weatherunderground.js b/api/weatherunderground.js index 18497b8..530bd42 100644 --- a/api/weatherunderground.js +++ b/api/weatherunderground.js @@ -56,13 +56,13 @@ var update = function (callback) { this.wunderground.conditions().forecast().request(this.location, function (error, response) { if (!error) { // Current weather report - weather.report = parseReport(response['current_observation']); + weather.report = parseReport(response.current_observation); // Forecasts for today and next 3 days - weather.forecasts.push(parseForecast(response['forecast']['simpleforecast']['forecastday'][0])); - weather.forecasts.push(parseForecast(response['forecast']['simpleforecast']['forecastday'][1])); - weather.forecasts.push(parseForecast(response['forecast']['simpleforecast']['forecastday'][2])); - weather.forecasts.push(parseForecast(response['forecast']['simpleforecast']['forecastday'][3])); + weather.forecasts.push(parseForecast(response.forecast.simpleforecast.forecastday[0])); + weather.forecasts.push(parseForecast(response.forecast.simpleforecast.forecastday[1])); + weather.forecasts.push(parseForecast(response.forecast.simpleforecast.forecastday[2])); + weather.forecasts.push(parseForecast(response.forecast.simpleforecast.forecastday[3])); callback(null, weather); } else { @@ -76,21 +76,21 @@ var update = function (callback) { var parseReport = function (values) { let report = {}; - report.AirPressure = parseInt(values['pressure_mb']); - report.Condition = values['weather']; - report.ConditionCategory = converter.getConditionCategory(values['icon']); - report.Humidity = parseInt(values['relative_humidity'].substr(0, values['relative_humidity'].length - 1)); - report.ObservationStation = values['observation_location']['full']; - report.ObservationTime = values['observation_time_rfc822'].split(' ')[4]; - report.Rain1h = isNaN(parseInt(values['precip_1hr_metric'])) ? 0 : parseInt(values['precip_1hr_metric']); - report.RainDay = isNaN(parseInt(values['precip_today_metric'])) ? 0 : parseInt(values['precip_today_metric']); - report.SolarRadiation = isNaN(parseInt(values['solarradiation'])) ? 0 : parseInt(values['solarradiation']); - report.Temperature = values['temp_c']; - report.UVIndex = isNaN(parseInt(values['UV'])) ? 0 : parseInt(values['UV']); - report.Visibility = isNaN(parseInt(values['visibility_km'])) ? 0 : parseInt(values['visibility_km']); - report.WindDirection = values['wind_dir']; - report.WindSpeed = parseFloat(values['wind_kph']); - report.WindSpeedMax = parseFloat(values['wind_gust_kph']); + report.AirPressure = parseInt(values.pressure_mb); + report.Condition = values.weather; + report.ConditionCategory = converter.getConditionCategory(values.icon); + report.Humidity = parseInt(values.relative_humidity.substr(0, values.relative_humidity.length - 1)); + report.ObservationStation = values.observation_location.full; + report.ObservationTime = values.observation_time_rfc822.split(' ')[4]; + report.Rain1h = isNaN(parseInt(values.precip_1hr_metric)) ? 0 : parseInt(values.precip_1hr_metric); + report.RainDay = isNaN(parseInt(values.precip_today_metric)) ? 0 : parseInt(values.precip_today_metric); + report.SolarRadiation = isNaN(parseInt(values.solarradiation)) ? 0 : parseInt(values.solarradiation); + report.Temperature = values.temp_c; + report.UVIndex = isNaN(parseInt(values.UV)) ? 0 : parseInt(values.UV); + report.Visibility = isNaN(parseInt(values.visibility_km)) ? 0 : parseInt(values.visibility_km); + report.WindDirection = values.wind_dir; + report.WindSpeed = parseFloat(values.wind_kph); + report.WindSpeedMax = parseFloat(values.wind_gust_kph); return report; }; @@ -98,17 +98,17 @@ var parseReport = function (values) { var parseForecast = function (values) { let forecast = {}; - forecast.Condition = values['conditions']; - forecast.ConditionCategory = converter.getConditionCategory(values['icon']); - forecast.ForecastDay = values['date']['weekday']; - forecast.Humidity = parseInt(values['avehumidity']) - forecast.RainChance = values['pop']; - forecast.RainDay = isNaN(parseInt(values['qpf_allday']['mm'])) ? 0 : parseInt(values['qpf_allday']['mm']); - forecast.Temperature = values['high']['celsius']; - forecast.TemperatureMin = values['low']['celsius']; - forecast.WindDirection = values['avewind']['dir']; - forecast.WindSpeed = parseFloat(values['avewind']['kph']); - forecast.WindSpeedMax = parseFloat(values['maxwind']['kph']); + forecast.Condition = values.conditions; + forecast.ConditionCategory = converter.getConditionCategory(values.icon); + forecast.ForecastDay = values.date.weekday; + forecast.Humidity = parseInt(values.avehumidity); + forecast.RainChance = values.pop; + forecast.RainDay = isNaN(parseInt(values.qpf_allday.mm)) ? 0 : parseInt(values.qpf_allday.mm); + forecast.Temperature = values.high.celsius; + forecast.TemperatureMin = values.low.celsius; + forecast.WindDirection = values.avewind.dir; + forecast.WindSpeed = parseFloat(values.avewind.kph); + forecast.WindSpeedMax = parseFloat(values.maxwind.kph); return forecast; }; diff --git a/api/yahoo.js b/api/yahoo.js index c1aad77..412f94a 100644 --- a/api/yahoo.js +++ b/api/yahoo.js @@ -44,7 +44,7 @@ var update = function (callback) { if (!err) { // Current weather report const jsonObj = JSON.parse(body); - parseReport(jsonObj['query']['results']['channel'], callback); + parseReport(jsonObj.query.results.channel, callback); } else { log.error("Error retrieving weather report and forecast"); log.error("Error Message: " + err); @@ -55,25 +55,25 @@ var update = function (callback) { var parseReport = function (values, callback) { let report = {}; - const timezone = geoTz(parseFloat(values['item']['lat']), parseFloat(values['item']['long'])); + const timezone = geoTz(parseFloat(values.item.lat), parseFloat(values.item.long)); debug("Using Timezone: " + timezone); - report.AirPressure = parseInt(values['atmosphere']['pressure']); - report.Condition = values['item']['condition']['text']; - report.ConditionCategory = converter.getConditionCategoryYahoo(parseInt(values['item']['condition']['code'])); - report.ForecastDay = moment(values['item']['forecast'][0]['date'], "DD MMM YYYY").tz(timezone).format("dddd"); - report.Humidity = parseInt(values['atmosphere']['humidity']); - report.ObservationTime = moment(values['item']['pubDate'].substr(17), "hh:mm A [CEST]").tz(timezone).format('HH:mm:ss'); - report.Temperature = parseInt(values['item']['condition']['temp']); - report.TemperatureMin = parseInt(values['item']['forecast'][0]['low']); - report.Visibility = parseFloat(values['atmosphere']['visibility']); - report.WindDirection = converter.getWindDirection(parseInt(values['wind']['direction'])); - report.WindSpeed = parseFloat(values['wind']['speed']); + report.AirPressure = parseInt(values.atmosphere.pressure); + report.Condition = values.item.condition.text; + report.ConditionCategory = converter.getConditionCategoryYahoo(parseInt(values.item.condition.code)); + report.ForecastDay = moment(values.item.forecast[0].date, "DD MMM YYYY").tz(timezone).format("dddd"); + report.Humidity = parseInt(values.atmosphere.humidity); + report.ObservationTime = moment(values.item.pubDate.substr(17), "hh:mm A [CEST]").tz(timezone).format('HH:mm:ss'); + report.Temperature = parseInt(values.item.condition.temp); + report.TemperatureMin = parseInt(values.item.forecast[0].low); + report.Visibility = parseFloat(values.atmosphere.visibility); + report.WindDirection = converter.getWindDirection(parseInt(values.wind.direction)); + report.WindSpeed = parseFloat(values.wind.speed); const weather = {}; weather.report = report; - weather.forecasts = parseForecasts(values['item']['forecast'], timezone); - callback(null, weather) + weather.forecasts = parseForecasts(values.item.forecast, timezone); + callback(null, weather); }; var parseForecasts = function (forecastObjs, timezone) { @@ -81,11 +81,11 @@ var parseForecasts = function (forecastObjs, timezone) { for (let i = 0; i < forecastObjs.length; i++) { const values = forecastObjs[i]; const forecast = {}; - forecast.Condition = values['text']; - forecast.ConditionCategory = converter.getConditionCategoryOwm(parseInt(values['code'])); - forecast.ForecastDay = moment(values['date'], "DD MMM YYYY").tz(timezone).format("dddd"); - forecast.Temperature = values['high']; - forecast.TemperatureMin = values['low']; + forecast.Condition = values.text; + forecast.ConditionCategory = converter.getConditionCategoryOwm(parseInt(values.code)); + forecast.ForecastDay = moment(values.date, "DD MMM YYYY").tz(timezone).format("dddd"); + forecast.Temperature = values.high; + forecast.TemperatureMin = values.low; forecasts[forecasts.length] = forecast; } return forecasts; diff --git a/index.js b/index.js index b648b55..d65b183 100644 --- a/index.js +++ b/index.js @@ -31,17 +31,19 @@ function WeatherStationPlatform(log, config, api) { debug("Init platform"); this.log = log; this.config = config; - this.displayName = config['displayName']; - this.displayNameForecast = config['displayNameForecast']; - this.key = config['key']; - this.units = config['units'] || 'si'; - this.location = config['location']; - this.locationGeo = config['locationGeo']; - this.locationCity = config['locationCity']; - this.forecastDays = ('forecast' in config ? config['forecast'] : []); - this.language = ('language' in config ? config['language'] : 'en'); - this.currentObservationsMode = ('currentObservations' in config ? config['currentObservations'] : 'normal'); - this.fakegatoParameters = ('fakegatoParameters' in config ? config['fakegatoParameters'] : {storage:'fs'}); + this.displayName = config.displayName; + this.displayNameForecast = config.displayNameForecast; + this.key = config.key; + this.units = config.units || 'si'; + this.location = config.location; + this.locationGeo = config.locationGeo; + this.locationCity = config.locationCity; + this.forecastDays = ('forecast' in config ? config.forecast : []); + this.language = ('language' in config ? config.language : 'en'); + this.currentObservationsMode = ('currentObservations' in config ? config.currentObservations : 'normal'); + this.fakegatoParameters = ('fakegatoParameters' in config ? config.fakegatoParameters : {storage:'fs'}); + this.serial = config.serial || config.location || 999; + // Custom Characteristics CustomCharacteristic = require('./util/characteristics')(api, this.units); @@ -50,7 +52,7 @@ function WeatherStationPlatform(log, config, api) { CustomService = require('./util/services')(api); // API Service - let service = config['service'].toLowerCase().replace(/\s/g, ''); + let service = config.service.toLowerCase().replace(/\s/g, ''); if (service === 'darksky') { debug("Using service dark sky"); // TODO adapt unit of characteristics @@ -77,7 +79,7 @@ function WeatherStationPlatform(log, config, api) { } // Update interval - this.interval = ('interval' in config ? parseInt(config['interval']) : 4); + 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; } @@ -119,6 +121,13 @@ WeatherStationPlatform.prototype = { const name = that.api.reportCharacteristics[i]; that.saveCharacteristic(service, name, data[name]); } + debug("Saving history entry"); + that.accessories[i].historyService.addEntry({ + time: new Date().getTime() / 1000, + temp: that.accessories[i].currentConditionsService.getCharacteristic(Characteristic.CurrentTemperature).value, + pressure: that.accessories[i].currentConditionsService.getCharacteristic(CustomCharacteristic.AirPressure).value, + humidity: that.accessories[i].currentConditionsService.getCharacteristic(Characteristic.CurrentRelativeHumidity).value + }); } catch (error) { that.log.error("Exception while parsing weather report: " + error); @@ -143,7 +152,7 @@ WeatherStationPlatform.prototype = { } } } - }); + }, this.forecastDays.length); setTimeout(this.updateWeather.bind(this), (this.interval) * 60 * 1000); }, @@ -163,27 +172,6 @@ WeatherStationPlatform.prototype = { service.setCharacteristic(CustomCharacteristic[name], value); } }, - - // Add history entry - addHistory: function () { - debug("Saving history entry"); - - for (var i = 0; i < this.accessories.length; i++) { - if (this.accessories[i] !== undefined && this.accessories[i].currentConditionsService !== undefined) { - // Add entry to history - this.accessories[i].historyService.addEntry({ - time: new Date().getTime() / 1000, - temp: this.accessories[i].currentConditionsService.getCharacteristic(Characteristic.CurrentTemperature).value, - pressure: this.accessories[i].currentConditionsService.getCharacteristic(CustomCharacteristic.AirPressure).value, - humidity: this.accessories[i].currentConditionsService.getCharacteristic(Characteristic.CurrentRelativeHumidity).value - }); - break; - } - } - - // Call function every 9:50 minutes (a new entry every 10 minutes is required to avoid gaps in the graph) - setTimeout(this.addHistory.bind(this), (10 * 60 * 1000) - 10000); - } }; // =============================== @@ -211,7 +199,7 @@ function CurrentConditionsWeatherAccessory(platform) { // humidity not a custom but a general apple home kit characteristic if (name === 'Humidity') { - this.currentConditionsService.addCharacteristic(Characteristic['CurrentRelativeHumidity']); + this.currentConditionsService.addCharacteristic(Characteristic.CurrentRelativeHumidity); } // temperature is already in the service else if (name !== 'Temperature') { @@ -224,12 +212,11 @@ function CurrentConditionsWeatherAccessory(platform) { this.informationService .setCharacteristic(Characteristic.Manufacturer, "github.com naofireblade") .setCharacteristic(Characteristic.Model, this.platform.api.attribution) - .setCharacteristic(Characteristic.SerialNumber, this.platform.location || 999) + .setCharacteristic(Characteristic.SerialNumber, this.platform.serial) .setCharacteristic(Characteristic.FirmwareRevision, version); // Create history service this.historyService = new FakeGatoHistoryService("weather", this, this.platform.fakegatoParameters); - setTimeout(this.platform.addHistory.bind(this.platform), 10000); // Start the weather update process this.platform.updateWeather(); @@ -279,7 +266,7 @@ function ForecastWeatherAccessory(platform, day) { // humidity not a custom but a general apple home kit characteristic if (name === 'Humidity') { - this.forecastService.addCharacteristic(Characteristic['CurrentRelativeHumidity']); + this.forecastService.addCharacteristic(Characteristic.CurrentRelativeHumidity); } // temperature is already in the service else if (name !== 'Temperature') { @@ -292,7 +279,7 @@ function ForecastWeatherAccessory(platform, day) { this.informationService .setCharacteristic(Characteristic.Manufacturer, "github.com naofireblade") .setCharacteristic(Characteristic.Model, this.platform.api.attribution) - .setCharacteristic(Characteristic.SerialNumber, this.platform.location || this.day) + .setCharacteristic(Characteristic.SerialNumber, this.serial + " Day " + this.day) .setCharacteristic(Characteristic.FirmwareRevision, version); } diff --git a/package.json b/package.json index c256c80..a6d6605 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "homebridge-weather-plus", - "version": "2.5.0", + "version": "2.6.0", "description": "A comprehensive weather plugin for homekit with current observations, forecasts and history.", "license": "MIT", "keywords": [