[hue] Fixed ColorTemperature set to UNDEF (#10609)

* 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>
This commit is contained in:
Christoph Weitkamp 2021-05-09 20:10:45 +02:00 committed by GitHub
parent ea4315adaf
commit 22eebc797a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 41 additions and 78 deletions

View File

@ -121,10 +121,7 @@ public class ApiVersion {
if (micro != other.micro) { if (micro != other.micro) {
return false; return false;
} }
if (minor != other.minor) { return minor == other.minor;
return false;
}
return true;
} }
@Override @Override

View File

@ -132,7 +132,7 @@ public class Config {
* @return ip address of proxy or null * @return ip address of proxy or null
*/ */
public String getProxyAddress() { 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 * @return port of proxy or null
*/ */
public Integer getProxyPort() { public Integer getProxyPort() {
return proxyaddress.equals("none") ? null : proxyport; return "none".equals(proxyaddress) ? null : proxyport;
} }
/** /**

View File

@ -58,7 +58,7 @@ public class Group {
* @return modifiability of group * @return modifiability of group
*/ */
public boolean isModifiable() { public boolean isModifiable() {
return !id.equals("0"); return !"0".equals(id);
} }
/** /**

View File

@ -840,7 +840,7 @@ public class HueBridge {
@Override @Override
protected Result doNetwork(String address, String requestMethod, @Nullable String body) throws IOException { protected Result doNetwork(String address, String requestMethod, @Nullable String body) throws IOException {
// GET requests cannot be scheduled, so will continue working normally for convenience // 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); return super.doNetwork(address, requestMethod, body);
} else { } else {
String extractedAddress = Util.quickMatch("^http://[^/]+(.+)$", address); String extractedAddress = Util.quickMatch("^http://[^/]+(.+)$", address);

View File

@ -51,7 +51,7 @@ public class State {
HS, HS,
/** /**
* Color temperature in mirek * Color temperature in mired
*/ */
CT CT
} }
@ -287,9 +287,6 @@ public class State {
if (sat != other.sat) { if (sat != other.sat) {
return false; return false;
} }
if (!Arrays.equals(xy, other.xy)) { return Arrays.equals(xy, other.xy);
return false;
}
return true;
} }
} }

View File

@ -164,14 +164,12 @@ public class HueBridgeHandler extends ConfigStatusBridgeHandler implements HueCl
} catch (IOException e) { } catch (IOException e) {
return false; return false;
} catch (ApiException e) { } catch (ApiException e) {
if (e.getMessage().contains("SocketTimeout") || e.getMessage().contains("ConnectException") String message = e.getMessage();
|| e.getMessage().contains("SocketException") return message != null && //
|| e.getMessage().contains("NoRouteToHostException")) { !message.contains("SocketTimeout") && //
return false; !message.contains("ConnectException") && //
} else { !message.contains("SocketException") && //
// this seems to be only an authentication issue !message.contains("NoRouteToHostException");
return true;
}
} }
return true; return true;
} }

View File

