2 * Copyright (c) 2010-2024 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.mqtt.homie.generic.internal;
17 import org.eclipse.jdt.annotation.NonNullByDefault;
18 import org.eclipse.jdt.annotation.Nullable;
19 import org.openhab.binding.mqtt.generic.MqttChannelStateDescriptionProvider;
20 import org.openhab.binding.mqtt.generic.MqttChannelTypeProvider;
21 import org.openhab.binding.mqtt.generic.TransformationServiceProvider;
22 import org.openhab.binding.mqtt.homie.internal.handler.HomieThingHandler;
23 import org.openhab.core.thing.Thing;
24 import org.openhab.core.thing.ThingTypeUID;
25 import org.openhab.core.thing.binding.BaseThingHandlerFactory;
26 import org.openhab.core.thing.binding.ThingHandler;
27 import org.openhab.core.thing.binding.ThingHandlerFactory;
28 import org.openhab.core.thing.type.ChannelTypeRegistry;
29 import org.openhab.core.transform.TransformationHelper;
30 import org.openhab.core.transform.TransformationService;
31 import org.osgi.service.component.annotations.Activate;
32 import org.osgi.service.component.annotations.Component;
33 import org.osgi.service.component.annotations.Reference;
36 * The {@link MqttThingHandlerFactory} is responsible for creating things and thing
39 * @author David Graeff - Initial contribution
41 @Component(service = ThingHandlerFactory.class)
43 public class MqttThingHandlerFactory extends BaseThingHandlerFactory implements TransformationServiceProvider {
44 private final MqttChannelTypeProvider typeProvider;
45 private final MqttChannelStateDescriptionProvider stateDescriptionProvider;
46 private final ChannelTypeRegistry channelTypeRegistry;
49 public MqttThingHandlerFactory(final @Reference MqttChannelTypeProvider typeProvider,
50 final @Reference MqttChannelStateDescriptionProvider stateDescriptionProvider,
51 final @Reference ChannelTypeRegistry channelTypeRegistry) {
52 this.typeProvider = typeProvider;
53 this.stateDescriptionProvider = stateDescriptionProvider;
54 this.channelTypeRegistry = channelTypeRegistry;
57 private static final Set<ThingTypeUID> SUPPORTED_THING_TYPES_UIDS = Set
58 .of(MqttBindingConstants.HOMIE300_MQTT_THING);
61 public boolean supportsThingType(ThingTypeUID thingTypeUID) {
62 return SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID) || isHomieDynamicType(thingTypeUID);
65 private boolean isHomieDynamicType(ThingTypeUID thingTypeUID) {
66 return MqttBindingConstants.BINDING_ID.equals(thingTypeUID.getBindingId())
67 && thingTypeUID.getId().startsWith(MqttBindingConstants.HOMIE300_MQTT_THING.getId());
71 protected @Nullable ThingHandler createHandler(Thing thing) {
72 ThingTypeUID thingTypeUID = thing.getThingTypeUID();
74 if (supportsThingType(thingTypeUID)) {
75 return new HomieThingHandler(thing, typeProvider, stateDescriptionProvider, channelTypeRegistry,
76 MqttBindingConstants.HOMIE_DEVICE_TIMEOUT_MS, MqttBindingConstants.HOMIE_SUBSCRIBE_TIMEOUT_MS,
77 MqttBindingConstants.HOMIE_ATTRIBUTE_TIMEOUT_MS);
83 public @Nullable TransformationService getTransformationService(String type) {
84 return TransformationHelper.getTransformationService(bundleContext, type);