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.bondhome.internal;
15 import static org.openhab.binding.bondhome.internal.BondHomeBindingConstants.*;
17 import java.util.HashMap;
20 import org.eclipse.jdt.annotation.NonNullByDefault;
21 import org.eclipse.jdt.annotation.Nullable;
22 import org.openhab.binding.bondhome.internal.handler.BondBridgeHandler;
23 import org.openhab.binding.bondhome.internal.handler.BondDeviceHandler;
24 import org.openhab.core.io.net.http.HttpClientFactory;
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.framework.ServiceRegistration;
33 import org.osgi.service.component.ComponentContext;
34 import org.osgi.service.component.annotations.Activate;
35 import org.osgi.service.component.annotations.Component;
36 import org.osgi.service.component.annotations.Reference;
39 * The {@link BondHomeHandlerFactory} is responsible for creating things and thing
42 * @author Sara Geleskie Damiano - Initial contribution
45 @Component(configurationPid = "binding.bondhome", service = ThingHandlerFactory.class)
46 public class BondHomeHandlerFactory extends BaseThingHandlerFactory {
47 private Map<ThingUID, ServiceRegistration<?>> discoveryServiceRegs = new HashMap<>();
48 private final HttpClientFactory httpClientFactory;
51 public BondHomeHandlerFactory(final @Reference HttpClientFactory httpClientFactory,
52 ComponentContext componentContext) {
53 super.activate(componentContext);
54 this.httpClientFactory = httpClientFactory;
58 public boolean supportsThingType(ThingTypeUID thingTypeUID) {
59 return SUPPORTED_BRIDGE_TYPES.contains(thingTypeUID) || SUPPORTED_THING_TYPES.contains(thingTypeUID);
63 protected @Nullable ThingHandler createHandler(Thing thing) {
64 ThingTypeUID thingTypeUID = thing.getThingTypeUID();
66 if (THING_TYPE_BOND_BRIDGE.equals(thingTypeUID)) {
67 final BondBridgeHandler handler = new BondBridgeHandler((Bridge) thing, httpClientFactory);
69 } else if (SUPPORTED_THING_TYPES.contains(thingTypeUID)) {
70 return new BondDeviceHandler(thing);
77 protected synchronized void removeHandler(ThingHandler thingHandler) {
78 if (thingHandler instanceof BondBridgeHandler) {
79 ServiceRegistration<?> serviceReg = this.discoveryServiceRegs.remove(thingHandler.getThing().getUID());
80 if (serviceReg != null) {
81 serviceReg.unregister();