From 13a06167ccf97121da16982632e02c5bc0149d6b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ga=C3=ABl=20L=27hopital?= Date: Thu, 28 Dec 2023 08:27:29 +0100 Subject: [PATCH] Enable home being both energy and security (#16026) Signed-off-by: clinique --- .../internal/api/dto/NAHomeStatus.java | 10 +---- .../deserialization/NADeserializer.java | 6 --- .../handler/capability/EnergyCapability.java | 41 +++++++++---------- .../capability/SecurityCapability.java | 25 +++++------ .../channelhelper/SecurityChannelHelper.java | 4 +- 5 files changed, 34 insertions(+), 52 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 5583bc4aef8..d889e9c8bcb 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 @@ -33,24 +33,18 @@ public class NAHomeStatus { public class HomeStatus extends NAThing { private @Nullable NAObjectMap modules; + private @Nullable NAObjectMap rooms; + private @Nullable NAObjectMap persons; public NAObjectMap getModules() { NAObjectMap local = modules; return local != null ? local : new NAObjectMap<>(); } - } - - public class Energy extends HomeStatus { - private @Nullable NAObjectMap rooms; public NAObjectMap getRooms() { NAObjectMap local = rooms; return local != null ? local : new NAObjectMap<>(); } - } - - public class Security extends HomeStatus { - private @Nullable NAObjectMap persons; public NAObjectMap getPersons() { NAObjectMap local = persons; diff --git a/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/deserialization/NADeserializer.java b/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/deserialization/NADeserializer.java index 93c931d3c15..e1dcdf69ef2 100644 --- a/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/deserialization/NADeserializer.java +++ b/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/deserialization/NADeserializer.java @@ -20,8 +20,6 @@ import org.eclipse.jdt.annotation.Nullable; import org.openhab.binding.netatmo.internal.api.NetatmoException; import org.openhab.binding.netatmo.internal.api.data.ModuleType; import org.openhab.binding.netatmo.internal.api.dto.HomeData; -import org.openhab.binding.netatmo.internal.api.dto.NAHomeStatus; -import org.openhab.binding.netatmo.internal.api.dto.NAHomeStatus.HomeStatus; import org.openhab.core.i18n.TimeZoneProvider; import org.openhab.core.library.types.OnOffType; import org.openhab.core.library.types.OpenClosedType; @@ -52,10 +50,6 @@ public class NADeserializer { .registerTypeAdapter(NAObjectMap.class, new NAObjectMapDeserializer()) .registerTypeAdapter(NAPushType.class, new NAPushTypeDeserializer()) .registerTypeAdapter(ModuleType.class, new ModuleTypeDeserializer()) - .registerTypeAdapter(HomeStatus.class, - (JsonDeserializer) (json, type, context) -> context.deserialize(json, - json.getAsJsonObject().has("persons") ? NAHomeStatus.Security.class - : NAHomeStatus.Energy.class)) .registerTypeAdapter(HomeData.class, (JsonDeserializer) (json, type, context) -> context.deserialize(json, json.getAsJsonObject().has("therm_mode") ? HomeData.Energy.class 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 39752014ec5..0b6903edf3f 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 @@ -25,7 +25,6 @@ import org.openhab.binding.netatmo.internal.api.dto.HomeData; import org.openhab.binding.netatmo.internal.api.dto.HomeDataModule; import org.openhab.binding.netatmo.internal.api.dto.HomeDataRoom; import org.openhab.binding.netatmo.internal.api.dto.HomeStatusModule; -import org.openhab.binding.netatmo.internal.api.dto.NAHomeStatus; import org.openhab.binding.netatmo.internal.api.dto.NAHomeStatus.HomeStatus; import org.openhab.binding.netatmo.internal.api.dto.Room; import org.openhab.binding.netatmo.internal.config.HomeConfiguration; @@ -85,29 +84,27 @@ public class EnergyCapability extends RestCapability { } @Override - protected void updateHomeStatus(HomeStatus homeStatus) { - if (homeStatus instanceof NAHomeStatus.Energy energyStatus) { - NAObjectMap rooms = energyStatus.getRooms(); - NAObjectMap modules = energyStatus.getModules(); - handler.getActiveChildren(FeatureArea.ENERGY).forEach(childHandler -> { - String childId = childHandler.getId(); - 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 -> { - logger.trace("bridgedModule: {}", bridgedModule); - childHandler.setNewData(bridgedModule); - }); - }); + protected void updateHomeStatus(HomeStatus energyStatus) { + NAObjectMap rooms = energyStatus.getRooms(); + NAObjectMap modules = energyStatus.getModules(); + handler.getActiveChildren(FeatureArea.ENERGY).forEach(childHandler -> { + String childId = childHandler.getId(); + 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 -> { + logger.trace("bridgedModule: {}", bridgedModule); + childHandler.setNewData(bridgedModule); + }); }); }); - } + }); } public void setThermPoint(String roomId, SetpointMode mode, long endtime, double temp) { diff --git a/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/handler/capability/SecurityCapability.java b/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/handler/capability/SecurityCapability.java index f412ce57baf..76c57429b65 100644 --- a/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/handler/capability/SecurityCapability.java +++ b/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/handler/capability/SecurityCapability.java @@ -32,7 +32,6 @@ import org.openhab.binding.netatmo.internal.api.dto.HomeDataPerson; import org.openhab.binding.netatmo.internal.api.dto.HomeEvent; import org.openhab.binding.netatmo.internal.api.dto.HomeStatusModule; import org.openhab.binding.netatmo.internal.api.dto.HomeStatusPerson; -import org.openhab.binding.netatmo.internal.api.dto.NAHomeStatus; import org.openhab.binding.netatmo.internal.api.dto.NAHomeStatus.HomeStatus; import org.openhab.binding.netatmo.internal.api.dto.NAObject; import org.openhab.binding.netatmo.internal.config.HomeConfiguration; @@ -88,21 +87,19 @@ class SecurityCapability extends RestCapability { } @Override - protected void updateHomeStatus(HomeStatus homeStatus) { - if (homeStatus instanceof NAHomeStatus.Security securityStatus) { - NAObjectMap persons = securityStatus.getPersons(); - NAObjectMap modules = securityStatus.getModules(); - handler.getActiveChildren(FeatureArea.SECURITY).forEach(childHandler -> { - String childId = childHandler.getId(); - persons.getOpt(childId).ifPresentOrElse(personData -> childHandler.setNewData(personData), () -> { - modules.getOpt(childId).ifPresent(childData -> { - childHandler.setNewData(childData); - modules.values().stream().filter(module -> childId.equals(module.getBridge())) - .forEach(bridgedModule -> childHandler.setNewData(bridgedModule)); - }); + protected void updateHomeStatus(HomeStatus securityStatus) { + NAObjectMap persons = securityStatus.getPersons(); + NAObjectMap modules = securityStatus.getModules(); + handler.getActiveChildren(FeatureArea.SECURITY).forEach(childHandler -> { + String childId = childHandler.getId(); + persons.getOpt(childId).ifPresentOrElse(personData -> childHandler.setNewData(personData), () -> { + modules.getOpt(childId).ifPresent(childData -> { + childHandler.setNewData(childData); + modules.values().stream().filter(module -> childId.equals(module.getBridge())) + .forEach(bridgedModule -> childHandler.setNewData(bridgedModule)); }); }); - } + }); } @Override diff --git a/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/handler/channelhelper/SecurityChannelHelper.java b/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/handler/channelhelper/SecurityChannelHelper.java index ea1886122e6..a3c12aad882 100644 --- a/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/handler/channelhelper/SecurityChannelHelper.java +++ b/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/handler/channelhelper/SecurityChannelHelper.java @@ -23,7 +23,7 @@ import org.eclipse.jdt.annotation.Nullable; import org.openhab.binding.netatmo.internal.api.dto.HomeData; import org.openhab.binding.netatmo.internal.api.dto.HomeDataPerson; import org.openhab.binding.netatmo.internal.api.dto.HomeStatusPerson; -import org.openhab.binding.netatmo.internal.api.dto.NAHomeStatus; +import org.openhab.binding.netatmo.internal.api.dto.NAHomeStatus.HomeStatus; import org.openhab.binding.netatmo.internal.api.dto.NAObject; import org.openhab.core.library.types.DecimalType; import org.openhab.core.types.State; @@ -51,7 +51,7 @@ public class SecurityChannelHelper extends ChannelHelper { super.setNewData(data); if (data instanceof HomeData.Security securityData) { knownIds = securityData.getKnownPersons().stream().map(HomeDataPerson::getId).toList(); - } else if (data instanceof NAHomeStatus.Security securityStatus) { + } else if (data instanceof HomeStatus securityStatus) { List present = securityStatus.getPersons().values().stream() .filter(HomeStatusPerson::atHome).toList();