]> git.basschouten.com Git - openhab-addons.git/blob
4bc07f45bc5635c54d26b3cf740e188df6390a4d
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2021 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.Set;
19 import java.util.stream.Collectors;
20 import java.util.stream.Stream;
21
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;
46
47 /**
48  * {@link HueThingHandlerFactory} is a factory for {@link HueBridgeHandler}s.
49  *
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
56  */
57 @NonNullByDefault
58 @Component(service = ThingHandlerFactory.class, configurationPid = "binding.hue")
59 public class HueThingHandlerFactory extends BaseThingHandlerFactory {
60
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()));
69
70     private final HueStateDescriptionOptionProvider stateOptionProvider;
71
72     @Activate
73     public HueThingHandlerFactory(final @Reference HueStateDescriptionOptionProvider stateOptionProvider) {
74         this.stateOptionProvider = stateOptionProvider;
75     }
76
77     @Override
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);
97         }
98
99         throw new IllegalArgumentException("The thing type " + thingTypeUID + " is not supported by the hue binding.");
100     }
101
102     @Override
103     public boolean supportsThingType(ThingTypeUID thingTypeUID) {
104         return SUPPORTED_THING_TYPES.contains(thingTypeUID);
105     }
106
107     private ThingUID getLightUID(ThingTypeUID thingTypeUID, @Nullable ThingUID thingUID, Configuration configuration,
108             @Nullable ThingUID bridgeUID) {
109         if (thingUID != null) {
110             return thingUID;
111         } else {
112             return getThingUID(thingTypeUID, configuration.get(LIGHT_ID).toString(), bridgeUID);
113         }
114     }
115
116     private ThingUID getSensorUID(ThingTypeUID thingTypeUID, @Nullable ThingUID thingUID, Configuration configuration,
117             @Nullable ThingUID bridgeUID) {
118         if (thingUID != null) {
119             return thingUID;
120         } else {
121             return getThingUID(thingTypeUID, configuration.get(SENSOR_ID).toString(), bridgeUID);
122         }
123     }
124
125     private ThingUID getGroupUID(ThingTypeUID thingTypeUID, @Nullable ThingUID thingUID, Configuration configuration,
126             @Nullable ThingUID bridgeUID) {
127         if (thingUID != null) {
128             return thingUID;
129         } else {
130             return getThingUID(thingTypeUID, configuration.get(GROUP_ID).toString(), bridgeUID);
131         }
132     }
133
134     private ThingUID getThingUID(ThingTypeUID thingTypeUID, String id, @Nullable ThingUID bridgeUID) {
135         if (bridgeUID != null) {
136             return new ThingUID(thingTypeUID, id, bridgeUID.getId());
137         } else {
138             return new ThingUID(thingTypeUID, id);
139         }
140     }
141
142     @Override
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);
164         } else {
165             return null;
166         }
167     }
168 }