Skip to content

Commit

Permalink
Remove duplicated code. (#16393)
Browse files Browse the repository at this point in the history
Signed-off-by: Alexander Falkenstern <[email protected]>
  • Loading branch information
falkena authored Feb 11, 2024
1 parent 4c4f292 commit 473b0ac
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -415,7 +415,7 @@ public void handleCommand(ChannelUID channelUID, Command command) {

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);
Expand All @@ -432,7 +432,7 @@ public void handleCommand(ChannelUID channelUID, Command command) {
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))) {
Expand All @@ -458,7 +458,7 @@ public void handleCommand(ChannelUID channelUID, Command command) {
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);
Expand All @@ -470,7 +470,7 @@ public void handleCommand(ChannelUID channelUID, Command command) {
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) {
Expand Down Expand Up @@ -514,19 +514,6 @@ public void handleCommand(ChannelUID channelUID, Command command) {
}
}

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
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -236,13 +236,16 @@ public static DecimalType getDecimal(@Nullable Long value) {
}

public static Double getNumber(Command command) throws IllegalArgumentException {
if (command instanceof QuantityType<?> quantityCommand) {
return quantityCommand.doubleValue();
}
if (command instanceof DecimalType decimalCommand) {
return decimalCommand.doubleValue();
}
if (command instanceof QuantityType<?> quantityCommand) {
return quantityCommand.doubleValue();
if (command instanceof Number numberCommand) {
return numberCommand.doubleValue();
}
throw new IllegalArgumentException("Unable to convert number");
throw new IllegalArgumentException("Invalid Number type for conversion: " + command);
}

public static OnOffType getOnOff(@Nullable Boolean value) {
Expand Down

0 comments on commit 473b0ac

Please sign in to comment.