diff --git a/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/api/WeatherApi.java b/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/api/WeatherApi.java index 336b432b63c..5249a71689f 100644 --- a/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/api/WeatherApi.java +++ b/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/api/WeatherApi.java @@ -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 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 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 getFavoriteAndGuestStationsData() throws NetatmoException { + ListBodyResponse 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); diff --git a/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/discovery/NetatmoDiscoveryService.java b/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/discovery/NetatmoDiscoveryService.java index 523ff2d009e..9e95ea86ac4 100644 --- a/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/discovery/NetatmoDiscoveryService.java +++ b/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/discovery/NetatmoDiscoveryService.java @@ -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 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); diff --git a/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/handler/capability/DeviceCapability.java b/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/handler/capability/DeviceCapability.java index 2b7db3da593..fa9b1525168 100644 --- a/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/handler/capability/DeviceCapability.java +++ b/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/handler/capability/DeviceCapability.java @@ -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)); diff --git a/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/handler/capability/WeatherCapability.java b/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/handler/capability/WeatherCapability.java index b5ddb315f30..8b7acf24939 100644 --- a/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/handler/capability/WeatherCapability.java +++ b/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/handler/capability/WeatherCapability.java @@ -39,7 +39,7 @@ public class WeatherCapability extends RestCapability { @Override protected List 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()); }