]> git.basschouten.com Git - openhab-addons.git/blob
4e783e9804d3ce5ca015efd20943d5d3b78bfec2
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 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.tplinksmarthome.internal;
14
15 import static org.openhab.binding.tplinksmarthome.internal.TPLinkSmartHomeThingType.SUPPORTED_THING_TYPES;
16
17 import org.eclipse.jdt.annotation.NonNullByDefault;
18 import org.eclipse.jdt.annotation.Nullable;
19 import org.openhab.binding.tplinksmarthome.internal.device.BulbDevice;
20 import org.openhab.binding.tplinksmarthome.internal.device.DimmerDevice;
21 import org.openhab.binding.tplinksmarthome.internal.device.EnergySwitchDevice;
22 import org.openhab.binding.tplinksmarthome.internal.device.LightStripDevice;
23 import org.openhab.binding.tplinksmarthome.internal.device.PowerStripDevice;
24 import org.openhab.binding.tplinksmarthome.internal.device.RangeExtenderDevice;
25 import org.openhab.binding.tplinksmarthome.internal.device.SmartHomeDevice;
26 import org.openhab.binding.tplinksmarthome.internal.device.SwitchDevice;
27 import org.openhab.binding.tplinksmarthome.internal.handler.SmartHomeHandler;
28 import org.openhab.core.thing.Thing;
29 import org.openhab.core.thing.ThingTypeUID;
30 import org.openhab.core.thing.binding.BaseThingHandlerFactory;
31 import org.openhab.core.thing.binding.ThingHandler;
32 import org.openhab.core.thing.binding.ThingHandlerFactory;
33 import org.osgi.service.component.annotations.Component;
34 import org.osgi.service.component.annotations.Reference;
35
36 /**
37  * The {@link TPLinkSmartHomeHandlerFactory} is responsible for creating things and thing handlers.
38  *
39  * @author Christian Fischer - Initial contribution
40  * @author Hilbrand Bouwkamp - Specific handlers for different type of devices.
41  */
42 @NonNullByDefault
43 @Component(service = ThingHandlerFactory.class, configurationPid = "binding.tplinksmarthome")
44 public class TPLinkSmartHomeHandlerFactory extends BaseThingHandlerFactory {
45
46     private @NonNullByDefault({}) TPLinkIpAddressService ipAddressService;
47
48     @Override
49     public boolean supportsThingType(final ThingTypeUID thingTypeUID) {
50         return SUPPORTED_THING_TYPES.contains(thingTypeUID);
51     }
52
53     @Nullable
54     @Override
55     protected ThingHandler createHandler(final Thing thing) {
56         final ThingTypeUID thingTypeUID = thing.getThingTypeUID();
57         final TPLinkSmartHomeThingType type = TPLinkSmartHomeThingType.THING_TYPE_MAP.get(thingTypeUID);
58
59         if (type == null) {
60             return null;
61         }
62         final SmartHomeDevice device;
63
64         switch (type.getDeviceType()) {
65             case BULB:
66                 device = new BulbDevice(type);
67                 break;
68             case DIMMER:
69                 device = new DimmerDevice();
70                 break;
71             case LIGHT_STRIP:
72                 device = new LightStripDevice(type);
73                 break;
74             case PLUG:
75                 device = new SwitchDevice();
76                 break;
77             case PLUG_WITH_ENERGY:
78                 device = new EnergySwitchDevice();
79                 break;
80             case STRIP:
81                 device = new PowerStripDevice(type);
82                 break;
83             case SWITCH:
84                 device = new SwitchDevice();
85                 break;
86             case RANGE_EXTENDER:
87                 device = new RangeExtenderDevice();
88                 break;
89             default:
90                 return null;
91         }
92         return new SmartHomeHandler(thing, device, type, ipAddressService);
93     }
94
95     @Reference
96     protected void setTPLinkIpAddressCache(final TPLinkIpAddressService ipAddressCache) {
97         this.ipAddressService = ipAddressCache;
98     }
99
100     protected void unsetTPLinkIpAddressCache(final TPLinkIpAddressService ipAddressCache) {
101         this.ipAddressService = null;
102     }
103 }