2 * Copyright (c) 2010-2023 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;
15 import org.eclipse.jdt.annotation.NonNullByDefault;
16 import org.eclipse.jdt.annotation.Nullable;
17 import org.eclipse.jetty.client.HttpClient;
18 import org.openhab.binding.spotify.internal.handler.SpotifyBridgeHandler;
19 import org.openhab.binding.spotify.internal.handler.SpotifyDeviceHandler;
20 import org.openhab.binding.spotify.internal.handler.SpotifyDynamicStateDescriptionProvider;
21 import org.openhab.core.auth.client.oauth2.OAuthFactory;
22 import org.openhab.core.io.net.http.HttpClientFactory;
23 import org.openhab.core.thing.Bridge;
24 import org.openhab.core.thing.Thing;
25 import org.openhab.core.thing.ThingTypeUID;
26 import org.openhab.core.thing.binding.BaseThingHandlerFactory;
27 import org.openhab.core.thing.binding.ThingHandler;
28 import org.openhab.core.thing.binding.ThingHandlerFactory;
29 import org.osgi.service.component.annotations.Activate;
30 import org.osgi.service.component.annotations.Component;
31 import org.osgi.service.component.annotations.Reference;
34 * The {@link SpotifyHandlerFactory} is responsible for creating things and thing
37 * @author Andreas Stenlund - Initial contribution
38 * @author Matthew Bowman - Initial contribution
39 * @author Hilbrand Bouwkamp - Added registration of discovery service to binding to this class
41 @Component(service = ThingHandlerFactory.class, configurationPid = "binding.spotify")
43 public class SpotifyHandlerFactory extends BaseThingHandlerFactory {
45 private final OAuthFactory oAuthFactory;
46 private final HttpClient httpClient;
47 private final SpotifyAuthService authService;
48 private final SpotifyDynamicStateDescriptionProvider spotifyDynamicStateDescriptionProvider;
51 public SpotifyHandlerFactory(@Reference OAuthFactory oAuthFactory,
52 @Reference final HttpClientFactory httpClientFactory, @Reference SpotifyAuthService authService,
53 @Reference SpotifyDynamicStateDescriptionProvider spotifyDynamicStateDescriptionProvider) {
54 this.oAuthFactory = oAuthFactory;
55 this.httpClient = httpClientFactory.getCommonHttpClient();
56 this.authService = authService;
57 this.spotifyDynamicStateDescriptionProvider = spotifyDynamicStateDescriptionProvider;
61 public boolean supportsThingType(ThingTypeUID thingTypeUID) {
62 return SpotifyBindingConstants.THING_TYPE_PLAYER.equals(thingTypeUID)
63 || SpotifyBindingConstants.THING_TYPE_DEVICE.equals(thingTypeUID);
67 protected @Nullable ThingHandler createHandler(Thing thing) {
68 final ThingTypeUID thingTypeUID = thing.getThingTypeUID();
70 if (SpotifyBindingConstants.THING_TYPE_PLAYER.equals(thingTypeUID)) {
71 final SpotifyBridgeHandler handler = new SpotifyBridgeHandler((Bridge) thing, oAuthFactory, httpClient,
72 spotifyDynamicStateDescriptionProvider);
73 authService.addSpotifyAccountHandler(handler);
76 if (SpotifyBindingConstants.THING_TYPE_DEVICE.equals(thingTypeUID)) {
77 return new SpotifyDeviceHandler(thing);
83 protected synchronized void removeHandler(ThingHandler thingHandler) {
84 if (thingHandler instanceof SpotifyBridgeHandler bridgeHandler) {
85 authService.removeSpotifyAccountHandler(bridgeHandler);