From 596015d2baf96c1ecd026adb1af1f3030b60dd88 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ga=C3=ABl=20L=27hopital?= Date: Fri, 17 Nov 2023 18:41:59 +0100 Subject: [PATCH] [netatmo] Enhance errored modules handling (#15866) * Resolves issue #15858 * Enhance null control in DTO --------- Signed-off-by: clinique --- .../internal/api/dto/NAHomeStatus.java | 14 +++++++------ .../internal/deserialization/NAObjectMap.java | 4 ++-- .../internal/handler/CommonInterface.java | 20 +++++++++++++++++++ .../handler/capability/EnergyCapability.java | 12 +++++++++-- .../handler/capability/HomeCapability.java | 13 ++++++++---- 5 files changed, 49 insertions(+), 14 deletions(-) diff --git a/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/api/dto/NAHomeStatus.java b/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/api/dto/NAHomeStatus.java index 59861c4dd..5583bc4ae 100644 --- a/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/api/dto/NAHomeStatus.java +++ b/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/api/dto/NAHomeStatus.java @@ -35,24 +35,26 @@ public class NAHomeStatus { private @Nullable NAObjectMap modules; public NAObjectMap getModules() { - NAObjectMap localModules = modules; - return localModules != null ? localModules : new NAObjectMap<>(); + NAObjectMap local = modules; + return local != null ? local : new NAObjectMap<>(); } } public class Energy extends HomeStatus { - private NAObjectMap rooms = new NAObjectMap<>(); + private @Nullable NAObjectMap rooms; public NAObjectMap getRooms() { - return rooms; + NAObjectMap local = rooms; + return local != null ? local : new NAObjectMap<>(); } } public class Security extends HomeStatus { - private NAObjectMap persons = new NAObjectMap<>(); + private @Nullable NAObjectMap persons; public NAObjectMap getPersons() { - return persons; + NAObjectMap local = persons; + return local != null ? local : new NAObjectMap<>(); } } diff --git a/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/deserialization/NAObjectMap.java b/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/deserialization/NAObjectMap.java index c4357e6d4..4251c4f43 100644 --- a/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/deserialization/NAObjectMap.java +++ b/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/deserialization/NAObjectMap.java @@ -33,7 +33,7 @@ public class NAObjectMap extends HashMap { return super.put(thing.getId(), thing); } - public Optional getOpt(String key) { - return Optional.ofNullable(super.get(key)); + public Optional getOpt(@Nullable String key) { + return Optional.ofNullable(key != null ? super.get(key) : null); } } diff --git a/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/handler/CommonInterface.java b/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/handler/CommonInterface.java index 1a6c97369..85ba84c90 100644 --- a/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/handler/CommonInterface.java +++ b/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/handler/CommonInterface.java @@ -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 getAllActiveChildren(Bridge bridge) { + List 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 getActiveChildren() { Thing thing = getThing(); if (thing instanceof Bridge bridge) { diff --git a/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/handler/capability/EnergyCapability.java b/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/handler/capability/EnergyCapability.java index 832d2252d..39752014e 100644 --- a/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/handler/capability/EnergyCapability.java +++ b/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/handler/capability/EnergyCapability.java @@ -91,11 +91,19 @@ public class EnergyCapability extends RestCapability { NAObjectMap 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); + }); }); }); }); diff --git a/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/handler/capability/HomeCapability.java b/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/handler/capability/HomeCapability.java index f9e281ab5..4b7a62ef0 100644 --- a/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/handler/capability/HomeCapability.java +++ b/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/handler/capability/HomeCapability.java @@ -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 { 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 { } 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;