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.eclipse.jetty.client.HttpClient;
24 import org.openhab.binding.hue.internal.handler.HueBridgeHandler;
25 import org.openhab.binding.hue.internal.handler.HueGroupHandler;
26 import org.openhab.binding.hue.internal.handler.HueLightHandler;
27 import org.openhab.binding.hue.internal.handler.HueStateDescriptionProvider;
28 import org.openhab.binding.hue.internal.handler.sensors.ClipHandler;
29 import org.openhab.binding.hue.internal.handler.sensors.DimmerSwitchHandler;
30 import org.openhab.binding.hue.internal.handler.sensors.GeofencePresenceHandler;
31 import org.openhab.binding.hue.internal.handler.sensors.LightLevelHandler;
32 import org.openhab.binding.hue.internal.handler.sensors.PresenceHandler;
33 import org.openhab.binding.hue.internal.handler.sensors.TapSwitchHandler;
34 import org.openhab.binding.hue.internal.handler.sensors.TemperatureHandler;
35 import org.openhab.core.config.core.Configuration;
36 import org.openhab.core.i18n.LocaleProvider;
37 import org.openhab.core.i18n.TranslationProvider;
38 import org.openhab.core.io.net.http.HttpClientFactory;
39 import org.openhab.core.thing.Bridge;
40 import org.openhab.core.thing.Thing;
41 import org.openhab.core.thing.ThingTypeUID;
42 import org.openhab.core.thing.ThingUID;
43 import org.openhab.core.thing.binding.BaseThingHandlerFactory;
44 import org.openhab.core.thing.binding.ThingHandler;
45 import org.openhab.core.thing.binding.ThingHandlerFactory;
46 import org.osgi.service.component.annotations.Activate;
47 import org.osgi.service.component.annotations.Component;
48 import org.osgi.service.component.annotations.Reference;
51 * {@link HueThingHandlerFactory} is a factory for {@link HueBridgeHandler}s.
53 * @author Dennis Nobel - Initial contribution of hue binding
54 * @author Kai Kreuzer - added supportsThingType method
55 * @author Andre Fuechsel - implemented to use one discovery service per bridge
56 * @author Samuel Leisering - Added support for sensor API
57 * @author Christoph Weitkamp - Added support for sensor API
58 * @author Laurent Garnier - Added support for groups
61 @Component(service = ThingHandlerFactory.class, configurationPid = "binding.hue")
62 public class HueThingHandlerFactory extends BaseThingHandlerFactory {
64 public static final Set<ThingTypeUID> SUPPORTED_THING_TYPES = Stream
65 .of(HueBridgeHandler.SUPPORTED_THING_TYPES.stream(), HueLightHandler.SUPPORTED_THING_TYPES.stream(),
66 DimmerSwitchHandler.SUPPORTED_THING_TYPES.stream(), TapSwitchHandler.SUPPORTED_THING_TYPES.stream(),
67 PresenceHandler.SUPPORTED_THING_TYPES.stream(),
68 GeofencePresenceHandler.SUPPORTED_THING_TYPES.stream(),
69 TemperatureHandler.SUPPORTED_THING_TYPES.stream(), LightLevelHandler.SUPPORTED_THING_TYPES.stream(),
70 ClipHandler.SUPPORTED_THING_TYPES.stream(), HueGroupHandler.SUPPORTED_THING_TYPES.stream())
71 .flatMap(i -> i).collect(Collectors.toUnmodifiableSet());
73 private final HttpClient httpClient;
74 private final HueStateDescriptionProvider stateDescriptionProvider;
75 private final TranslationProvider i18nProvider;
76 private final LocaleProvider localeProvider;
79 public HueThingHandlerFactory(final @Reference HttpClientFactory httpClientFactory,
80 final @Reference HueStateDescriptionProvider stateDescriptionProvider,
81 final @Reference TranslationProvider i18nProvider, final @Reference LocaleProvider localeProvider) {
82 this.httpClient = httpClientFactory.getCommonHttpClient();
83 this.stateDescriptionProvider = stateDescriptionProvider;
84 this.i18nProvider = i18nProvider;
85 this.localeProvider = localeProvider;
89 public @Nullable Thing createThing(ThingTypeUID thingTypeUID, Configuration configuration,
90 @Nullable ThingUID thingUID, @Nullable ThingUID bridgeUID) {
91 if (HueBridgeHandler.SUPPORTED_THING_TYPES.contains(thingTypeUID)) {
92 return super.createThing(thingTypeUID, configuration, thingUID, null);
93 } else if (HueLightHandler.SUPPORTED_THING_TYPES.contains(thingTypeUID)) {
94 ThingUID hueLightUID = getLightUID(thingTypeUID, thingUID, configuration, bridgeUID);
95 return super.createThing(thingTypeUID, configuration, hueLightUID, bridgeUID);
96 } else if (DimmerSwitchHandler.SUPPORTED_THING_TYPES.contains(thingTypeUID)
97 || TapSwitchHandler.SUPPORTED_THING_TYPES.contains(thingTypeUID)
98 || PresenceHandler.SUPPORTED_THING_TYPES.contains(thingTypeUID)
99 || GeofencePresenceHandler.SUPPORTED_THING_TYPES.contains(thingTypeUID)
100 || TemperatureHandler.SUPPORTED_THING_TYPES.contains(thingTypeUID)
101 || LightLevelHandler.SUPPORTED_THING_TYPES.contains(thingTypeUID)
102 || ClipHandler.SUPPORTED_THING_TYPES.contains(thingTypeUID)) {
103 ThingUID hueSensorUID = getSensorUID(thingTypeUID, thingUID, configuration, bridgeUID);
104 return super.createThing(thingTypeUID, configuration, hueSensorUID, bridgeUID);
105 } else if (HueGroupHandler.SUPPORTED_THING_TYPES.contains(thingTypeUID)) {
106 ThingUID hueGroupUID = getGroupUID(thingTypeUID, thingUID, configuration, bridgeUID);
107 return super.createThing(thingTypeUID, configuration, hueGroupUID, bridgeUID);
110 throw new IllegalArgumentException("The thing type " + thingTypeUID + " is not supported by the Hue binding.");
114 public boolean supportsThingType(ThingTypeUID thingTypeUID) {
115 return SUPPORTED_THING_TYPES.contains(thingTypeUID);
118 private ThingUID getLightUID(ThingTypeUID thingTypeUID, @Nullable ThingUID thingUID, Configuration configuration,
119 @Nullable ThingUID bridgeUID) {
120 if (thingUID != null) {
123 return getThingUID(thingTypeUID, configuration.get(LIGHT_ID).toString(), bridgeUID);
127 private ThingUID getSensorUID(ThingTypeUID thingTypeUID, @Nullable ThingUID thingUID, Configuration configuration,
128 @Nullable ThingUID bridgeUID) {
129 if (thingUID != null) {
132 return getThingUID(thingTypeUID, configuration.get(SENSOR_ID).toString(), bridgeUID);
136 private ThingUID getGroupUID(ThingTypeUID thingTypeUID, @Nullable ThingUID thingUID, Configuration configuration,
137 @Nullable ThingUID bridgeUID) {
138 if (thingUID != null) {
141 return getThingUID(thingTypeUID, configuration.get(GROUP_ID).toString(), bridgeUID);
145 private ThingUID getThingUID(ThingTypeUID thingTypeUID, String id, @Nullable ThingUID bridgeUID) {
146 if (bridgeUID != null) {
147 return new ThingUID(thingTypeUID, id, bridgeUID.getId());
149 return new ThingUID(thingTypeUID, id);
154 protected @Nullable ThingHandler createHandler(Thing thing) {
155 if (HueBridgeHandler.SUPPORTED_THING_TYPES.contains(thing.getThingTypeUID())) {
156 return new HueBridgeHandler((Bridge) thing, httpClient, stateDescriptionProvider, i18nProvider,
158 } else if (HueLightHandler.SUPPORTED_THING_TYPES.contains(thing.getThingTypeUID())) {
159 return new HueLightHandler(thing, stateDescriptionProvider);
160 } else if (DimmerSwitchHandler.SUPPORTED_THING_TYPES.contains(thing.getThingTypeUID())) {
161 return new DimmerSwitchHandler(thing);
162 } else if (TapSwitchHandler.SUPPORTED_THING_TYPES.contains(thing.getThingTypeUID())) {
163 return new TapSwitchHandler(thing);
164 } else if (PresenceHandler.SUPPORTED_THING_TYPES.contains(thing.getThingTypeUID())) {
165 return new PresenceHandler(thing);
166 } else if (GeofencePresenceHandler.SUPPORTED_THING_TYPES.contains(thing.getThingTypeUID())) {
167 return new GeofencePresenceHandler(thing);
168 } else if (TemperatureHandler.SUPPORTED_THING_TYPES.contains(thing.getThingTypeUID())) {
169 return new TemperatureHandler(thing);
170 } else if (LightLevelHandler.SUPPORTED_THING_TYPES.contains(thing.getThingTypeUID())) {
171 return new LightLevelHandler(thing);
172 } else if (ClipHandler.SUPPORTED_THING_TYPES.contains(thing.getThingTypeUID())) {
173 return new ClipHandler(thing);
174 } else if (HueGroupHandler.SUPPORTED_THING_TYPES.contains(thing.getThingTypeUID())) {
175 return new HueGroupHandler(thing, stateDescriptionProvider);