]> git.basschouten.com Git - openhab-addons.git/commitdiff
[elroconnects] Set thingStatus earlier in initialize (#12877)
authorMark Herwege <mherwege@users.noreply.github.com>
Sun, 5 Jun 2022 11:19:12 +0000 (13:19 +0200)
committerGitHub <noreply@github.com>
Sun, 5 Jun 2022 11:19:12 +0000 (13:19 +0200)
* Set thingStatus earlier in initialize
* Check device configuration
* Make sure to always set thing status
* Make thing status message localizable

Signed-off-by: Mark Herwege <mark.herwege@telenet.be>
bundles/org.openhab.binding.elroconnects/src/main/java/org/openhab/binding/elroconnects/internal/devices/ElroConnectsDeviceCxsmAlarm.java
bundles/org.openhab.binding.elroconnects/src/main/java/org/openhab/binding/elroconnects/internal/devices/ElroConnectsDeviceEntrySensor.java
bundles/org.openhab.binding.elroconnects/src/main/java/org/openhab/binding/elroconnects/internal/devices/ElroConnectsDeviceGenericAlarm.java
bundles/org.openhab.binding.elroconnects/src/main/java/org/openhab/binding/elroconnects/internal/devices/ElroConnectsDeviceMotionSensor.java
bundles/org.openhab.binding.elroconnects/src/main/java/org/openhab/binding/elroconnects/internal/devices/ElroConnectsDevicePowerSocket.java
bundles/org.openhab.binding.elroconnects/src/main/java/org/openhab/binding/elroconnects/internal/devices/ElroConnectsDeviceTemperatureSensor.java
bundles/org.openhab.binding.elroconnects/src/main/java/org/openhab/binding/elroconnects/internal/handler/ElroConnectsAccountHandler.java
bundles/org.openhab.binding.elroconnects/src/main/java/org/openhab/binding/elroconnects/internal/handler/ElroConnectsBridgeHandler.java
bundles/org.openhab.binding.elroconnects/src/main/java/org/openhab/binding/elroconnects/internal/handler/ElroConnectsDeviceHandler.java
bundles/org.openhab.binding.elroconnects/src/main/resources/OH-INF/i18n/elroconnects.properties

index 70beffc4ec50842aec7116a89b83fd9a5a838b31..8d65d4df8b6327bc4930c0b9820d6032a0d93260 100644 (file)
@@ -123,14 +123,15 @@ public class ElroConnectsDeviceCxsmAlarm extends ElroConnectsDevice {
                 handler.updateState(SIGNAL_STRENGTH, UnDefType.UNDEF);
                 handler.updateState(BATTERY_LEVEL, UnDefType.UNDEF);
                 handler.updateState(LOW_BATTERY, UnDefType.UNDEF);
-                handler.updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR,
-                        "Device " + deviceId + " is not syncing with K1 hub");
+                String msg = String.format("@text/offline.device-not-syncing [ \"%d\" ]", deviceId);
+                handler.updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, msg);
                 break;
             case FAULT:
                 handler.updateState(SIGNAL_STRENGTH, UnDefType.UNDEF);
                 handler.updateState(BATTERY_LEVEL, UnDefType.UNDEF);
                 handler.updateState(LOW_BATTERY, UnDefType.UNDEF);
-                handler.updateStatus(ThingStatus.ONLINE, ThingStatusDetail.NONE, "Device " + deviceId + " has a fault");
+                msg = String.format("@text/offline.device-fault [ \"%d\" ]", deviceId);
+                handler.updateStatus(ThingStatus.ONLINE, ThingStatusDetail.NONE, msg);
                 break;
             default:
                 handler.updateState(SIGNAL_STRENGTH, new DecimalType(signalStrength));
