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.homeassistant.generic.internal;
16 import java.util.stream.Collectors;
17 import java.util.stream.Stream;
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;
38 * The {@link MqttThingHandlerFactory} is responsible for creating things and thing
41 * @author David Graeff - Initial contribution
43 @Component(service = ThingHandlerFactory.class)
45 public class MqttThingHandlerFactory extends BaseThingHandlerFactory implements TransformationServiceProvider {
46 private final MqttChannelTypeProvider typeProvider;
47 private final MqttChannelStateDescriptionProvider stateDescriptionProvider;
48 private final ChannelTypeRegistry channelTypeRegistry;
50 private static final Set<ThingTypeUID> SUPPORTED_THING_TYPES_UIDS = Stream
51 .of(MqttBindingConstants.HOMEASSISTANT_MQTT_THING).collect(Collectors.toSet());
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;
63 public boolean supportsThingType(ThingTypeUID thingTypeUID) {
64 return SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID) || isHomeassistantDynamicType(thingTypeUID);
67 private boolean isHomeassistantDynamicType(ThingTypeUID thingTypeUID) {
68 return MqttBindingConstants.BINDING_ID.equals(thingTypeUID.getBindingId())
69 && thingTypeUID.getId().startsWith(MqttBindingConstants.HOMEASSISTANT_MQTT_THING.getId());
73 protected @Nullable ThingHandler createHandler(Thing thing) {
74 ThingTypeUID thingTypeUID = thing.getThingTypeUID();
76 if (supportsThingType(thingTypeUID)) {
77 return new HomeAssistantThingHandler(thing, typeProvider, stateDescriptionProvider, channelTypeRegistry,
84 public @Nullable TransformationService getTransformationService(String type) {
85 return TransformationHelper.getTransformationService(bundleContext, type);