forked from openhab/openhab-addons
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request openhab#2 from coeing/feature/shutter-control
Support for Bosch Shutter Control in-wall device
- Loading branch information
Showing
9 changed files
with
360 additions
and
108 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 5 additions & 0 deletions
5
...hc/src/main/java/org/openhab/binding/boschshc/internal/shuttercontrol/OperationState.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package org.openhab.binding.boschshc.internal.shuttercontrol; | ||
|
||
public enum OperationState { | ||
MOVING, STOPPED; | ||
} |
127 changes: 127 additions & 0 deletions
127
...main/java/org/openhab/binding/boschshc/internal/shuttercontrol/ShutterControlHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
package org.openhab.binding.boschshc.internal.shuttercontrol; | ||
|
||
import static org.openhab.binding.boschshc.internal.BoschSHCBindingConstants.*; | ||
|
||
import org.eclipse.jdt.annotation.NonNull; | ||
import org.eclipse.jdt.annotation.Nullable; | ||
import org.eclipse.smarthome.core.library.types.PercentType; | ||
import org.eclipse.smarthome.core.library.types.StopMoveType; | ||
import org.eclipse.smarthome.core.library.types.UpDownType; | ||
import org.eclipse.smarthome.core.thing.Bridge; | ||
import org.eclipse.smarthome.core.thing.ChannelUID; | ||
import org.eclipse.smarthome.core.thing.Thing; | ||
import org.eclipse.smarthome.core.types.Command; | ||
import org.eclipse.smarthome.core.types.RefreshType; | ||
import org.openhab.binding.boschshc.internal.BoschSHCBridgeHandler; | ||
import org.openhab.binding.boschshc.internal.BoschSHCHandler; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import com.google.gson.Gson; | ||
import com.google.gson.JsonElement; | ||
import com.google.gson.JsonSyntaxException; | ||
|
||
/** | ||
* Utility functions to convert data between Bosch things and openHAB items | ||
*/ | ||
final class DataConversion { | ||
public static int levelToOpenPercentage(double level) { | ||
return (int) Math.round((1 - level) * 100); | ||
} | ||
|
||
public static double openPercentageToLevel(double openPercentage) { | ||
return (100 - openPercentage) / 100.0; | ||
} | ||
} | ||
|
||
/** | ||
* Handler for a shutter control device | ||
*/ | ||
public class ShutterControlHandler extends BoschSHCHandler { | ||
private final Logger logger = LoggerFactory.getLogger(BoschSHCHandler.class); | ||
|
||
final String ShutterControlServiceName = "ShutterControl"; | ||
|
||
public ShutterControlHandler(Thing thing) { | ||
super(thing); | ||
} | ||
|
||
@Override | ||
public void handleCommand(ChannelUID channelUID, Command command) { | ||
if (command instanceof RefreshType) { | ||
ShutterControlState state = this.getDeviceState(); | ||
if (state == null) { | ||
logger.warn("Could not fetch device state, skipping refresh"); | ||
return; | ||
} | ||
this.updateState(state); | ||
} else if (command instanceof UpDownType) { | ||
// Set full close/open as target state | ||
UpDownType upDownType = (UpDownType) command; | ||
ShutterControlState state = new ShutterControlState(); | ||
if (upDownType == UpDownType.UP) { | ||
state.level = 1; | ||
} else if (upDownType == UpDownType.DOWN) { | ||
state.level = 0; | ||
} else { | ||
logger.warn("Received unknown UpDownType command: {}", upDownType); | ||
return; | ||
} | ||
this.setDeviceState(state); | ||
} else if (command instanceof StopMoveType) { | ||
// Set STOPPED operation state | ||
ShutterControlState state = new ShutterControlState(); | ||
state.operationState = OperationState.STOPPED; | ||
this.setDeviceState(state); | ||
} else if (command instanceof PercentType) { | ||
// Set specific level | ||
PercentType percentType = (PercentType) command; | ||
double level = DataConversion.openPercentageToLevel(percentType.doubleValue()); | ||
this.setDeviceState(new ShutterControlState(level)); | ||
} | ||
} | ||
|
||
@Override | ||
public void processUpdate(String id, @NonNull JsonElement state) { | ||
try { | ||
Gson gson = new Gson(); | ||
updateState(gson.fromJson(state, ShutterControlState.class)); | ||
} catch (JsonSyntaxException e) { | ||
logger.warn("Received unknown update in Shutter Control: {}", state); | ||
} | ||
} | ||
|
||
private BoschSHCBridgeHandler getBridgeHandler() { | ||
Bridge bridge = this.getBridge(); | ||
if (bridge == null) { | ||
return null; | ||
} | ||
return (BoschSHCBridgeHandler) bridge.getHandler(); | ||
} | ||
|
||
private @Nullable ShutterControlState getDeviceState() { | ||
BoschSHCBridgeHandler bridgeHandler = this.getBridgeHandler(); | ||
if (bridgeHandler == null) { | ||
return null; | ||
} | ||
return bridgeHandler.refreshState(getThing(), ShutterControlServiceName, ShutterControlState.class); | ||
} | ||
|
||
private void setDeviceState(ShutterControlState state) { | ||
BoschSHCBridgeHandler bridgeHandler = this.getBridgeHandler(); | ||
if (bridgeHandler == null) { | ||
return; | ||
} | ||
String deviceId = this.getBoschID(); | ||
if (deviceId == null) { | ||
return; | ||
} | ||
bridgeHandler.putState(deviceId, ShutterControlServiceName, state); | ||
} | ||
|
||
private void updateState(@NonNull ShutterControlState state) { | ||
// Convert level to open ratio | ||
int openPercentage = DataConversion.levelToOpenPercentage(state.level); | ||
updateState(CHANNEL_LEVEL, new PercentType(openPercentage)); | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
...c/main/java/org/openhab/binding/boschshc/internal/shuttercontrol/ShutterControlState.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package org.openhab.binding.boschshc.internal.shuttercontrol; | ||
|
||
import com.google.gson.annotations.SerializedName; | ||
|
||
public class ShutterControlState { | ||
@SerializedName("@type") | ||
public String type = "shutterControlState"; | ||
|
||
/** | ||
* Current open ratio of shutter (0.0 [closed] to 1.0 [open]) | ||
*/ | ||
public double level; | ||
|
||
/** | ||
* Current operation state of shutter | ||
*/ | ||
public OperationState operationState; | ||
|
||
public ShutterControlState() { | ||
this.level = 0.0; | ||
} | ||
|
||
public ShutterControlState(double level) { | ||
this.level = level; | ||
} | ||
} |
Oops, something went wrong.