index 4ac00ee7d0f8369f73b52a6b40ca4ca63d5b3572..d0c682af2b263cf1f5dde245e346cdec331db9e8 100644 (file)
@@ -88,15 +88,16 @@ public class ElroConnectsDeviceEntrySensor extends ElroConnectsDevice {
                 handler.updateState(SIGNAL_STRENGTH, UnDefType.UNDEF);
                 handler.updateState(BATTERY_LEVEL, UnDefType.UNDEF);
                 handler.updateState(LOW_BATTERY, UnDefType.UNDEF);
-                handler.updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR,
-                        "Device " + deviceId + " is not syncing with K1 hub");
+                String msg = String.format("@text/offline.device-not-syncing [ \"%d\" ]", deviceId);
+                handler.updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, msg);
                 break;
             case FAULT:
                 handler.updateState(ENTRY, UnDefType.UNDEF);
                 handler.updateState(SIGNAL_STRENGTH, UnDefType.UNDEF);
                 handler.updateState(BATTERY_LEVEL, UnDefType.UNDEF);
                 handler.updateState(LOW_BATTERY, UnDefType.UNDEF);
-                handler.updateStatus(ThingStatus.ONLINE, ThingStatusDetail.NONE, "Device " + deviceId + " has a fault");
+                msg = String.format("@text/offline.device-fault [ \"%d\" ]", deviceId);
+                handler.updateStatus(ThingStatus.ONLINE, ThingStatusDetail.NONE, msg);
                 break;
             default:
                 handler.updateState(ENTRY,
index b0edcefdca886c28f31298950fe77c0e7bfd2661..d56e61e1bc248ded7f8227d162e2cfcc1548ab7e 100644 (file)
@@ -116,14 +116,15 @@ public class ElroConnectsDeviceGenericAlarm extends ElroConnectsDevice {
                 handler.updateState(SIGNAL_STRENGTH, UnDefType.UNDEF);
                 handler.updateState(BATTERY_LEVEL, UnDefType.UNDEF);
                 handler.updateState(LOW_BATTERY, UnDefType.UNDEF);
-                handler.updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR,
-                        "Device " + deviceId + " is not syncing with K1 hub");
+                String msg = String.format("@text/offline.device-not-syncing [ \"%d\" ]", deviceId);
+                handler.updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, msg);
                 break;
             case FAULT:
                 handler.updateState(SIGNAL_STRENGTH, UnDefType.UNDEF);
                 handler.updateState(BATTERY_LEVEL, UnDefType.UNDEF);
                 handler.updateState(LOW_BATTERY, UnDefType.UNDEF);
-                handler.updateStatus(ThingStatus.ONLINE, ThingStatusDetail.NONE, "Device " + deviceId + " has a fault");
+                msg = String.format("@text/offline.device-fault [ \"%d\" ]", deviceId);
+                handler.updateStatus(ThingStatus.ONLINE, ThingStatusDetail.NONE, msg);
                 break;
             default:
                 handler.updateState(SIGNAL_STRENGTH, new DecimalType(signalStrength));
index be7d92c915ef0dac31fe48ae4e03078d372d231f..93e3c0f6a6efa1b757caf61838362f62dc40e4a8 100644 (file)
@@ -87,15 +87,16 @@ public class ElroConnectsDeviceMotionSensor extends ElroConnectsDevice {
                 handler.updateState(SIGNAL_STRENGTH, UnDefType.UNDEF);
                 handler.updateState(BATTERY_LEVEL, UnDefType.UNDEF);
                 handler.updateState(LOW_BATTERY, UnDefType.UNDEF);
-                handler.updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR,
-                        "Device " + deviceId + " is not syncing with K1 hub");
+                String msg = String.format("@text/offline.device-not-syncing [ \"%d\" ]", deviceId);
+                handler.updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, msg);
                 break;
             case FAULT:
                 handler.updateState(MOTION, UnDefType.UNDEF);
                 handler.updateState(SIGNAL_STRENGTH, UnDefType.UNDEF);
                 handler.updateState(BATTERY_LEVEL, UnDefType.UNDEF);
                 handler.updateState(LOW_BATTERY, UnDefType.UNDEF);
