[netatmo] Enhance errored modules handling (#15866)

* Resolves issue #15858
* Enhance null control in DTO

---------

Signed-off-by: clinique <gael@lhopital.org>
This commit is contained in:
Gaël L'hopital 2023-11-17 18:41:59 +01:00 committed by GitHub
parent 12f02124b6
commit 596015d2ba
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 49 additions and 14 deletions

View File

@ -35,24 +35,26 @@ public class NAHomeStatus {
private @Nullable NAObjectMap<HomeStatusModule> modules;
public NAObjectMap<HomeStatusModule> getModules() {
NAObjectMap<HomeStatusModule> localModules = modules;
return localModules != null ? localModules : new NAObjectMap<>();
NAObjectMap<HomeStatusModule> local = modules;
return local != null ? local : new NAObjectMap<>();
}
}
public class Energy extends HomeStatus {
private NAObjectMap<Room> rooms = new NAObjectMap<>();
private @Nullable NAObjectMap<Room> rooms;
public NAObjectMap<Room> getRooms() {
return rooms;
NAObjectMap<Room> local = rooms;
return local != null ? local : new NAObjectMap<>();
}
}
public class Security extends HomeStatus {
private NAObjectMap<HomeStatusPerson> persons = new NAObjectMap<>();
private @Nullable NAObjectMap<HomeStatusPerson> persons;
public NAObjectMap<HomeStatusPerson> getPersons() {
return persons;
NAObjectMap<HomeStatusPerson> local = persons;
return local != null ? local : new NAObjectMap<>();
}
}

View File

@ -33,7 +33,7 @@ public class NAObjectMap<T extends NAObject> extends HashMap<String, T> {
return super.put(thing.getId(), thing);
}
public Optional<T> getOpt(String key) {
return Optional.ofNullable(super.get(key));
public Optional<T> getOpt(@Nullable String key) {
return Optional.ofNullable(key != null ? super.get(key) : null);
}
}

View File

@ -128,6 +128,26 @@ public interface CommonInterface {
: recurseUpToHomeHandler(handler.getBridgeHandler());
}
/**
* Recurses down in the home/module/device tree
*
* @param bridge
* @return the list of childs of the bridge
*/
default List<CommonInterface> getAllActiveChildren(Bridge bridge) {
List<CommonInterface> result = new ArrayList<>();
bridge.getThings().stream().filter(Thing::isEnabled).map(Thing::getHandler).forEach(childHandler -> {
if (childHandler != null) {
Thing childThing = childHandler.getThing();
if (childThing instanceof Bridge bridgeChild) {
result.addAll(getAllActiveChildren(bridgeChild));
}
result.add((CommonInterface) childHandler);
}
});
return result;
}
default List<CommonInterface> getActiveChildren() {
Thing thing = getThing();
if (thing instanceof Bridge bridge) {

View File

@ -91,11 +91,19 @@ public class EnergyCapability extends RestCapability<EnergyApi> {
NAObjectMap<HomeStatusModule> modules = energyStatus.getModules();
handler.getActiveChildren(FeatureArea.ENERGY).forEach(childHandler -> {
String childId = childHandler.getId();
rooms.getOpt(childId).ifPresentOrElse(roomData -> childHandler.setNewData(roomData), () -> {
logger.trace("childId: {}", childId);
rooms.getOpt(childId).ifPresentOrElse(roomData -> {
logger.trace("roomData: {}", roomData);
childHandler.setNewData(roomData);
}, () -> {
modules.getOpt(childId).ifPresent(moduleData -> {
logger.trace("moduleData: {}", moduleData);
childHandler.setNewData(moduleData);
modules.values().stream().filter(module -> childId.equals(module.getBridge()))
.forEach(bridgedModule -> childHandler.setNewData(bridgedModule));
.forEach(bridgedModule -> {
logger.trace("bridgedModule: {}", bridgedModule);
childHandler.setNewData(bridgedModule);
});
});
});
});

View File

@ -32,6 +32,7 @@ import org.openhab.binding.netatmo.internal.api.dto.NAObject;
import org.openhab.binding.netatmo.internal.config.HomeConfiguration;
import org.openhab.binding.netatmo.internal.handler.CommonInterface;
import org.openhab.binding.netatmo.internal.providers.NetatmoDescriptionProvider;
import org.openhab.core.thing.Bridge;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -105,10 +106,14 @@ public class HomeCapability extends RestCapability<HomeApi> {
return featureAreas.contains(searched);
}
/**
* Errored equipments are reported at home level - so we need to explore all the tree to identify modules
* depending from a child device.
*/
@Override
protected void updateErrors(NAError error) {
handler.getActiveChildren().stream().filter(handler -> handler.getId().equals(error.getId())).findFirst()
.ifPresent(handler -> handler.setNewData(error));
handler.getAllActiveChildren((Bridge) thing).stream().filter(handler -> handler.getId().equals(error.getId()))
.findFirst().ifPresent(handler -> handler.setNewData(error));
}
@Override
@ -123,11 +128,11 @@ public class HomeCapability extends RestCapability<HomeApi> {
}
api.getHomeStatus(id).ifPresent(body -> {
body.getHomeStatus().ifPresent(homeStatus -> result.add(homeStatus));
body.getHomeStatus().ifPresent(result::add);
result.addAll(body.getErrors());
});
} catch (NetatmoException e) {
logger.warn("Error getting Home informations : {}", e.getMessage());
logger.warn("Error getting Home informations: {}", e.getMessage());
}
});
return result;