Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 2 commits into from
Mar 14, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions bundles/org.openhab.binding.chromecast/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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")

```
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ private void handleMute(final Command command) {
}
}

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)) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -26,6 +27,7 @@
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;
Expand All @@ -39,6 +41,7 @@
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;
Expand All @@ -53,6 +56,7 @@
* @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 {
Expand Down Expand Up @@ -205,6 +209,20 @@ public void setVolume(PercentType percentType) throws IOException {
}
}

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

Expand Down
Original file line number Diff line number Diff line change
@@ -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.