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