diff --git a/bundles/org.openhab.binding.chromecast/README.md b/bundles/org.openhab.binding.chromecast/README.md index 489dec544..35d3185ba 100644 --- a/bundles/org.openhab.binding.chromecast/README.md +++ b/bundles/org.openhab.binding.chromecast/README.md @@ -144,3 +144,19 @@ sitemap chromecast label="Chromecasts" { } } ``` + +## Rule Action + +This binding includes rule actions for casting media. + +* `playURL(String url)` +* `playURL(String url, String mimeType)` + +Examples: + +``` +val castActions = getActions("chromecast","chromecast:chromecast:29fcf535da") +val success = castActions.playURL("http://192.168.1.160:81/mjpg/front1/video.mjpg") +val success2 = castActions.playURL("http://192.168.1.160:81/mjpg/front1/video.mjpg", "image/jpeg") + +``` \ No newline at end of file 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 7b2f40783..2a031a4bd 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 @@ -219,7 +219,7 @@ public class ChromecastCommander { } } - void playMedia(@Nullable String title, @Nullable String url, @Nullable String mimeType) { + public void playMedia(@Nullable String title, @Nullable String url, @Nullable String mimeType) { try { if (chromeCast.isAppAvailable(MEDIA_PLAYER)) { if (!chromeCast.isAppRunning(MEDIA_PLAYER)) { diff --git a/bundles/org.openhab.binding.chromecast/src/main/java/org/openhab/binding/chromecast/internal/action/ChromecastActions.java b/bundles/org.openhab.binding.chromecast/src/main/java/org/openhab/binding/chromecast/internal/action/ChromecastActions.java new file mode 100644 index 000000000..ba7e07aae --- /dev/null +++ b/bundles/org.openhab.binding.chromecast/src/main/java/org/openhab/binding/chromecast/internal/action/ChromecastActions.java @@ -0,0 +1,94 @@ +/** + * Copyright (c) 2010-2021 Contributors to the openHAB project + * + * See the NOTICE file(s) distributed with this work for additional + * information. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License 2.0 which is available at + * http://www.eclipse.org/legal/epl-2.0 + * + * SPDX-License-Identifier: EPL-2.0 + */ +package org.openhab.binding.chromecast.internal.action; + +import org.eclipse.jdt.annotation.NonNullByDefault; +import org.eclipse.jdt.annotation.Nullable; +import org.openhab.binding.chromecast.internal.handler.ChromecastHandler; +import org.openhab.core.automation.annotation.ActionInput; +import org.openhab.core.automation.annotation.ActionOutput; +import org.openhab.core.automation.annotation.RuleAction; +import org.openhab.core.thing.binding.ThingActions; +import org.openhab.core.thing.binding.ThingActionsScope; +import org.openhab.core.thing.binding.ThingHandler; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * The {@link ChromecastActions} class defines rule actions for playing URLs + * + * @author Scott Hanson - Added Actions + */ +@ThingActionsScope(name = "chromecast") +@NonNullByDefault +public class ChromecastActions implements ThingActions { + + private final Logger logger = LoggerFactory.getLogger(ChromecastActions.class); + + private @Nullable ChromecastHandler handler; + + @RuleAction(label = "@text/playURLActionLabel", description = "@text/playURLActionDescription") + public @ActionOutput(name = "success", type = "java.lang.Boolean") Boolean playURL( + @ActionInput(name = "url") @Nullable String url) { + if (url == null) { + logger.warn("Cannot Play as URL is missing."); + return false; + } + + final ChromecastHandler handler = this.handler; + if (handler == null) { + logger.warn("Handler is null, cannot play."); + return false; + } else { + return handler.playURL(url, null); + } + } + + @RuleAction(label = "@text/playURLTypeActionLabel", description = "@text/playURLTypeActionDescription") + public @ActionOutput(name = "success", type = "java.lang.Boolean") Boolean playURL( + @ActionInput(name = "url") @Nullable String url, + @ActionInput(name = "mediaType") @Nullable String mediaType) { + if (url == null) { + logger.warn("Cannot Play as URL is missing."); + return false; + } + + final ChromecastHandler handler = this.handler; + if (handler == null) { + logger.warn("Handler is null, cannot tweet."); + return false; + } else { + return handler.playURL(url, mediaType); + } + } + + public static boolean playURL(ThingActions actions, @Nullable String url) { + return ((ChromecastActions) actions).playURL(url); + } + + public static boolean playURL(ThingActions actions, @Nullable String url, @Nullable String mediaType) { + return ((ChromecastActions) actions).playURL(url, mediaType); + } + + @Override + public void setThingHandler(@Nullable ThingHandler handler) { + if (handler instanceof ChromecastHandler) { + this.handler = (ChromecastHandler) handler; + } + } + + @Override + public @Nullable ThingHandler getThingHandler() { + return handler; + } +} diff --git a/bundles/org.openhab.binding.chromecast/src/main/java/org/openhab/binding/chromecast/internal/handler/ChromecastHandler.java b/bundles/org.openhab.binding.chromecast/src/main/java/org/openhab/binding/chromecast/internal/handler/ChromecastHandler.java index 664fbd267..b68ea5377 100644 --- a/bundles/org.openhab.binding.chromecast/src/main/java/org/openhab/binding/chromecast/internal/handler/ChromecastHandler.java +++ b/bundles/org.openhab.binding.chromecast/src/main/java/org/openhab/binding/chromecast/internal/handler/ChromecastHandler.java @@ -13,6 +13,7 @@ package org.openhab.binding.chromecast.internal.handler; import java.io.IOException; +import java.util.Collection; import java.util.Collections; import java.util.Locale; import java.util.Set; @@ -26,6 +27,7 @@ import org.openhab.binding.chromecast.internal.ChromecastCommander; import org.openhab.binding.chromecast.internal.ChromecastEventReceiver; import org.openhab.binding.chromecast.internal.ChromecastScheduler; import org.openhab.binding.chromecast.internal.ChromecastStatusUpdater; +import org.openhab.binding.chromecast.internal.action.ChromecastActions; import org.openhab.binding.chromecast.internal.config.ChromecastConfig; import org.openhab.core.audio.AudioFormat; import org.openhab.core.audio.AudioHTTPServer; @@ -39,6 +41,7 @@ import org.openhab.core.thing.Thing; import org.openhab.core.thing.ThingStatus; import org.openhab.core.thing.ThingStatusDetail; import org.openhab.core.thing.binding.BaseThingHandler; +import org.openhab.core.thing.binding.ThingHandlerService; import org.openhab.core.types.Command; import org.openhab.core.types.State; import org.slf4j.Logger; @@ -53,6 +56,7 @@ import su.litvak.chromecast.api.v2.ChromeCast; * @author Markus Rathgeb, Kai Kreuzer - Initial contribution * @author Daniel Walters - Online status fix, handle playuri channel and refactor play media code * @author Jason Holmes - Media Status. Refactor the monolith into separate classes. + * @author Scott Hanson - Added Actions. */ @NonNullByDefault public class ChromecastHandler extends BaseThingHandler implements AudioSink { @@ -205,6 +209,20 @@ public class ChromecastHandler extends BaseThingHandler implements AudioSink { } } + @Override + public Collection> getServices() { + return Collections.singletonList(ChromecastActions.class); + } + + public boolean playURL(String url, @Nullable String mediaType) { + Coordinator localCoordinator = coordinator; + if (localCoordinator != null) { + localCoordinator.commander.playMedia(null, url, mediaType); + return true; + } + return false; + } + private static class Coordinator { private final Logger logger = LoggerFactory.getLogger(Coordinator.class); diff --git a/bundles/org.openhab.binding.chromecast/src/main/resources/OH-INF/i18n/chromecast.properties b/bundles/org.openhab.binding.chromecast/src/main/resources/OH-INF/i18n/chromecast.properties new file mode 100644 index 000000000..a6cb3a91f --- /dev/null +++ b/bundles/org.openhab.binding.chromecast/src/main/resources/OH-INF/i18n/chromecast.properties @@ -0,0 +1,6 @@ +# actions +playURLActionLabel = play a URL +playURLActionDescription = Plays a URL. + +playURLTypeActionLabel = play a URL with a media type +playURLTypeActionDescription = Plays a URL with a defined media type attribute. \ No newline at end of file