[netatmo] Correct Discovery service (#12869)

* Correct Discovery service
* Enhancing weather station module handling

Signed-off-by: clinique <gael@lhopital.org>
This commit is contained in:
Gaël L'hopital 2022-06-05 19:14:04 +02:00 committed by GitHub
parent f5e5b5189a
commit 5a2ec07876
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -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<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,20 +73,21 @@ 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));
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 -> {
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));
@ -98,12 +101,18 @@ public class NetatmoDiscoveryService extends AbstractDiscoveryService implements
.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) {