2 * Copyright (c) 2010-2021 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;
15 import static org.openhab.binding.hue.internal.HueBindingConstants.*;
17 import java.util.Collections;
19 import java.util.stream.Collectors;
20 import java.util.stream.Stream;
22 import org.eclipse.jdt.annotation.NonNullByDefault;
23 import org.eclipse.jdt.annotation.Nullable;
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.HueStateDescriptionOptionProvider;
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.thing.Bridge;
37 import org.openhab.core.thing.Thing;
38 import org.openhab.core.thing.ThingTypeUID;
39 import org.openhab.core.thing.ThingUID;
40 import org.openhab.core.thing.binding.BaseThingHandlerFactory;
41 import org.openhab.core.thing.binding.ThingHandler;
42 import org.openhab.core.thing.binding.ThingHandlerFactory;
43 import org.osgi.service.component.annotations.Activate;
44 import org.osgi.service.component.annotations.Component;
45 import org.osgi.service.component.annotations.Reference;
48 * {@link HueThingHandlerFactory} is a factory for {@link HueBridgeHandler}s.
50 * @author Dennis Nobel - Initial contribution of hue binding
51 * @author Kai Kreuzer - added supportsThingType method
52 * @author Andre Fuechsel - implemented to use one discovery service per bridge
53 * @author Samuel Leisering - Added support for sensor API
54 * @author Christoph Weitkamp - Added support for sensor API
55 * @author Laurent Garnier - Added support for groups
58 @Component(service = ThingHandlerFactory.class, configurationPid = "binding.hue")
59 public class HueThingHandlerFactory extends BaseThingHandlerFactory {
61 public static final Set<ThingTypeUID> SUPPORTED_THING_TYPES = Collections.unmodifiableSet(Stream
62 .of(HueBridgeHandler.SUPPORTED_THING_TYPES.stream(), HueLightHandler.SUPPORTED_THING_TYPES.stream(),
63 DimmerSwitchHandler.SUPPORTED_THING_TYPES.stream(), TapSwitchHandler.SUPPORTED_THING_TYPES.stream(),
64 PresenceHandler.SUPPORTED_THING_TYPES.stream(),
65 GeofencePresenceHandler.SUPPORTED_THING_TYPES.stream(),
66 TemperatureHandler.SUPPORTED_THING_TYPES.stream(), LightLevelHandler.SUPPORTED_THING_TYPES.stream(),
67 ClipHandler.SUPPORTED_THING_TYPES.stream(), HueGroupHandler.SUPPORTED_THING_TYPES.stream())
68 .flatMap(i -> i).collect(Collectors.toSet()));
70 private final HueStateDescriptionOptionProvider stateOptionProvider;
73 public HueThingHandlerFactory(final @Reference HueStateDescriptionOptionProvider stateOptionProvider) {
74 this.stateOptionProvider = stateOptionProvider;
78 public @Nullable Thing createThing(ThingTypeUID thingTypeUID, Configuration configuration,
79 @Nullable ThingUID thingUID, @Nullable ThingUID bridgeUID) {
80 if (HueBridgeHandler.SUPPORTED_THING_TYPES.contains(thingTypeUID)) {
81 return super.createThing(thingTypeUID, configuration, thingUID, null);
82 } else if (HueLightHandler.SUPPORTED_THING_TYPES.contains(thingTypeUID)) {
83 ThingUID hueLightUID = getLightUID(thingTypeUID, thingUID, configuration, bridgeUID);
84 return super.createThing(thingTypeUID, configuration, hueLightUID, bridgeUID);
85 } else if (DimmerSwitchHandler.SUPPORTED_THING_TYPES.contains(thingTypeUID)
86 || TapSwitchHandler.SUPPORTED_THING_TYPES.contains(thingTypeUID)
87 || PresenceHandler.SUPPORTED_THING_TYPES.contains(thingTypeUID)
88 || GeofencePresenceHandler.SUPPORTED_THING_TYPES.contains(thingTypeUID)
89 || TemperatureHandler.SUPPORTED_THING_TYPES.contains(thingTypeUID)
90 || LightLevelHandler.SUPPORTED_THING_TYPES.contains(thingTypeUID)
91 || ClipHandler.SUPPORTED_THING_TYPES.contains(thingTypeUID)) {
92 ThingUID hueSensorUID = getSensorUID(thingTypeUID, thingUID, configuration, bridgeUID);
93 return super.createThing(thingTypeUID, configuration, hueSensorUID, bridgeUID);
94 } else if (HueGroupHandler.SUPPORTED_THING_TYPES.contains(thingTypeUID)) {
95 ThingUID hueGroupUID = getGroupUID(thingTypeUID, thingUID, configuration, bridgeUID);
96 return super.createThing(thingTypeUID, configuration, hueGroupUID, bridgeUID);
99 throw new IllegalArgumentException("The thing type " + thingTypeUID + " is not supported by the hue binding.");
103 public boolean supportsThingType(ThingTypeUID thingTypeUID) {
104 return SUPPORTED_THING_TYPES.contains(thingTypeUID);
107 private ThingUID getLightUID(ThingTypeUID thingTypeUID, @Nullable ThingUID thingUID, Configuration configuration,
108 @Nullable ThingUID bridgeUID) {
109 if (thingUID != null) {
112 return getThingUID(thingTypeUID, configuration.get(LIGHT_ID).toString(), bridgeUID);
116 private ThingUID getSensorUID(ThingTypeUID thingTypeUID, @Nullable ThingUID thingUID, Configuration configuration,
117 @Nullable ThingUID bridgeUID) {
118 if (thingUID != null) {
121 return getThingUID(thingTypeUID, configuration.get(SENSOR_ID).toString(), bridgeUID);
125 private ThingUID getGroupUID(ThingTypeUID thingTypeUID, @Nullable ThingUID thingUID, Configuration configuration,
126 @Nullable ThingUID bridgeUID) {
127 if (thingUID != null) {
130 return getThingUID(thingTypeUID, configuration.get(GROUP_ID).toString(), bridgeUID);
134 private ThingUID getThingUID(ThingTypeUID thingTypeUID, String id, @Nullable ThingUID bridgeUID) {
135 if (bridgeUID != null) {
136 return new ThingUID(thingTypeUID, id, bridgeUID.getId());
138 return new ThingUID(thingTypeUID, id);
143 protected @Nullable ThingHandler createHandler(Thing thing) {
144 if (HueBridgeHandler.SUPPORTED_THING_TYPES.contains(thing.getThingTypeUID())) {
145 return new HueBridgeHandler((Bridge) thing, stateOptionProvider);
146 } else if (HueLightHandler.SUPPORTED_THING_TYPES.contains(thing.getThingTypeUID())) {
147 return new HueLightHandler(thing);
148 } else if (DimmerSwitchHandler.SUPPORTED_THING_TYPES.contains(thing.getThingTypeUID())) {
149 return new DimmerSwitchHandler(thing);
150 } else if (TapSwitchHandler.SUPPORTED_THING_TYPES.contains(thing.getThingTypeUID())) {
151 return new TapSwitchHandler(thing);
152 } else if (PresenceHandler.SUPPORTED_THING_TYPES.contains(thing.getThingTypeUID())) {
153 return new PresenceHandler(thing);
154 } else if (GeofencePresenceHandler.SUPPORTED_THING_TYPES.contains(thing.getThingTypeUID())) {
155 return new GeofencePresenceHandler(thing);
156 } else if (TemperatureHandler.SUPPORTED_THING_TYPES.contains(thing.getThingTypeUID())) {
157 return new TemperatureHandler(thing);
158 } else if (LightLevelHandler.SUPPORTED_THING_TYPES.contains(thing.getThingTypeUID())) {
159 return new LightLevelHandler(thing);
160 } else if (ClipHandler.SUPPORTED_THING_TYPES.contains(thing.getThingTypeUID())) {
161 return new ClipHandler(thing);
162 } else if (HueGroupHandler.SUPPORTED_THING_TYPES.contains(thing.getThingTypeUID())) {
163 return new HueGroupHandler(thing, stateOptionProvider);