Skip to content

Commit

Permalink
Added handling for INCREASE/DECREASE commands on volume channel (open…
Browse files Browse the repository at this point in the history
…hab#4742)

Signed-off-by: Christoph Weitkamp <[email protected]>
  • Loading branch information
cweitkamp authored and wborn committed Jan 30, 2019
1 parent b1c45ae commit 14dce31
Showing 1 changed file with 20 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import java.io.IOException;

import org.eclipse.smarthome.core.library.types.IncreaseDecreaseType;
import org.eclipse.smarthome.core.library.types.NextPreviousType;
import org.eclipse.smarthome.core.library.types.OnOffType;
import org.eclipse.smarthome.core.library.types.PercentType;
Expand Down Expand Up @@ -46,6 +47,8 @@ public class ChromecastCommander {
private final ChromecastScheduler scheduler;
private final ChromecastStatusUpdater statusUpdater;

private static final int VOLUMESTEP = 10;

public ChromecastCommander(ChromeCast chromeCast, ChromecastScheduler scheduler,
ChromecastStatusUpdater statusUpdater) {
this.chromeCast = chromeCast;
Expand Down Expand Up @@ -178,14 +181,23 @@ private void handleControl(final Command command) {

public void handleVolume(final Command command) {
if (command instanceof PercentType) {
final PercentType num = (PercentType) command;
try {
chromeCast.setVolumeByIncrement(num.floatValue() / 100);
statusUpdater.updateStatus(ThingStatus.ONLINE);
} catch (final IOException ex) {
logger.debug("Set volume failed: {}", ex.getMessage());
statusUpdater.updateStatus(ThingStatus.OFFLINE, COMMUNICATION_ERROR, ex.getMessage());
}
setVolumeInternal((PercentType) command);
} else if (command == IncreaseDecreaseType.INCREASE) {
setVolumeInternal(new PercentType(
Math.max(statusUpdater.getVolume().intValue() + VOLUMESTEP, PercentType.ZERO.intValue())));
} else if (command == IncreaseDecreaseType.DECREASE) {
setVolumeInternal(new PercentType(
Math.min(statusUpdater.getVolume().intValue() - VOLUMESTEP, PercentType.HUNDRED.intValue())));
}
}

private void setVolumeInternal(PercentType volume) {
try {
chromeCast.setVolumeByIncrement(volume.floatValue() / 100);
statusUpdater.updateStatus(ThingStatus.ONLINE);
} catch (final IOException ex) {
logger.debug("Set volume failed: {}", ex.getMessage());
statusUpdater.updateStatus(ThingStatus.OFFLINE, COMMUNICATION_ERROR, ex.getMessage());
}
}

Expand Down

0 comments on commit 14dce31

Please sign in to comment.