package org.openhab.binding.deconz.internal;
import org.eclipse.jdt.annotation.NonNullByDefault;
+import org.openhab.core.library.types.PercentType;
import org.openhab.core.thing.ThingTypeUID;
/**
public static final int ZCL_CT_INVALID = 65535; // 0xFFFF
public static final double HUE_FACTOR = 65535 / 360.0;
- public static final double BRIGHTNESS_FACTOR = 2.54;
+ public static final int BRIGHTNESS_MIN = 0;
+ public static final int BRIGHTNESS_MAX = 254;
+ public static final double BRIGHTNESS_FACTOR = BRIGHTNESS_MAX / PercentType.HUNDRED.intValue();
}
*/
public static PercentType toPercentType(int val) {
int scaledValue = (int) Math.ceil(val / BRIGHTNESS_FACTOR);
- if (scaledValue < 0 || scaledValue > 100) {
- LOGGER.trace("received value {} (converted to {}). Coercing.", val, scaledValue);
- scaledValue = scaledValue < 0 ? 0 : scaledValue;
- scaledValue = scaledValue > 100 ? 100 : scaledValue;
- }
-
- return new PercentType(scaledValue);
+ return new PercentType(
+ Util.constrainToRange(scaledValue, PercentType.ZERO.intValue(), PercentType.HUNDRED.intValue()));
}
/**
import org.openhab.binding.deconz.internal.dto.LightState;
import org.openhab.binding.deconz.internal.netutils.AsyncHttpClient;
import org.openhab.binding.deconz.internal.types.ResourceType;
-import org.openhab.core.library.types.DecimalType;
-import org.openhab.core.library.types.HSBType;
-import org.openhab.core.library.types.OnOffType;
-import org.openhab.core.library.types.PercentType;
-import org.openhab.core.library.types.StopMoveType;
-import org.openhab.core.library.types.UpDownType;
+import org.openhab.core.library.types.*;
import org.openhab.core.thing.ChannelUID;
import org.openhab.core.thing.Thing;
import org.openhab.core.thing.ThingStatus;
THING_TYPE_WINDOW_COVERING, THING_TYPE_WARNING_DEVICE);
private static final long DEFAULT_COMMAND_EXPIRY_TIME = 250; // in ms
+ private static final int BRIGHTNESS_DIM_STEP = 26; // ~ 10%
private final Logger logger = LoggerFactory.getLogger(LightThingHandler.class);
case CHANNEL_COLOR:
if (command instanceof OnOffType) {
newLightState.on = (command == OnOffType.ON);
+ } else if (command instanceof IncreaseDecreaseType) {
+ // try to get best value for current brightness
+ int oldBri = currentBri != null ? currentBri
+ : (Boolean.TRUE.equals(currentOn) ? BRIGHTNESS_MAX : BRIGHTNESS_MIN);
+ if (command.equals(IncreaseDecreaseType.INCREASE)) {
+ newLightState.bri = Util.constrainToRange(oldBri + BRIGHTNESS_DIM_STEP, BRIGHTNESS_MIN,
+ BRIGHTNESS_MAX);
+ } else {
+ newLightState.bri = Util.constrainToRange(oldBri - BRIGHTNESS_DIM_STEP, BRIGHTNESS_MIN,
+ BRIGHTNESS_MAX);
+ }
} else if (command instanceof HSBType) {
HSBType hsbCommand = (HSBType) command;
if (command instanceof UpDownType) {
newLightState.on = (command == UpDownType.DOWN);
} else if (command == StopMoveType.STOP) {
- if (currentOn != null && currentOn && currentBri != null && currentBri <= 254) {
+ if (currentOn != null && currentOn && currentBri != null && currentBri <= BRIGHTNESS_MAX) {
// going down or currently stop (254 because of rounding error)
newLightState.on = true;
- } else if (currentOn != null && !currentOn && currentBri != null && currentBri > 0) {
+ } else if (currentOn != null && !currentOn && currentBri != null && currentBri > BRIGHTNESS_MIN) {
// going up or currently stopped
newLightState.on = false;
}