Skip to content
This repository has been archived by the owner on May 7, 2020. It is now read-only.

Commit

Permalink
[Hue] Added state updates for switch changes (#3967)
Browse files Browse the repository at this point in the history
* Added state updates for switch changes.
Also added a few log messages and fixed a compiler warning.

Fixes #2775.

Signed-off-by: 9037568 <namraccr@gmail.com>
  • Loading branch information
9037568 authored and kaikreuzer committed Aug 30, 2017
1 parent b53b704 commit abb299d
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 28 deletions.
Expand Up @@ -449,19 +449,26 @@ private <T> T withReAuthentication(String taskDescription, Callable<T> runnable)
}

/**
* Iterate through lightStatusListeners and notify them about a changed ot added light state.
* Iterate through lightStatusListeners and notify them about a changed or added light state.
*
* @param fullLight
* @param type Can be "changed" if just a state has changed or "added" if this is a new light on the bridge.
*/
private void notifyLightStatusListeners(final FullLight fullLight, final String type) {
if (lightStatusListeners.isEmpty()) {
logger.debug("No light status listeners to notify of light change for light {}", fullLight.getId());
return;
}

for (LightStatusListener lightStatusListener : lightStatusListeners) {
try {
switch (type) {
case LIGHT_STATE_ADDED:
lightStatusListener.onLightAdded(hueBridge, fullLight);
logger.debug("Sending lightAdded for light {}", fullLight.getId());
break;
case LIGHT_STATE_CHANGED:
logger.debug("Sending lightStateChanged for light {}", fullLight.getId());
lightStatusListener.onLightStateChanged(hueBridge, fullLight);
break;
default:
Expand Down
Expand Up @@ -213,6 +213,7 @@ public void handleCommand(ChannelUID channelUID, Command command) {
}
break;
case CHANNEL_SWITCH:
logger.trace("CHANNEL_SWITCH handling command {}", command);
if (command instanceof OnOffType) {
lightState = LightStateConverter.toOnOffLightState((OnOffType) command);
if (isOsramPar16) {
Expand Down Expand Up @@ -260,7 +261,7 @@ public void handleCommand(ChannelUID channelUID, Command command) {
if (lightState != null) {
hueBridge.updateLightState(light, lightState);
} else {
logger.warn("Command send to an unknown channel id: {}", channelUID);
logger.warn("Command sent to an unknown channel id: {}", channelUID);
}
}

Expand Down Expand Up @@ -353,12 +354,17 @@ private synchronized HueBridgeHandler getHueBridgeHandler() {

@Override
public void onLightStateChanged(HueBridge bridge, FullLight fullLight) {
if (fullLight != null && fullLight.getId().equals(lightId)) {
logger.trace("onLightStateChanged() was called");

initializeProperties();
if (!fullLight.getId().equals(lightId)) {
logger.trace("Received state change for another handler's light ({}). Will be ignored.", fullLight.getId());
return;
}

initializeProperties();

lastSentColorTemp = null;
lastSentBrightness = null;
lastSentColorTemp = null;
lastSentBrightness = null;

// update status (ONLINE, OFFLINE)
if (fullLight.getState().isReachable()) {
Expand All @@ -369,26 +375,31 @@ public void onLightStateChanged(HueBridge bridge, FullLight fullLight) {
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.NONE, "@text/offline.light-not-reachable");
}

HSBType hsbType = LightStateConverter.toHSBType(fullLight.getState());
if (!fullLight.getState().isOn()) {
hsbType = new HSBType(hsbType.getHue(), hsbType.getSaturation(), new PercentType(0));
}
updateState(CHANNEL_COLOR, hsbType);
HSBType hsbType = LightStateConverter.toHSBType(fullLight.getState());
if (!fullLight.getState().isOn()) {
hsbType = new HSBType(hsbType.getHue(), hsbType.getSaturation(), new PercentType(0));
}
updateState(CHANNEL_COLOR, hsbType);

PercentType percentType = LightStateConverter.toColorTemperaturePercentType(fullLight.getState());
updateState(CHANNEL_COLORTEMPERATURE, percentType);
PercentType percentType = LightStateConverter.toColorTemperaturePercentType(fullLight.getState());
updateState(CHANNEL_COLORTEMPERATURE, percentType);

percentType = LightStateConverter.toBrightnessPercentType(fullLight.getState());
if (!fullLight.getState().isOn()) {
percentType = new PercentType(0);
}
updateState(CHANNEL_BRIGHTNESS, percentType);
percentType = LightStateConverter.toBrightnessPercentType(fullLight.getState());
if (!fullLight.getState().isOn()) {
percentType = new PercentType(0);
}
updateState(CHANNEL_BRIGHTNESS, percentType);

StringType stringType = LightStateConverter.toAlertStringType(fullLight.getState());
if (!stringType.toString().equals("NULL")) {
updateState(CHANNEL_ALERT, stringType);
scheduleAlertStateRestore(stringType);
}
if (fullLight.getState().isOn()) {
updateState(CHANNEL_SWITCH, OnOffType.ON);
} else {
updateState(CHANNEL_SWITCH, OnOffType.OFF);
}

StringType stringType = LightStateConverter.toAlertStringType(fullLight.getState());
if (!stringType.toString().equals("NULL")) {
updateState(CHANNEL_ALERT, stringType);
scheduleAlertStateRestore(stringType);
}

}
Expand All @@ -397,7 +408,10 @@ public void onLightStateChanged(HueBridge bridge, FullLight fullLight) {
public void channelLinked(ChannelUID channelUID) {
HueBridgeHandler handler = getHueBridgeHandler();
if (handler != null) {
onLightStateChanged(null, handler.getLightById(lightId));
FullLight light = handler.getLightById(lightId);
if (light != null) {
onLightStateChanged(null, light);
}
}
}

Expand Down
Expand Up @@ -7,6 +7,7 @@
*/
package org.eclipse.smarthome.binding.hue.handler;

import org.eclipse.jdt.annotation.NonNull;
import org.eclipse.smarthome.binding.hue.internal.FullLight;
import org.eclipse.smarthome.binding.hue.internal.HueBridge;

Expand All @@ -26,22 +27,21 @@ public interface LightStatusListener {
* @param bridge The bridge the changed light is connected to.
* @param light The light which received the state update.
*/
void onLightStateChanged(HueBridge bridge, FullLight light);
void onLightStateChanged(HueBridge bridge, @NonNull FullLight light);

/**
* This method us called whenever a light is removed.
* This method is called whenever a light is removed.
*
* @param bridge The bridge the removed light was connected to.
* @param light The light which is removed.
*/
void onLightRemoved(HueBridge bridge, FullLight light);

/**
* This method us called whenever a light is added.
* This method is called whenever a light is added.
*
* @param bridge The bridge the added light was connected to.
* @param light The light which is added.
*/
void onLightAdded(HueBridge bridge, FullLight light);

}

0 comments on commit abb299d

Please sign in to comment.