]> git.basschouten.com Git - openhab-addons.git/blob
b228b1b0e3b9b071799d7d49ea914a0b7661d93a
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2024 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
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
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.mqtt.homeassistant.generic.internal;
14
15 import java.util.Set;
16 import java.util.stream.Collectors;
17 import java.util.stream.Stream;
18
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;
33
34 /**
35  * The {@link MqttThingHandlerFactory} is responsible for creating things and thing
36  * handlers.
37  *
38  * @author David Graeff - Initial contribution
39  */
40 @Component(service = ThingHandlerFactory.class)
41 @NonNullByDefault
42 public class MqttThingHandlerFactory extends BaseThingHandlerFactory {
43     private final MqttChannelTypeProvider typeProvider;
44     private final MqttChannelStateDescriptionProvider stateDescriptionProvider;
45     private final ChannelTypeRegistry channelTypeRegistry;
46
47     private static final Set<ThingTypeUID> SUPPORTED_THING_TYPES_UIDS = Stream
48             .of(MqttBindingConstants.HOMEASSISTANT_MQTT_THING).collect(Collectors.toSet());
49
50     @Activate
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;
57     }
58
59     @Override
60     public boolean supportsThingType(ThingTypeUID thingTypeUID) {
61         return SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID) || isHomeassistantDynamicType(thingTypeUID);
62     }
63
64     private boolean isHomeassistantDynamicType(ThingTypeUID thingTypeUID) {
65         return MqttBindingConstants.BINDING_ID.equals(thingTypeUID.getBindingId())
66                 && thingTypeUID.getId().startsWith(MqttBindingConstants.HOMEASSISTANT_MQTT_THING.getId());
67     }
68
69     @Override
70     protected @Nullable ThingHandler createHandler(Thing thing) {
71         ThingTypeUID thingTypeUID = thing.getThingTypeUID();
72
73         if (supportsThingType(thingTypeUID)) {
74             return new HomeAssistantThingHandler(thing, typeProvider, stateDescriptionProvider, channelTypeRegistry,
75                     10000, 2000);
76         }
77         return null;
78     }
79 }