]> git.basschouten.com Git - openhab-addons.git/commitdiff
Fixes BLU Discovery when Shelly Cloud Bluetooth Gateway is enabled (in (#17167)
authorMarkus Michels <markus7017@gmail.com>
Tue, 30 Jul 2024 12:04:37 +0000 (14:04 +0200)
committerGitHub <noreply@github.com>
Tue, 30 Jul 2024 12:04:37 +0000 (14:04 +0200)
this case the binding's gateway script could not get access to the BLE
observer).

Signed-off-by: Markus Michels <markus7017@gmail.com>
bundles/org.openhab.binding.shelly/src/main/java/org/openhab/binding/shelly/internal/api/ShellyApiInterface.java
bundles/org.openhab.binding.shelly/src/main/java/org/openhab/binding/shelly/internal/api1/Shelly1HttpApi.java
bundles/org.openhab.binding.shelly/src/main/java/org/openhab/binding/shelly/internal/api2/Shelly2ApiJsonDTO.java
bundles/org.openhab.binding.shelly/src/main/java/org/openhab/binding/shelly/internal/api2/Shelly2ApiRpc.java
bundles/org.openhab.binding.shelly/src/main/java/org/openhab/binding/shelly/internal/api2/ShellyBluApi.java

index bad011bd2e82e038b51084bd788e0a3667a0a624..3ee90f7e5910d63d5c21b80e89caf191d3dc9334 100644 (file)
@@ -112,7 +112,7 @@ public interface ShellyApiInterface {
 
     boolean setBluetooth(boolean enable) throws ShellyApiException;
 
-    String deviceReboot() throws ShellyApiException;
+    void deviceReboot() throws ShellyApiException;
 
     String setDebug(boolean enabled) throws ShellyApiException;
 
index a83f22d2510c1c93fa4fd16c2e3aa7f423fdbeef..3b568b156fb5fe29b9c77c3f60bb9c0ed0940e91 100644 (file)
@@ -357,8 +357,8 @@ public class Shelly1HttpApi extends ShellyHttpClient implements ShellyApiInterfa
     }
 
     @Override
-    public String deviceReboot() throws ShellyApiException {
-        return callApi(SHELLY_URL_RESTART, String.class);
+    public void deviceReboot() throws ShellyApiException {
+        callApi(SHELLY_URL_RESTART, String.class);
     }
 
     @Override
index 4fa2b34dc0dcba65bcd0b30057055441ea4de4ff..48bcc0ff215bb60733b1db43d6989b71be5a8ab6 100644 (file)
@@ -14,6 +14,8 @@ package org.openhab.binding.shelly.internal.api2;
 
 import java.util.ArrayList;
 
+import org.openhab.binding.shelly.internal.api2.Shelly2ApiJsonDTO.Shelly2DevConfigBle.Shelly2DevConfigBleObserver;
+import org.openhab.binding.shelly.internal.api2.Shelly2ApiJsonDTO.Shelly2DevConfigBle.Shelly2DevConfigBleRpc;
 import org.openhab.binding.shelly.internal.api2.Shelly2ApiJsonDTO.Shelly2DeviceStatus.Shelly2DeviceStatusResult;
 import org.openhab.binding.shelly.internal.api2.Shelly2ApiJsonDTO.Shelly2RpcBaseMessage.Shelly2RpcMessageError;
 
@@ -166,11 +168,21 @@ public class Shelly2ApiJsonDTO {
     public static final String SHELLY2_POWERLED_MATCH = "match_output";
     public static final String SHELLY2_POWERLED_INVERT = "inverted_output";
 
-    public class Shelly2DevConfigBle {
+    public static class Shelly2DevConfigBle {
+        public static class Shelly2DevConfigBleRpc {
+            public Boolean enable;
+        }
+
+        public static class Shelly2DevConfigBleObserver {
+            public Boolean enable;
+        }
+
         public Boolean enable;
+        public Shelly2DevConfigBleRpc rpc;
+        public Shelly2DevConfigBleObserver observer;
     }
 
-    public class Shelly2DevConfigEth {
+    public static class Shelly2DevConfigEth {
         public Boolean enable;
         public String ipv4mode;
         public String ip;
@@ -921,6 +933,10 @@ public class Shelly2ApiJsonDTO {
         public Boolean sysLedEnable;
         @SerializedName("power_led")
         public String powerLed;
+
+        // BLE
+        public Shelly2DevConfigBleRpc rpc;
+        public Shelly2DevConfigBleObserver observer;
     }
 
     public static class Shelly2RpcRequest {
index c9b854f7857804829f3bf8e917382e2676bc1037..09cd64ca4f7fa39eac39ee0418d61fe9dd04f468 100644 (file)
@@ -58,6 +58,7 @@ import org.openhab.binding.shelly.internal.api1.Shelly1ApiJsonDTO.ShellyStatusSe
 import org.openhab.binding.shelly.internal.api2.Shelly2ApiJsonDTO.Shelly2APClientList;
 import org.openhab.binding.shelly.internal.api2.Shelly2ApiJsonDTO.Shelly2AuthChallenge;
 import org.openhab.binding.shelly.internal.api2.Shelly2ApiJsonDTO.Shelly2ConfigParms;
+import org.openhab.binding.shelly.internal.api2.Shelly2ApiJsonDTO.Shelly2DevConfigBle.Shelly2DevConfigBleObserver;
 import org.openhab.binding.shelly.internal.api2.Shelly2ApiJsonDTO.Shelly2DeviceConfig.Shelly2DeviceConfigSta;
 import org.openhab.binding.shelly.internal.api2.Shelly2ApiJsonDTO.Shelly2DeviceConfig.Shelly2GetConfigResult;
 import org.openhab.binding.shelly.internal.api2.Shelly2ApiJsonDTO.Shelly2DeviceConfigAp;
@@ -317,15 +318,32 @@ public class Shelly2ApiRpc extends Shelly2ApiClient implements ShellyApiInterfac
             asyncApiRequest(SHELLYRPC_METHOD_GETSTATUS); // request periodic status updates from device
 
             try {
-                if (profile.alwaysOn && config.enableBluGateway != null) {
+                if (profile.alwaysOn && config.enableBluGateway != null && dc.ble != null) {
                     logger.debug("{}: BLU Gateway support is {} for this device", thingName,
                             config.enableBluGateway ? "enabled" : "disabled");
                     if (config.enableBluGateway) {
-                        boolean bluetooth = getBool(profile.settings.bluetooth);
-                        if (config.enableBluGateway && !bluetooth) {
-                            logger.info("{}: Bluetooth needs to be enabled to activate BLU Gateway mode", thingName);
+                        boolean bluetooth = getBool(dc.ble.enable);
+                        boolean observer = dc.ble.observer != null && getBool(dc.ble.observer.enable);
+                        if (!bluetooth) {
+                            logger.warn("{}: Bluetooth will be enabled to activate BLU Gateway mode", thingName);
                         }
+                        if (observer) {
+                            logger.warn("{}: Shelly Cloud Bluetooth Gateway conflicts with openHAB, disabling it",
+                                    thingName);
+                        }
+                        boolean restart = false;
+                        if (!bluetooth || observer) {
+                            logger.info("{}: Setup openHAB BLU Gateway", thingName);
+                            restart = setBluetooth(true);
+                        }
+
                         installScript(SHELLY2_BLU_GWSCRIPT, config.enableBluGateway && bluetooth);
+
+                        if (restart) {
+                            logger.info("{}: Restart device to activate BLU Gateway", thingName);
+                            deviceReboot();
+                            getThing().reinitializeThing();
+                        }
                     }
                 }
             } catch (ShellyApiException e) {
@@ -1013,13 +1031,17 @@ public class Shelly2ApiRpc extends Shelly2ApiClient implements ShellyApiInterfac
     public boolean setBluetooth(boolean enable) throws ShellyApiException {
         Shelly2RpcRequestParams params = new Shelly2RpcRequestParams().withConfig();
         params.config.enable = enable;
+        if (enable) {
+            params.config.observer = new Shelly2DevConfigBleObserver();
+            params.config.observer.enable = false;
+        }
         Shelly2WsConfigResult res = apiRequest(SHELLYRPC_METHOD_BLESETCONG, params, Shelly2WsConfigResult.class);
         return res.restartRequired;
     }
 
     @Override
-    public String deviceReboot() throws ShellyApiException {
-        return apiRequest(SHELLYRPC_METHOD_REBOOT, null, String.class);
+    public void deviceReboot() throws ShellyApiException {
+        apiRequest(SHELLYRPC_METHOD_REBOOT, null, String.class);
     }
 
     @Override
index 276bd50bcd69ac4951720b5afcb98db2d417089f..8fcb53a6f7dbb2d45a486ed38477332cafb05e93 100644 (file)
@@ -92,7 +92,6 @@ public class ShellyBluApi extends Shelly2ApiRpc {
         if (!initialized) {
             initialized = true;
             connected = false;
-        } else {
         }
     }
 
@@ -164,10 +163,6 @@ public class ShellyBluApi extends Shelly2ApiRpc {
             profile.status = deviceStatus;
         }
 
-        if (!connected) {
-            throw new ShellyApiException("BLU Device not yet connected");
-        }
-
         profile.initialized = true;
         return profile;
     }