]> git.basschouten.com Git - openhab-addons.git/blob
a3d450ecfd9165d17a59164bf7b7987464e4eaaf
[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;
14
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;
32
33 /**
34  * The {@link SpotifyHandlerFactory} is responsible for creating things and thing
35  * handlers.
36  *
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
40  */
41 @Component(service = ThingHandlerFactory.class, configurationPid = "binding.spotify")
42 @NonNullByDefault
43 public class SpotifyHandlerFactory extends BaseThingHandlerFactory {
44
45     private final OAuthFactory oAuthFactory;
46     private final HttpClient httpClient;
47     private final SpotifyAuthService authService;
48     private final SpotifyDynamicStateDescriptionProvider spotifyDynamicStateDescriptionProvider;
49
50     @Activate
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;
58     }
59
60     @Override
61     public boolean supportsThingType(ThingTypeUID thingTypeUID) {
62         return SpotifyBindingConstants.THING_TYPE_PLAYER.equals(thingTypeUID)
63                 || SpotifyBindingConstants.THING_TYPE_DEVICE.equals(thingTypeUID);
64     }
65
66     @Override
67     protected @Nullable ThingHandler createHandler(Thing thing) {
68         final ThingTypeUID thingTypeUID = thing.getThingTypeUID();
69
70         if (SpotifyBindingConstants.THING_TYPE_PLAYER.equals(thingTypeUID)) {
71             final SpotifyBridgeHandler handler = new SpotifyBridgeHandler((Bridge) thing, oAuthFactory, httpClient,
72                     spotifyDynamicStateDescriptionProvider);
73             authService.addSpotifyAccountHandler(handler);
74             return handler;
75         }
76         if (SpotifyBindingConstants.THING_TYPE_DEVICE.equals(thingTypeUID)) {
77             return new SpotifyDeviceHandler(thing);
78         }
79         return null;
80     }
81
82     @Override
83     protected synchronized void removeHandler(ThingHandler thingHandler) {
84         if (thingHandler instanceof SpotifyBridgeHandler bridgeHandler) {
85             authService.removeSpotifyAccountHandler(bridgeHandler);
86         }
87     }
88 }