[deconz] support for IncreaseDecreaseType commands (#8943)

* support for IncreaseDecreaseType commands
* fix spotless

Signed-off-by: Jan N. Klug <jan.n.klug@rub.de>
This commit is contained in:
J-N-K 2020-11-03 23:10:33 +01:00 committed by GitHub
parent 1c93eb9737
commit aaa464a255
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 21 additions and 16 deletions

View File

@ -13,6 +13,7 @@
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;
/**
@ -128,5 +129,7 @@ public class BindingConstants {
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();
}

View File

@ -69,13 +69,8 @@ public class Util {
*/
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()));
}
/**

View File

@ -29,12 +29,7 @@ import org.openhab.binding.deconz.internal.dto.LightMessage;
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;
@ -71,6 +66,7 @@ public class LightThingHandler extends DeconzBaseThingHandler<LightMessage> {
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);
@ -151,6 +147,17 @@ public class LightThingHandler extends DeconzBaseThingHandler<LightMessage> {
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;
@ -203,10 +210,10 @@ public class LightThingHandler extends DeconzBaseThingHandler<LightMessage> {
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;
}