From 5a2ec07876811a95ca6ace64d8be233a33b468af Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ga=C3=ABl=20L=27hopital?= Date: Sun, 5 Jun 2022 19:14:04 +0200 Subject: [PATCH] [netatmo] Correct Discovery service (#12869) * Correct Discovery service * Enhancing weather station module handling Signed-off-by: clinique --- .../discovery/NetatmoDiscoveryService.java | 71 +++++++++++-------- 1 file changed, 40 insertions(+), 31 deletions(-) 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 a518971a4..b76c426c1 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 @@ -12,9 +12,10 @@ */ 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 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); + 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)); + home.getKnownPersons().forEach(person -> createThing(person, homeUID)); - Map bridgesUids = new HashMap<>(); + Map 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))); - }); + 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))); - }); + // 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());