-                handler.updateStatus(ThingStatus.ONLINE, ThingStatusDetail.NONE, "Device " + deviceId + " has a fault");
+                msg = String.format("@text/offline.device-fault [ \"%d\" ]", deviceId);
+                handler.updateStatus(ThingStatus.ONLINE, ThingStatusDetail.NONE, msg);
                 break;
             default:
                 handler.updateState(MOTION,
index 2683fb688dcb879a3c4397e2869d6b02bec9cab2..08f3de643242c93f3ac0b41367317769581f1c16 100644 (file)
@@ -69,6 +69,8 @@ public class ElroConnectsDevicePowerSocket extends ElroConnectsDevice {
         String deviceStatus = this.deviceStatus;
         if (deviceStatus.length() < 6) {
             logger.debug("Could not decode device status: {}", deviceStatus);
+            String msg = String.format("@text/offline.device-fault [ \"%d\" ]", deviceId);
+            handler.updateStatus(ThingStatus.ONLINE, ThingStatusDetail.NONE, msg);
             return;
         }
 
@@ -81,8 +83,8 @@ public class ElroConnectsDevicePowerSocket extends ElroConnectsDevice {
                 : (STAT_OFF.equals(status) ? OnOffType.OFF : UnDefType.UNDEF);
         handler.updateState(POWER_STATE, state);
         if (UnDefType.UNDEF.equals(state)) {
-            handler.updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR,
-                    "Device " + deviceId + " is not syncing with K1 hub");
+            String msg = String.format("@text/offline.device-not-syncing [ \"%d\" ]", deviceId);
+            handler.updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, msg);
         } else {
             handler.updateStatus(ThingStatus.ONLINE);
         }
index 76314e2ab423cf7dd08f99b6140b427799572e7f..6ee1455ddf3062c8f705245024f86a4b5723a50f 100644 (file)
@@ -75,7 +75,8 @@ public class ElroConnectsDeviceTemperatureSensor extends ElroConnectsDevice {
                 handler.updateState(LOW_BATTERY, UnDefType.UNDEF);
                 handler.updateState(TEMPERATURE, UnDefType.UNDEF);
                 handler.updateState(HUMIDITY, UnDefType.UNDEF);
-                handler.updateStatus(ThingStatus.ONLINE, ThingStatusDetail.NONE, "Device " + deviceId + " has a fault");
+                String msg = String.format("@text/offline.device-fault [ \"%d\" ]", deviceId);
+                handler.updateStatus(ThingStatus.ONLINE, ThingStatusDetail.NONE, msg);
                 break;
             default:
                 handler.updateState(SIGNAL_STRENGTH, new DecimalType(signalStrength));
index a9aeda36edc5834582d6052ee3abced35384ccac..23a9dc528bcdb3daecb5540c84c9e19ad473ac69 100644 (file)
@@ -121,6 +121,8 @@ public class ElroConnectsAccountHandler extends BaseBridgeHandler {
         login.put("password", password);
         loginJson = gson.toJson(login);
 
+        updateStatus(ThingStatus.UNKNOWN);
+
         // If background discovery is enabled, start polling (will do login first), else only login to take the thing
         // online if successful
         if (enableBackgroundDiscovery) {
index b1fa7d676e0f706cd51184e46c54046f59331172..d23feb7eb0ba86722e465e259362dea35d276af9 100644 (file)
@@ -162,16 +162,18 @@ public class ElroConnectsBridgeHandler extends BaseBridgeHandler {
         legacyFirmware = config.legacyFirmware;
 
         if (connectorId.isEmpty()) {
-            updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, "@text/offline.no-device-id");
+            updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, "@text/offline.no-connector-id");
             return;
         } else if (!CONNECTOR_ID_PATTERN.matcher(connectorId).matches()) {
-            String msg = String.format("@text/offline.invalid-device-id [ \"%s\" ]", connectorId);
+            String msg = String.format("@text/offline.invalid-connector-id [ \"%s\" ]", connectorId);
             updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, msg);
             return;
         }
 
         queryString = QUERY_BASE_STRING + connectorId;
 
+        updateStatus(ThingStatus.UNKNOWN);
+
         scheduler.submit(this::startCommunication);
     }
 
index e55f7ea24a613acb4de75bedb53d4e966b5273d6..9779343ef743978121b15aed91c4ce1b6f5785a0 100644 (file)
  */
 package org.openhab.binding.elroconnects.internal.handler;
 
+import static org.openhab.binding.elroconnects.internal.ElroConnectsBindingConstants.*;
+
 import java.util.HashMap;
 import java.util.Map;
 
 import org.eclipse.jdt.annotation.NonNullByDefault;
 import org.eclipse.jdt.annotation.Nullable;
+import org.openhab.binding.elroconnects.internal.ElroConnectsBindingConstants.ElroDeviceType;
 import org.openhab.binding.elroconnects.internal.devices.ElroConnectsDevice;
 import org.openhab.binding.elroconnects.internal.util.ElroConnectsUtil;
 import org.openhab.core.thing.Bridge;
@@ -24,6 +27,7 @@ import org.openhab.core.thing.ChannelUID;
 import org.openhab.core.thing.Thing;
 import org.openhab.core.thing.ThingStatus;
 import org.openhab.core.thing.ThingStatusDetail;
+import org.openhab.core.thing.ThingStatusInfo;
 import org.openhab.core.thing.binding.BaseThingHandler;
 import org.openhab.core.types.Command;
 import org.openhab.core.types.RefreshType;
@@ -49,12 +53,30 @@ public class ElroConnectsDeviceHandler extends BaseThingHandler {
         deviceId = config.deviceId;
 
         ElroConnectsBridgeHandler bridgeHandler = getBridgeHandler();
+        if (bridgeHandler == null) {
+            // Thing status has already been updated in getBridgeHandler()
+            return;
+        }
 
-        if (bridgeHandler != null) {
-            bridgeHandler.setDeviceHandler(deviceId, this);
-            updateProperties(bridgeHandler);
-            updateDeviceName(bridgeHandler);
-            refreshChannels(bridgeHandler);
+        if (bridgeHandler.getThing().getStatus() == ThingStatus.ONLINE) {
+            ElroConnectsDevice device = bridgeHandler.getDevice(deviceId);
+            if (device != null) {
+                ElroDeviceType deviceType = TYPE_MAP.get(device.getDeviceType());
+                if ((deviceType == null) || !thing.getThingTypeUID().equals(THING_TYPE_MAP.get(deviceType))) {
+                    String msg = String.format("@text/offline.invalid-device-type [ \"%s\" ]", deviceType);
+                    updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, msg);
+                } else {
+                    bridgeHandler.setDeviceHandler(deviceId, this);
+                    updateProperties(bridgeHandler);
+                    updateDeviceName(bridgeHandler);
+                    refreshChannels(bridgeHandler);
+                }
+            } else {
+                String msg = String.format("@text/offline.invalid-device-id [ \"%d\" ]", deviceId);
+                updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, msg);
+            }
+        } else {
+            updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.BRIDGE_OFFLINE);
         }
     }
 
