Skip to content

Commit

Permalink
[hue] Channel alert added for groups (openhab#7810)
Browse files Browse the repository at this point in the history
* [hue] Channel alert added for groups

Fix openhab#7742
* Call cancelSceduledFuture in dispo()

Signed-off-by: Laurent Garnier <lg.hc@free.fr>
Signed-off-by: Daan Meijer <daan@studioseptember.nl>
  • Loading branch information
lolodomo authored and DaanMeijer committed Sep 1, 2020
1 parent 27455ed commit 29e46a9
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,16 @@
import java.math.BigDecimal;
import java.util.Collections;
import java.util.Set;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.eclipse.smarthome.core.library.types.HSBType;
import org.eclipse.smarthome.core.library.types.IncreaseDecreaseType;
import org.eclipse.smarthome.core.library.types.OnOffType;
import org.eclipse.smarthome.core.library.types.PercentType;
import org.eclipse.smarthome.core.library.types.StringType;
import org.eclipse.smarthome.core.thing.Bridge;
import org.eclipse.smarthome.core.thing.ChannelUID;
import org.eclipse.smarthome.core.thing.Thing;
Expand Down Expand Up @@ -64,6 +67,8 @@ public class HueGroupHandler extends BaseThingHandler implements GroupStatusList

private @Nullable HueClient hueClient;

private @Nullable ScheduledFuture<?> scheduledFuture;

public HueGroupHandler(Thing thing) {
super(thing);
}
Expand Down Expand Up @@ -110,6 +115,7 @@ private void initializeThing(@Nullable ThingStatus bridgeStatus) {
@Override
public void dispose() {
logger.debug("Hue group handler disposes. Unregistering listener.");
cancelScheduledFuture();
if (groupId != null) {
HueClient bridgeHandler = getHueClient();
if (bridgeHandler != null) {
Expand Down Expand Up @@ -231,6 +237,21 @@ public void handleCommand(String channel, Command command, long fadeTime) {
groupState.setTransitionTime(fadeTime);
}
break;
case CHANNEL_ALERT:
if (command instanceof StringType) {
groupState = LightStateConverter.toAlertState((StringType) command);
if (groupState == null) {
// Unsupported StringType is passed. Log a warning
// message and return.
logger.warn("Unsupported String command: {}. Supported commands are: {}, {}, {} ", command,
LightStateConverter.ALERT_MODE_NONE, LightStateConverter.ALERT_MODE_SELECT,
LightStateConverter.ALERT_MODE_LONG_SELECT);
return;
} else {
scheduleAlertStateRestore(command);
}
}
break;
default:
break;
}
Expand Down Expand Up @@ -355,6 +376,12 @@ public void onGroupStateChanged(@Nullable HueBridge bridge, FullGroup group) {
updateState(CHANNEL_BRIGHTNESS, brightnessPercentType);

updateState(CHANNEL_SWITCH, state.isOn() ? OnOffType.ON : OnOffType.OFF);

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

@Override
Expand All @@ -377,4 +404,69 @@ public void onGroupGone(@Nullable HueBridge bridge, FullGroup group) {
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.GONE, "@text/offline.group-removed");
}
}

/**
* Schedules restoration of the alert item state to {@link LightStateConverter#ALERT_MODE_NONE} after a given time.
* <br>
* Based on the initial command:
* <ul>
* <li>For {@link LightStateConverter#ALERT_MODE_SELECT} restoration will be triggered after <strong>2
* seconds</strong>.
* <li>For {@link LightStateConverter#ALERT_MODE_LONG_SELECT} restoration will be triggered after <strong>15
* seconds</strong>.
* </ul>
* This method also cancels any previously scheduled restoration.
*
* @param command The {@link Command} sent to the item
*/
private void scheduleAlertStateRestore(Command command) {
cancelScheduledFuture();
int delay = getAlertDuration(command);

if (delay > 0) {
scheduledFuture = scheduler.schedule(() -> {
updateState(CHANNEL_ALERT, new StringType(LightStateConverter.ALERT_MODE_NONE));
}, delay, TimeUnit.MILLISECONDS);
}
}

/**
* This method will cancel previously scheduled alert item state
* restoration.
*/
private void cancelScheduledFuture() {
if (scheduledFuture != null) {
scheduledFuture.cancel(true);
scheduledFuture = null;
}
}

/**
* This method returns the time in <strong>milliseconds</strong> after
* which, the state of the alert item has to be restored to {@link LightStateConverter#ALERT_MODE_NONE}.
*
* @param command The initial command sent to the alert item.
* @return Based on the initial command will return:
* <ul>
* <li><strong>2000</strong> for {@link LightStateConverter#ALERT_MODE_SELECT}.
* <li><strong>15000</strong> for {@link LightStateConverter#ALERT_MODE_LONG_SELECT}.
* <li><strong>-1</strong> for any command different from the previous two.
* </ul>
*/
private int getAlertDuration(Command command) {
int delay;
switch (command.toString()) {
case LightStateConverter.ALERT_MODE_LONG_SELECT:
delay = 15000;
break;
case LightStateConverter.ALERT_MODE_SELECT:
delay = 2000;
break;
default:
delay = -1;
break;
}

return delay;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,7 @@ private synchronized void initializeProperties(@Nullable FullLight fullLight) {
@Override
public void dispose() {
logger.debug("Hue light handler disposes. Unregistering listener.");
cancelScheduledFuture();
if (lightId != null) {
HueClient bridgeHandler = getHueClient();
if (bridgeHandler != null) {
Expand Down Expand Up @@ -553,6 +554,7 @@ private void scheduleAlertStateRestore(Command command) {
private void cancelScheduledFuture() {
if (scheduledFuture != null) {
scheduledFuture.cancel(true);
scheduledFuture = null;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
<channel id="color_temperature" typeId="color_temperature"/>
<channel id="brightness" typeId="brightness"/>
<channel id="color" typeId="color"/>
<channel id="alert" typeId="alert"/>
</channels>

<representation-property>groupId</representation-property>
Expand Down

0 comments on commit 29e46a9

Please sign in to comment.