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;
35 * The {@link MqttThingHandlerFactory} is responsible for creating things and thing
38 * @author David Graeff - Initial contribution
40 @Component(service = ThingHandlerFactory.class)
42 public class MqttThingHandlerFactory extends BaseThingHandlerFactory {
43 private final MqttChannelTypeProvider typeProvider;
44 private final MqttChannelStateDescriptionProvider stateDescriptionProvider;
45 private final ChannelTypeRegistry channelTypeRegistry;
47 private static final Set<ThingTypeUID> SUPPORTED_THING_TYPES_UIDS = Stream
48 .of(MqttBindingConstants.HOMEASSISTANT_MQTT_THING).collect(Collectors.toSet());
51 public MqttThingHandlerFactory(final @Reference MqttChannelTypeProvider typeProvider,
52 final @Reference MqttChannelStateDescriptionProvider stateDescriptionProvider,
53 final @Reference ChannelTypeRegistry channelTypeRegistry) {
54 this.typeProvider = typeProvider;
55 this.stateDescriptionProvider = stateDescriptionProvider;
56 this.channelTypeRegistry = channelTypeRegistry;
60 public boolean supportsThingType(ThingTypeUID thingTypeUID) {
61 return SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID) || isHomeassistantDynamicType(thingTypeUID);
64 private boolean isHomeassistantDynamicType(ThingTypeUID thingTypeUID) {
65 return MqttBindingConstants.BINDING_ID.equals(thingTypeUID.getBindingId())
66 && thingTypeUID.getId().startsWith(MqttBindingConstants.HOMEASSISTANT_MQTT_THING.getId());
70 protected @Nullable ThingHandler createHandler(Thing thing) {
71 ThingTypeUID thingTypeUID = thing.getThingTypeUID();
73 if (supportsThingType(thingTypeUID)) {
74 return new HomeAssistantThingHandler(thing, typeProvider, stateDescriptionProvider, channelTypeRegistry,