@ -28,7 +28,6 @@ import org.eclipse.jdt.annotation.Nullable;
import org.openhab.binding.hue.internal.FullGroup; import org.openhab.binding.hue.internal.FullGroup;
import org.openhab.binding.hue.internal.Scene; import org.openhab.binding.hue.internal.Scene;
import org.openhab.binding.hue.internal.State; 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.StateUpdate;
import org.openhab.binding.hue.internal.dto.ColorTemperature; import org.openhab.binding.hue.internal.dto.ColorTemperature;
import org.openhab.core.library.types.DecimalType; 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.thing.binding.ThingHandler;
import org.openhab.core.types.Command; import org.openhab.core.types.Command;
import org.openhab.core.types.StateOption; import org.openhab.core.types.StateOption;
import org.openhab.core.types.UnDefType;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@ -325,13 +323,13 @@ public class HueGroupHandler extends BaseThingHandler implements GroupStatusList
private @Nullable Integer getCurrentColorTemp(@Nullable State groupState) { private @Nullable Integer getCurrentColorTemp(@Nullable State groupState) {
Integer colorTemp = lastSentColorTemp; Integer colorTemp = lastSentColorTemp;
if (colorTemp == null && groupState != null) { if (colorTemp == null && groupState != null) {
colorTemp = groupState.getColorTemperature(); return groupState.getColorTemperature();
} }
return colorTemp; return colorTemp;
} }
private @Nullable StateUpdate convertBrightnessChangeToStateUpdate(IncreaseDecreaseType command, FullGroup group) { private @Nullable StateUpdate convertBrightnessChangeToStateUpdate(IncreaseDecreaseType command, FullGroup group) {
Integer currentBrightness = getCurrentBrightness(group); Integer currentBrightness = getCurrentBrightness(group.getState());
if (currentBrightness == null) { if (currentBrightness == null) {
return null; return null;
} }
@ -339,15 +337,11 @@ public class HueGroupHandler extends BaseThingHandler implements GroupStatusList
return createBrightnessStateUpdate(currentBrightness, newBrightness); return createBrightnessStateUpdate(currentBrightness, newBrightness);
} }
private @Nullable Integer getCurrentBrightness(FullGroup group) { private @Nullable Integer getCurrentBrightness(@Nullable State groupState) {
if (lastSentBrightness != null) { if (lastSentBrightness == null && groupState != null) {
return lastSentBrightness; return groupState.isOn() ? groupState.getBrightness() : 0;
} }
State currentState = group.getState(); return lastSentBrightness;
if (currentState == null) {
return null;
}
return currentState.isOn() ? currentState.getBrightness() : 0;
} }
private StateUpdate createBrightnessStateUpdate(int currentBrightness, int newBrightness) { private StateUpdate createBrightnessStateUpdate(int currentBrightness, int newBrightness) {
@ -406,24 +400,16 @@ public class HueGroupHandler extends BaseThingHandler implements GroupStatusList
} }
updateState(CHANNEL_COLOR, hsbType); updateState(CHANNEL_COLOR, hsbType);
ColorMode colorMode = state.getColorMode(); PercentType brightnessPercentType = state.isOn() ? LightStateConverter.toBrightnessPercentType(state)
if (ColorMode.CT.equals(colorMode)) { : PercentType.ZERO;
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;
}
updateState(CHANNEL_BRIGHTNESS, brightnessPercentType); updateState(CHANNEL_BRIGHTNESS, brightnessPercentType);
updateState(CHANNEL_SWITCH, OnOffType.from(state.isOn())); updateState(CHANNEL_SWITCH, OnOffType.from(state.isOn()));
updateState(CHANNEL_COLORTEMPERATURE,
LightStateConverter.toColorTemperaturePercentType(state, colorTemperatureCapabilties));
updateState(CHANNEL_COLORTEMPERATURE_ABS, LightStateConverter.toColorTemperature(state));
return true; return true;
} }

View File

