]> git.basschouten.com Git - openhab-addons.git/blob
8961fe6026eba768071a38116d902c60caaadfbd
[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.mqtt.homeassistant.generic.internal;
14
15 import java.util.Set;
16 import java.util.stream.Collectors;
17 import java.util.stream.Stream;
18
19 import org.apache.commons.lang.StringUtils;
20 import org.eclipse.jdt.annotation.NonNullByDefault;
21 import org.eclipse.jdt.annotation.Nullable;
22 import org.openhab.binding.mqtt.generic.MqttChannelTypeProvider;
23 import org.openhab.binding.mqtt.generic.TransformationServiceProvider;
24 import org.openhab.binding.mqtt.homeassistant.internal.handler.HomeAssistantThingHandler;
25 import org.openhab.core.thing.Thing;
26 import org.openhab.core.thing.ThingTypeUID;
27 import org.openhab.core.thing.binding.BaseThingHandlerFactory;
28 import org.openhab.core.thing.binding.ThingHandler;
29 import org.openhab.core.thing.binding.ThingHandlerFactory;
30 import org.openhab.core.transform.TransformationHelper;
31 import org.openhab.core.transform.TransformationService;
32 import org.osgi.service.component.ComponentContext;
33 import org.osgi.service.component.annotations.Activate;
34 import org.osgi.service.component.annotations.Component;
35 import org.osgi.service.component.annotations.Deactivate;
36 import org.osgi.service.component.annotations.Reference;
37
38 /**
39  * The {@link MqttThingHandlerFactory} is responsible for creating things and thing
40  * handlers.
41  *
42  * @author David Graeff - Initial contribution
43  */
44 @Component(service = ThingHandlerFactory.class)
45 @NonNullByDefault
46 public class MqttThingHandlerFactory extends BaseThingHandlerFactory implements TransformationServiceProvider {
47     private @NonNullByDefault({}) MqttChannelTypeProvider typeProvider;
48     private static final Set<ThingTypeUID> SUPPORTED_THING_TYPES_UIDS = Stream
49             .of(MqttBindingConstants.HOMEASSISTANT_MQTT_THING).collect(Collectors.toSet());
50
51     @Override
52     public boolean supportsThingType(ThingTypeUID thingTypeUID) {
53         return SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID) || isHomeassistantDynamicType(thingTypeUID);
54     }
55
56     private boolean isHomeassistantDynamicType(ThingTypeUID thingTypeUID) {
57         return StringUtils.equals(MqttBindingConstants.BINDING_ID, thingTypeUID.getBindingId())
58                 && StringUtils.startsWith(thingTypeUID.getId(), MqttBindingConstants.HOMEASSISTANT_MQTT_THING.getId());
59     }
60
61     @Activate
62     @Override
63     protected void activate(ComponentContext componentContext) {
64         super.activate(componentContext);
65     }
66
67     @Deactivate
68     @Override
69     protected void deactivate(ComponentContext componentContext) {
70         super.deactivate(componentContext);
71     }
72
73     @Reference
74     protected void setChannelProvider(MqttChannelTypeProvider provider) {
75         this.typeProvider = provider;
76     }
77
78     protected void unsetChannelProvider(MqttChannelTypeProvider provider) {
79         this.typeProvider = null;
80     }
81
82     @Override
83     protected @Nullable ThingHandler createHandler(Thing thing) {
84         ThingTypeUID thingTypeUID = thing.getThingTypeUID();
85
86         if (supportsThingType(thingTypeUID)) {
87             return new HomeAssistantThingHandler(thing, typeProvider, this, 10000, 2000);
88         }
89         return null;
90     }
91
92     @Override
93     public @Nullable TransformationService getTransformationService(String type) {
94         return TransformationHelper.getTransformationService(bundleContext, type);
95     }
96 }