]> git.basschouten.com Git - openhab-addons.git/commitdiff
[hue] Fixed ColorTemperature set to UNDEF (#10609)
authorChristoph Weitkamp <github@christophweitkamp.de>
Sun, 9 May 2021 18:10:45 +0000 (20:10 +0200)
committerGitHub <noreply@github.com>
Sun, 9 May 2021 18:10:45 +0000 (20:10 +0200)
* Fixed ColorTemperature set to UNDEF

Signed-off-by: Christoph Weitkamp <github@christophweitkamp.de>
* Fixed SAT findings

Signed-off-by: Christoph Weitkamp <github@christophweitkamp.de>
* Fixed warning during unit tests

Signed-off-by: Christoph Weitkamp <github@christophweitkamp.de>
* Changed color temperature handling in GroupHandler

Signed-off-by: Christoph Weitkamp <github@christophweitkamp.de>
bundles/org.openhab.binding.hue/src/main/java/org/openhab/binding/hue/internal/ApiVersion.java
bundles/org.openhab.binding.hue/src/main/java/org/openhab/binding/hue/internal/Config.java
bundles/org.openhab.binding.hue/src/main/java/org/openhab/binding/hue/internal/Group.java
bundles/org.openhab.binding.hue/src/main/java/org/openhab/binding/hue/internal/HueBridge.java
bundles/org.openhab.binding.hue/src/main/java/org/openhab/binding/hue/internal/State.java
bundles/org.openhab.binding.hue/src/main/java/org/openhab/binding/hue/internal/handler/HueBridgeHandler.java
bundles/org.openhab.binding.hue/src/main/java/org/openhab/binding/hue/internal/handler/HueGroupHandler.java
bundles/org.openhab.binding.hue/src/main/java/org/openhab/binding/hue/internal/handler/HueLightHandler.java
bundles/org.openhab.binding.hue/src/test/java/org/openhab/binding/hue/internal/handler/HueLightHandlerTest.java

index 5075f95332a419a947810f71e4490e5f24584cc7..b338827b63c74814b83ad79560ba54123edd7537 100644 (file)
@@ -121,10 +121,7 @@ public class ApiVersion {
         if (micro != other.micro) {
             return false;
         }
-        if (minor != other.minor) {
-            return false;
-        }
-        return true;
+        return minor == other.minor;
     }
 
     @Override
index ab587ad19a6feda33b4893e2c303fd57cf29641b..81c8c28858a758ff6c6e440a55b8348cecd26cac 100644 (file)
@@ -132,7 +132,7 @@ public class Config {
      * @return ip address of proxy or null
      */
     public String getProxyAddress() {
-        return proxyaddress.equals("none") ? null : proxyaddress;
+        return "none".equals(proxyaddress) ? null : proxyaddress;
     }
 
     /**
@@ -141,7 +141,7 @@ public class Config {
      * @return port of proxy or null
      */
     public Integer getProxyPort() {
-        return proxyaddress.equals("none") ? null : proxyport;
+        return "none".equals(proxyaddress) ? null : proxyport;
     }
 
     /**
index 4808eb4221a6fb8125ecc86f359fedaae4d00508..5235d383d4434ce90ec7b094393b9b119928e52b 100644 (file)
@@ -58,7 +58,7 @@ public class Group {
      * @return modifiability of group
      */
     public boolean isModifiable() {
-        return !id.equals("0");
+        return !"0".equals(id);
     }
 
     /**
index 14b25f8cf17f01fe3f03b1c37b5847c642e72fdb..7b5b70c1c53b39c8fd1df482613607dae4e220c7 100644 (file)
@@ -840,7 +840,7 @@ public class HueBridge {
             @Override
             protected Result doNetwork(String address, String requestMethod, @Nullable String body) throws IOException {
                 // GET requests cannot be scheduled, so will continue working normally for convenience
-                if (requestMethod.equals("GET")) {
+                if ("GET".equals(requestMethod)) {
                     return super.doNetwork(address, requestMethod, body);
                 } else {
                     String extractedAddress = Util.quickMatch("^http://[^/]+(.+)$", address);
index 7ce101bdcf31fcb488bbca4fee368bba1dd9c8aa..1155691fc3c39d796216aa8d4ba3733ef85b4a37 100644 (file)
@@ -51,7 +51,7 @@ public class State {
         HS,
 
         /**
-         * Color temperature in mirek
+         * Color temperature in mired
          */
         CT
     }
@@ -287,9 +287,6 @@ public class State {
         if (sat != other.sat) {
             return false;
         }
-        if (!Arrays.equals(xy, other.xy)) {
-            return false;
-        }
-        return true;
+        return Arrays.equals(xy, other.xy);
     }
 }
