2 * Copyright (c) 2010-2021 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.apache.commons.lang.StringUtils;
20 import org.eclipse.jdt.annotation.NonNullByDefault;
21 import org.eclipse.jdt.annotation.Nullable;
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.transform.TransformationHelper;
31 import org.openhab.core.transform.TransformationService;
32 import org.osgi.service.component.ComponentContext;
33 import org.osgi.service.component.annotations.Activate;
34 import org.osgi.service.component.annotations.Component;
35 import org.osgi.service.component.annotations.Deactivate;
36 import org.osgi.service.component.annotations.Reference;
39 * The {@link MqttThingHandlerFactory} is responsible for creating things and thing
42 * @author David Graeff - Initial contribution
44 @Component(service = ThingHandlerFactory.class)
46 public class MqttThingHandlerFactory extends BaseThingHandlerFactory implements TransformationServiceProvider {
47 private @NonNullByDefault({}) MqttChannelTypeProvider typeProvider;
48 private static final Set<ThingTypeUID> SUPPORTED_THING_TYPES_UIDS = Stream
49 .of(MqttBindingConstants.HOMEASSISTANT_MQTT_THING).collect(Collectors.toSet());
52 public boolean supportsThingType(ThingTypeUID thingTypeUID) {
53 return SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID) || isHomeassistantDynamicType(thingTypeUID);
56 private boolean isHomeassistantDynamicType(ThingTypeUID thingTypeUID) {
57 return StringUtils.equals(MqttBindingConstants.BINDING_ID, thingTypeUID.getBindingId())
58 && StringUtils.startsWith(thingTypeUID.getId(), MqttBindingConstants.HOMEASSISTANT_MQTT_THING.getId());
63 protected void activate(ComponentContext componentContext) {
64 super.activate(componentContext);
69 protected void deactivate(ComponentContext componentContext) {
70 super.deactivate(componentContext);
74 protected void setChannelProvider(MqttChannelTypeProvider provider) {
75 this.typeProvider = provider;
78 protected void unsetChannelProvider(MqttChannelTypeProvider provider) {
79 this.typeProvider = null;
83 protected @Nullable ThingHandler createHandler(Thing thing) {
84 ThingTypeUID thingTypeUID = thing.getThingTypeUID();
86 if (supportsThingType(thingTypeUID)) {
87 return new HomeAssistantThingHandler(thing, typeProvider, this, 10000, 2000);
93 public @Nullable TransformationService getTransformationService(String type) {
94 return TransformationHelper.getTransformationService(bundleContext, type);