diff --git a/bundles/org.openhab.binding.yeelight/src/main/java/org/openhab/binding/yeelight/internal/handler/YeelightHandlerBase.java b/bundles/org.openhab.binding.yeelight/src/main/java/org/openhab/binding/yeelight/internal/handler/YeelightHandlerBase.java index 827d5543e3e..8543fcdae9f 100644 --- a/bundles/org.openhab.binding.yeelight/src/main/java/org/openhab/binding/yeelight/internal/handler/YeelightHandlerBase.java +++ b/bundles/org.openhab.binding.yeelight/src/main/java/org/openhab/binding/yeelight/internal/handler/YeelightHandlerBase.java @@ -80,6 +80,11 @@ public abstract class YeelightHandlerBase extends BaseThingHandler DeviceManager.getInstance().startDiscovery(15 * 1000); } + @Override + public void dispose() { + mDevice.disconnect(); + } + private DeviceType getDeviceModel(ThingTypeUID typeUID) { if (typeUID.equals(THING_TYPE_CEILING)) { return DeviceType.ceiling; diff --git a/bundles/org.openhab.binding.yeelight/src/main/java/org/openhab/binding/yeelight/internal/lib/device/DeviceBase.java b/bundles/org.openhab.binding.yeelight/src/main/java/org/openhab/binding/yeelight/internal/lib/device/DeviceBase.java index d8c17b3084e..f933e62493e 100644 --- a/bundles/org.openhab.binding.yeelight/src/main/java/org/openhab/binding/yeelight/internal/lib/device/DeviceBase.java +++ b/bundles/org.openhab.binding.yeelight/src/main/java/org/openhab/binding/yeelight/internal/lib/device/DeviceBase.java @@ -299,6 +299,11 @@ public abstract class DeviceBase { mConnection.connect(); } + public void disconnect() { + setConnectionState(ConnectState.DISCONNECTED); + mConnection.disconnect(); + } + public void setConnectionState(ConnectState connectState) { logger.debug("{}: set connection state to: {}", TAG, connectState.name()); if (connectState == ConnectState.DISCONNECTED) { diff --git a/bundles/org.openhab.binding.yeelight/src/main/java/org/openhab/binding/yeelight/internal/lib/device/connection/WifiConnection.java b/bundles/org.openhab.binding.yeelight/src/main/java/org/openhab/binding/yeelight/internal/lib/device/connection/WifiConnection.java index d257816d18e..cb12e82d644 100644 --- a/bundles/org.openhab.binding.yeelight/src/main/java/org/openhab/binding/yeelight/internal/lib/device/connection/WifiConnection.java +++ b/bundles/org.openhab.binding.yeelight/src/main/java/org/openhab/binding/yeelight/internal/lib/device/connection/WifiConnection.java @@ -128,6 +128,20 @@ public class WifiConnection implements ConnectionBase { @Override public boolean disconnect() { mDevice.setAutoConnect(false); - return false; + mCmdRun = false; + try { + if (mConnectThread != null) { + mConnectThread.interrupt(); + } + if (mSocket != null) { + mSocket.close(); + } + } catch (Exception e) { + logger.debug("Exception while terminating connection", e); + } finally { + mSocket = null; + mDevice.setConnectionState(ConnectState.DISCONNECTED); + } + return true; } }