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.osgi.service.component.annotations.Component;
29 import org.osgi.service.component.annotations.ServiceScope;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
34 * The class is responsible to call corresponding action on HEOS Handler
36 * @author Martin van Wingerden - Initial contribution
38 @Component(scope = ServiceScope.PROTOTYPE, service = HeosActions.class)
39 @ThingActionsScope(name = "heos")
41 public class HeosActions implements ThingActions {
43 private static final Logger logger = LoggerFactory.getLogger(HeosActions.class);
45 private @Nullable HeosBridgeHandler handler;
48 public void setThingHandler(@Nullable ThingHandler handler) {
49 if (handler instanceof HeosBridgeHandler bridgeHandler) {
50 this.handler = bridgeHandler;
55 public @Nullable ThingHandler getThingHandler() {
59 private @Nullable HeosFacade getConnection() throws HeosNotConnectedException {
60 HeosBridgeHandler handler = this.handler;
61 if (handler == null) {
65 return handler.getApiConnection();
68 @RuleAction(label = "play an input", description = "Play an input from another device.")
69 public void playInputFromPlayer(
70 @ActionInput(name = "source", label = "Source Player", description = "Player used for input") @Nullable Integer sourcePlayer,
71 @ActionInput(name = "input", label = "Source Input", description = "Input source used") @Nullable String input,
72 @ActionInput(name = "destination", label = "Destination Player", description = "Device for audio output") @Nullable Integer destinationPlayer) {
73 if (sourcePlayer == null || input == null || destinationPlayer == null) {
75 "Skipping HEOS playInputFromPlayer due to null value: sourcePlayer: {}, input: {}, destination: {}",
76 sourcePlayer, input, destinationPlayer);
81 HeosFacade connection = getConnection();
83 if (connection == null) {
84 logger.debug("Skipping HEOS playInputFromPlayer because no connection was available");
88 connection.playInputSource(destinationPlayer.toString(), sourcePlayer.toString(), input);
89 } catch (IOException | Telnet.ReadException e) {
90 logger.warn("Failed to play input source!", e);
94 public static void playInputFromPlayer(ThingActions actions, @Nullable Integer sourcePlayer, @Nullable String input,
95 @Nullable Integer destinationPlayer) {
96 ((HeosActions) actions).playInputFromPlayer(sourcePlayer, input, destinationPlayer);