2 * Copyright (c) 2010-2020 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;
18 import java.util.HashMap;
19 import java.util.Hashtable;
22 import java.util.stream.Collectors;
23 import java.util.stream.Stream;
25 import org.eclipse.jdt.annotation.NonNullByDefault;
26 import org.eclipse.jdt.annotation.Nullable;
27 import org.openhab.binding.hue.internal.discovery.HueLightDiscoveryService;
28 import org.openhab.binding.hue.internal.handler.HueBridgeHandler;
29 import org.openhab.binding.hue.internal.handler.HueGroupHandler;
30 import org.openhab.binding.hue.internal.handler.HueLightHandler;
31 import org.openhab.binding.hue.internal.handler.HueStateDescriptionOptionProvider;
32 import org.openhab.binding.hue.internal.handler.sensors.ClipHandler;
33 import org.openhab.binding.hue.internal.handler.sensors.DimmerSwitchHandler;
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.config.discovery.DiscoveryService;
40 import org.openhab.core.thing.Bridge;
41 import org.openhab.core.thing.Thing;
42 import org.openhab.core.thing.ThingTypeUID;
43 import org.openhab.core.thing.ThingUID;
44 import org.openhab.core.thing.binding.BaseThingHandlerFactory;
45 import org.openhab.core.thing.binding.ThingHandler;
46 import org.openhab.core.thing.binding.ThingHandlerFactory;
47 import org.osgi.framework.ServiceRegistration;
48 import org.osgi.service.component.annotations.Activate;
49 import org.osgi.service.component.annotations.Component;
50 import org.osgi.service.component.annotations.Reference;
53 * {@link HueThingHandlerFactory} is a factory for {@link HueBridgeHandler}s.
55 * @author Dennis Nobel - Initial contribution of hue binding
56 * @author Kai Kreuzer - added supportsThingType method
57 * @author Andre Fuechsel - implemented to use one discovery service per bridge
58 * @author Samuel Leisering - Added support for sensor API
59 * @author Christoph Weitkamp - Added support for sensor API
60 * @author Laurent Garnier - Added support for groups
63 @Component(service = ThingHandlerFactory.class, configurationPid = "binding.hue")
64 public class HueThingHandlerFactory extends BaseThingHandlerFactory {
66 public static final Set<ThingTypeUID> SUPPORTED_THING_TYPES = Collections.unmodifiableSet(
67 Stream.of(HueBridgeHandler.SUPPORTED_THING_TYPES.stream(), HueLightHandler.SUPPORTED_THING_TYPES.stream(),
68 DimmerSwitchHandler.SUPPORTED_THING_TYPES.stream(), TapSwitchHandler.SUPPORTED_THING_TYPES.stream(),
69 PresenceHandler.SUPPORTED_THING_TYPES.stream(), TemperatureHandler.SUPPORTED_THING_TYPES.stream(),
70 LightLevelHandler.SUPPORTED_THING_TYPES.stream(), ClipHandler.SUPPORTED_THING_TYPES.stream(),
71 HueGroupHandler.SUPPORTED_THING_TYPES.stream()).flatMap(i -> i).collect(Collectors.toSet()));
73 private final HueStateDescriptionOptionProvider stateOptionProvider;
75 private final Map<ThingUID, @Nullable ServiceRegistration<?>> discoveryServiceRegs = new HashMap<>();
78 public HueThingHandlerFactory(final @Reference HueStateDescriptionOptionProvider stateOptionProvider) {
79 this.stateOptionProvider = stateOptionProvider;
83 public @Nullable Thing createThing(ThingTypeUID thingTypeUID, Configuration configuration,
84 @Nullable ThingUID thingUID, @Nullable ThingUID bridgeUID) {
85 if (HueBridgeHandler.SUPPORTED_THING_TYPES.contains(thingTypeUID)) {
86 return super.createThing(thingTypeUID, configuration, thingUID, null);
87 } else if (HueLightHandler.SUPPORTED_THING_TYPES.contains(thingTypeUID)) {
88 ThingUID hueLightUID = getLightUID(thingTypeUID, thingUID, configuration, bridgeUID);
89 return super.createThing(thingTypeUID, configuration, hueLightUID, bridgeUID);
90 } else if (DimmerSwitchHandler.SUPPORTED_THING_TYPES.contains(thingTypeUID)
91 || TapSwitchHandler.SUPPORTED_THING_TYPES.contains(thingTypeUID)
92 || PresenceHandler.SUPPORTED_THING_TYPES.contains(thingTypeUID)
93 || TemperatureHandler.SUPPORTED_THING_TYPES.contains(thingTypeUID)
94 || LightLevelHandler.SUPPORTED_THING_TYPES.contains(thingTypeUID)
95 || ClipHandler.SUPPORTED_THING_TYPES.contains(thingTypeUID)) {
96 ThingUID hueSensorUID = getSensorUID(thingTypeUID, thingUID, configuration, bridgeUID);
97 return super.createThing(thingTypeUID, configuration, hueSensorUID, bridgeUID);
98 } else if (HueGroupHandler.SUPPORTED_THING_TYPES.contains(thingTypeUID)) {
99 ThingUID hueGroupUID = getGroupUID(thingTypeUID, thingUID, configuration, bridgeUID);
100 return super.createThing(thingTypeUID, configuration, hueGroupUID, bridgeUID);
103 throw new IllegalArgumentException("The thing type " + thingTypeUID + " is not supported by the hue binding.");
107 public boolean supportsThingType(ThingTypeUID thingTypeUID) {
108 return SUPPORTED_THING_TYPES.contains(thingTypeUID);
111 private ThingUID getLightUID(ThingTypeUID thingTypeUID, @Nullable ThingUID thingUID, Configuration configuration,
112 @Nullable ThingUID bridgeUID) {
113 if (thingUID != null) {
116 return getThingUID(thingTypeUID, configuration.get(LIGHT_ID).toString(), bridgeUID);
120 private ThingUID getSensorUID(ThingTypeUID thingTypeUID, @Nullable ThingUID thingUID, Configuration configuration,
121 @Nullable ThingUID bridgeUID) {
122 if (thingUID != null) {
125 return getThingUID(thingTypeUID, configuration.get(SENSOR_ID).toString(), bridgeUID);
129 private ThingUID getGroupUID(ThingTypeUID thingTypeUID, @Nullable ThingUID thingUID, Configuration configuration,
130 @Nullable ThingUID bridgeUID) {
131 if (thingUID != null) {
134 return getThingUID(thingTypeUID, configuration.get(GROUP_ID).toString(), bridgeUID);
138 private ThingUID getThingUID(ThingTypeUID thingTypeUID, String id, @Nullable ThingUID bridgeUID) {
139 if (bridgeUID != null) {
140 return new ThingUID(thingTypeUID, id, bridgeUID.getId());
142 return new ThingUID(thingTypeUID, id);
147 protected @Nullable ThingHandler createHandler(Thing thing) {
148 if (HueBridgeHandler.SUPPORTED_THING_TYPES.contains(thing.getThingTypeUID())) {
149 HueBridgeHandler handler = new HueBridgeHandler((Bridge) thing, stateOptionProvider);
150 registerLightDiscoveryService(handler);
152 } else if (HueLightHandler.SUPPORTED_THING_TYPES.contains(thing.getThingTypeUID())) {
153 return new HueLightHandler(thing);
154 } else if (DimmerSwitchHandler.SUPPORTED_THING_TYPES.contains(thing.getThingTypeUID())) {
155 return new DimmerSwitchHandler(thing);
156 } else if (TapSwitchHandler.SUPPORTED_THING_TYPES.contains(thing.getThingTypeUID())) {
157 return new TapSwitchHandler(thing);
158 } else if (PresenceHandler.SUPPORTED_THING_TYPES.contains(thing.getThingTypeUID())) {
159 return new PresenceHandler(thing);
160 } else if (TemperatureHandler.SUPPORTED_THING_TYPES.contains(thing.getThingTypeUID())) {
161 return new TemperatureHandler(thing);
162 } else if (LightLevelHandler.SUPPORTED_THING_TYPES.contains(thing.getThingTypeUID())) {
163 return new LightLevelHandler(thing);
164 } else if (ClipHandler.SUPPORTED_THING_TYPES.contains(thing.getThingTypeUID())) {
165 return new ClipHandler(thing);
166 } else if (HueGroupHandler.SUPPORTED_THING_TYPES.contains(thing.getThingTypeUID())) {
167 return new HueGroupHandler(thing, stateOptionProvider);
173 private synchronized void registerLightDiscoveryService(HueBridgeHandler bridgeHandler) {
174 HueLightDiscoveryService discoveryService = new HueLightDiscoveryService(bridgeHandler);
175 discoveryService.activate();
176 this.discoveryServiceRegs.put(bridgeHandler.getThing().getUID(),
177 bundleContext.registerService(DiscoveryService.class.getName(), discoveryService, new Hashtable<>()));
181 protected synchronized void removeHandler(ThingHandler thingHandler) {
182 if (thingHandler instanceof HueBridgeHandler) {
183 ServiceRegistration<?> serviceReg = this.discoveryServiceRegs.remove(thingHandler.getThing().getUID());
184 if (serviceReg != null) {
185 // remove discovery service, if bridge handler is removed
186 HueLightDiscoveryService service = (HueLightDiscoveryService) bundleContext
187 .getService(serviceReg.getReference());
188 serviceReg.unregister();
189 if (service != null) {
190 service.deactivate();