]> git.basschouten.com Git - openhab-addons.git/commitdiff
[netatmo] Correct Discovery service (#12869)
authorGaël L'hopital <gael@lhopital.org>
Sun, 5 Jun 2022 17:14:04 +0000 (19:14 +0200)
committerGitHub <noreply@github.com>
Sun, 5 Jun 2022 17:14:04 +0000 (19:14 +0200)
* Correct Discovery service
* Enhancing weather station module handling

Signed-off-by: clinique <gael@lhopital.org>
bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/discovery/NetatmoDiscoveryService.java

index a518971a41e96a8a5c46ef8a0b8c483a230d9974..b76c426c1c004829183479e8215c6339157e7ac3 100644 (file)
  */
 package org.openhab.binding.netatmo.internal.discovery;
 
+import static java.util.Comparator.*;
+
 import java.util.HashMap;
 import java.util.Map;
-import java.util.Set;
 import java.util.stream.Collectors;
 
 import org.eclipse.jdt.annotation.NonNullByDefault;
@@ -26,6 +27,7 @@ import org.openhab.binding.netatmo.internal.api.NetatmoException;
 import org.openhab.binding.netatmo.internal.api.WeatherApi;
 import org.openhab.binding.netatmo.internal.api.data.ModuleType;
 import org.openhab.binding.netatmo.internal.api.data.NetatmoConstants.FeatureArea;
+import org.openhab.binding.netatmo.internal.api.dto.HomeDataModule;
 import org.openhab.binding.netatmo.internal.api.dto.NAMain;
 import org.openhab.binding.netatmo.internal.api.dto.NAModule;
 import org.openhab.binding.netatmo.internal.config.NAThingConfiguration;
@@ -48,13 +50,13 @@ import org.slf4j.LoggerFactory;
  */
 @NonNullByDefault
 public class NetatmoDiscoveryService extends AbstractDiscoveryService implements ThingHandlerService, DiscoveryService {
-    private static final Set<ModuleType> SKIPPED_TYPES = Set.of(ModuleType.UNKNOWN, ModuleType.ACCOUNT);
     private static final int DISCOVER_TIMEOUT_SECONDS = 3;
     private final Logger logger = LoggerFactory.getLogger(NetatmoDiscoveryService.class);
+
     private @Nullable ApiBridgeHandler handler;
 
     public NetatmoDiscoveryService() {
-        super(ModuleType.AS_SET.stream().filter(mt -> !SKIPPED_TYPES.contains(mt)).map(mt -> mt.thingTypeUID)
+        super(ModuleType.AS_SET.stream().filter(mt -> !mt.apiName.isBlank()).map(mt -> mt.thingTypeUID)
                 .collect(Collectors.toSet()), DISCOVER_TIMEOUT_SECONDS);
     }
 
@@ -71,40 +73,47 @@ public class NetatmoDiscoveryService extends AbstractDiscoveryService implements
                         body.getElements().stream().forEach(homeCoach -> createThing(homeCoach, accountUID));
                     }
                 }
-                if (localHandler.getReadFriends()) {
-                    WeatherApi weatherApi = localHandler.getRestManager(WeatherApi.class);
-                    if (weatherApi != null) { // Search favorite stations
-                        weatherApi.getFavoriteAndGuestStationsData().stream().filter(NAMain::isReadOnly)
-                                .forEach(station -> {
-                                    ThingUID bridgeUID = createThing(station, accountUID);
-                                    station.getModules().values().stream()
-                                            .forEach(module -> createThing(module, bridgeUID));
-                                });
-                    }
+                WeatherApi weatherApi = localHandler.getRestManager(WeatherApi.class);
+                if (weatherApi != null) { // Search owned or favorite stations
+                    weatherApi.getFavoriteAndGuestStationsData().stream().forEach(station -> {
+                        if (!station.isReadOnly() || localHandler.getReadFriends()) {
+                            ThingUID stationUID = createThing(station, accountUID);
+                            station.getModules().values().stream().forEach(module -> createThing(module, stationUID));
+                        }
+                    });
                 }
                 HomeApi homeApi = localHandler.getRestManager(HomeApi.class);
-                if (homeApi != null) { // Search those who depend from a home
-                    homeApi.getHomesData(null, null).stream().filter(h -> !h.getFeatures().isEmpty()).forEach(home -> {
-                        ThingUID homeUID = createThing(home, accountUID);
-
-                        home.getKnownPersons().forEach(person -> createThing(person, homeUID));
-
-                        Map<String, ThingUID> bridgesUids = new HashMap<>();
-
-                        home.getRooms().values().stream().forEach(room -> {
-                            room.getModuleIds().stream().map(id -> home.getModules().get(id))
-                                    .map(m -> m != null ? m.getType().feature : FeatureArea.NONE)
-                                    .filter(f -> FeatureArea.ENERGY.equals(f)).findAny()
-                                    .ifPresent(f -> bridgesUids.put(room.getId(), createThing(room, homeUID)));
-                        });
-
-                        // Creating modules that have no bridge first
-                        home.getModules().values().stream().filter(module -> module.getBridge() == null)
-                                .forEach(device -> bridgesUids.put(device.getId(), createThing(device, homeUID)));
-                        // Then the others
-                        home.getModules().values().stream().filter(module -> module.getBridge() != null).forEach(
-                                device -> createThing(device, bridgesUids.getOrDefault(device.getBridge(), homeUID)));
-                    });
+                if (homeApi != null) { // Search those depending from a home that has modules + not only weather modules
+                    homeApi.getHomesData(null, null).stream()
+                            .filter(h -> !(h.getFeatures().isEmpty()
+                                    || h.getFeatures().contains(FeatureArea.WEATHER) && h.getFeatures().size() == 1))
+                            .forEach(home -> {
+                                ThingUID homeUID = createThing(home, accountUID);
+
+                                home.getKnownPersons().forEach(person -> createThing(person, homeUID));
+
+                                Map<String, ThingUID> bridgesUids = new HashMap<>();
+
+                                home.getRooms().values().stream().forEach(room -> {
+                                    room.getModuleIds().stream().map(id -> home.getModules().get(id))
+                                            .map(m -> m != null ? m.getType().feature : FeatureArea.NONE)
+                                            .filter(f -> FeatureArea.ENERGY.equals(f)).findAny()
+                                            .ifPresent(f -> bridgesUids.put(room.getId(), createThing(room, homeUID)));
+                                });
+
+                                // Creating modules that have no bridge first, avoiding weather station itself
+                                home.getModules().values().stream()
+                                        .filter(module -> module.getType().feature != FeatureArea.WEATHER)
+                                        .sorted(comparing(HomeDataModule::getBridge, nullsFirst(naturalOrder())))
+                                        .forEach(module -> {
+                                            String bridgeId = module.getBridge();
+                                            if (bridgeId == null) {
+                                                bridgesUids.put(module.getId(), createThing(module, homeUID));
+                                            } else {
+                                                createThing(module, bridgesUids.getOrDefault(bridgeId, homeUID));
+                                            }
+                                        });
+                            });
                 }
             } catch (NetatmoException e) {
                 logger.warn("Error during discovery process : {}", e.getMessage());