2 * Copyright (c) 2010-2022 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.handler;
15 import static org.openhab.binding.spotify.internal.SpotifyBindingConstants.*;
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;
39 * The {@link SpotifyDeviceHandler} is responsible for handling commands, which are sent to one of the channels.
41 * @author Andreas Stenlund - Initial contribution
42 * @author Hilbrand Bouwkamp - Code cleanup, moved channel state to this class, generic stability.
45 public class SpotifyDeviceHandler extends BaseThingHandler {
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 = "";
53 private boolean active;
58 * @param thing Thing representing this device.
60 public SpotifyDeviceHandler(Thing thing) {
65 public void handleCommand(ChannelUID channelUID, Command command) {
67 if (commandHandler != null && !deviceId.isEmpty()) {
68 commandHandler.handleCommand(channelUID, command, active, deviceId);
70 } catch (SpotifyException e) {
71 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.NONE, e.getMessage());
76 public void initialize() {
77 final SpotifyBridgeHandler bridgeHandler = (SpotifyBridgeHandler) getBridge().getHandler();
78 spotifyApi = bridgeHandler.getSpotifyApi();
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()));
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.");
92 commandHandler = new SpotifyHandleCommands(spotifyApi);
93 updateStatus(ThingStatus.UNKNOWN);
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());
107 * Updates the status if the given device matches with this handler.
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
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);
135 * Updates the device as showing status is gone and reset all device status to default.
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);
150 * Sets the device online status. If the device is restricted it will be set offline.
152 * @param restricted true if device is restricted (no access)
153 * @return true if device is online
155 private boolean setOnlineStatus(boolean restricted) {
156 updateChannelState(CHANNEL_DEVICERESTRICTED, OnOffType.from(restricted));
157 final boolean statusUnknown = thing.getStatus() == ThingStatus.UNKNOWN;
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.");
166 } else if (statusUnknown || thing.getStatus() == ThingStatus.OFFLINE) {
167 updateStatus(ThingStatus.ONLINE);
173 * Convenience method to update the channel state but only if the channel is linked.
175 * @param channelId id of the channel to update
176 * @param state State to set on the channel
178 private void updateChannelState(String channelId, State state) {
179 final Channel channel = thing.getChannel(channelId);
181 if (channel != null && isLinked(channel.getUID())) {
182 updateState(channel.getUID(), state);