2 * Copyright (c) 2010-2023 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.irtrans.internal;
15 import static org.openhab.binding.irtrans.internal.IRtransBindingConstants.*;
17 import java.util.Collections;
19 import java.util.stream.Collectors;
20 import java.util.stream.Stream;
22 import org.openhab.binding.irtrans.internal.handler.BlasterHandler;
23 import org.openhab.binding.irtrans.internal.handler.EthernetBridgeHandler;
24 import org.openhab.core.config.core.Configuration;
25 import org.openhab.core.thing.Bridge;
26 import org.openhab.core.thing.Thing;
27 import org.openhab.core.thing.ThingTypeUID;
28 import org.openhab.core.thing.ThingUID;
29 import org.openhab.core.thing.binding.BaseThingHandlerFactory;
30 import org.openhab.core.thing.binding.ThingHandler;
31 import org.openhab.core.thing.binding.ThingHandlerFactory;
32 import org.osgi.service.component.annotations.Component;
35 * The {@link IRtransHandlerFactory} is responsible for creating things and
38 * @author Karel Goderis - Initial contribution
41 @Component(service = ThingHandlerFactory.class, configurationPid = "binding.irtrans")
42 public class IRtransHandlerFactory extends BaseThingHandlerFactory {
44 public static final Set<ThingTypeUID> SUPPORTED_THING_TYPES_UIDS = Collections
45 .unmodifiableSet(Stream.of(THING_TYPE_BLASTER, THING_TYPE_ETHERNET_BRIDGE).collect(Collectors.toSet()));
48 public boolean supportsThingType(ThingTypeUID thingTypeUID) {
49 return SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID);
53 public Thing createThing(ThingTypeUID thingTypeUID, Configuration configuration, ThingUID thingUID,
55 if (IRtransBindingConstants.THING_TYPE_ETHERNET_BRIDGE.equals(thingTypeUID)) {
56 ThingUID ethernetBridgeUID = getEthernetBridgeThingUID(thingTypeUID, thingUID, configuration);
57 return super.createThing(thingTypeUID, configuration, ethernetBridgeUID, null);
59 if (IRtransBindingConstants.THING_TYPE_BLASTER.equals(thingTypeUID)) {
60 ThingUID blasterUID = getBlasterUID(thingTypeUID, thingUID, configuration, bridgeUID);
61 return super.createThing(thingTypeUID, configuration, blasterUID, bridgeUID);
63 throw new IllegalArgumentException(
64 "The thing type " + thingTypeUID + " is not supported by the IRtrans binding.");
68 protected ThingHandler createHandler(Thing thing) {
69 if (thing.getThingTypeUID().equals(IRtransBindingConstants.THING_TYPE_ETHERNET_BRIDGE)) {
70 return new EthernetBridgeHandler((Bridge) thing);
71 } else if (thing.getThingTypeUID().equals(IRtransBindingConstants.THING_TYPE_BLASTER)) {
72 return new BlasterHandler(thing);
78 private ThingUID getEthernetBridgeThingUID(ThingTypeUID thingTypeUID, ThingUID thingUID,
79 Configuration configuration) {
80 if (thingUID == null) {
81 String ipAddress = (String) configuration.get(EthernetBridgeHandler.IP_ADDRESS);
82 return new ThingUID(thingTypeUID, ipAddress);
87 private ThingUID getBlasterUID(ThingTypeUID thingTypeUID, ThingUID thingUID, Configuration configuration,
89 String ledId = (String) configuration.get(BlasterHandler.LED);
91 if (thingUID == null) {
92 return new ThingUID(thingTypeUID, "Led" + ledId, bridgeUID.getId());