From 8aef0ee3eeaa97d7e86fab502ff97a73b420d3d4 Mon Sep 17 00:00:00 2001 From: robnielsen Date: Tue, 14 Jun 2022 16:26:08 -0500 Subject: [PATCH] [insteon] set device offline if it doesn't exist in the plm/hub database (#12904) * [insteon] set device offline if it doesn't exist in the plm/hub database * [insteon] use a flag to indicate if a device is linked or not * [insteon] set config to @NonNullByDefault({}) instead of @Nullable * [insteon] cleanup Signed-off-by: Rob Nielsen --- .../insteon/internal/InsteonBinding.java | 2 ++ .../handler/InsteonDeviceHandler.java | 21 ++++++++++++++++--- .../handler/InsteonNetworkHandler.java | 11 ++++++++++ 3 files changed, 31 insertions(+), 3 deletions(-) diff --git a/bundles/org.openhab.binding.insteon/src/main/java/org/openhab/binding/insteon/internal/InsteonBinding.java b/bundles/org.openhab.binding.insteon/src/main/java/org/openhab/binding/insteon/internal/InsteonBinding.java index bafc5f0edee40..fa21489efeb47 100644 --- a/bundles/org.openhab.binding.insteon/src/main/java/org/openhab/binding/insteon/internal/InsteonBinding.java +++ b/bundles/org.openhab.binding.insteon/src/main/java/org/openhab/binding/insteon/internal/InsteonBinding.java @@ -320,6 +320,7 @@ private int checkIfInModemDatabase(InsteonDevice dev) { } else { if (driver.isModemDBComplete() && !addr.isX10()) { logger.warn("device {} not found in the modem database. Did you forget to link?", addr); + handler.deviceNotLinked(addr); } } return dbes.size(); @@ -488,6 +489,7 @@ public void driverCompletelyInitialized() { if (!dbes.containsKey(a)) { if (!a.isX10()) { logger.warn("device {} not found in the modem database. Did you forget to link?", a); + handler.deviceNotLinked(a); } } else { if (!dev.hasModemDBEntry()) { diff --git a/bundles/org.openhab.binding.insteon/src/main/java/org/openhab/binding/insteon/internal/handler/InsteonDeviceHandler.java b/bundles/org.openhab.binding.insteon/src/main/java/org/openhab/binding/insteon/internal/handler/InsteonDeviceHandler.java index 14d7db070e800..4c2bf66e3b944 100644 --- a/bundles/org.openhab.binding.insteon/src/main/java/org/openhab/binding/insteon/internal/handler/InsteonDeviceHandler.java +++ b/bundles/org.openhab.binding.insteon/src/main/java/org/openhab/binding/insteon/internal/handler/InsteonDeviceHandler.java @@ -26,7 +26,6 @@ import java.util.stream.Stream; import org.eclipse.jdt.annotation.NonNullByDefault; -import org.eclipse.jdt.annotation.Nullable; import org.openhab.binding.insteon.internal.InsteonBinding; import org.openhab.binding.insteon.internal.InsteonBindingConstants; import org.openhab.binding.insteon.internal.config.InsteonChannelConfiguration; @@ -125,7 +124,8 @@ public class InsteonDeviceHandler extends BaseThingHandler { private final Logger logger = LoggerFactory.getLogger(InsteonDeviceHandler.class); - private @Nullable InsteonDeviceConfiguration config; + private @NonNullByDefault({}) InsteonDeviceConfiguration config; + private boolean deviceLinked = true; public InsteonDeviceHandler(Thing thing) { super(thing); @@ -134,6 +134,7 @@ public InsteonDeviceHandler(Thing thing) { @Override public void initialize() { config = getConfigAs(InsteonDeviceConfiguration.class); + deviceLinked = true; scheduler.execute(() -> { final Bridge bridge = getBridge(); @@ -373,7 +374,9 @@ public void initialize() { }); if (ThingStatus.ONLINE == bridge.getStatus()) { - updateStatus(ThingStatus.ONLINE); + if (deviceLinked) { + updateStatus(ThingStatus.ONLINE); + } } else { updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.BRIDGE_OFFLINE); } @@ -533,6 +536,18 @@ public void channelUnlinked(ChannelUID channelUID) { logger.debug("channel {} unlinked ", channelUID.getAsString()); } + public InsteonAddress getInsteonAddress() { + return new InsteonAddress(config.getAddress()); + } + + public void deviceNotLinked() { + String msg = "device with the address '" + config.getAddress() + + "' was not found in the modem database. Did you forget to link?"; + updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, msg); + + deviceLinked = false; + } + private InsteonNetworkHandler getInsteonNetworkHandler() { Bridge bridge = getBridge(); if (bridge == null) { diff --git a/bundles/org.openhab.binding.insteon/src/main/java/org/openhab/binding/insteon/internal/handler/InsteonNetworkHandler.java b/bundles/org.openhab.binding.insteon/src/main/java/org/openhab/binding/insteon/internal/handler/InsteonNetworkHandler.java index c803e88d82b1e..56ef95c93e456 100644 --- a/bundles/org.openhab.binding.insteon/src/main/java/org/openhab/binding/insteon/internal/handler/InsteonNetworkHandler.java +++ b/bundles/org.openhab.binding.insteon/src/main/java/org/openhab/binding/insteon/internal/handler/InsteonNetworkHandler.java @@ -23,6 +23,7 @@ import org.eclipse.jdt.annotation.Nullable; import org.openhab.binding.insteon.internal.InsteonBinding; import org.openhab.binding.insteon.internal.config.InsteonNetworkConfiguration; +import org.openhab.binding.insteon.internal.device.InsteonAddress; import org.openhab.binding.insteon.internal.discovery.InsteonDeviceDiscoveryService; import org.openhab.core.io.console.Console; import org.openhab.core.io.transport.serial.SerialPortManager; @@ -205,6 +206,16 @@ public void addMissingDevices(List missing) { }); } + public void deviceNotLinked(InsteonAddress addr) { + getThing().getThings().stream().forEach((thing) -> { + InsteonDeviceHandler handler = (InsteonDeviceHandler) thing.getHandler(); + if (handler != null && addr.equals(handler.getInsteonAddress())) { + handler.deviceNotLinked(); + return; + } + }); + } + public void displayDevices(Console console) { display(console, deviceInfo); }