[chromecast] Added play URL actions to chromecast binding (#10245)

* Added play url actions to chromeCast binding

Signed-off-by: Scott Hanson <scooter_seh@yahoo.com>
This commit is contained in:
Scott H 2021-03-14 13:16:30 -04:00 committed by GitHub
parent 849442cd47
commit 5f1dd38083
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 135 additions and 1 deletions

View File

@ -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")
```

View File

@ -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)) {

View File

@ -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;
}
}

View File

@ -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<Class<? extends ThingHandlerService>> 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);

View File

@ -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.