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