*/
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;
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;
*/
@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);
}
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());