Remove duplicated code. (#16393)

Signed-off-by: Alexander Falkenstern <alexander.falkenstern@gmail.com>
Signed-off-by: Ciprian Pascu <contact@ciprianpascu.ro>
This commit is contained in:
Alexander Falkenstern 2024-02-11 22:59:29 +01:00 committed by Ciprian Pascu
parent 6967a7ba71
commit 7af2f56734
2 changed files with 11 additions and 21 deletions

View File

@ -415,7 +415,7 @@ public abstract class ShellyBaseHandler extends BaseThingHandler
case CHANNEL_SENSOR_SLEEPTIME:
logger.debug("{}: Set sensor sleep time to {}", thingName, command);
int value = (int) getNumber(command);
int value = getNumber(command).intValue();
value = value > 0 ? Math.max(SHELLY_MOTION_SLEEPTIME_OFFSET, value - SHELLY_MOTION_SLEEPTIME_OFFSET)
: 0;
api.setSleepTime(value);
@ -432,7 +432,7 @@ public abstract class ShellyBaseHandler extends BaseThingHandler
logger.debug("{}: Select profile {}", thingName, command);
int id = -1;
if (command instanceof Number) {
id = (int) getNumber(command);
id = getNumber(command).intValue();
} else {
String cmd = command.toString();
if (isDigit(cmd.charAt(0))) {
@ -458,7 +458,7 @@ public abstract class ShellyBaseHandler extends BaseThingHandler
break;
case CHANNEL_CONTROL_SETTEMP:
logger.debug("{}: Set temperature to {}", thingName, command);
api.setValveTemperature(0, (int) getNumber(command));
api.setValveTemperature(0, getNumber(command).intValue());
break;
case CHANNEL_CONTROL_POSITION:
logger.debug("{}: Set position to {}", thingName, command);
@ -470,7 +470,7 @@ public abstract class ShellyBaseHandler extends BaseThingHandler
break;
case CHANNEL_CONTROL_BTIMER:
logger.debug("{}: Set boost timer to {}", thingName, command);
api.setValveBoostTime(0, (int) getNumber(command));
api.setValveBoostTime(0, getNumber(command).intValue());
break;
case CHANNEL_SENSOR_MUTE:
if (profile.isSmoke && ((OnOffType) command) == OnOffType.ON) {
@ -514,19 +514,6 @@ public abstract class ShellyBaseHandler extends BaseThingHandler
}
}
private double getNumber(Command command) {
if (command instanceof QuantityType<?> quantityCommand) {
return quantityCommand.doubleValue();
}
if (command instanceof DecimalType decimalCommand) {
return decimalCommand.doubleValue();
}
if (command instanceof Number numberCommand) {
return numberCommand.doubleValue();
}
throw new IllegalArgumentException("Invalid Number type for conversion: " + command);
}
/**
* Update device status and channels
*/

View File

@ -236,13 +236,16 @@ public class ShellyUtils {
}
public static Double getNumber(Command command) throws IllegalArgumentException {
if (command instanceof DecimalType decimalCommand) {
return decimalCommand.doubleValue();
}
if (command instanceof QuantityType<?> quantityCommand) {
return quantityCommand.doubleValue();
}
throw new IllegalArgumentException("Unable to convert number");
if (command instanceof DecimalType decimalCommand) {
return decimalCommand.doubleValue();
}
if (command instanceof Number numberCommand) {
return numberCommand.doubleValue();
}
throw new IllegalArgumentException("Invalid Number type for conversion: " + command);
}
public static OnOffType getOnOff(@Nullable Boolean value) {