]> git.basschouten.com Git - openhab-addons.git/commitdiff
Remove duplicated code. (#16393)
authorAlexander Falkenstern <falkena@users.noreply.github.com>
Sun, 11 Feb 2024 21:59:29 +0000 (22:59 +0100)
committerGitHub <noreply@github.com>
Sun, 11 Feb 2024 21:59:29 +0000 (22:59 +0100)
Signed-off-by: Alexander Falkenstern <alexander.falkenstern@gmail.com>
bundles/org.openhab.binding.shelly/src/main/java/org/openhab/binding/shelly/internal/handler/ShellyBaseHandler.java
bundles/org.openhab.binding.shelly/src/main/java/org/openhab/binding/shelly/internal/util/ShellyUtils.java

index 08e73cb78247a9eadc9a6af020bdc4e89619a906..e300f5d95cf622d8a608722c10a5d6618684102c 100755 (executable)
@@ -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
      */
index 5a4a82676676bf0e3e3b70ceaf517d74f4345af4..db494f2c177ec325113b4792d756b7605e58bd4f 100644 (file)
@@ -236,13 +236,16 @@ public class ShellyUtils {
     }
 
     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) {