]> git.basschouten.com Git - openhab-addons.git/blob
b28fadcab1bcd01276cea6dea42bfd7126d6c042
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2024 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.MqttChannelStateDescriptionProvider;
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.thing.type.ChannelTypeRegistry;
31 import org.openhab.core.transform.TransformationHelper;
32 import org.openhab.core.transform.TransformationService;
33 import org.osgi.service.component.annotations.Activate;
34 import org.osgi.service.component.annotations.Component;
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 final MqttChannelTypeProvider typeProvider;
47     private final MqttChannelStateDescriptionProvider stateDescriptionProvider;
48     private final ChannelTypeRegistry channelTypeRegistry;
49
50     private static final Set<ThingTypeUID> SUPPORTED_THING_TYPES_UIDS = Stream
51             .of(MqttBindingConstants.HOMEASSISTANT_MQTT_THING).collect(Collectors.toSet());
52
53     @Activate
54     public MqttThingHandlerFactory(final @Reference MqttChannelTypeProvider typeProvider,
55             final @Reference MqttChannelStateDescriptionProvider stateDescriptionProvider,
56             final @Reference ChannelTypeRegistry channelTypeRegistry) {
57         this.typeProvider = typeProvider;
58         this.stateDescriptionProvider = stateDescriptionProvider;
59         this.channelTypeRegistry = channelTypeRegistry;
60     }
61
62     @Override
63     public boolean supportsThingType(ThingTypeUID thingTypeUID) {
64         return SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID) || isHomeassistantDynamicType(thingTypeUID);
65     }
66
67     private boolean isHomeassistantDynamicType(ThingTypeUID thingTypeUID) {
68         return MqttBindingConstants.BINDING_ID.equals(thingTypeUID.getBindingId())
69                 && thingTypeUID.getId().startsWith(MqttBindingConstants.HOMEASSISTANT_MQTT_THING.getId());
70     }
71
72     @Override
73     protected @Nullable ThingHandler createHandler(Thing thing) {
74         ThingTypeUID thingTypeUID = thing.getThingTypeUID();
75
76         if (supportsThingType(thingTypeUID)) {
77             return new HomeAssistantThingHandler(thing, typeProvider, stateDescriptionProvider, channelTypeRegistry,
78                     this, 10000, 2000);
79         }
80         return null;
81     }
82
83     @Override
84     public @Nullable TransformationService getTransformationService(String type) {
85         return TransformationHelper.getTransformationService(bundleContext, type);
86     }
87 }