diff --git a/addons/binding/org.openhab.binding.chromecast/src/main/java/org/openhab/binding/chromecast/internal/ChromecastCommander.java b/addons/binding/org.openhab.binding.chromecast/src/main/java/org/openhab/binding/chromecast/internal/ChromecastCommander.java index 8afd758ea72fe..291f3fbc23b9a 100644 --- a/addons/binding/org.openhab.binding.chromecast/src/main/java/org/openhab/binding/chromecast/internal/ChromecastCommander.java +++ b/addons/binding/org.openhab.binding.chromecast/src/main/java/org/openhab/binding/chromecast/internal/ChromecastCommander.java @@ -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; @@ -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; @@ -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()); } }