2 * Copyright (c) 2010-2023 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
7 * This program and the accompanying materials are made available under the
8 * terms of the Eclipse Public License 2.0 which is available at
9 * http://www.eclipse.org/legal/epl-2.0
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.spotify.internal.actions;
15 import org.eclipse.jdt.annotation.NonNullByDefault;
16 import org.eclipse.jdt.annotation.Nullable;
17 import org.openhab.binding.spotify.internal.handler.SpotifyBridgeHandler;
18 import org.openhab.core.automation.annotation.ActionInput;
19 import org.openhab.core.automation.annotation.RuleAction;
20 import org.openhab.core.thing.binding.ThingActions;
21 import org.openhab.core.thing.binding.ThingActionsScope;
22 import org.openhab.core.thing.binding.ThingHandler;
23 import org.openhab.core.thing.binding.ThingHandlerService;
26 * Spotify Rule Actions.
28 * @author Hilbrand Bouwkamp - Initial contribution
30 @ThingActionsScope(name = "spotify")
32 public class SpotifyActions implements ThingActions, ThingHandlerService {
34 private @Nullable ThingHandler handler;
37 * Play a context uri (track or other) on the current active device (if null is passed for deviceID) or the given
38 * device at the given offset and/or position in milliseconds.
40 * @param actions Spotify Actions object.
41 * @param contextUri context uri (track or other)
42 * @param deviceId Id of the device to play on, or current device if given null
43 * @param offset Offset in the list, default 0.
44 * @param positionMs position in the track in milliseconds, default 0,
46 @RuleAction(label = "@text/actions.play.label", description = "@text/actions.play.description")
48 @ActionInput(name = "contextUri", label = "@text/actions.play.context_uri.label", description = "@text/actions.play.context_uri.description", type = "java.lang.String", required = true) String contextUri,
49 @ActionInput(name = "deviceId", label = "@text/actions.play.device_id.label", description = "@text/actions.play.device_id.description", type = "java.lang.String", defaultValue = "") @Nullable String deviceId,
50 @ActionInput(name = "offset", label = "@text/actions.play.offset.label", description = "@text/actions.play.offset.description", type = "java.lang.Integer", defaultValue = "0") final int offset,
51 @ActionInput(name = "positionMs", label = "@text/actions.play.positions_ms.label", description = "@text/actions.play.positions_ms.description", type = "java.lang.Integer", defaultValue = "0") final int positionMs) {
52 ((SpotifyBridgeHandler) getThingHandler()).getSpotifyApi().playTrack(deviceId == null ? "" : deviceId,
53 contextUri, offset, positionMs);
57 * Play a context uri (track or other) on the current active device.
59 * @param actions Spotify Actions object.
60 * @param contextUri context uri (track or other)
62 public static void play(ThingActions actions, String contextUri) {
63 ((SpotifyActions) actions).play(contextUri, null, 0, 0);
67 * Play a context uri (track or other) on the current active device at the given offset and/or position in
70 * @param actions Spotify Actions object.
71 * @param contextUri context uri (track or other)
72 * @param offset Offset in the list, default 0.
73 * @param positionMs position in the track in milliseconds, default 0,
75 public static void play(ThingActions actions, String contextUri, final int offset, final int positionMs) {
76 ((SpotifyActions) actions).play(contextUri, null, positionMs, positionMs);
80 * Play a context uri (track or other) on the given device.
82 * @param actions Spotify Actions object.
83 * @param contextUri context uri (track or other)
84 * @param deviceId Id of the device to play on, or current device if given null
86 public static void play(ThingActions actions, String contextUri, @Nullable String deviceId) {
87 ((SpotifyActions) actions).play(contextUri, deviceId, 0, 0);
91 * Play a context uri (track or other) on the current active device (if null is passed for deviceID) or the given
92 * device at the given offset and/or position in milliseconds.
94 * @param actions Spotify Actions object.
95 * @param contextUri context uri (track or other)
96 * @param deviceId Id of the device to play on, or current device if given null
97 * @param offset Offset in the list, default 0.
98 * @param positionMs position in the track in milliseconds, default 0,
100 public static void play(ThingActions actions, String contextUri, @Nullable String deviceId, final int offset,
101 final int positionMs) {
102 ((SpotifyActions) actions).play(contextUri, deviceId, positionMs, positionMs);
106 public void setThingHandler(ThingHandler handler) {
107 this.handler = handler;
111 public @Nullable ThingHandler getThingHandler() {