]> git.basschouten.com Git - openhab-addons.git/blob
c512d1090556582288566136ac2817fb8de5e8cc
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2024 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.boschshc.internal.devices;
14
15 import static org.openhab.binding.boschshc.internal.devices.BoschSHCBindingConstants.THING_TYPE_CAMERA_360;
16 import static org.openhab.binding.boschshc.internal.devices.BoschSHCBindingConstants.THING_TYPE_CAMERA_EYES;
17 import static org.openhab.binding.boschshc.internal.devices.BoschSHCBindingConstants.THING_TYPE_CLIMATE_CONTROL;
18 import static org.openhab.binding.boschshc.internal.devices.BoschSHCBindingConstants.THING_TYPE_INTRUSION_DETECTION_SYSTEM;
19 import static org.openhab.binding.boschshc.internal.devices.BoschSHCBindingConstants.THING_TYPE_INWALL_SWITCH;
20 import static org.openhab.binding.boschshc.internal.devices.BoschSHCBindingConstants.THING_TYPE_MOTION_DETECTOR;
21 import static org.openhab.binding.boschshc.internal.devices.BoschSHCBindingConstants.THING_TYPE_SHC;
22 import static org.openhab.binding.boschshc.internal.devices.BoschSHCBindingConstants.THING_TYPE_SHUTTER_CONTROL;
23 import static org.openhab.binding.boschshc.internal.devices.BoschSHCBindingConstants.THING_TYPE_SMART_BULB;
24 import static org.openhab.binding.boschshc.internal.devices.BoschSHCBindingConstants.THING_TYPE_SMART_PLUG_COMPACT;
25 import static org.openhab.binding.boschshc.internal.devices.BoschSHCBindingConstants.THING_TYPE_SMOKE_DETECTOR;
26 import static org.openhab.binding.boschshc.internal.devices.BoschSHCBindingConstants.THING_TYPE_SMOKE_DETECTOR_2;
27 import static org.openhab.binding.boschshc.internal.devices.BoschSHCBindingConstants.THING_TYPE_THERMOSTAT;
28 import static org.openhab.binding.boschshc.internal.devices.BoschSHCBindingConstants.THING_TYPE_TWINGUARD;
29 import static org.openhab.binding.boschshc.internal.devices.BoschSHCBindingConstants.THING_TYPE_UNIVERSAL_SWITCH;
30 import static org.openhab.binding.boschshc.internal.devices.BoschSHCBindingConstants.THING_TYPE_UNIVERSAL_SWITCH_2;
31 import static org.openhab.binding.boschshc.internal.devices.BoschSHCBindingConstants.THING_TYPE_USER_DEFINED_STATE;
32 import static org.openhab.binding.boschshc.internal.devices.BoschSHCBindingConstants.THING_TYPE_WALL_THERMOSTAT;
33 import static org.openhab.binding.boschshc.internal.devices.BoschSHCBindingConstants.THING_TYPE_WINDOW_CONTACT;
34 import static org.openhab.binding.boschshc.internal.devices.BoschSHCBindingConstants.THING_TYPE_WINDOW_CONTACT_2;
35
36 import java.util.Collection;
37 import java.util.List;
38 import java.util.function.Function;
39
40 import org.eclipse.jdt.annotation.NonNullByDefault;
41 import org.eclipse.jdt.annotation.Nullable;
42 import org.openhab.binding.boschshc.internal.devices.bridge.BridgeHandler;
43 import org.openhab.binding.boschshc.internal.devices.camera.CameraHandler;
44 import org.openhab.binding.boschshc.internal.devices.climatecontrol.ClimateControlHandler;
45 import org.openhab.binding.boschshc.internal.devices.intrusion.IntrusionDetectionHandler;
46 import org.openhab.binding.boschshc.internal.devices.lightcontrol.LightControlHandler;
47 import org.openhab.binding.boschshc.internal.devices.motiondetector.MotionDetectorHandler;
48 import org.openhab.binding.boschshc.internal.devices.plug.PlugHandler;
49 import org.openhab.binding.boschshc.internal.devices.shuttercontrol.ShutterControlHandler;
50 import org.openhab.binding.boschshc.internal.devices.smartbulb.SmartBulbHandler;
51 import org.openhab.binding.boschshc.internal.devices.smokedetector.SmokeDetector2Handler;
52 import org.openhab.binding.boschshc.internal.devices.smokedetector.SmokeDetectorHandler;
53 import org.openhab.binding.boschshc.internal.devices.thermostat.ThermostatHandler;
54 import org.openhab.binding.boschshc.internal.devices.twinguard.TwinguardHandler;
55 import org.openhab.binding.boschshc.internal.devices.universalswitch.UniversalSwitch2Handler;
56 import org.openhab.binding.boschshc.internal.devices.universalswitch.UniversalSwitchHandler;
57 import org.openhab.binding.boschshc.internal.devices.userdefinedstate.UserStateHandler;
58 import org.openhab.binding.boschshc.internal.devices.wallthermostat.WallThermostatHandler;
59 import org.openhab.binding.boschshc.internal.devices.windowcontact.WindowContact2Handler;
60 import org.openhab.binding.boschshc.internal.devices.windowcontact.WindowContactHandler;
61 import org.openhab.core.i18n.TimeZoneProvider;
62 import org.openhab.core.thing.Bridge;
63 import org.openhab.core.thing.Thing;
64 import org.openhab.core.thing.ThingTypeUID;
65 import org.openhab.core.thing.binding.BaseThingHandler;
66 import org.openhab.core.thing.binding.BaseThingHandlerFactory;
67 import org.openhab.core.thing.binding.ThingHandler;
68 import org.openhab.core.thing.binding.ThingHandlerFactory;
69 import org.osgi.service.component.annotations.Activate;
70 import org.osgi.service.component.annotations.Component;
71 import org.osgi.service.component.annotations.Reference;
72
73 /**
74  * The {@link BoschSHCHandlerFactory} is responsible for creating things and
75  * thing handlers.
76  *
77  * @author Stefan Kästle - Initial contribution
78  * @author Christian Oeing - Added Shutter Control and ThermostatHandler; refactored handler mapping
79  * @author Christian Oeing - Added WallThermostatHandler
80  * @author David Pace - Added cameras, intrusion detection system and smart plugs
81  * @author Christian Oeing - Added smoke detector
82  */
83 @NonNullByDefault
84 @Component(configurationPid = "binding.boschshc", service = ThingHandlerFactory.class)
85 public class BoschSHCHandlerFactory extends BaseThingHandlerFactory {
86
87     private TimeZoneProvider timeZoneProvider;
88
89     @Activate
90     public BoschSHCHandlerFactory(final @Reference TimeZoneProvider timeZoneProvider) {
91         this.timeZoneProvider = timeZoneProvider;
92     }
93
94     private static class ThingTypeHandlerMapping {
95         public ThingTypeUID thingTypeUID;
96         public Function<Thing, BaseThingHandler> handlerSupplier;
97
98         public ThingTypeHandlerMapping(ThingTypeUID thingTypeUID, Function<Thing, BaseThingHandler> handlerSupplier) {
99             this.thingTypeUID = thingTypeUID;
100             this.handlerSupplier = handlerSupplier;
101         }
102     }
103
104     private final Collection<ThingTypeHandlerMapping> supportedThingTypes = List.of(
105             new ThingTypeHandlerMapping(THING_TYPE_SHC, thing -> new BridgeHandler((Bridge) thing)),
106             new ThingTypeHandlerMapping(THING_TYPE_INWALL_SWITCH, LightControlHandler::new),
107             new ThingTypeHandlerMapping(THING_TYPE_TWINGUARD, TwinguardHandler::new),
108             new ThingTypeHandlerMapping(THING_TYPE_WINDOW_CONTACT, WindowContactHandler::new),
109             new ThingTypeHandlerMapping(THING_TYPE_WINDOW_CONTACT_2, WindowContact2Handler::new),
110             new ThingTypeHandlerMapping(THING_TYPE_MOTION_DETECTOR, MotionDetectorHandler::new),
111             new ThingTypeHandlerMapping(THING_TYPE_SHUTTER_CONTROL, ShutterControlHandler::new),
112             new ThingTypeHandlerMapping(THING_TYPE_THERMOSTAT, ThermostatHandler::new),
113             new ThingTypeHandlerMapping(THING_TYPE_CLIMATE_CONTROL, ClimateControlHandler::new),
114             new ThingTypeHandlerMapping(THING_TYPE_WALL_THERMOSTAT, WallThermostatHandler::new),
115             new ThingTypeHandlerMapping(THING_TYPE_CAMERA_360, CameraHandler::new),
116             new ThingTypeHandlerMapping(THING_TYPE_CAMERA_EYES, CameraHandler::new),
117             new ThingTypeHandlerMapping(THING_TYPE_INTRUSION_DETECTION_SYSTEM, IntrusionDetectionHandler::new),
118             new ThingTypeHandlerMapping(THING_TYPE_SMART_PLUG_COMPACT, PlugHandler::new),
119             new ThingTypeHandlerMapping(THING_TYPE_SMART_BULB, SmartBulbHandler::new),
120             new ThingTypeHandlerMapping(THING_TYPE_SMOKE_DETECTOR, SmokeDetectorHandler::new),
121             new ThingTypeHandlerMapping(THING_TYPE_USER_DEFINED_STATE, UserStateHandler::new),
122             new ThingTypeHandlerMapping(THING_TYPE_UNIVERSAL_SWITCH,
123                     thing -> new UniversalSwitchHandler(thing, timeZoneProvider)),
124             new ThingTypeHandlerMapping(THING_TYPE_UNIVERSAL_SWITCH_2,
125                     thing -> new UniversalSwitch2Handler(thing, timeZoneProvider)),
126             new ThingTypeHandlerMapping(THING_TYPE_SMOKE_DETECTOR_2, SmokeDetector2Handler::new));
127
128     @Override
129     public boolean supportsThingType(ThingTypeUID thingTypeUID) {
130         return supportedThingTypes.stream().anyMatch(mapping -> mapping.thingTypeUID.equals(thingTypeUID));
131     }
132
133     @Override
134     protected @Nullable ThingHandler createHandler(Thing thing) {
135         ThingTypeUID thingTypeUID = thing.getThingTypeUID();
136
137         // Search for mapping for thing type and return handler for it if found. Otherwise return null.
138         return supportedThingTypes.stream().filter(mapping -> mapping.thingTypeUID.equals(thingTypeUID)).findFirst()
139                 .<@Nullable BaseThingHandler> map(mapping -> mapping.handlerSupplier.apply(thing)).orElse(null);
140     }
141 }