]> git.basschouten.com Git - openhab-addons.git/blob
887cf6a992f3b9738ccb099514eb7ead78dcd72e
[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.spotify.internal.handler;
14
15 import static org.openhab.binding.spotify.internal.SpotifyBindingConstants.*;
16
17 import org.eclipse.jdt.annotation.NonNullByDefault;
18 import org.openhab.binding.spotify.internal.api.SpotifyApi;
19 import org.openhab.binding.spotify.internal.api.exception.SpotifyException;
20 import org.openhab.binding.spotify.internal.api.model.Device;
21 import org.openhab.core.library.types.OnOffType;
22 import org.openhab.core.library.types.PercentType;
23 import org.openhab.core.library.types.PlayPauseType;
24 import org.openhab.core.library.types.StringType;
25 import org.openhab.core.thing.Channel;
26 import org.openhab.core.thing.ChannelUID;
27 import org.openhab.core.thing.Thing;
28 import org.openhab.core.thing.ThingStatus;
29 import org.openhab.core.thing.ThingStatusDetail;
30 import org.openhab.core.thing.ThingStatusInfo;
31 import org.openhab.core.thing.binding.BaseThingHandler;
32 import org.openhab.core.types.Command;
33 import org.openhab.core.types.State;
34 import org.openhab.core.types.UnDefType;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
37
38 /**
39  * The {@link SpotifyDeviceHandler} is responsible for handling commands, which are sent to one of the channels.
40  *
41  * @author Andreas Stenlund - Initial contribution
42  * @author Hilbrand Bouwkamp - Code cleanup, moved channel state to this class, generic stability.
43  */
44 @NonNullByDefault
45 public class SpotifyDeviceHandler extends BaseThingHandler {
46
47     private final Logger logger = LoggerFactory.getLogger(SpotifyDeviceHandler.class);
48     private @NonNullByDefault({}) SpotifyHandleCommands commandHandler;
49     private @NonNullByDefault({}) SpotifyApi spotifyApi;
50     private String deviceName = "";
51     private String deviceId = "";
52
53     private boolean active;
54
55     /**
56      * Constructor.
57      *
58      * @param thing Thing representing this device.
59      */
60     public SpotifyDeviceHandler(Thing thing) {
61         super(thing);
62     }
63
64     @Override
65     public void handleCommand(ChannelUID channelUID, Command command) {
66         try {
67             if (commandHandler != null && !deviceId.isEmpty()) {
68                 commandHandler.handleCommand(channelUID, command, active, deviceId);
69             }
70         } catch (SpotifyException e) {
71             updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.NONE, e.getMessage());
72         }
73     }
74
75     @Override
76     public void initialize() {
77         final SpotifyBridgeHandler bridgeHandler = (SpotifyBridgeHandler) getBridge().getHandler();
78         spotifyApi = bridgeHandler.getSpotifyApi();
79
80         if (spotifyApi == null) {
81             updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, String.format(
82                     "Missing configuration from the Spotify Bridge (UID:%s). Fix configuration or report if this problem remains.",
83                     getBridge().getBridgeUID()));
84             return;
85         }
86         deviceName = (String) getConfig().get(PROPERTY_SPOTIFY_DEVICE_NAME);
87         if (deviceName == null || deviceName.isEmpty()) {
88             updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR,
89                     "The deviceName property is not set or empty. If you have an older thing please recreate this thing.");
90             deviceName = "";
91         } else {
92             commandHandler = new SpotifyHandleCommands(spotifyApi);
93             updateStatus(ThingStatus.UNKNOWN);
94         }
95     }
96
97     @Override
98     public void bridgeStatusChanged(ThingStatusInfo bridgeStatusInfo) {
99         if (bridgeStatusInfo.getStatus() != ThingStatus.ONLINE) {
100             updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.BRIDGE_OFFLINE, "Spotify Bridge Offline");
101             logger.debug("SpotifyDevice {}: SpotifyBridge is not online: {}", getThing().getThingTypeUID(),
102                     bridgeStatusInfo.getStatus());
103         }
104     }
105
106     /**
107      * Updates the status if the given device matches with this handler.
108      *
109      * @param device device with status information
110      * @param playing true if the current active device is playing
111      * @return returns true if given device matches with this handler
112      */
113     public boolean updateDeviceStatus(Device device, boolean playing) {
114         if (deviceName.equals(device.getName())) {
115             deviceId = device.getId() == null ? "" : device.getId();
116             logger.debug("Updating status of Thing: {} Device [ {} {}, {} ]", thing.getUID(), deviceId,
117                     device.getName(), device.getType());
118             final boolean online = setOnlineStatus(device.isRestricted());
119             updateChannelState(CHANNEL_DEVICEID, new StringType(deviceId));
120             updateChannelState(CHANNEL_DEVICENAME, new StringType(device.getName()));
121             updateChannelState(CHANNEL_DEVICETYPE, new StringType(device.getType()));
122             updateChannelState(CHANNEL_DEVICEVOLUME,
123                     device.getVolumePercent() == null ? UnDefType.UNDEF : new PercentType(device.getVolumePercent()));
124             active = device.isActive();
125             updateChannelState(CHANNEL_DEVICEACTIVE, OnOffType.from(active));
126             updateChannelState(CHANNEL_DEVICEPLAYER,
127                     online && active && playing ? PlayPauseType.PLAY : PlayPauseType.PAUSE);
128             return true;
129         } else {
130             return false;
131         }
132     }
133
134     /**
135      * Updates the device as showing status is gone and reset all device status to default.
136      */
137     public void setStatusGone() {
138         if (getThing().getStatus() != ThingStatus.OFFLINE
139                 && getThing().getStatusInfo().getStatusDetail() != ThingStatusDetail.GONE) {
140             logger.debug("Device is gone: {}", thing.getUID());
141             getThing().setStatusInfo(new ThingStatusInfo(ThingStatus.OFFLINE, ThingStatusDetail.GONE,
142                     "Device not available on Spotify"));
143             updateChannelState(CHANNEL_DEVICERESTRICTED, OnOffType.ON);
144             updateChannelState(CHANNEL_DEVICEACTIVE, OnOffType.OFF);
145             updateChannelState(CHANNEL_DEVICEPLAYER, PlayPauseType.PAUSE);
146         }
147     }
148
149     /**
150      * Sets the device online status. If the device is restricted it will be set offline.
151      *
152      * @param restricted true if device is restricted (no access)
153      * @return true if device is online
154      */
155     private boolean setOnlineStatus(boolean restricted) {
156         updateChannelState(CHANNEL_DEVICERESTRICTED, OnOffType.from(restricted));
157         final boolean statusUnknown = thing.getStatus() == ThingStatus.UNKNOWN;
158
159         if (restricted) {
160             // Only change status if device is currently online
161             if (thing.getStatus() == ThingStatus.ONLINE || statusUnknown) {
162                 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.NONE,
163                         "Restricted. No Web API commands will be accepted by this device.");
164             }
165             return false;
166         } else if (statusUnknown || thing.getStatus() == ThingStatus.OFFLINE) {
167             updateStatus(ThingStatus.ONLINE);
168         }
169         return true;
170     }
171
172     /**
173      * Convenience method to update the channel state but only if the channel is linked.
174      *
175      * @param channelId id of the channel to update
176      * @param state State to set on the channel
177      */
178     private void updateChannelState(String channelId, State state) {
179         final Channel channel = thing.getChannel(channelId);
180
181         if (channel != null && isLinked(channel.getUID())) {
182             updateState(channel.getUID(), state);
183         }
184     }
185 }