]> git.basschouten.com Git - openhab-addons.git/blob
c2eedc4d35c0678b1458d65458c58c352cae0f08
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2020 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.TPLinkSmartHomeBindingConstants.*;
16 import static org.openhab.binding.tplinksmarthome.internal.TPLinkSmartHomeThingType.*;
17
18 import org.eclipse.jdt.annotation.NonNullByDefault;
19 import org.eclipse.jdt.annotation.Nullable;
20 import org.openhab.binding.tplinksmarthome.internal.device.BulbDevice;
21 import org.openhab.binding.tplinksmarthome.internal.device.DimmerDevice;
22 import org.openhab.binding.tplinksmarthome.internal.device.EnergySwitchDevice;
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(ThingTypeUID thingTypeUID) {
50         return SUPPORTED_THING_TYPES.contains(thingTypeUID);
51     }
52
53     @Nullable
54     @Override
55     protected ThingHandler createHandler(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                 if (TPLinkSmartHomeThingType.isBulbDeviceWithTemperatureColor1(thingTypeUID)) {
67                     device = new BulbDevice(thingTypeUID, COLOR_TEMPERATURE_1_MIN, COLOR_TEMPERATURE_1_MAX);
68                 } else if (TPLinkSmartHomeThingType.isBulbDeviceWithTemperatureColor2(thingTypeUID)) {
69                     device = new BulbDevice(thingTypeUID, COLOR_TEMPERATURE_2_MIN, COLOR_TEMPERATURE_2_MAX);
70                 } else {
71                     device = new BulbDevice(thingTypeUID);
72                 }
73                 break;
74             case DIMMER:
75                 device = new DimmerDevice();
76                 break;
77             case PLUG:
78                 if (HS110.is(thingTypeUID)) {
79                     device = new EnergySwitchDevice();
80                 } else {
81                     device = new SwitchDevice();
82                 }
83                 break;
84             case SWITCH:
85                 device = new SwitchDevice();
86                 break;
87             case RANGE_EXTENDER:
88                 device = new RangeExtenderDevice();
89                 break;
90             case STRIP:
91                 device = new PowerStripDevice(type);
92                 break;
93             default:
94                 return null;
95         }
96         return new SmartHomeHandler(thing, device, type, ipAddressService);
97     }
98
99     @Reference
100     protected void setTPLinkIpAddressCache(TPLinkIpAddressService ipAddressCache) {
101         this.ipAddressService = ipAddressCache;
102     }
103
104     protected void unsetTPLinkIpAddressCache(TPLinkIpAddressService ipAddressCache) {
105         this.ipAddressService = null;
106     }
107 }