index 5b3891340013d73dd18239672d6832c829ff3379..3ee5fc01eb7c236baf9e888e75f4c187b488b39c 100644 (file)
@@ -164,14 +164,12 @@ public class HueBridgeHandler extends ConfigStatusBridgeHandler implements HueCl
             } catch (IOException e) {
                 return false;
             } catch (ApiException e) {
-                if (e.getMessage().contains("SocketTimeout") || e.getMessage().contains("ConnectException")
-                        || e.getMessage().contains("SocketException")
-                        || e.getMessage().contains("NoRouteToHostException")) {
-                    return false;
-                } else {
-                    // this seems to be only an authentication issue
-                    return true;
-                }
+                String message = e.getMessage();
+                return message != null && //
+                        !message.contains("SocketTimeout") && //
+                        !message.contains("ConnectException") && //
+                        !message.contains("SocketException") && //
+                        !message.contains("NoRouteToHostException");
             }
             return true;
         }
index 27cdb5f0af72e3c1f7abf9e5d7704b918cf15c5d..efa228f2fe1b9d095d4098d4ea4406e3f388e822 100644 (file)
@@ -28,7 +28,6 @@ import org.eclipse.jdt.annotation.Nullable;
 import org.openhab.binding.hue.internal.FullGroup;
 import org.openhab.binding.hue.internal.Scene;
 import org.openhab.binding.hue.internal.State;
-import org.openhab.binding.hue.internal.State.ColorMode;
 import org.openhab.binding.hue.internal.StateUpdate;
 import org.openhab.binding.hue.internal.dto.ColorTemperature;
 import org.openhab.core.library.types.DecimalType;
@@ -48,7 +47,6 @@ import org.openhab.core.thing.binding.BaseThingHandler;
 import org.openhab.core.thing.binding.ThingHandler;
 import org.openhab.core.types.Command;
 import org.openhab.core.types.StateOption;
-import org.openhab.core.types.UnDefType;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -325,13 +323,13 @@ public class HueGroupHandler extends BaseThingHandler implements GroupStatusList
     private @Nullable Integer getCurrentColorTemp(@Nullable State groupState) {
         Integer colorTemp = lastSentColorTemp;
         if (colorTemp == null && groupState != null) {
-            colorTemp = groupState.getColorTemperature();
+            return groupState.getColorTemperature();
         }
         return colorTemp;
     }
 
     private @Nullable StateUpdate convertBrightnessChangeToStateUpdate(IncreaseDecreaseType command, FullGroup group) {
-        Integer currentBrightness = getCurrentBrightness(group);
+        Integer currentBrightness = getCurrentBrightness(group.getState());
         if (currentBrightness == null) {
             return null;
         }
@@ -339,15 +337,11 @@ public class HueGroupHandler extends BaseThingHandler implements GroupStatusList
         return createBrightnessStateUpdate(currentBrightness, newBrightness);
     }
 
