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.max.internal.factory;
15 import static org.openhab.binding.max.internal.MaxBindingConstants.*;
17 import org.eclipse.jdt.annotation.NonNullByDefault;
18 import org.eclipse.jdt.annotation.Nullable;
19 import org.openhab.binding.max.internal.handler.MaxCubeBridgeHandler;
20 import org.openhab.binding.max.internal.handler.MaxDevicesHandler;
21 import org.openhab.core.config.core.Configuration;
22 import org.openhab.core.thing.Bridge;
23 import org.openhab.core.thing.Thing;
24 import org.openhab.core.thing.ThingTypeUID;
25 import org.openhab.core.thing.ThingUID;
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.osgi.service.component.annotations.Component;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
34 * The {@link MaxCubeHandlerFactory} is responsible for creating things and
37 * @author Marcel Verpaalen - Initial contribution
40 @Component(service = ThingHandlerFactory.class, configurationPid = "binding.max")
41 public class MaxCubeHandlerFactory extends BaseThingHandlerFactory {
43 private final Logger logger = LoggerFactory.getLogger(MaxCubeHandlerFactory.class);
46 public @Nullable Thing createThing(ThingTypeUID thingTypeUID, Configuration configuration,
47 @Nullable ThingUID thingUID, @Nullable ThingUID bridgeUID) {
48 if (CUBEBRIDGE_THING_TYPE.equals(thingTypeUID)) {
49 ThingUID cubeBridgeUID = getBridgeThingUID(thingTypeUID, thingUID, configuration);
50 return super.createThing(thingTypeUID, configuration, cubeBridgeUID, null);
52 if (supportsThingType(thingTypeUID) && bridgeUID != null) {
53 ThingUID deviceUID = getMaxCubeDeviceUID(thingTypeUID, thingUID, configuration, bridgeUID);
54 return super.createThing(thingTypeUID, configuration, deviceUID, bridgeUID);
56 throw new IllegalArgumentException("The thing type " + thingTypeUID + " is not supported by the binding.");
60 public boolean supportsThingType(ThingTypeUID thingTypeUID) {
61 return SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID);
64 private ThingUID getBridgeThingUID(ThingTypeUID thingTypeUID, @Nullable ThingUID thingUID,
65 Configuration configuration) {
66 if (thingUID == null) {
67 String serialNumber = (String) configuration.get(Thing.PROPERTY_SERIAL_NUMBER);
68 return new ThingUID(thingTypeUID, serialNumber);
73 private ThingUID getMaxCubeDeviceUID(ThingTypeUID thingTypeUID, @Nullable ThingUID thingUID,
74 Configuration configuration, ThingUID bridgeUID) {
75 if (thingUID == null) {
76 String serialNumber = (String) configuration.get(Thing.PROPERTY_SERIAL_NUMBER);
77 return new ThingUID(thingTypeUID, serialNumber, bridgeUID.getId());
83 protected @Nullable ThingHandler createHandler(Thing thing) {
84 ThingTypeUID thingTypeUID = thing.getThingTypeUID();
85 if (CUBEBRIDGE_THING_TYPE.equals(thingTypeUID)) {
86 return new MaxCubeBridgeHandler((Bridge) thing);
87 } else if (SUPPORTED_DEVICE_THING_TYPES_UIDS.contains(thingTypeUID)) {
88 return new MaxDevicesHandler(thing);
90 logger.debug("ThingHandler not found for {}", thingTypeUID);