Skip to content

Commit

Permalink
Merge pull request #38 from werthdavid/master
Browse files Browse the repository at this point in the history
Yahoo API
  • Loading branch information
naofireblade authored Aug 31, 2018
2 parents dcf5063 + e7140db commit a48b924
Show file tree
Hide file tree
Showing 7 changed files with 493 additions and 99 deletions.
8 changes: 6 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,12 +96,16 @@

## 2.1.0

* Added support for OpenWeatherMap api
* Added support for the OpenWeatherMap api

## 2.2.0

* Added support for different units

## 2.3.0

* Added displayName parameter
* Added displayName parameter

## 2.4.0

* Added support for the Yahoo api
39 changes: 29 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,15 +67,15 @@ With the eve app you can view the history for

This plugin supports multiple weather services. Each has it's own advantages. The following table shows a comparison to help you choosing one.

| | Dark Sky (recommended) | OpenWeatherMap | Weather Underground (legacy) |
|----------------------------|:--------------------------------------------:|:----------------------------------------------------------------:|:----------------------------------------------------------------:|
| Current observation values | 15 | 7 | 13 |
| Forecast values | 16 | 9 | 10 |
| Forecast days | 7 | 5 | 4 |
| Location | geo-coordinates | city name, city id, geo-coordinates | city name or zip |
| Personal weather stations | :x: | :heavy_check_mark: | :heavy_check_mark: |
| Free | :heavy_check_mark: | :heavy_check_mark: | :x: (only legacy accounts) |
| Register | [here](https://darksky.net/dev/register) | [here](https://openweathermap.org/appid) | [here](https://www.wunderground.com/weather/api/) |
| | Dark Sky (recommended) | OpenWeatherMap | Yahoo | Weather Underground (legacy) |
|----------------------------|:--------------------------------------------:|:----------------------------------------------------------------:|:----------------------------------------------------------------:|:----------------------------------------------------------------:|
| Current observation values | 15 | 7 | 10 | 13 |
| Forecast values | 16 | 9 | 4 | 10 |
| Forecast days | 7 | 5 | 10 | 4 |
| Location | geo-coordinates | city name, city id, geo-coordinates | city name | city name or zip |
| Personal weather stations | :x: | :heavy_check_mark: | :x: | :heavy_check_mark: |
| Free | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: | :x: (only legacy accounts) |
| Register | [here](https://darksky.net/dev/register) | [here](https://openweathermap.org/appid) | not needed | [here](https://www.wunderground.com/weather/api/) |

*You can add more services by forking the project and submitting a pull request.*

Expand Down Expand Up @@ -152,6 +152,24 @@ The **locationGeo** parameter must be a list with the latitude longitude for you
]
```

### Yahoo

The **location** parameter is a text for finding a location with YQL (see [here](https://developer.yahoo.com/weather/))

The **forecast** parameter is *optional* and defines a list of forecast days with 1 for today, 2 for tomorrow etc. Default are none.

```json
"platforms": [
{
"platform": "WeatherPlus",
"name": "WeatherPlus",
"service": "yahoo",
"location": "Berlin, DE",
"forecast": [1,2,3,4,5,6,7]
}
]
```

### Weather Underground

The **location** parameter can be a city name or a zip. You can also use a station from the **[Personal Weather Station Network](https://www.wunderground.com/weatherstation/overview.asp)** to receive weather information. Just enter pws:YOURID.
Expand Down Expand Up @@ -188,7 +206,7 @@ Many thanks go to
- [Clark Endrizzi](https://github.com/cendrizzi) for his wundergroundnode library
- [simont77](https://github.com/simont77) for his fakegato-history library
- [GatoPharaoh](https://github.com/GatoPharaoh) for his interval option pull request
- [David Werth](https://github.com/werthdavid) for integrating the openweathermap api
- [David Werth](https://github.com/werthdavid) for integrating the OpenWeatherMap and Yahoo apis
- [Marshall T. Rose](https://github.com/mrose17) for adding support for imperial units and the displayName parameter

This plugin is a fork of [homebridge-weather-station](https://github.com/kcharwood/homebridge-weather-station) which is no longer being developed. That one is a fork of [homebridge-wunderground](https://www.npmjs.com/package/homebridge-wunderground).
Expand All @@ -197,3 +215,4 @@ This plugin is a fork of [homebridge-weather-station](https://github.com/kcharwo
- [Powered by Dark Sky](https://darksky.net/poweredby/)
- [Powered by Weather Underground](https://www.wunderground.com/)
- [Powered by OpenWeatherMap](https://openweathermap.org/)
- [Powered by Yahoo](https://yahoo.com/)
100 changes: 100 additions & 0 deletions api/yahoo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
"use strict";

const request = require('request'),
converter = require('../util/converter'),
moment = require('moment-timezone'),
geoTz = require('geo-tz'),

attribution = 'Powered by Yahoo',
reportCharacteristics = [
'AirPressure',
'Condition',
'ConditionCategory',
'ForecastDay',
'Humidity',
'Temperature',
'TemperatureMin',
'Visibility',
'WindDirection',
'WindSpeed'
],
forecastCharacteristics = [
'Condition',
'ConditionCategory',
'ForecastDay',
'Temperature',
'TemperatureMin'
],
forecastDays = 10;

var debug, log;

var init = function (location, l, d) {
this.location = location;
log = l;
debug = d;
};

var update = function (callback) {
debug("Updating weather with Yahoo");

const queryUri = `https://query.yahooapis.com/v1/public/yql?q=select * from weather.forecast where u='c' AND woeid in (select woeid from geo.places(1) where text="${this.location}")&format=json`;
request(encodeURI(queryUri), function (err, response, body) {
if (!err) {
// Current weather report
const jsonObj = JSON.parse(body);
parseReport(jsonObj['query']['results']['channel'], callback);
} else {
log.error("Error retrieving weather report and forecast");
log.error("Error Message: " + err);
callback(err);
}
});
};

var parseReport = function (values, callback) {
let report = {};
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']);

const weather = {};
weather.report = report;
weather.forecasts = parseForecasts(values['item']['forecast'], timezone);
callback(null, weather)
};

var parseForecasts = function (forecastObjs, timezone) {
let forecasts = [];
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'];
forecasts[forecasts.length] = forecast;
}
return forecasts;
};

module.exports = {
init,
update,
reportCharacteristics,
forecastCharacteristics,
forecastDays,
attribution
};
6 changes: 6 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
const darksky = require('./api/darksky'),
weatherunderground = require('./api/weatherunderground'),
openweathermap = require('./api/openweathermap'),
yahoo = require('./api/yahoo'),
debug = require('debug')('homebridge-weather-plus');

var Service,
Expand Down Expand Up @@ -60,6 +61,11 @@ function WeatherStationPlatform(log, config, api) {
openweathermap.init(this.key, this.language, this.location, this.locationGeo, this.locationCity, log, debug);
this.api = openweathermap;
}
else if (service === 'yahoo') {
debug("Using service Yahoo");
yahoo.init(this.location, log, debug);
this.api = yahoo;
}

// Update interval
this.interval = ('interval' in config ? parseInt(config['interval']) : 4);
Expand Down
Loading

0 comments on commit a48b924

Please sign in to comment.