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.MqttChannelTypeProvider;
20 import org.openhab.binding.mqtt.generic.TransformationServiceProvider;
21 import org.openhab.binding.mqtt.homie.internal.handler.HomieThingHandler;
22 import org.openhab.core.thing.Thing;
23 import org.openhab.core.thing.ThingTypeUID;
24 import org.openhab.core.thing.binding.BaseThingHandlerFactory;
25 import org.openhab.core.thing.binding.ThingHandler;
26 import org.openhab.core.thing.binding.ThingHandlerFactory;
27 import org.openhab.core.transform.TransformationHelper;
28 import org.openhab.core.transform.TransformationService;
29 import org.osgi.service.component.ComponentContext;
30 import org.osgi.service.component.annotations.Activate;
31 import org.osgi.service.component.annotations.Component;
32 import org.osgi.service.component.annotations.Deactivate;
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 @NonNullByDefault({}) MqttChannelTypeProvider typeProvider;
45 private static final Set<ThingTypeUID> SUPPORTED_THING_TYPES_UIDS = Set
46 .of(MqttBindingConstants.HOMIE300_MQTT_THING);
49 public boolean supportsThingType(ThingTypeUID thingTypeUID) {
50 return SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID);
55 protected void activate(ComponentContext componentContext) {
56 super.activate(componentContext);
61 protected void deactivate(ComponentContext componentContext) {
62 super.deactivate(componentContext);
66 protected void setChannelProvider(MqttChannelTypeProvider provider) {
67 this.typeProvider = provider;
70 protected void unsetChannelProvider(MqttChannelTypeProvider provider) {
71 this.typeProvider = null;
75 protected @Nullable ThingHandler createHandler(Thing thing) {
76 ThingTypeUID thingTypeUID = thing.getThingTypeUID();
78 if (thingTypeUID.equals(MqttBindingConstants.HOMIE300_MQTT_THING)) {
79 return new HomieThingHandler(thing, typeProvider, MqttBindingConstants.HOMIE_DEVICE_TIMEOUT_MS,
80 MqttBindingConstants.HOMIE_SUBSCRIBE_TIMEOUT_MS, MqttBindingConstants.HOMIE_ATTRIBUTE_TIMEOUT_MS);
86 public @Nullable TransformationService getTransformationService(String type) {
87 return TransformationHelper.getTransformationService(bundleContext, type);