2 * Copyright (c) 2010-2024 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.heos.internal.action;
15 import java.io.IOException;
17 import org.eclipse.jdt.annotation.NonNullByDefault;
18 import org.eclipse.jdt.annotation.Nullable;
19 import org.openhab.binding.heos.internal.api.HeosFacade;
20 import org.openhab.binding.heos.internal.exception.HeosNotConnectedException;
21 import org.openhab.binding.heos.internal.handler.HeosBridgeHandler;
22 import org.openhab.binding.heos.internal.resources.Telnet;
23 import org.openhab.core.automation.annotation.ActionInput;
24 import org.openhab.core.automation.annotation.RuleAction;
25 import org.openhab.core.thing.binding.ThingActions;
26 import org.openhab.core.thing.binding.ThingActionsScope;
27 import org.openhab.core.thing.binding.ThingHandler;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
32 * The class is responsible to call corresponding action on HEOS Handler
34 * @author Martin van Wingerden - Initial contribution
36 @ThingActionsScope(name = "heos")
38 public class HeosActions implements ThingActions {
40 private static final Logger logger = LoggerFactory.getLogger(HeosActions.class);
42 private @Nullable HeosBridgeHandler handler;
45 public void setThingHandler(@Nullable ThingHandler handler) {
46 if (handler instanceof HeosBridgeHandler bridgeHandler) {
47 this.handler = bridgeHandler;
52 public @Nullable ThingHandler getThingHandler() {
56 private @Nullable HeosFacade getConnection() throws HeosNotConnectedException {
57 HeosBridgeHandler handler = this.handler;
58 if (handler == null) {
62 return handler.getApiConnection();
65 @RuleAction(label = "play an input", description = "Play an input from another device.")
66 public void playInputFromPlayer(
67 @ActionInput(name = "source", label = "Source Player", description = "Player used for input") @Nullable Integer sourcePlayer,
68 @ActionInput(name = "input", label = "Source Input", description = "Input source used") @Nullable String input,
69 @ActionInput(name = "destination", label = "Destination Player", description = "Device for audio output") @Nullable Integer destinationPlayer) {
70 if (sourcePlayer == null || input == null || destinationPlayer == null) {
72 "Skipping HEOS playInputFromPlayer due to null value: sourcePlayer: {}, input: {}, destination: {}",
73 sourcePlayer, input, destinationPlayer);
78 HeosFacade connection = getConnection();
80 if (connection == null) {
81 logger.debug("Skipping HEOS playInputFromPlayer because no connection was available");
85 connection.playInputSource(destinationPlayer.toString(), sourcePlayer.toString(), input);
86 } catch (IOException | Telnet.ReadException e) {
87 logger.warn("Failed to play input source!", e);
91 public static void playInputFromPlayer(ThingActions actions, @Nullable Integer sourcePlayer, @Nullable String input,
92 @Nullable Integer destinationPlayer) {
93 ((HeosActions) actions).playInputFromPlayer(sourcePlayer, input, destinationPlayer);