@@ -90,6 +112,15 @@ public class ElroConnectsDeviceHandler extends BaseThingHandler {
         return bridgeHandler;
     }
 
+    @Override
+    public void bridgeStatusChanged(ThingStatusInfo bridgeStatusInfo) {
+        if (bridgeStatusInfo.getStatus() == ThingStatus.ONLINE) {
+            initialize();
+        } else {
+            updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.BRIDGE_OFFLINE);
+        }
+    }
+
     @Override
     public void handleCommand(ChannelUID channelUID, Command command) {
         ElroConnectsBridgeHandler bridgeHandler = getBridgeHandler();
index 94da417072d59a84a7f5ff31caebbde1863db20e..56de5165b3c731b9ed305d01d2b469c8cb7dd085 100644 (file)
@@ -93,8 +93,8 @@ offline.credentials-error = Invalid username or password
 offline.request-timeout = Request timeout
 offline.request-failed = Request failed
 
-offline.no-device-id = Device ID not set
-offline.invalid-device-id = Device ID {0} not of format ST_xxxxxxxxxxxx with xxxxxxxxxxxx the lowercase MAC address of the connector
+offline.no-connector-id = Connector ID not set
+offline.invalid-connector-id = Connector ID {0} not of format ST_xxxxxxxxxxxx with xxxxxxxxxxxx the lowercase MAC address of the connector
 offline.find-ip-fail = Error trying to find IP address for connector with ID {0}
 offline.communication-data-error = Communication data error while starting communication
 offline.communication-error = Error in communication: {0}
@@ -104,3 +104,8 @@ offline.no-hub-address = Error in communication, hub address was not set
 
 offline.no-bridge = No bridge defined for device: {0}
 offline.no-bridge-handler = No bridge handler defined for device: {0}
+offline.invalid-device-id = Device ID {0} not defined in the connector
+offline.invalid-device-type = Thing type does not match device type {0}
+offline.device-not-syncing = Device {0} not syncing with K1 hub
+offline.device-fault = Device {0} has a fault
+