Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[hue] Reduce the number of warnings #8015

Merged
merged 3 commits into from
Jul 7, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,14 @@ public class HueBridgeConfig {
public static final String HTTP = "http";
public static final String HTTPS = "https";

private @NonNullByDefault({}) String ipAddress;
private @Nullable String ipAddress;
private @Nullable Integer port;
private String protocol = HTTP;
private @Nullable String userName;
private int pollingInterval = 10;
private int sensorPollingInterval = 500;

public String getIpAddress() {
public @Nullable String getIpAddress() {
return ipAddress;
}

Expand All @@ -42,7 +42,8 @@ public void setIpAddress(String ipAddress) {
}

public int getPort() {
return (port != null) ? port.intValue() : HTTPS.equals(protocol) ? 443 : 80;
Integer thePort = this.port;
return (thePort != null) ? thePort.intValue() : HTTPS.equals(protocol) ? 443 : 80;
}

public void setPort(int port) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,14 +89,14 @@ public class HueBridgeHandler extends ConfigStatusBridgeHandler implements HueCl

private final Logger logger = LoggerFactory.getLogger(HueBridgeHandler.class);

private final Map<String, FullLight> lastLightStates = new ConcurrentHashMap<>();
private final Map<String, FullSensor> lastSensorStates = new ConcurrentHashMap<>();
private final Map<String, FullGroup> lastGroupStates = new ConcurrentHashMap<>();
private final Map<String, @Nullable FullLight> lastLightStates = new ConcurrentHashMap<>();
private final Map<String, @Nullable FullSensor> lastSensorStates = new ConcurrentHashMap<>();
private final Map<String, @Nullable FullGroup> lastGroupStates = new ConcurrentHashMap<>();

private @Nullable HueLightDiscoveryService discoveryService;
private final Map<String, LightStatusListener> lightStatusListeners = new ConcurrentHashMap<>();
private final Map<String, SensorStatusListener> sensorStatusListeners = new ConcurrentHashMap<>();
private final Map<String, GroupStatusListener> groupStatusListeners = new ConcurrentHashMap<>();
private final Map<String, @Nullable LightStatusListener> lightStatusListeners = new ConcurrentHashMap<>();
private final Map<String, @Nullable SensorStatusListener> sensorStatusListeners = new ConcurrentHashMap<>();
private final Map<String, @Nullable GroupStatusListener> groupStatusListeners = new ConcurrentHashMap<>();

final ReentrantLock pollingLock = new ReentrantLock();

Expand Down Expand Up @@ -170,7 +170,7 @@ private boolean isReachable(String ipAddress) {
private final Runnable sensorPollingRunnable = new PollingRunnable() {
@Override
protected void doConnectedRun() throws IOException, ApiException {
Map<String, FullSensor> lastSensorStateCopy = new HashMap<>(lastSensorStates);
Map<String, @Nullable FullSensor> lastSensorStateCopy = new HashMap<>(lastSensorStates);

final HueLightDiscoveryService discovery = discoveryService;

Expand Down Expand Up @@ -204,7 +204,7 @@ protected void doConnectedRun() throws IOException, ApiException {
sensorStatusListener.onSensorRemoved();
}

if (discovery != null) {
if (discovery != null && sensor != null) {
discovery.removeSensorDiscovery(sensor);
}
});
Expand All @@ -214,7 +214,7 @@ protected void doConnectedRun() throws IOException, ApiException {
private final Runnable lightPollingRunnable = new PollingRunnable() {
@Override
protected void doConnectedRun() throws IOException, ApiException {
Map<String, FullLight> lastLightStateCopy = new HashMap<>(lastLightStates);
Map<String, @Nullable FullLight> lastLightStateCopy = new HashMap<>(lastLightStates);

List<FullLight> lights;
if (ApiVersionUtils.supportsFullLights(hueBridge.getVersion())) {
Expand Down Expand Up @@ -255,12 +255,12 @@ protected void doConnectedRun() throws IOException, ApiException {
lightStatusListener.onLightRemoved();
}

if (discovery != null) {
if (discovery != null && light != null) {
discovery.removeLightDiscovery(light);
}
});

Map<String, FullGroup> lastGroupStateCopy = new HashMap<>(lastGroupStates);
Map<String, @Nullable FullGroup> lastGroupStateCopy = new HashMap<>(lastGroupStates);

for (final FullGroup fullGroup : hueBridge.getGroups()) {
State groupState = new State();
Expand Down Expand Up @@ -339,7 +339,7 @@ protected void doConnectedRun() throws IOException, ApiException {
groupStatusListener.onGroupRemoved();
}

if (discovery != null) {
if (discovery != null && group != null) {
discovery.removeGroupDiscovery(group);
}
});
Expand Down Expand Up @@ -917,7 +917,7 @@ public void startSearch(List<String> serialNumbers) {
});
}

private <T> T withReAuthentication(String taskDescription, Callable<T> runnable) {
private @Nullable <T> T withReAuthentication(String taskDescription, Callable<T> runnable) {
if (hueBridge != null) {
try {
try {
Expand All @@ -941,7 +941,8 @@ public Collection<ConfigStatusMessage> getConfigStatus() {
Collection<ConfigStatusMessage> configStatusMessages;

// Check whether an IP address is provided
if (hueBridgeConfig.getIpAddress() == null || hueBridgeConfig.getIpAddress().isEmpty()) {
String ip = hueBridgeConfig.getIpAddress();
if (ip == null || ip.isEmpty()) {
configStatusMessages = Collections.singletonList(ConfigStatusMessage.Builder.error(HOST)
.withMessageKeySuffix(HueConfigStatusMessage.IP_ADDRESS_MISSING).withArguments(HOST).build());
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,9 @@ public void dispose() {
}
ThingHandler handler = bridge.getHandler();
if (handler instanceof HueBridgeHandler) {
hueClient = (HueClient) handler;
hueClient.registerGroupStatusListener(this);
HueClient bridgeHandler = (HueClient) handler;
hueClient = bridgeHandler;
bridgeHandler.registerGroupStatusListener(this);
} else {
return null;
}
Expand Down Expand Up @@ -164,6 +165,7 @@ public void handleCommand(String channel, Command command, long fadeTime) {
return;
}

Integer lastColorTemp;
StateUpdate groupState = null;
switch (channel) {
case CHANNEL_COLOR:
Expand All @@ -174,15 +176,11 @@ public void handleCommand(String channel, Command command, long fadeTime) {
} else {
groupState = LightStateConverter.toColorLightState(hsbCommand, group.getState());
groupState.setOn(true);
if (groupState != null) {
groupState.setTransitionTime(fadeTime);
}
groupState.setTransitionTime(fadeTime);
}
} else if (command instanceof PercentType) {
groupState = LightStateConverter.toBrightnessLightState((PercentType) command);
if (groupState != null) {
groupState.setTransitionTime(fadeTime);
}
groupState.setTransitionTime(fadeTime);
} else if (command instanceof OnOffType) {
groupState = LightStateConverter.toOnOffLightState((OnOffType) command);
} else if (command instanceof IncreaseDecreaseType) {
Expand All @@ -195,9 +193,7 @@ public void handleCommand(String channel, Command command, long fadeTime) {
case CHANNEL_COLORTEMPERATURE:
if (command instanceof PercentType) {
groupState = LightStateConverter.toColorTemperatureLightState((PercentType) command);
if (groupState != null) {
groupState.setTransitionTime(fadeTime);
}
groupState.setTransitionTime(fadeTime);
} else if (command instanceof OnOffType) {
groupState = LightStateConverter.toOnOffLightState((OnOffType) command);
} else if (command instanceof IncreaseDecreaseType) {
Expand All @@ -210,9 +206,7 @@ public void handleCommand(String channel, Command command, long fadeTime) {
case CHANNEL_BRIGHTNESS:
if (command instanceof PercentType) {
groupState = LightStateConverter.toBrightnessLightState((PercentType) command);
if (groupState != null) {
groupState.setTransitionTime(fadeTime);
}
groupState.setTransitionTime(fadeTime);
} else if (command instanceof OnOffType) {
groupState = LightStateConverter.toOnOffLightState((OnOffType) command);
} else if (command instanceof IncreaseDecreaseType) {
Expand All @@ -221,21 +215,23 @@ public void handleCommand(String channel, Command command, long fadeTime) {
groupState.setTransitionTime(fadeTime);
}
}
if (groupState != null && lastSentColorTemp != null) {
lastColorTemp = lastSentColorTemp;
if (groupState != null && lastColorTemp != null) {
// make sure that the light also has the latest color temp
// this might not have been yet set in the light, if it was off
groupState.setColorTemperature(lastSentColorTemp);
groupState.setColorTemperature(lastColorTemp);
groupState.setTransitionTime(fadeTime);
}
break;
case CHANNEL_SWITCH:
if (command instanceof OnOffType) {
groupState = LightStateConverter.toOnOffLightState((OnOffType) command);
}
if (groupState != null && lastSentColorTemp != null) {
lastColorTemp = lastSentColorTemp;
if (groupState != null && lastColorTemp != null) {
// make sure that the light also has the latest color temp
// this might not have been yet set in the light, if it was off
groupState.setColorTemperature(lastSentColorTemp);
groupState.setColorTemperature(lastColorTemp);
groupState.setTransitionTime(fadeTime);
}
break;
Expand Down Expand Up @@ -431,8 +427,9 @@ private void scheduleAlertStateRestore(Command command) {
* restoration.
*/
private void cancelScheduledFuture() {
if (scheduledFuture != null) {
scheduledFuture.cancel(true);
ScheduledFuture<?> scheduledJob = scheduledFuture;
if (scheduledJob != null) {
scheduledJob.cancel(true);
scheduledFuture = null;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,7 @@ public class HueLightHandler extends BaseThingHandler implements LightStatusList

private @Nullable HueClient hueClient;

@Nullable
ScheduledFuture<?> scheduledFuture;
private @Nullable ScheduledFuture<?> scheduledFuture;

public HueLightHandler(Thing hueLight) {
super(hueLight);
Expand Down Expand Up @@ -231,14 +230,13 @@ public void handleCommand(String channel, Command command, long fadeTime) {
return;
}

Integer lastColorTemp;
StateUpdate lightState = null;
switch (channel) {
case CHANNEL_COLORTEMPERATURE:
if (command instanceof PercentType) {
lightState = LightStateConverter.toColorTemperatureLightState((PercentType) command);
if (lightState != null) {
lightState.setTransitionTime(fadeTime);
}
lightState.setTransitionTime(fadeTime);
} else if (command instanceof OnOffType) {
lightState = LightStateConverter.toOnOffLightState((OnOffType) command);
if (isOsramPar16) {
Expand All @@ -255,9 +253,7 @@ public void handleCommand(String channel, Command command, long fadeTime) {
case CHANNEL_BRIGHTNESS:
if (command instanceof PercentType) {
lightState = LightStateConverter.toBrightnessLightState((PercentType) command);
if (lightState != null) {
lightState.setTransitionTime(fadeTime);
}
lightState.setTransitionTime(fadeTime);
} else if (command instanceof OnOffType) {
lightState = LightStateConverter.toOnOffLightState((OnOffType) command);
if (isOsramPar16) {
Expand All @@ -269,10 +265,11 @@ public void handleCommand(String channel, Command command, long fadeTime) {
lightState.setTransitionTime(fadeTime);
}
}
if (lightState != null && lastSentColorTemp != null) {
lastColorTemp = lastSentColorTemp;
if (lightState != null && lastColorTemp != null) {
// make sure that the light also has the latest color temp
// this might not have been yet set in the light, if it was off
lightState.setColorTemperature(lastSentColorTemp);
lightState.setColorTemperature(lastColorTemp);
lightState.setTransitionTime(fadeTime);
}
break;
Expand All @@ -284,10 +281,11 @@ public void handleCommand(String channel, Command command, long fadeTime) {
lightState = addOsramSpecificCommands(lightState, (OnOffType) command);
}
}
if (lightState != null && lastSentColorTemp != null) {
lastColorTemp = lastSentColorTemp;
if (lightState != null && lastColorTemp != null) {
// make sure that the light also has the latest color temp
// this might not have been yet set in the light, if it was off
lightState.setColorTemperature(lastSentColorTemp);
lightState.setColorTemperature(lastColorTemp);
lightState.setTransitionTime(fadeTime);
}
break;
Expand All @@ -298,15 +296,11 @@ public void handleCommand(String channel, Command command, long fadeTime) {
lightState = LightStateConverter.toOnOffLightState(OnOffType.OFF);
} else {
lightState = LightStateConverter.toColorLightState(hsbCommand, light.getState());
if (lightState != null) {
lightState.setTransitionTime(fadeTime);
}
lightState.setTransitionTime(fadeTime);
}
} else if (command instanceof PercentType) {
lightState = LightStateConverter.toBrightnessLightState((PercentType) command);
if (lightState != null) {
lightState.setTransitionTime(fadeTime);
}
lightState.setTransitionTime(fadeTime);
} else if (command instanceof OnOffType) {
lightState = LightStateConverter.toOnOffLightState((OnOffType) command);
} else if (command instanceof IncreaseDecreaseType) {
Expand Down Expand Up @@ -428,8 +422,9 @@ private StateUpdate createBrightnessStateUpdate(int currentBrightness, int newBr
}
ThingHandler handler = bridge.getHandler();
if (handler instanceof HueClient) {
hueClient = (HueClient) handler;
hueClient.registerLightStatusListener(this);
HueClient bridgeHandler = (HueClient) handler;
hueClient = bridgeHandler;
bridgeHandler.registerLightStatusListener(this);
} else {
return null;
}
Expand Down Expand Up @@ -576,8 +571,9 @@ private void scheduleAlertStateRestore(Command command) {
* restoration.
*/
private void cancelScheduledFuture() {
if (scheduledFuture != null) {
scheduledFuture.cancel(true);
ScheduledFuture<?> scheduledJob = scheduledFuture;
if (scheduledJob != null) {
scheduledJob.cancel(true);
scheduledFuture = null;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,8 +149,9 @@ public void dispose() {
}
ThingHandler handler = bridge.getHandler();
if (handler instanceof HueBridgeHandler) {
hueClient = (HueClient) handler;
hueClient.registerSensorStatusListener(this);
HueClient bridgeHandler = (HueClient) handler;
hueClient = bridgeHandler;
bridgeHandler.registerSensorStatusListener(this);
} else {
return null;
}
Expand Down