-    private @Nullable Integer getCurrentBrightness(FullGroup group) {
-        if (lastSentBrightness != null) {
-            return lastSentBrightness;
+    private @Nullable Integer getCurrentBrightness(@Nullable State groupState) {
+        if (lastSentBrightness == null && groupState != null) {
+            return groupState.isOn() ? groupState.getBrightness() : 0;
         }
-        State currentState = group.getState();
-        if (currentState == null) {
-            return null;
-        }
-        return currentState.isOn() ? currentState.getBrightness() : 0;
+        return lastSentBrightness;
     }
 
     private StateUpdate createBrightnessStateUpdate(int currentBrightness, int newBrightness) {
@@ -406,24 +400,16 @@ public class HueGroupHandler extends BaseThingHandler implements GroupStatusList
         }
         updateState(CHANNEL_COLOR, hsbType);
 
-        ColorMode colorMode = state.getColorMode();
-        if (ColorMode.CT.equals(colorMode)) {
-            updateState(CHANNEL_COLORTEMPERATURE,
-                    LightStateConverter.toColorTemperaturePercentType(state, colorTemperatureCapabilties));
-            updateState(CHANNEL_COLORTEMPERATURE_ABS, LightStateConverter.toColorTemperature(state));
-        } else {
-            updateState(CHANNEL_COLORTEMPERATURE, UnDefType.UNDEF);
-            updateState(CHANNEL_COLORTEMPERATURE_ABS, UnDefType.UNDEF);
-        }
-
-        PercentType brightnessPercentType = LightStateConverter.toBrightnessPercentType(state);
-        if (!state.isOn()) {
-            brightnessPercentType = PercentType.ZERO;
-        }
+        PercentType brightnessPercentType = state.isOn() ? LightStateConverter.toBrightnessPercentType(state)
+                : PercentType.ZERO;
         updateState(CHANNEL_BRIGHTNESS, brightnessPercentType);
 
         updateState(CHANNEL_SWITCH, OnOffType.from(state.isOn()));
 
+        updateState(CHANNEL_COLORTEMPERATURE,
+                LightStateConverter.toColorTemperaturePercentType(state, colorTemperatureCapabilties));
+        updateState(CHANNEL_COLORTEMPERATURE_ABS, LightStateConverter.toColorTemperature(state));
+
         return true;
     }
 
index fcd6d37f43035249843deefd232ce32e3e91b8cb..fd727666b70bdbcea7c321f58fc1ede4c543adfc 100644 (file)
@@ -28,7 +28,6 @@ import org.eclipse.jdt.annotation.NonNullByDefault;
 import org.eclipse.jdt.annotation.Nullable;
 import org.openhab.binding.hue.internal.FullLight;
 import org.openhab.binding.hue.internal.State;
-import org.openhab.binding.hue.internal.State.ColorMode;
 import org.openhab.binding.hue.internal.StateUpdate;
 import org.openhab.binding.hue.internal.action.LightActions;
 import org.openhab.binding.hue.internal.dto.Capabilities;
@@ -52,7 +51,6 @@ import org.openhab.core.thing.binding.ThingHandlerService;
 import org.openhab.core.types.Command;
 import org.openhab.core.types.StateDescription;
 import org.openhab.core.types.StateDescriptionFragmentBuilder;
-import org.openhab.core.types.UnDefType;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -290,7 +288,6 @@ public class HueLightHandler extends BaseThingHandler implements LightStatusList
                 }
                 break;
             case CHANNEL_SWITCH:
