]> git.basschouten.com Git - openhab-addons.git/blob
f9749b2b00a4a22b7a99c4db055ad880d4fa0da9
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
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
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.heos.internal.handler;
14
15 import static org.openhab.binding.heos.internal.HeosBindingConstants.CH_ID_FAVORITES;
16 import static org.openhab.binding.heos.internal.resources.HeosConstants.FAVORITE_SID;
17
18 import java.io.IOException;
19
20 import org.eclipse.jdt.annotation.NonNullByDefault;
21 import org.eclipse.jdt.annotation.Nullable;
22 import org.openhab.binding.heos.internal.exception.HeosNotFoundException;
23 import org.openhab.binding.heos.internal.resources.Telnet.ReadException;
24 import org.openhab.core.thing.ChannelUID;
25 import org.openhab.core.thing.ThingUID;
26 import org.openhab.core.types.Command;
27 import org.openhab.core.types.RefreshType;
28
29 /**
30  * The {@link HeosChannelHandlerFavorite} handles the playlist selection channel command
31  * from the implementing thing.
32  *
33  * @author Johannes Einig - Initial contribution
34  */
35 @NonNullByDefault
36 public class HeosChannelHandlerFavorite extends BaseHeosChannelHandler {
37     private final HeosDynamicStateDescriptionProvider heosDynamicStateDescriptionProvider;
38
39     public HeosChannelHandlerFavorite(HeosDynamicStateDescriptionProvider heosDynamicStateDescriptionProvider,
40             HeosBridgeHandler bridge) {
41         super(bridge);
42         this.heosDynamicStateDescriptionProvider = heosDynamicStateDescriptionProvider;
43     }
44
45     @Override
46     public void handlePlayerCommand(Command command, String id, ThingUID uid) throws IOException, ReadException {
47         handleCommand(command, id, uid);
48     }
49
50     @Override
51     public void handleGroupCommand(Command command, @Nullable String id, ThingUID uid,
52             HeosGroupHandler heosGroupHandler) throws IOException, ReadException {
53         if (id == null) {
54             throw new HeosNotFoundException();
55         }
56         handleCommand(command, id, uid);
57     }
58
59     @Override
60     public void handleBridgeCommand(Command command, ThingUID uid) {
61         // not used on bridge
62     }
63
64     private void handleCommand(Command command, String id, ThingUID uid) throws IOException, ReadException {
65         ChannelUID channelUID = new ChannelUID(uid, CH_ID_FAVORITES);
66         if (command instanceof RefreshType) {
67             heosDynamicStateDescriptionProvider.setFavorites(channelUID, getApi().getFavorites());
68             return;
69         }
70
71         String idCommand = heosDynamicStateDescriptionProvider.getValueByLabel(channelUID, command.toString());
72         getApi().playStream(id, FAVORITE_SID, idCommand);
73     }
74 }