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.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.thing.type.ChannelTypeRegistry;
30 import org.osgi.service.component.annotations.Activate;
31 import org.osgi.service.component.annotations.Component;
32 import org.osgi.service.component.annotations.Reference;
34 import com.hubspot.jinjava.Jinjava;
37 * The {@link MqttThingHandlerFactory} is responsible for creating things and thing
40 * @author David Graeff - Initial contribution
42 @Component(service = ThingHandlerFactory.class)
44 public class MqttThingHandlerFactory extends BaseThingHandlerFactory {
45 private final MqttChannelTypeProvider typeProvider;
46 private final MqttChannelStateDescriptionProvider stateDescriptionProvider;
47 private final ChannelTypeRegistry channelTypeRegistry;
48 private final Jinjava jinjava = new Jinjava();
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,
78 jinjava, 10000, 2000);