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.hue.internal.factory;
15 import static org.openhab.binding.hue.internal.HueBindingConstants.*;
18 import java.util.stream.Collectors;
19 import java.util.stream.Stream;
21 import org.eclipse.jdt.annotation.NonNullByDefault;
22 import org.eclipse.jdt.annotation.Nullable;
23 import org.openhab.binding.hue.internal.HueBindingConstants;
24 import org.openhab.binding.hue.internal.handler.Clip2BridgeHandler;
25 import org.openhab.binding.hue.internal.handler.Clip2StateDescriptionProvider;
26 import org.openhab.binding.hue.internal.handler.Clip2ThingHandler;
27 import org.openhab.binding.hue.internal.handler.HueBridgeHandler;
28 import org.openhab.binding.hue.internal.handler.HueGroupHandler;
29 import org.openhab.binding.hue.internal.handler.HueLightHandler;
30 import org.openhab.binding.hue.internal.handler.HueStateDescriptionProvider;
31 import org.openhab.binding.hue.internal.handler.sensors.ClipHandler;
32 import org.openhab.binding.hue.internal.handler.sensors.DimmerSwitchHandler;
33 import org.openhab.binding.hue.internal.handler.sensors.GeofencePresenceHandler;
34 import org.openhab.binding.hue.internal.handler.sensors.LightLevelHandler;
35 import org.openhab.binding.hue.internal.handler.sensors.PresenceHandler;
36 import org.openhab.binding.hue.internal.handler.sensors.TapSwitchHandler;
37 import org.openhab.binding.hue.internal.handler.sensors.TemperatureHandler;
38 import org.openhab.core.config.core.Configuration;
39 import org.openhab.core.i18n.LocaleProvider;
40 import org.openhab.core.i18n.TranslationProvider;
41 import org.openhab.core.io.net.http.HttpClientFactory;
42 import org.openhab.core.thing.Bridge;
43 import org.openhab.core.thing.Thing;
44 import org.openhab.core.thing.ThingRegistry;
45 import org.openhab.core.thing.ThingTypeUID;
46 import org.openhab.core.thing.ThingUID;
47 import org.openhab.core.thing.binding.BaseThingHandlerFactory;
48 import org.openhab.core.thing.binding.ThingHandler;
49 import org.openhab.core.thing.binding.ThingHandlerFactory;
50 import org.openhab.core.thing.link.ItemChannelLinkRegistry;
51 import org.osgi.service.component.annotations.Activate;
52 import org.osgi.service.component.annotations.Component;
53 import org.osgi.service.component.annotations.Reference;
56 * The factory for all varieties of Hue thing handlers.
58 * @author Dennis Nobel - Initial contribution of hue binding
59 * @author Kai Kreuzer - added supportsThingType method
60 * @author Andre Fuechsel - implemented to use one discovery service per bridge
61 * @author Samuel Leisering - Added support for sensor API
62 * @author Christoph Weitkamp - Added support for sensor API
63 * @author Laurent Garnier - Added support for groups
64 * @author Andrew Fiddian-Green - Added support for CLIP 2 things
67 @Component(service = ThingHandlerFactory.class, configurationPid = "binding.hue")
68 public class HueThingHandlerFactory extends BaseThingHandlerFactory {
70 public static final Set<ThingTypeUID> SUPPORTED_THING_TYPES = Stream
71 .of(Clip2BridgeHandler.SUPPORTED_THING_TYPES.stream(), Clip2ThingHandler.SUPPORTED_THING_TYPES.stream(),
72 HueBridgeHandler.SUPPORTED_THING_TYPES.stream(), HueLightHandler.SUPPORTED_THING_TYPES.stream(),
73 DimmerSwitchHandler.SUPPORTED_THING_TYPES.stream(), TapSwitchHandler.SUPPORTED_THING_TYPES.stream(),
74 PresenceHandler.SUPPORTED_THING_TYPES.stream(),
75 GeofencePresenceHandler.SUPPORTED_THING_TYPES.stream(),
76 TemperatureHandler.SUPPORTED_THING_TYPES.stream(), LightLevelHandler.SUPPORTED_THING_TYPES.stream(),
77 ClipHandler.SUPPORTED_THING_TYPES.stream(), HueGroupHandler.SUPPORTED_THING_TYPES.stream())
78 .flatMap(i -> i).collect(Collectors.toUnmodifiableSet());
80 private final HttpClientFactory httpClientFactory;
81 private final HueStateDescriptionProvider stateDescriptionProvider;
82 private final Clip2StateDescriptionProvider clip2StateDescriptionProvider;
83 private final TranslationProvider i18nProvider;
84 private final LocaleProvider localeProvider;
85 private final ThingRegistry thingRegistry;
86 private final ItemChannelLinkRegistry itemChannelLinkRegistry;
89 public HueThingHandlerFactory(final @Reference HttpClientFactory httpClientFactory,
90 final @Reference HueStateDescriptionProvider stateDescriptionProvider,
91 final @Reference Clip2StateDescriptionProvider clip2StateDescriptionProvider,
92 final @Reference TranslationProvider i18nProvider, final @Reference LocaleProvider localeProvider,
93 final @Reference ThingRegistry thingRegistry,
94 final @Reference ItemChannelLinkRegistry itemChannelLinkRegistry) {
95 this.httpClientFactory = httpClientFactory;
96 this.stateDescriptionProvider = stateDescriptionProvider;
97 this.clip2StateDescriptionProvider = clip2StateDescriptionProvider;
98 this.i18nProvider = i18nProvider;
99 this.localeProvider = localeProvider;
100 this.thingRegistry = thingRegistry;
101 this.itemChannelLinkRegistry = itemChannelLinkRegistry;
105 public @Nullable Thing createThing(ThingTypeUID thingTypeUID, Configuration configuration,
106 @Nullable ThingUID thingUID, @Nullable ThingUID bridgeUID) {
107 if (HueBindingConstants.THING_TYPE_BRIDGE_API2.equals(thingTypeUID)) {
108 return super.createThing(thingTypeUID, configuration, thingUID, null);
109 } else if (Clip2ThingHandler.SUPPORTED_THING_TYPES.contains(thingTypeUID)) {
110 ThingUID clip2ThingUID = getClip2ThingUID(thingTypeUID, thingUID, configuration, bridgeUID);
111 return super.createThing(thingTypeUID, configuration, clip2ThingUID, bridgeUID);
112 } else if (HueBridgeHandler.SUPPORTED_THING_TYPES.contains(thingTypeUID)) {
113 return super.createThing(thingTypeUID, configuration, thingUID, null);
114 } else if (HueLightHandler.SUPPORTED_THING_TYPES.contains(thingTypeUID)) {
115 ThingUID hueLightUID = getLightUID(thingTypeUID, thingUID, configuration, bridgeUID);
116 return super.createThing(thingTypeUID, configuration, hueLightUID, bridgeUID);
117 } else if (DimmerSwitchHandler.SUPPORTED_THING_TYPES.contains(thingTypeUID)
118 || TapSwitchHandler.SUPPORTED_THING_TYPES.contains(thingTypeUID)
119 || PresenceHandler.SUPPORTED_THING_TYPES.contains(thingTypeUID)
120 || GeofencePresenceHandler.SUPPORTED_THING_TYPES.contains(thingTypeUID)
121 || TemperatureHandler.SUPPORTED_THING_TYPES.contains(thingTypeUID)
122 || LightLevelHandler.SUPPORTED_THING_TYPES.contains(thingTypeUID)
123 || ClipHandler.SUPPORTED_THING_TYPES.contains(thingTypeUID)) {
124 ThingUID hueSensorUID = getSensorUID(thingTypeUID, thingUID, configuration, bridgeUID);
125 return super.createThing(thingTypeUID, configuration, hueSensorUID, bridgeUID);
126 } else if (HueGroupHandler.SUPPORTED_THING_TYPES.contains(thingTypeUID)) {
127 ThingUID hueGroupUID = getGroupUID(thingTypeUID, thingUID, configuration, bridgeUID);
128 return super.createThing(thingTypeUID, configuration, hueGroupUID, bridgeUID);
131 throw new IllegalArgumentException("The thing type " + thingTypeUID + " is not supported by the Hue binding.");
135 public boolean supportsThingType(ThingTypeUID thingTypeUID) {
136 return SUPPORTED_THING_TYPES.contains(thingTypeUID);
139 private ThingUID getClip2ThingUID(ThingTypeUID thingTypeUID, @Nullable ThingUID thingUID,
140 Configuration configuration, @Nullable ThingUID bridgeUID) {
141 return thingUID != null ? thingUID
142 : getThingUID(thingTypeUID, configuration.get(PROPERTY_RESOURCE_ID).toString(), bridgeUID);
145 private ThingUID getLightUID(ThingTypeUID thingTypeUID, @Nullable ThingUID thingUID, Configuration configuration,
146 @Nullable ThingUID bridgeUID) {
147 if (thingUID != null) {
150 return getThingUID(thingTypeUID, configuration.get(LIGHT_ID).toString(), bridgeUID);
154 private ThingUID getSensorUID(ThingTypeUID thingTypeUID, @Nullable ThingUID thingUID, Configuration configuration,
155 @Nullable ThingUID bridgeUID) {
156 if (thingUID != null) {
159 return getThingUID(thingTypeUID, configuration.get(SENSOR_ID).toString(), bridgeUID);
163 private ThingUID getGroupUID(ThingTypeUID thingTypeUID, @Nullable ThingUID thingUID, Configuration configuration,
164 @Nullable ThingUID bridgeUID) {
165 if (thingUID != null) {
168 return getThingUID(thingTypeUID, configuration.get(GROUP_ID).toString(), bridgeUID);
172 private ThingUID getThingUID(ThingTypeUID thingTypeUID, String id, @Nullable ThingUID bridgeUID) {
173 if (bridgeUID != null) {
174 return new ThingUID(thingTypeUID, id, bridgeUID.getId());
176 return new ThingUID(thingTypeUID, id);
181 protected @Nullable ThingHandler createHandler(Thing thing) {
182 ThingTypeUID thingTypeUID = thing.getThingTypeUID();
183 if (HueBindingConstants.THING_TYPE_BRIDGE_API2.equals(thingTypeUID)) {
184 return new Clip2BridgeHandler((Bridge) thing, httpClientFactory, thingRegistry, localeProvider,
186 } else if (Clip2ThingHandler.SUPPORTED_THING_TYPES.contains(thingTypeUID)) {
187 return new Clip2ThingHandler(thing, clip2StateDescriptionProvider, thingRegistry, itemChannelLinkRegistry);
188 } else if (HueBridgeHandler.SUPPORTED_THING_TYPES.contains(thingTypeUID)) {
189 return new HueBridgeHandler((Bridge) thing, httpClientFactory.getCommonHttpClient(),
190 stateDescriptionProvider, i18nProvider, localeProvider);
191 } else if (HueLightHandler.SUPPORTED_THING_TYPES.contains(thingTypeUID)) {
192 return new HueLightHandler(thing, stateDescriptionProvider);
193 } else if (DimmerSwitchHandler.SUPPORTED_THING_TYPES.contains(thingTypeUID)) {
194 return new DimmerSwitchHandler(thing);
195 } else if (TapSwitchHandler.SUPPORTED_THING_TYPES.contains(thingTypeUID)) {
196 return new TapSwitchHandler(thing);
197 } else if (PresenceHandler.SUPPORTED_THING_TYPES.contains(thingTypeUID)) {
198 return new PresenceHandler(thing);
199 } else if (GeofencePresenceHandler.SUPPORTED_THING_TYPES.contains(thingTypeUID)) {
200 return new GeofencePresenceHandler(thing);
201 } else if (TemperatureHandler.SUPPORTED_THING_TYPES.contains(thingTypeUID)) {
202 return new TemperatureHandler(thing);
203 } else if (LightLevelHandler.SUPPORTED_THING_TYPES.contains(thingTypeUID)) {
204 return new LightLevelHandler(thing);
205 } else if (ClipHandler.SUPPORTED_THING_TYPES.contains(thingTypeUID)) {
206 return new ClipHandler(thing);
207 } else if (HueGroupHandler.SUPPORTED_THING_TYPES.contains(thingTypeUID)) {
208 return new HueGroupHandler(thing, stateDescriptionProvider);