@ -28,7 +28,6 @@ import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable; import org.eclipse.jdt.annotation.Nullable;
import org.openhab.binding.hue.internal.FullLight; import org.openhab.binding.hue.internal.FullLight;
import org.openhab.binding.hue.internal.State; 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.StateUpdate;
import org.openhab.binding.hue.internal.action.LightActions; import org.openhab.binding.hue.internal.action.LightActions;
import org.openhab.binding.hue.internal.dto.Capabilities; 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.Command;
import org.openhab.core.types.StateDescription; import org.openhab.core.types.StateDescription;
import org.openhab.core.types.StateDescriptionFragmentBuilder; import org.openhab.core.types.StateDescriptionFragmentBuilder;
import org.openhab.core.types.UnDefType;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@ -290,7 +288,6 @@ public class HueLightHandler extends BaseThingHandler implements LightStatusList
} }
break; break;
case CHANNEL_SWITCH: case CHANNEL_SWITCH:
logger.trace("CHANNEL_SWITCH handling command {}", command);
if (command instanceof OnOffType) { if (command instanceof OnOffType) {
lightState = LightStateConverter.toOnOffLightState((OnOffType) command); lightState = LightStateConverter.toOnOffLightState((OnOffType) command);
if (isOsramPar16) { if (isOsramPar16) {
@ -391,31 +388,25 @@ public class HueLightHandler extends BaseThingHandler implements LightStatusList
private @Nullable Integer getCurrentColorTemp(@Nullable State lightState) { private @Nullable Integer getCurrentColorTemp(@Nullable State lightState) {
Integer colorTemp = lastSentColorTemp; Integer colorTemp = lastSentColorTemp;
if (colorTemp == null && lightState != null) { if (colorTemp == null && lightState != null) {
colorTemp = lightState.getColorTemperature(); return lightState.getColorTemperature();
} }
return colorTemp; return colorTemp;
} }
private @Nullable StateUpdate convertBrightnessChangeToStateUpdate(IncreaseDecreaseType command, FullLight light) { private @Nullable StateUpdate convertBrightnessChangeToStateUpdate(IncreaseDecreaseType command, FullLight light) {
StateUpdate stateUpdate = null;
Integer currentBrightness = getCurrentBrightness(light.getState()); Integer currentBrightness = getCurrentBrightness(light.getState());
if (currentBrightness != null) { if (currentBrightness == null) {
int newBrightness = LightStateConverter.toAdjustedBrightness(command, currentBrightness); return null;
stateUpdate = createBrightnessStateUpdate(currentBrightness, newBrightness);
} }
return stateUpdate; int newBrightness = LightStateConverter.toAdjustedBrightness(command, currentBrightness);
return createBrightnessStateUpdate(currentBrightness, newBrightness);
} }
private @Nullable Integer getCurrentBrightness(@Nullable State lightState) { private @Nullable Integer getCurrentBrightness(@Nullable State lightState) {
Integer brightness = lastSentBrightness; if (lastSentBrightness == null && lightState != null) {
if (brightness == null && lightState != null) { return lightState.isOn() ? lightState.getBrightness() : 0;
if (!lightState.isOn()) {
brightness = 0;
} else {
brightness = lightState.getBrightness();
}
} }
return brightness; return lastSentBrightness;
} }
private StateUpdate createBrightnessStateUpdate(int currentBrightness, int newBrightness) { private StateUpdate createBrightnessStateUpdate(int currentBrightness, int newBrightness) {
@ -503,24 +494,16 @@ public class HueLightHandler extends BaseThingHandler implements LightStatusList
} }
updateState(CHANNEL_COLOR, hsbType); updateState(CHANNEL_COLOR, hsbType);
ColorMode colorMode = state.getColorMode(); PercentType brightnessPercentType = state.isOn() ? LightStateConverter.toBrightnessPercentType(state)
if (ColorMode.CT.equals(colorMode)) { : PercentType.ZERO;
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;
}
updateState(CHANNEL_BRIGHTNESS, brightnessPercentType); updateState(CHANNEL_BRIGHTNESS, brightnessPercentType);
updateState(CHANNEL_SWITCH, OnOffType.from(state.isOn())); 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); StringType stringType = LightStateConverter.toAlertStringType(state);
if (!"NULL".equals(stringType.toString())) { if (!"NULL".equals(stringType.toString())) {
updateState(CHANNEL_ALERT, stringType); updateState(CHANNEL_ALERT, stringType);

View File

@ -38,6 +38,7 @@ import org.openhab.core.thing.ChannelUID;
import org.openhab.core.thing.Thing; import org.openhab.core.thing.Thing;
import org.openhab.core.thing.ThingStatus; import org.openhab.core.thing.ThingStatus;
import org.openhab.core.thing.ThingUID; import org.openhab.core.thing.ThingUID;
import org.openhab.core.thing.binding.ThingHandlerCallback;
import org.openhab.core.thing.i18n.ChannelTypeI18nLocalizationService; import org.openhab.core.thing.i18n.ChannelTypeI18nLocalizationService;
import org.openhab.core.types.Command; import org.openhab.core.types.Command;
@ -411,6 +412,7 @@ public class HueLightHandlerTest {
return mockBridge; return mockBridge;
} }
}; };
hueLightHandler.setCallback(mock(ThingHandlerCallback.class));
hueLightHandler.initialize(); hueLightHandler.initialize();
verify(mockThing).setProperty(eq(Thing.PROPERTY_MODEL_ID), eq(expectedModel)); verify(mockThing).setProperty(eq(Thing.PROPERTY_MODEL_ID), eq(expectedModel));