]> git.basschouten.com Git - openhab-addons.git/blob
27f584e2828489c88bfa8f4338a8f5e9d1a73d76
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2022 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.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.thing.Bridge;
39 import org.openhab.core.thing.Thing;
40 import org.openhab.core.thing.ThingTypeUID;
41 import org.openhab.core.thing.ThingUID;
42 import org.openhab.core.thing.binding.BaseThingHandlerFactory;
43 import org.openhab.core.thing.binding.ThingHandler;
44 import org.openhab.core.thing.binding.ThingHandlerFactory;
45 import org.osgi.service.component.annotations.Activate;
46 import org.osgi.service.component.annotations.Component;
47 import org.osgi.service.component.annotations.Reference;
48
49 /**
50  * {@link HueThingHandlerFactory} is a factory for {@link HueBridgeHandler}s.
51  *
52  * @author Dennis Nobel - Initial contribution of hue binding
53  * @author Kai Kreuzer - added supportsThingType method
54  * @author Andre Fuechsel - implemented to use one discovery service per bridge
55  * @author Samuel Leisering - Added support for sensor API
56  * @author Christoph Weitkamp - Added support for sensor API
57  * @author Laurent Garnier - Added support for groups
58  */
59 @NonNullByDefault
60 @Component(service = ThingHandlerFactory.class, configurationPid = "binding.hue")
61 public class HueThingHandlerFactory extends BaseThingHandlerFactory {
62
63     public static final Set<ThingTypeUID> SUPPORTED_THING_TYPES = Collections.unmodifiableSet(Stream
64             .of(HueBridgeHandler.SUPPORTED_THING_TYPES.stream(), HueLightHandler.SUPPORTED_THING_TYPES.stream(),
65                     DimmerSwitchHandler.SUPPORTED_THING_TYPES.stream(), TapSwitchHandler.SUPPORTED_THING_TYPES.stream(),
66                     PresenceHandler.SUPPORTED_THING_TYPES.stream(),
67                     GeofencePresenceHandler.SUPPORTED_THING_TYPES.stream(),
68                     TemperatureHandler.SUPPORTED_THING_TYPES.stream(), LightLevelHandler.SUPPORTED_THING_TYPES.stream(),
69                     ClipHandler.SUPPORTED_THING_TYPES.stream(), HueGroupHandler.SUPPORTED_THING_TYPES.stream())
70             .flatMap(i -> i).collect(Collectors.toSet()));
71
72     private final HueStateDescriptionProvider stateDescriptionProvider;
73     private final TranslationProvider i18nProvider;
74     private final LocaleProvider localeProvider;
75
76     @Activate
77     public HueThingHandlerFactory(final @Reference HueStateDescriptionProvider stateDescriptionProvider,
78             final @Reference TranslationProvider i18nProvider, final @Reference LocaleProvider localeProvider) {
79         this.stateDescriptionProvider = stateDescriptionProvider;
80         this.i18nProvider = i18nProvider;
81         this.localeProvider = localeProvider;
82     }
83
84     @Override
85     public @Nullable Thing createThing(ThingTypeUID thingTypeUID, Configuration configuration,
86             @Nullable ThingUID thingUID, @Nullable ThingUID bridgeUID) {
87         if (HueBridgeHandler.SUPPORTED_THING_TYPES.contains(thingTypeUID)) {
88             return super.createThing(thingTypeUID, configuration, thingUID, null);
89         } else if (HueLightHandler.SUPPORTED_THING_TYPES.contains(thingTypeUID)) {
90             ThingUID hueLightUID = getLightUID(thingTypeUID, thingUID, configuration, bridgeUID);
91             return super.createThing(thingTypeUID, configuration, hueLightUID, bridgeUID);
92         } else if (DimmerSwitchHandler.SUPPORTED_THING_TYPES.contains(thingTypeUID)
93                 || TapSwitchHandler.SUPPORTED_THING_TYPES.contains(thingTypeUID)
94                 || PresenceHandler.SUPPORTED_THING_TYPES.contains(thingTypeUID)
95                 || GeofencePresenceHandler.SUPPORTED_THING_TYPES.contains(thingTypeUID)
96                 || TemperatureHandler.SUPPORTED_THING_TYPES.contains(thingTypeUID)
97                 || LightLevelHandler.SUPPORTED_THING_TYPES.contains(thingTypeUID)
98                 || ClipHandler.SUPPORTED_THING_TYPES.contains(thingTypeUID)) {
99             ThingUID hueSensorUID = getSensorUID(thingTypeUID, thingUID, configuration, bridgeUID);
100             return super.createThing(thingTypeUID, configuration, hueSensorUID, bridgeUID);
101         } else if (HueGroupHandler.SUPPORTED_THING_TYPES.contains(thingTypeUID)) {
102             ThingUID hueGroupUID = getGroupUID(thingTypeUID, thingUID, configuration, bridgeUID);
103             return super.createThing(thingTypeUID, configuration, hueGroupUID, bridgeUID);
104         }
105
106         throw new IllegalArgumentException("The thing type " + thingTypeUID + " is not supported by the hue binding.");
107     }
108
109     @Override
110     public boolean supportsThingType(ThingTypeUID thingTypeUID) {
111         return SUPPORTED_THING_TYPES.contains(thingTypeUID);
112     }
113
114     private ThingUID getLightUID(ThingTypeUID thingTypeUID, @Nullable ThingUID thingUID, Configuration configuration,
115             @Nullable ThingUID bridgeUID) {
116         if (thingUID != null) {
117             return thingUID;
118         } else {
119             return getThingUID(thingTypeUID, configuration.get(LIGHT_ID).toString(), bridgeUID);
120         }
121     }
122
123     private ThingUID getSensorUID(ThingTypeUID thingTypeUID, @Nullable ThingUID thingUID, Configuration configuration,
124             @Nullable ThingUID bridgeUID) {
125         if (thingUID != null) {
126             return thingUID;
127         } else {
128             return getThingUID(thingTypeUID, configuration.get(SENSOR_ID).toString(), bridgeUID);
129         }
130     }
131
132     private ThingUID getGroupUID(ThingTypeUID thingTypeUID, @Nullable ThingUID thingUID, Configuration configuration,
133             @Nullable ThingUID bridgeUID) {
134         if (thingUID != null) {
135             return thingUID;
136         } else {
137             return getThingUID(thingTypeUID, configuration.get(GROUP_ID).toString(), bridgeUID);
138         }
139     }
140
141     private ThingUID getThingUID(ThingTypeUID thingTypeUID, String id, @Nullable ThingUID bridgeUID) {
142         if (bridgeUID != null) {
143             return new ThingUID(thingTypeUID, id, bridgeUID.getId());
144         } else {
145             return new ThingUID(thingTypeUID, id);
146         }
147     }
148
149     @Override
150     protected @Nullable ThingHandler createHandler(Thing thing) {
151         if (HueBridgeHandler.SUPPORTED_THING_TYPES.contains(thing.getThingTypeUID())) {
152             return new HueBridgeHandler((Bridge) thing, stateDescriptionProvider, i18nProvider, localeProvider);
153         } else if (HueLightHandler.SUPPORTED_THING_TYPES.contains(thing.getThingTypeUID())) {
154             return new HueLightHandler(thing, stateDescriptionProvider);
155         } else if (DimmerSwitchHandler.SUPPORTED_THING_TYPES.contains(thing.getThingTypeUID())) {
156             return new DimmerSwitchHandler(thing);
157         } else if (TapSwitchHandler.SUPPORTED_THING_TYPES.contains(thing.getThingTypeUID())) {
158             return new TapSwitchHandler(thing);
159         } else if (PresenceHandler.SUPPORTED_THING_TYPES.contains(thing.getThingTypeUID())) {
160             return new PresenceHandler(thing);
161         } else if (GeofencePresenceHandler.SUPPORTED_THING_TYPES.contains(thing.getThingTypeUID())) {
162             return new GeofencePresenceHandler(thing);
163         } else if (TemperatureHandler.SUPPORTED_THING_TYPES.contains(thing.getThingTypeUID())) {
164             return new TemperatureHandler(thing);
165         } else if (LightLevelHandler.SUPPORTED_THING_TYPES.contains(thing.getThingTypeUID())) {
166             return new LightLevelHandler(thing);
167         } else if (ClipHandler.SUPPORTED_THING_TYPES.contains(thing.getThingTypeUID())) {
168             return new ClipHandler(thing);
169         } else if (HueGroupHandler.SUPPORTED_THING_TYPES.contains(thing.getThingTypeUID())) {
170             return new HueGroupHandler(thing, stateDescriptionProvider);
171         } else {
172             return null;
173         }
174     }
175 }