diff --git a/bundles/org.openhab.binding.shelly/src/main/java/org/openhab/binding/shelly/internal/api/ShellyApiInterface.java b/bundles/org.openhab.binding.shelly/src/main/java/org/openhab/binding/shelly/internal/api/ShellyApiInterface.java index bad011bd2e8..3ee90f7e591 100644 --- a/bundles/org.openhab.binding.shelly/src/main/java/org/openhab/binding/shelly/internal/api/ShellyApiInterface.java +++ b/bundles/org.openhab.binding.shelly/src/main/java/org/openhab/binding/shelly/internal/api/ShellyApiInterface.java @@ -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; diff --git a/bundles/org.openhab.binding.shelly/src/main/java/org/openhab/binding/shelly/internal/api1/Shelly1HttpApi.java b/bundles/org.openhab.binding.shelly/src/main/java/org/openhab/binding/shelly/internal/api1/Shelly1HttpApi.java index a83f22d2510..3b568b156fb 100644 --- a/bundles/org.openhab.binding.shelly/src/main/java/org/openhab/binding/shelly/internal/api1/Shelly1HttpApi.java +++ b/bundles/org.openhab.binding.shelly/src/main/java/org/openhab/binding/shelly/internal/api1/Shelly1HttpApi.java @@ -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 diff --git a/bundles/org.openhab.binding.shelly/src/main/java/org/openhab/binding/shelly/internal/api2/Shelly2ApiJsonDTO.java b/bundles/org.openhab.binding.shelly/src/main/java/org/openhab/binding/shelly/internal/api2/Shelly2ApiJsonDTO.java index 4fa2b34dc0d..48bcc0ff215 100644 --- a/bundles/org.openhab.binding.shelly/src/main/java/org/openhab/binding/shelly/internal/api2/Shelly2ApiJsonDTO.java +++ b/bundles/org.openhab.binding.shelly/src/main/java/org/openhab/binding/shelly/internal/api2/Shelly2ApiJsonDTO.java @@ -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 { diff --git a/bundles/org.openhab.binding.shelly/src/main/java/org/openhab/binding/shelly/internal/api2/Shelly2ApiRpc.java b/bundles/org.openhab.binding.shelly/src/main/java/org/openhab/binding/shelly/internal/api2/Shelly2ApiRpc.java index c9b854f7857..09cd64ca4f7 100644 --- a/bundles/org.openhab.binding.shelly/src/main/java/org/openhab/binding/shelly/internal/api2/Shelly2ApiRpc.java +++ b/bundles/org.openhab.binding.shelly/src/main/java/org/openhab/binding/shelly/internal/api2/Shelly2ApiRpc.java @@ -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 diff --git a/bundles/org.openhab.binding.shelly/src/main/java/org/openhab/binding/shelly/internal/api2/ShellyBluApi.java b/bundles/org.openhab.binding.shelly/src/main/java/org/openhab/binding/shelly/internal/api2/ShellyBluApi.java index 276bd50bcd6..8fcb53a6f7d 100644 --- a/bundles/org.openhab.binding.shelly/src/main/java/org/openhab/binding/shelly/internal/api2/ShellyBluApi.java +++ b/bundles/org.openhab.binding.shelly/src/main/java/org/openhab/binding/shelly/internal/api2/ShellyBluApi.java @@ -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; }