-                logger.trace("CHANNEL_SWITCH handling command {}", command);
                 if (command instanceof OnOffType) {
                     lightState = LightStateConverter.toOnOffLightState((OnOffType) command);
                     if (isOsramPar16) {
@@ -391,31 +388,25 @@ public class HueLightHandler extends BaseThingHandler implements LightStatusList
     private @Nullable Integer getCurrentColorTemp(@Nullable State lightState) {
         Integer colorTemp = lastSentColorTemp;
         if (colorTemp == null && lightState != null) {
-            colorTemp = lightState.getColorTemperature();
+            return lightState.getColorTemperature();
         }
         return colorTemp;
     }
 
     private @Nullable StateUpdate convertBrightnessChangeToStateUpdate(IncreaseDecreaseType command, FullLight light) {
-        StateUpdate stateUpdate = null;
         Integer currentBrightness = getCurrentBrightness(light.getState());
-        if (currentBrightness != null) {
-            int newBrightness = LightStateConverter.toAdjustedBrightness(command, currentBrightness);
-            stateUpdate = createBrightnessStateUpdate(currentBrightness, newBrightness);
+        if (currentBrightness == null) {
+            return null;
         }
-        return stateUpdate;
+        int newBrightness = LightStateConverter.toAdjustedBrightness(command, currentBrightness);
+        return createBrightnessStateUpdate(currentBrightness, newBrightness);
     }
 
     private @Nullable Integer getCurrentBrightness(@Nullable State lightState) {
-        Integer brightness = lastSentBrightness;
-        if (brightness == null && lightState != null) {
-            if (!lightState.isOn()) {
-                brightness = 0;
-            } else {
-                brightness = lightState.getBrightness();
-            }
+        if (lastSentBrightness == null && lightState != null) {
+            return lightState.isOn() ? lightState.getBrightness() : 0;
         }
-        return brightness;
+        return lastSentBrightness;
     }
 
     private StateUpdate createBrightnessStateUpdate(int currentBrightness, int newBrightness) {
@@ -503,24 +494,16 @@ public class HueLightHandler extends BaseThingHandler implements LightStatusList
         }
         updateState(CHANNEL_COLOR, hsbType);
 
-        ColorMode colorMode = state.getColorMode();
-        if (ColorMode.CT.equals(colorMode)) {
-            updateState(CHANNEL_COLORTEMPERATURE,
-                    LightStateConverter.toColorTemperaturePercentType(state, colorTemperatureCapabilties));
-            updateState(CHANNEL_COLORTEMPERATURE_ABS, LightStateConverter.toColorTemperature(state));
-        } else {
-            updateState(CHANNEL_COLORTEMPERATURE, UnDefType.UNDEF);
-            updateState(CHANNEL_COLORTEMPERATURE_ABS, UnDefType.UNDEF);
-        }
-
-        PercentType brightnessPercentType = LightStateConverter.toBrightnessPercentType(state);
-        if (!state.isOn()) {
-            brightnessPercentType = PercentType.ZERO;
-        }
+        PercentType brightnessPercentType = state.isOn() ? LightStateConverter.toBrightnessPercentType(state)
+                : PercentType.ZERO;
         updateState(CHANNEL_BRIGHTNESS, brightnessPercentType);
 
         updateState(CHANNEL_SWITCH, OnOffType.from(state.isOn()));
 
+        updateState(CHANNEL_COLORTEMPERATURE,
+                LightStateConverter.toColorTemperaturePercentType(state, colorTemperatureCapabilties));
+        updateState(CHANNEL_COLORTEMPERATURE_ABS, LightStateConverter.toColorTemperature(state));
+
         StringType stringType = LightStateConverter.toAlertStringType(state);
         if (!"NULL".equals(stringType.toString())) {
             updateState(CHANNEL_ALERT, stringType);
index 58adabcb269096951626a64370314470b3afe7d3..7f2398af4bb5051cccf16aea83f5990f2f24f086 100644 (file)
@@ -38,6 +38,7 @@ import org.openhab.core.thing.ChannelUID;
 import org.openhab.core.thing.Thing;
 import org.openhab.core.thing.ThingStatus;
 import org.openhab.core.thing.ThingUID;
+import org.openhab.core.thing.binding.ThingHandlerCallback;
 import org.openhab.core.thing.i18n.ChannelTypeI18nLocalizationService;
 import org.openhab.core.types.Command;
 
@@ -411,6 +412,7 @@ public class HueLightHandlerTest {
                 return mockBridge;
             }
         };
+        hueLightHandler.setCallback(mock(ThingHandlerCallback.class));
         hueLightHandler.initialize();
 
         verify(mockThing).setProperty(eq(Thing.PROPERTY_MODEL_ID), eq(expectedModel));