Handle properly UNKNOWN module types in discovery (#13500)

Signed-off-by: clinique <gael@lhopital.org>
This commit is contained in:
Gaël L'hopital 2022-10-07 13:15:04 +02:00 committed by GitHub
parent 56d72ca1c3
commit 959e65814b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 40 additions and 32 deletions

View File

@ -16,6 +16,7 @@ import static java.util.Comparator.*;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import org.eclipse.jdt.annotation.NonNullByDefault; import org.eclipse.jdt.annotation.NonNullByDefault;
@ -77,8 +78,8 @@ public class NetatmoDiscoveryService extends AbstractDiscoveryService implements
if (weatherApi != null) { // Search owned or favorite stations if (weatherApi != null) { // Search owned or favorite stations
weatherApi.getFavoriteAndGuestStationsData().stream().forEach(station -> { weatherApi.getFavoriteAndGuestStationsData().stream().forEach(station -> {
if (!station.isReadOnly() || localHandler.getReadFriends()) { if (!station.isReadOnly() || localHandler.getReadFriends()) {
ThingUID stationUID = createThing(station, accountUID); createThing(station, accountUID).ifPresent(stationUID -> station.getModules().values()
station.getModules().values().stream().forEach(module -> createThing(module, stationUID)); .stream().forEach(module -> createThing(module, stationUID)));
} }
}); });
} }
@ -88,8 +89,7 @@ public class NetatmoDiscoveryService extends AbstractDiscoveryService implements
.filter(h -> !(h.getFeatures().isEmpty() .filter(h -> !(h.getFeatures().isEmpty()
|| h.getFeatures().contains(FeatureArea.WEATHER) && h.getFeatures().size() == 1)) || h.getFeatures().contains(FeatureArea.WEATHER) && h.getFeatures().size() == 1))
.forEach(home -> { .forEach(home -> {
ThingUID homeUID = createThing(home, accountUID); createThing(home, accountUID).ifPresent(homeUID -> {
home.getKnownPersons().forEach(person -> createThing(person, homeUID)); home.getKnownPersons().forEach(person -> createThing(person, homeUID));
Map<String, ThingUID> bridgesUids = new HashMap<>(); Map<String, ThingUID> bridgesUids = new HashMap<>();
@ -97,8 +97,10 @@ public class NetatmoDiscoveryService extends AbstractDiscoveryService implements
home.getRooms().values().stream().forEach(room -> { home.getRooms().values().stream().forEach(room -> {
room.getModuleIds().stream().map(id -> home.getModules().get(id)) room.getModuleIds().stream().map(id -> home.getModules().get(id))
.map(m -> m != null ? m.getType().feature : FeatureArea.NONE) .map(m -> m != null ? m.getType().feature : FeatureArea.NONE)
.filter(f -> FeatureArea.ENERGY.equals(f)).findAny() .filter(f -> FeatureArea.ENERGY.equals(f)).findAny().ifPresent(f -> {
.ifPresent(f -> bridgesUids.put(room.getId(), createThing(room, homeUID))); createThing(room, homeUID).ifPresent(
roomUID -> bridgesUids.put(room.getId(), roomUID));
});
}); });
// Creating modules that have no bridge first, avoiding weather station itself // Creating modules that have no bridge first, avoiding weather station itself
@ -108,12 +110,14 @@ public class NetatmoDiscoveryService extends AbstractDiscoveryService implements
.forEach(module -> { .forEach(module -> {
String bridgeId = module.getBridge(); String bridgeId = module.getBridge();
if (bridgeId == null) { if (bridgeId == null) {
bridgesUids.put(module.getId(), createThing(module, homeUID)); createThing(module, homeUID).ifPresent(
moduleUID -> bridgesUids.put(module.getId(), moduleUID));
} else { } else {
createThing(module, bridgesUids.getOrDefault(bridgeId, homeUID)); createThing(module, bridgesUids.getOrDefault(bridgeId, homeUID));
} }
}); });
}); });
});
} }
} catch (NetatmoException e) { } catch (NetatmoException e) {
logger.warn("Error during discovery process : {}", e.getMessage()); logger.warn("Error during discovery process : {}", e.getMessage());
@ -121,21 +125,25 @@ public class NetatmoDiscoveryService extends AbstractDiscoveryService implements
} }
} }
private ThingUID findThingUID(ModuleType thingType, String thingId, ThingUID bridgeUID) { private @Nullable ThingUID findThingUID(ModuleType thingType, String thingId, ThingUID bridgeUID) {
ThingTypeUID thingTypeUID = thingType.thingTypeUID; ThingTypeUID thingTypeUID = thingType.thingTypeUID;
return getSupportedThingTypes().stream().filter(supported -> supported.equals(thingTypeUID)).findFirst() return getSupportedThingTypes().stream().filter(supported -> supported.equals(thingTypeUID)).findFirst()
.map(supported -> new ThingUID(supported, bridgeUID, thingId.replaceAll("[^a-zA-Z0-9_]", ""))) .map(supported -> new ThingUID(supported, bridgeUID, thingId.replaceAll("[^a-zA-Z0-9_]", "")))
.orElseThrow(() -> new IllegalArgumentException("Unsupported device type discovered : " + thingType)); .orElse(null);
} }
private ThingUID createThing(NAModule module, ThingUID bridgeUID) { private Optional<ThingUID> createThing(NAModule module, ThingUID bridgeUID) {
ThingUID moduleUID = findThingUID(module.getType(), module.getId(), bridgeUID); ThingUID moduleUID = findThingUID(module.getType(), module.getId(), bridgeUID);
if (moduleUID != null) {
DiscoveryResultBuilder resultBuilder = DiscoveryResultBuilder.create(moduleUID) DiscoveryResultBuilder resultBuilder = DiscoveryResultBuilder.create(moduleUID)
.withProperty(NAThingConfiguration.ID, module.getId()) .withProperty(NAThingConfiguration.ID, module.getId())
.withRepresentationProperty(NAThingConfiguration.ID) .withRepresentationProperty(NAThingConfiguration.ID)
.withLabel(module.getName() != null ? module.getName() : module.getId()).withBridge(bridgeUID); .withLabel(module.getName() != null ? module.getName() : module.getId()).withBridge(bridgeUID);
thingDiscovered(resultBuilder.build()); thingDiscovered(resultBuilder.build());
return moduleUID; } else {
logger.info("Module '{}' is not handled by this version of the binding - it is ignored.", module.getName());
}
return Optional.ofNullable(moduleUID);
} }
@Override @Override