From: rossbuggins <20972856+rossbuggins@users.noreply.github.com> Date: Sat, 24 Sep 2022 19:15:19 +0000 (+0100) Subject: How to support OpenWeatherMap one call api 3.0. (#13414) X-Git-Url: https://git.basschouten.com/?a=commitdiff_plain;h=80804a59b01a6ea3c4d8bdc084eaadedf29bf23f;p=openhab-addons.git How to support OpenWeatherMap one call api 3.0. (#13414) Add OpenCall API version property. Allowing for version 3.0 API to be set. Signed-off-by: Ross Buggins <20972856+rossbuggins@users.noreply.github.com> --- diff --git a/bundles/org.openhab.binding.openweathermap/README.md b/bundles/org.openhab.binding.openweathermap/README.md index bc97a8e314..88fba39574 100644 --- a/bundles/org.openhab.binding.openweathermap/README.md +++ b/bundles/org.openhab.binding.openweathermap/README.md @@ -39,6 +39,11 @@ It requires coordinates of the location of your interest. Air pollution forecast is available for 5 days with hourly granularity. You can add as much `air-pollution` things for different locations to your setup as you like to observe. +#### One Call API Version +New Subscribers to the One Call API will require setting the API version to 3.0 (The API key will not work with 2.5). Existing subscribers can continue to use their existing API key with version 2.5. + + One Call API Version 3.0 [requires payment details](https://openweathermap.org/price) for future forecast information. However, it is possible to set a [daily API call limit to 1000](https://openweathermap.org/faq#onecall), which will avoid charges. + ### One Call API Weather and Forecast The thing `onecall` supports the [current and forecast weather data](https://openweathermap.org/api/one-call-api#how) for a specific location using the One Call API. diff --git a/bundles/org.openhab.binding.openweathermap/src/main/java/org/openhab/binding/openweathermap/internal/config/OpenWeatherMapAPIConfiguration.java b/bundles/org.openhab.binding.openweathermap/src/main/java/org/openhab/binding/openweathermap/internal/config/OpenWeatherMapAPIConfiguration.java index c58503c6b6..abede00324 100644 --- a/bundles/org.openhab.binding.openweathermap/src/main/java/org/openhab/binding/openweathermap/internal/config/OpenWeatherMapAPIConfiguration.java +++ b/bundles/org.openhab.binding.openweathermap/src/main/java/org/openhab/binding/openweathermap/internal/config/OpenWeatherMapAPIConfiguration.java @@ -35,4 +35,5 @@ public class OpenWeatherMapAPIConfiguration { public @Nullable String apikey; public int refreshInterval; public @Nullable String language; + public String apiVersion = "2.5"; } diff --git a/bundles/org.openhab.binding.openweathermap/src/main/java/org/openhab/binding/openweathermap/internal/connection/OpenWeatherMapConnection.java b/bundles/org.openhab.binding.openweathermap/src/main/java/org/openhab/binding/openweathermap/internal/connection/OpenWeatherMapConnection.java index 06c547f159..b7ebb6e728 100644 --- a/bundles/org.openhab.binding.openweathermap/src/main/java/org/openhab/binding/openweathermap/internal/connection/OpenWeatherMapConnection.java +++ b/bundles/org.openhab.binding.openweathermap/src/main/java/org/openhab/binding/openweathermap/internal/connection/OpenWeatherMapConnection.java @@ -97,8 +97,9 @@ public class OpenWeatherMapConnection { // Weather icons (see https://openweathermap.org/weather-conditions) private static final String ICON_URL = "https://openweathermap.org/img/w/%s.png"; // One Call API (see https://openweathermap.org/api/one-call-api ) - private static final String ONECALL_URL = "https://api.openweathermap.org/data/2.5/onecall"; - private static final String ONECALL_HISTORY_URL = "https://api.openweathermap.org/data/2.5/onecall/timemachine"; + private static final String ONECALL_URL = "https://api.openweathermap.org/data"; + private static final String ONECALL_DATA_SUFFIX_URL = "onecall"; + private static final String ONECALL_HISTORY_SUFFIX_URL = "onecall/timemachine"; private final OpenWeatherMapAPIHandler handler; private final HttpClient httpClient; @@ -325,7 +326,8 @@ public class OpenWeatherMapConnection { if (!exclude.isEmpty()) { params.put(PARAM_EXCLUDE, exclude.stream().collect(Collectors.joining(","))); } - return gson.fromJson(getResponseFromCache(buildURL(ONECALL_URL, params)), OpenWeatherMapOneCallAPIData.class); + return gson.fromJson(getResponseFromCache(buildURL(buildOneCallURL(), params)), + OpenWeatherMapOneCallAPIData.class); } /** @@ -346,7 +348,7 @@ public class OpenWeatherMapConnection { // the API requests the history as timestamp in Unix time format. params.put(PARAM_HISTORY_DATE, Long.toString(ZonedDateTime.now(ZoneId.of("UTC")).minusDays(days).toEpochSecond())); - return gson.fromJson(getResponseFromCache(buildURL(ONECALL_HISTORY_URL, params)), + return gson.fromJson(getResponseFromCache(buildURL(buildOneCallHistoryURL(), params)), OpenWeatherMapOneCallHistAPIData.class); } @@ -383,6 +385,16 @@ public class OpenWeatherMapConnection { .collect(Collectors.joining("&", url + "?", "")); } + private String buildOneCallURL() { + var config = handler.getOpenWeatherMapAPIConfig(); + return ONECALL_URL + "/" + config.apiVersion + "/" + ONECALL_DATA_SUFFIX_URL; + } + + private String buildOneCallHistoryURL() { + var config = handler.getOpenWeatherMapAPIConfig(); + return ONECALL_URL + "/" + config.apiVersion + "/" + ONECALL_HISTORY_SUFFIX_URL; + } + private String encodeParam(@Nullable String value) { return value == null ? "" : URLEncoder.encode(value, StandardCharsets.UTF_8); } diff --git a/bundles/org.openhab.binding.openweathermap/src/main/resources/OH-INF/config/config.xml b/bundles/org.openhab.binding.openweathermap/src/main/resources/OH-INF/config/config.xml index 116c166600..728a2c7a96 100644 --- a/bundles/org.openhab.binding.openweathermap/src/main/resources/OH-INF/config/config.xml +++ b/bundles/org.openhab.binding.openweathermap/src/main/resources/OH-INF/config/config.xml @@ -11,6 +11,15 @@ API key to access the OpenWeatherMap API. + + + One Call API version (defaults to 2.5, version 3.0 is available, but needs different subscription). + 2.5 + + + + + Specifies the refresh interval (in minutes). diff --git a/bundles/org.openhab.binding.openweathermap/src/main/resources/OH-INF/i18n/openweathermap.properties b/bundles/org.openhab.binding.openweathermap/src/main/resources/OH-INF/i18n/openweathermap.properties index dde7aa2918..6f37aa0e52 100644 --- a/bundles/org.openhab.binding.openweathermap/src/main/resources/OH-INF/i18n/openweathermap.properties +++ b/bundles/org.openhab.binding.openweathermap/src/main/resources/OH-INF/i18n/openweathermap.properties @@ -76,6 +76,8 @@ thing-type.openweathermap.weather-api.description = Provides access to the OpenW bridge-type.config.openweathermap.weather-api.apikey.label = API Key bridge-type.config.openweathermap.weather-api.apikey.description = API key to access the OpenWeatherMap API. +bridge-type.config.openweathermap.weather-api.apiVersion.label = One Call API Version +bridge-type.config.openweathermap.weather-api.apiVersion.description = One Call API version (defaults to 2.5, version 3.0 is available, but needs different subscription). bridge-type.config.openweathermap.weather-api.language.label = Language bridge-type.config.openweathermap.weather-api.language.description = Language to be used by the OpenWeatherMap API. bridge-type.config.openweathermap.weather-api.language.option.af = Afrikaans