From cbe4024f81d946d19aaaea8d94bde426cb538202 Mon Sep 17 00:00:00 2001 From: Brian O'Connell Date: Thu, 4 Nov 2021 08:24:14 -0400 Subject: [PATCH] feat(chromecast): added support for next command Signed-off-by: Brian O'Connell --- .../org.openhab.binding.chromecast/README.md | 2 +- .../internal/ChromecastCommander.java | 17 +++++++++++++---- .../internal/ChromecastStatusUpdater.java | 8 ++++++++ 3 files changed, 22 insertions(+), 5 deletions(-) diff --git a/bundles/org.openhab.binding.chromecast/README.md b/bundles/org.openhab.binding.chromecast/README.md index f4d3f2f59e49c..9b15e2452b9a8 100644 --- a/bundles/org.openhab.binding.chromecast/README.md +++ b/bundles/org.openhab.binding.chromecast/README.md @@ -49,7 +49,7 @@ Thing chromecast:audiogroup:bathroom [ ipAddress="192.168.0.23", port=42139] | Channel Type ID | Item Type | Description | |-----------------|-------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| -| control | Player | Player control; currently only supports play/pause and does not correctly update, if the state changes on the device itself | +| control | Player | Player control; currently only supports play/pause/next and does not correctly update, if the state changes on the device itself | | stop | Switch | Send `ON` to this channel: Stops the Chromecast. If this channel is `ON`, the Chromecast is stopped, otherwise it is in another state (see control channel) | | volume | Dimmer | Control the volume, this is also updated if the volume is changed by another app | | mute | Switch | Mute the audio | diff --git a/bundles/org.openhab.binding.chromecast/src/main/java/org/openhab/binding/chromecast/internal/ChromecastCommander.java b/bundles/org.openhab.binding.chromecast/src/main/java/org/openhab/binding/chromecast/internal/ChromecastCommander.java index 2a031a4bd2def..cd8be2087ab96 100644 --- a/bundles/org.openhab.binding.chromecast/src/main/java/org/openhab/binding/chromecast/internal/ChromecastCommander.java +++ b/bundles/org.openhab.binding.chromecast/src/main/java/org/openhab/binding/chromecast/internal/ChromecastCommander.java @@ -135,10 +135,19 @@ private void handlePlayUri(Command command) { private void handleControl(final Command command) { try { if (command instanceof NextPreviousType) { - // I can't find a way to control next/previous from the API. The Google app doesn't seem to - // allow it either, so I suspect there isn't a way. - logger.info("{} command not yet implemented", command); - return; + // Next is implemented by seeking to the end of the current media + if (command == NextPreviousType.NEXT) { + + Double duration = statusUpdater.getLastDuration(); + if (duration != null) { + chromeCast.seek(duration.doubleValue() - 5); + } else { + logger.info("{} command failed - unknown media duration", command); + } + } else { + logger.info("{} command not yet implemented", command); + return; + } } Application app = chromeCast.getRunningApp(); diff --git a/bundles/org.openhab.binding.chromecast/src/main/java/org/openhab/binding/chromecast/internal/ChromecastStatusUpdater.java b/bundles/org.openhab.binding.chromecast/src/main/java/org/openhab/binding/chromecast/internal/ChromecastStatusUpdater.java index 8e8581c8daf27..1ce09bf5c69b6 100644 --- a/bundles/org.openhab.binding.chromecast/src/main/java/org/openhab/binding/chromecast/internal/ChromecastStatusUpdater.java +++ b/bundles/org.openhab.binding.chromecast/src/main/java/org/openhab/binding/chromecast/internal/ChromecastStatusUpdater.java @@ -73,6 +73,9 @@ public class ChromecastStatusUpdater { private @Nullable String appSessionId; private PercentType volume = PercentType.ZERO; + // Null is valid value for last duration + private @Nullable Double lastDuration = null; + public ChromecastStatusUpdater(Thing thing, ChromecastHandler callback) { this.thing = thing; this.callback = callback; @@ -82,6 +85,10 @@ public PercentType getVolume() { return volume; } + public @Nullable Double getLastDuration() { + return lastDuration; + } + public @Nullable String getAppSessionId() { return appSessionId; } @@ -186,6 +193,7 @@ private void updateMediaInfoStatus(final @Nullable Media media) { if (media != null) { metadataType = media.getMetadataType().name(); + lastDuration = media.duration; // duration can be null when a new song is about to play. if (media.duration != null) { duration = new QuantityType<>(media.duration, Units.SECOND);