]> git.basschouten.com Git - openhab-addons.git/commitdiff
[netatmo] Avoid requesting favorite stations when not needed (#12777)
authorlolodomo <lg.hc@free.fr>
Sun, 22 May 2022 08:32:09 +0000 (10:32 +0200)
committerGitHub <noreply@github.com>
Sun, 22 May 2022 08:32:09 +0000 (10:32 +0200)
* [netatmo] Avoid requesting favorite stations when not needed
* Add new method getFavoriteAndGuestStationsData
* Enhanced description for method getOwnedStationData
* getFavoriteAndGuestStationsData returns a collection of NAMain

Fix #12769

Signed-off-by: Laurent Garnier <lg.hc@free.fr>
bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/api/WeatherApi.java
bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/discovery/NetatmoDiscoveryService.java
bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/handler/capability/DeviceCapability.java
bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/handler/capability/WeatherCapability.java

index 336b432b63c7f1a197f30c7f0a9c528aba936c91..5249a71689f4f190baf037c225f7e63ece123d97 100644 (file)
@@ -15,6 +15,7 @@ package org.openhab.binding.netatmo.internal.api;
 import static org.openhab.binding.netatmo.internal.api.data.NetatmoConstants.*;
 
 import java.time.ZonedDateTime;
+import java.util.Collection;
 import java.util.List;
 
 import javax.ws.rs.core.UriBuilder;
@@ -45,16 +46,15 @@ public class WeatherApi extends RestManager {
     }
 
     /**
-     *
      * Returns data from a user's Weather Stations (measures and device specific data);
      *
      * @param deviceId Id of the device you want to retrieve information of (optional)
      * @param getFavorites Whether to include the user's favorite Weather Stations in addition to the user's
-     *            own Weather Stations (optional, default to false)
+     *            own Weather Stations
      * @return StationDataResponse
      * @throws NetatmoException If fail to call the API, e.g. server error or deserializing
      */
-    public StationDataResponse getStationsData(@Nullable String deviceId, boolean getFavorites)
+    private StationDataResponse getStationsData(@Nullable String deviceId, boolean getFavorites)
             throws NetatmoException {
         UriBuilder uriBuilder = getApiUriBuilder(SUB_PATH_GETSTATION, PARAM_DEVICEID, deviceId, //
                 PARAM_FAVORITES, getFavorites);
@@ -62,6 +62,14 @@ public class WeatherApi extends RestManager {
         return response;
     }
 
+    /**
+     * Returns data from a user's Weather Station, this stations can be a station owned by the user or a favorite
+     * station or a guest station.
+     *
+     * @param deviceId Id of the device you want to retrieve information
+     * @return NAMain
+     * @throws NetatmoException If fail to call the API, e.g. server error or deserializing
+     */
     public NAMain getStationData(String deviceId) throws NetatmoException {
         ListBodyResponse<NAMain> answer = getStationsData(deviceId, true).getBody();
         if (answer != null) {
@@ -73,6 +81,32 @@ public class WeatherApi extends RestManager {
         throw new NetatmoException("Unexpected answer searching device '%s' : not found.", deviceId);
     }
 
+    /**
+     * Returns data from a Weather Station owned by the user
+     *
+     * This method must be preferred to getStationData when you know that the device is a station owned by the user
+     * (because it avoids requesting additional data for favorite/guest stations).
+     *
+     * @param deviceId Id of the device you want to retrieve information
+     * @return NAMain
+     * @throws NetatmoException If fail to call the API, e.g. server error or deserializing
+     */
+    public NAMain getOwnedStationData(String deviceId) throws NetatmoException {
+        ListBodyResponse<NAMain> answer = getStationsData(deviceId, false).getBody();
+        if (answer != null) {
+            NAMain station = answer.getElement(deviceId);
+            if (station != null) {
+                return station;
+            }
+        }
+        throw new NetatmoException("Unexpected answer searching device '%s' : not found.", deviceId);
+    }
+
+    public Collection<NAMain> getFavoriteAndGuestStationsData() throws NetatmoException {
+        ListBodyResponse<NAMain> answer = getStationsData(null, true).getBody();
+        return answer != null ? answer.getElements() : List.of();
+    }
+
     public @Nullable Object getMeasures(String deviceId, @Nullable String moduleId, @Nullable String scale,
             String apiDescriptor) throws NetatmoException {
         MeasureBodyElem<?> result = getMeasure(deviceId, moduleId, scale, apiDescriptor);
index 523ff2d009e07ff87b82a10dddbb46c9b9db8f00..9e95ea86ac4f0ba82073b1ada53453e42169adad 100644 (file)
@@ -73,14 +73,12 @@ public class NetatmoDiscoveryService extends AbstractDiscoveryService implements
                 if (readFriends) {
                     WeatherApi weatherApi = localHandler.getRestManager(WeatherApi.class);
                     if (weatherApi != null) { // Search favorite stations
-                        ListBodyResponse<NAMain> body = weatherApi.getStationsData(null, true).getBody();
-                        if (body != null) {
-                            body.getElements().stream().filter(NAMain::isReadOnly).forEach(station -> {
-                                ThingUID bridgeUID = createThing(station, apiBridgeUID);
-                                station.getModules().values().stream()
-                                        .forEach(module -> createThing(module, bridgeUID));
-                            });
-                        }
+                        weatherApi.getFavoriteAndGuestStationsData().stream().filter(NAMain::isReadOnly)
+                                .forEach(station -> {
+                                    ThingUID bridgeUID = createThing(station, apiBridgeUID);
+                                    station.getModules().values().stream()
+                                            .forEach(module -> createThing(module, bridgeUID));
+                                });
                     }
                 }
                 HomeApi homeApi = localHandler.getRestManager(HomeApi.class);
index 2b7db3da593c042fac41e5985c4069f75b280e7b..fa9b1525168f01defdbf67a147313a7028c84d07 100644 (file)
@@ -28,12 +28,21 @@ import org.openhab.binding.netatmo.internal.handler.CommonInterface;
 public class DeviceCapability extends Capability {
     private static final int DATA_AGE_LIMIT_S = 3600;
 
+    /**
+     * Whether the device is owned or not by the user (a favorite station or a guest station is not owned by the user).
+     * It will be updated when handling the result of the getstationsdata API.
+     * It must be initialized to false to be sure that the first call to the API will not fail for a favorite/guest
+     * weather stations.
+     */
+    protected boolean owned;
+
     public DeviceCapability(CommonInterface handler) {
         super(handler);
     }
 
     @Override
     protected void updateNAMain(NAMain newData) {
+        owned = !newData.isReadOnly();
         if (firstLaunch) {
             newData.getPlace().ifPresent(place -> {
                 place.getCity().map(city -> properties.put(PROPERTY_CITY, city));
index b5ddb315f30e899f9942a3182bf607cf4a6c9198..8b7acf24939fafd0c8ba2f671077e9303b2dcb2c 100644 (file)
@@ -39,7 +39,7 @@ public class WeatherCapability extends RestCapability<WeatherApi> {
     @Override
     protected List<NAObject> updateReadings(WeatherApi api) {
         try {
-            return List.of(api.getStationData(handler.getId()));
+            return List.of(owned ? api.getOwnedStationData(handler.getId()) : api.getStationData(handler.getId()));
         } catch (NetatmoException e) {
             logger.warn("Error retrieving weather data '{}' : {}", handler.getId(), e.getMessage());
         }