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.hdanywhere.internal;
15 import static org.openhab.binding.hdanywhere.internal.HDanywhereBindingConstants.*;
17 import java.util.Collections;
19 import java.util.stream.Collectors;
20 import java.util.stream.Stream;
22 import org.openhab.binding.hdanywhere.internal.handler.Mhub4K431Handler;
23 import org.openhab.binding.hdanywhere.internal.handler.MultiroomPlusHandler;
24 import org.openhab.core.config.core.Configuration;
25 import org.openhab.core.thing.Thing;
26 import org.openhab.core.thing.ThingTypeUID;
27 import org.openhab.core.thing.ThingUID;
28 import org.openhab.core.thing.binding.BaseThingHandlerFactory;
29 import org.openhab.core.thing.binding.ThingHandler;
30 import org.openhab.core.thing.binding.ThingHandlerFactory;
31 import org.osgi.service.component.annotations.Component;
34 * The {@link HDanywhereHandlerFactory} is responsible for creating things and
37 * @author Karel Goderis - Initial contribution
39 @Component(service = ThingHandlerFactory.class, configurationPid = "binding.hdanywhere")
40 public class HDanywhereHandlerFactory extends BaseThingHandlerFactory {
42 private static final Set<ThingTypeUID> SUPPORTED_THING_TYPES_UIDS = Collections
43 .unmodifiableSet(Stream.of(THING_TYPE_MULTIROOMPLUS, THING_TYPE_MHUB4K431).collect(Collectors.toSet()));
46 public boolean supportsThingType(ThingTypeUID thingTypeUID) {
47 return SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID);
51 protected ThingHandler createHandler(Thing thing) {
52 ThingTypeUID thingTypeUID = thing.getThingTypeUID();
54 if (thingTypeUID.equals(THING_TYPE_MULTIROOMPLUS)) {
55 return new MultiroomPlusHandler(thing);
58 if (thingTypeUID.equals(THING_TYPE_MHUB4K431)) {
59 return new Mhub4K431Handler(thing);
66 public Thing createThing(ThingTypeUID thingTypeUID, Configuration configuration, ThingUID thingUID,
68 if (HDanywhereBindingConstants.THING_TYPE_MULTIROOMPLUS.equals(thingTypeUID)) {
69 ThingUID matrixUID = getMultiroomPlusUID(thingTypeUID, thingUID, configuration);
70 return super.createThing(thingTypeUID, configuration, matrixUID, null);
72 if (HDanywhereBindingConstants.THING_TYPE_MHUB4K431.equals(thingTypeUID)) {
73 ThingUID matrixUID = getMhub4K431UID(thingTypeUID, thingUID, configuration);
74 return super.createThing(thingTypeUID, configuration, matrixUID, null);
76 throw new IllegalArgumentException(
77 "The thing type " + thingTypeUID + " is not supported by the HDanywhere binding.");
80 private ThingUID getMultiroomPlusUID(ThingTypeUID thingTypeUID, ThingUID thingUID, Configuration configuration) {
81 String ipAddress = (String) configuration.get(MultiroomPlusHandler.IP_ADDRESS);
83 if (thingUID == null) {
84 thingUID = new ThingUID(thingTypeUID, ipAddress.replaceAll("[^A-Za-z0-9_]", "_"));
89 private ThingUID getMhub4K431UID(ThingTypeUID thingTypeUID, ThingUID thingUID, Configuration configuration) {
90 String ipAddress = (String) configuration.get(Mhub4K431Handler.IP_ADDRESS);
92 if (thingUID == null) {
93 thingUID = new ThingUID(thingTypeUID, ipAddress.replaceAll("[^A-Za-z0-9_]", "_"));