Skip to content

Commit

Permalink
Fix API version in yr weather provider call (#3223)
Browse files Browse the repository at this point in the history
Fixes #3227 

and also
- removes unused code
- de-duplicates code fragments
- fixes typos
- inlines code
- adds more weather util tests

@martingron and @Justheretoreportanissue would you be so kind to check
if I didnt mess anything up?
  • Loading branch information
rejas authored Oct 15, 2023
1 parent 5d2ddbd commit a0b444d
Show file tree
Hide file tree
Showing 5 changed files with 120 additions and 141 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ _This release is scheduled to be released on 2024-01-01._
### Fixed

- Fix the option eventClass on customEvents.
- Fix yr API version in locationforecast call (#3227)

## [2.25.0] - 2023-10-01

Expand Down Expand Up @@ -75,6 +76,7 @@ Special thanks to @khassel, @rejas and @sdetweil for taking over most (if not al
- Fix `Uncaught SyntaxError: Identifier 'getCorsUrl' has already been declared (at utils.js:1:1)` when using `clock` and `weather` module (#3204)
- Fix overriding `config.js` when running tests (#3201)
- Fix issue in weathergov provider with probability of precipitation not showing up on hourly or daily forecast
- Fix yr weather provider after changes in yr API (#3189)

## [2.24.0] - 2023-07-01

Expand Down Expand Up @@ -117,7 +119,6 @@ Special thanks to @khassel, @rejas and @sdetweil for taking over most (if not al
- Fix date not shown when clock in analog mode (#3100)
- Fix envcanada today percentage-of-precipitation (#3106)
- Fix updatenotification where no branch is checked out but e.g. a version tag (#3130)
- Fix yr weather provider after changes in yr API (#3189)

## [2.23.0] - 2023-04-04

Expand Down
52 changes: 16 additions & 36 deletions modules/default/weather/providers/yr.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ WeatherProvider.register("yr", {
defaults: {
useCorsProxy: true,
apiBase: "https://api.met.no/weatherapi",
forecastApiVersion: "3.0",
sunriseApiVersion: "3.0",
altitude: 0,
currentForecastHours: 1 //1, 6 or 12
},
Expand All @@ -44,8 +46,7 @@ WeatherProvider.register("yr", {
},

async getCurrentWeather() {
const getRequests = [this.getWeatherData(), this.getStellarData()];
const [weatherData, stellarData] = await Promise.all(getRequests);
const [weatherData, stellarData] = await Promise.all([this.getWeatherData(), this.getStellarData()]);
if (!stellarData) {
Log.warn("No stellar data available.");
}
Expand Down Expand Up @@ -170,7 +171,7 @@ WeatherProvider.register("yr", {
});
},

getForecastUrl() {
getConfigOptions() {
if (!this.config.lat) {
Log.error("Latitude not provided.");
throw new Error("Latitude not provided.");
Expand All @@ -183,6 +184,11 @@ WeatherProvider.register("yr", {
let lat = this.config.lat.toString();
let lon = this.config.lon.toString();
const altitude = this.config.altitude ?? 0;
return { lat, lon, altitude };
},

getForecastUrl() {
let { lat, lon, altitude } = this.getConfigOptions();

if (lat.includes(".") && lat.split(".")[1].length > 4) {
Log.warn("Latitude is too specific for weather data. Do not use more than four decimals. Trimming to maximum length.");
Expand All @@ -195,18 +201,13 @@ WeatherProvider.register("yr", {
lon = `${lonParts[0]}.${lonParts[1].substring(0, 4)}`;
}

return `${this.config.apiBase}/locationforecast/2.0/complete?&altitude=${altitude}&lat=${lat}&lon=${lon}`;
return `${this.config.apiBase}/locationforecast/${this.config.forecastApiVersion}/complete?&altitude=${altitude}&lat=${lat}&lon=${lon}`;
},

cacheWeatherData(weatherData) {
localStorage.setItem("weatherData", JSON.stringify(weatherData));
},

getAuthenticationString() {
if (!this.config.authenticationEmail) throw new Error("Authentication email not provided.");
return `${this.config.applicaitionName} ${this.config.authenticationEmail}`;
},

getStellarData() {
// If a user has several Yr-modules, for instance one current and one forecast, the API calls must be synchronized across classes.
// This is to avoid multiple similar calls to the API.
Expand Down Expand Up @@ -302,7 +303,7 @@ WeatherProvider.register("yr", {

getStellarDataFromYr(date, days = 1) {
const requestHeaders = [{ name: "Accept", value: "application/json" }];
return this.fetchData(this.getStellarDatatUrl(date, days), "json", requestHeaders)
return this.fetchData(this.getStellarDataUrl(date, days), "json", requestHeaders)
.then((data) => {
Log.debug("Got stellar data from yr.");
return data;
Expand All @@ -313,19 +314,8 @@ WeatherProvider.register("yr", {
});
},

getStellarDatatUrl(date, days) {
if (!this.config.lat) {
Log.error("Latitude not provided.");
throw new Error("Latitude not provided.");
}
if (!this.config.lon) {
Log.error("Longitude not provided.");
throw new Error("Longitude not provided.");
}

let lat = this.config.lat.toString();
let lon = this.config.lon.toString();
const altitude = this.config.altitude ?? 0;
getStellarDataUrl(date, days) {
let { lat, lon, altitude } = this.getConfigOptions();

if (lat.includes(".") && lat.split(".")[1].length > 4) {
Log.warn("Latitude is too specific for stellar data. Do not use more than four decimals. Trimming to maximum length.");
Expand All @@ -352,7 +342,7 @@ WeatherProvider.register("yr", {
if (hours.length < 2) {
hours = `0${hours}`;
}
return `${this.config.apiBase}/sunrise/2.3/sun?lat=${lat}&lon=${lon}&date=${date}&offset=${utcOffsetPrefix}${hours}%3A${minutes}`;
return `${this.config.apiBase}/sunrise/${this.config.sunriseApiVersion}/sun?lat=${lat}&lon=${lon}&date=${date}&offset=${utcOffsetPrefix}${hours}%3A${minutes}`;
},

cacheStellarData(data) {
Expand Down Expand Up @@ -472,15 +462,6 @@ WeatherProvider.register("yr", {
return weatherTypes.hasOwnProperty(weatherType) ? weatherTypes[weatherType] : null;
},

getStellarTimesFrom(stellarData, date) {
for (const time of stellarData.location.time) {
if (time.date === date) {
return time;
}
}
return undefined;
},

getForecastForXHoursFrom(weather) {
if (this.config.currentForecastHours === 1) {
if (weather.next_1_hours) {
Expand Down Expand Up @@ -522,8 +503,7 @@ WeatherProvider.register("yr", {
},

async getWeatherForecast(type) {
const getRequests = [this.getWeatherData(), this.getStellarData()];
const [weatherData, stellarData] = await Promise.all(getRequests);
const [weatherData, stellarData] = await Promise.all([this.getWeatherData(), this.getStellarData()]);
if (!weatherData.properties.timeseries || !weatherData.properties.timeseries[0]) {
Log.error("No weather data available.");
return;
Expand Down Expand Up @@ -573,7 +553,7 @@ WeatherProvider.register("yr", {
return days;
}, Object.create(null));

Object.keys(days).forEach(function (time, index) {
Object.keys(days).forEach(function (time) {
let minTemperature = undefined;
let maxTemperature = undefined;

Expand Down
Loading

0 comments on commit a0b444d

Please sign in to comment.