]> git.basschouten.com Git - openhab-addons.git/blob
21fdc431ebee226f751fbb3ac264b1cdb2ba6389
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2020 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.hue.internal;
14
15 import static org.openhab.binding.hue.internal.HueBindingConstants.*;
16
17 import java.util.Collections;
18 import java.util.HashMap;
19 import java.util.Hashtable;
20 import java.util.Map;
21 import java.util.Set;
22 import java.util.stream.Collectors;
23 import java.util.stream.Stream;
24
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.GeofencePresenceHandler;
35 import org.openhab.binding.hue.internal.handler.sensors.LightLevelHandler;
36 import org.openhab.binding.hue.internal.handler.sensors.PresenceHandler;
37 import org.openhab.binding.hue.internal.handler.sensors.TapSwitchHandler;
38 import org.openhab.binding.hue.internal.handler.sensors.TemperatureHandler;
39 import org.openhab.core.config.core.Configuration;
40 import org.openhab.core.config.discovery.DiscoveryService;
41 import org.openhab.core.thing.Bridge;
42 import org.openhab.core.thing.Thing;
43 import org.openhab.core.thing.ThingTypeUID;
44 import org.openhab.core.thing.ThingUID;
45 import org.openhab.core.thing.binding.BaseThingHandlerFactory;
46 import org.openhab.core.thing.binding.ThingHandler;
47 import org.openhab.core.thing.binding.ThingHandlerFactory;
48 import org.osgi.framework.ServiceRegistration;
49 import org.osgi.service.component.annotations.Activate;
50 import org.osgi.service.component.annotations.Component;
51 import org.osgi.service.component.annotations.Reference;
52
53 /**
54  * {@link HueThingHandlerFactory} is a factory for {@link HueBridgeHandler}s.
55  *
56  * @author Dennis Nobel - Initial contribution of hue binding
57  * @author Kai Kreuzer - added supportsThingType method
58  * @author Andre Fuechsel - implemented to use one discovery service per bridge
59  * @author Samuel Leisering - Added support for sensor API
60  * @author Christoph Weitkamp - Added support for sensor API
61  * @author Laurent Garnier - Added support for groups
62  */
63 @NonNullByDefault
64 @Component(service = ThingHandlerFactory.class, configurationPid = "binding.hue")
65 public class HueThingHandlerFactory extends BaseThingHandlerFactory {
66
67     public static final Set<ThingTypeUID> SUPPORTED_THING_TYPES = Collections.unmodifiableSet(Stream
68             .of(HueBridgeHandler.SUPPORTED_THING_TYPES.stream(), HueLightHandler.SUPPORTED_THING_TYPES.stream(),
69                     DimmerSwitchHandler.SUPPORTED_THING_TYPES.stream(), TapSwitchHandler.SUPPORTED_THING_TYPES.stream(),
70                     PresenceHandler.SUPPORTED_THING_TYPES.stream(),
71                     GeofencePresenceHandler.SUPPORTED_THING_TYPES.stream(),
72                     TemperatureHandler.SUPPORTED_THING_TYPES.stream(), LightLevelHandler.SUPPORTED_THING_TYPES.stream(),
73                     ClipHandler.SUPPORTED_THING_TYPES.stream(), HueGroupHandler.SUPPORTED_THING_TYPES.stream())
74             .flatMap(i -> i).collect(Collectors.toSet()));
75
76     private final HueStateDescriptionOptionProvider stateOptionProvider;
77
78     private final Map<ThingUID, @Nullable ServiceRegistration<?>> discoveryServiceRegs = new HashMap<>();
79
80     @Activate
81     public HueThingHandlerFactory(final @Reference HueStateDescriptionOptionProvider stateOptionProvider) {
82         this.stateOptionProvider = stateOptionProvider;
83     }
84
85     @Override
86     public @Nullable Thing createThing(ThingTypeUID thingTypeUID, Configuration configuration,
87             @Nullable ThingUID thingUID, @Nullable ThingUID bridgeUID) {
88         if (HueBridgeHandler.SUPPORTED_THING_TYPES.contains(thingTypeUID)) {
89             return super.createThing(thingTypeUID, configuration, thingUID, null);
90         } else if (HueLightHandler.SUPPORTED_THING_TYPES.contains(thingTypeUID)) {
91             ThingUID hueLightUID = getLightUID(thingTypeUID, thingUID, configuration, bridgeUID);
92             return super.createThing(thingTypeUID, configuration, hueLightUID, bridgeUID);
93         } else if (DimmerSwitchHandler.SUPPORTED_THING_TYPES.contains(thingTypeUID)
94                 || TapSwitchHandler.SUPPORTED_THING_TYPES.contains(thingTypeUID)
95                 || PresenceHandler.SUPPORTED_THING_TYPES.contains(thingTypeUID)
96                 || GeofencePresenceHandler.SUPPORTED_THING_TYPES.contains(thingTypeUID)
97                 || TemperatureHandler.SUPPORTED_THING_TYPES.contains(thingTypeUID)
98                 || LightLevelHandler.SUPPORTED_THING_TYPES.contains(thingTypeUID)
99                 || ClipHandler.SUPPORTED_THING_TYPES.contains(thingTypeUID)) {
100             ThingUID hueSensorUID = getSensorUID(thingTypeUID, thingUID, configuration, bridgeUID);
101             return super.createThing(thingTypeUID, configuration, hueSensorUID, bridgeUID);
102         } else if (HueGroupHandler.SUPPORTED_THING_TYPES.contains(thingTypeUID)) {
103             ThingUID hueGroupUID = getGroupUID(thingTypeUID, thingUID, configuration, bridgeUID);
104             return super.createThing(thingTypeUID, configuration, hueGroupUID, bridgeUID);
105         }
106
107         throw new IllegalArgumentException("The thing type " + thingTypeUID + " is not supported by the hue binding.");
108     }
109
110     @Override
111     public boolean supportsThingType(ThingTypeUID thingTypeUID) {
112         return SUPPORTED_THING_TYPES.contains(thingTypeUID);
113     }
114
115     private ThingUID getLightUID(ThingTypeUID thingTypeUID, @Nullable ThingUID thingUID, Configuration configuration,
116             @Nullable ThingUID bridgeUID) {
117         if (thingUID != null) {
118             return thingUID;
119         } else {
120             return getThingUID(thingTypeUID, configuration.get(LIGHT_ID).toString(), bridgeUID);
121         }
122     }
123
124     private ThingUID getSensorUID(ThingTypeUID thingTypeUID, @Nullable ThingUID thingUID, Configuration configuration,
125             @Nullable ThingUID bridgeUID) {
126         if (thingUID != null) {
127             return thingUID;
128         } else {
129             return getThingUID(thingTypeUID, configuration.get(SENSOR_ID).toString(), bridgeUID);
130         }
131     }
132
133     private ThingUID getGroupUID(ThingTypeUID thingTypeUID, @Nullable ThingUID thingUID, Configuration configuration,
134             @Nullable ThingUID bridgeUID) {
135         if (thingUID != null) {
136             return thingUID;
137         } else {
138             return getThingUID(thingTypeUID, configuration.get(GROUP_ID).toString(), bridgeUID);
139         }
140     }
141
142     private ThingUID getThingUID(ThingTypeUID thingTypeUID, String id, @Nullable ThingUID bridgeUID) {
143         if (bridgeUID != null) {
144             return new ThingUID(thingTypeUID, id, bridgeUID.getId());
145         } else {
146             return new ThingUID(thingTypeUID, id);
147         }
148     }
149
150     @Override
151     protected @Nullable ThingHandler createHandler(Thing thing) {
152         if (HueBridgeHandler.SUPPORTED_THING_TYPES.contains(thing.getThingTypeUID())) {
153             HueBridgeHandler handler = new HueBridgeHandler((Bridge) thing, stateOptionProvider);
154             registerLightDiscoveryService(handler);
155             return handler;
156         } else if (HueLightHandler.SUPPORTED_THING_TYPES.contains(thing.getThingTypeUID())) {
157             return new HueLightHandler(thing);
158         } else if (DimmerSwitchHandler.SUPPORTED_THING_TYPES.contains(thing.getThingTypeUID())) {
159             return new DimmerSwitchHandler(thing);
160         } else if (TapSwitchHandler.SUPPORTED_THING_TYPES.contains(thing.getThingTypeUID())) {
161             return new TapSwitchHandler(thing);
162         } else if (PresenceHandler.SUPPORTED_THING_TYPES.contains(thing.getThingTypeUID())) {
163             return new PresenceHandler(thing);
164         } else if (GeofencePresenceHandler.SUPPORTED_THING_TYPES.contains(thing.getThingTypeUID())) {
165             return new GeofencePresenceHandler(thing);
166         } else if (TemperatureHandler.SUPPORTED_THING_TYPES.contains(thing.getThingTypeUID())) {
167             return new TemperatureHandler(thing);
168         } else if (LightLevelHandler.SUPPORTED_THING_TYPES.contains(thing.getThingTypeUID())) {
169             return new LightLevelHandler(thing);
170         } else if (ClipHandler.SUPPORTED_THING_TYPES.contains(thing.getThingTypeUID())) {
171             return new ClipHandler(thing);
172         } else if (HueGroupHandler.SUPPORTED_THING_TYPES.contains(thing.getThingTypeUID())) {
173             return new HueGroupHandler(thing, stateOptionProvider);
174         } else {
175             return null;
176         }
177     }
178
179     private synchronized void registerLightDiscoveryService(HueBridgeHandler bridgeHandler) {
180         HueLightDiscoveryService discoveryService = new HueLightDiscoveryService(bridgeHandler);
181         discoveryService.activate();
182         this.discoveryServiceRegs.put(bridgeHandler.getThing().getUID(),
183                 bundleContext.registerService(DiscoveryService.class.getName(), discoveryService, new Hashtable<>()));
184     }
185
186     @Override
187     protected synchronized void removeHandler(ThingHandler thingHandler) {
188         if (thingHandler instanceof HueBridgeHandler) {
189             ServiceRegistration<?> serviceReg = this.discoveryServiceRegs.remove(thingHandler.getThing().getUID());
190             if (serviceReg != null) {
191                 // remove discovery service, if bridge handler is removed
192                 HueLightDiscoveryService service = (HueLightDiscoveryService) bundleContext
193                         .getService(serviceReg.getReference());
194                 serviceReg.unregister();
195                 if (service != null) {
196                     service.deactivate();
197                 }
198             }
199         }
200     }
201 }