Fixes BLU Discovery when Shelly Cloud Bluetooth Gateway is enabled (in (#17167)

this case the binding's gateway script could not get access to the BLE
observer).

Signed-off-by: Markus Michels <markus7017@gmail.com>
This commit is contained in:
Markus Michels 2024-07-30 14:04:37 +02:00 committed by Jacob Laursen
parent ae5d5ee701
commit c2877e08a6
5 changed files with 49 additions and 16 deletions

View 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;

View 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

View 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 {

View 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

View 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;
}