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.zway.internal;
15 import java.util.Collections;
16 import java.util.HashMap;
17 import java.util.Hashtable;
20 import java.util.stream.Collectors;
21 import java.util.stream.Stream;
23 import org.eclipse.jdt.annotation.NonNullByDefault;
24 import org.eclipse.jdt.annotation.Nullable;
25 import org.openhab.binding.zway.internal.discovery.ZWayDeviceDiscoveryService;
26 import org.openhab.binding.zway.internal.handler.ZWayBridgeHandler;
27 import org.openhab.binding.zway.internal.handler.ZWayZAutomationDeviceHandler;
28 import org.openhab.binding.zway.internal.handler.ZWayZWaveDeviceHandler;
29 import org.openhab.core.config.discovery.DiscoveryService;
30 import org.openhab.core.thing.Bridge;
31 import org.openhab.core.thing.Thing;
32 import org.openhab.core.thing.ThingTypeUID;
33 import org.openhab.core.thing.ThingUID;
34 import org.openhab.core.thing.binding.BaseThingHandlerFactory;
35 import org.openhab.core.thing.binding.ThingHandler;
36 import org.openhab.core.thing.binding.ThingHandlerFactory;
37 import org.osgi.framework.ServiceRegistration;
38 import org.osgi.service.component.annotations.Component;
41 * The {@link ZWayHandlerFactory} is responsible for creating things and thing
44 * @author Patrick Hecker - Initial contribution
47 @Component(service = ThingHandlerFactory.class, configurationPid = "binding.zway")
48 public class ZWayHandlerFactory extends BaseThingHandlerFactory {
50 private static final Set<ThingTypeUID> SUPPORTED_THING_TYPES_UIDS = Collections.unmodifiableSet(
51 Stream.of(ZWayBridgeHandler.SUPPORTED_THING_TYPE, ZWayZAutomationDeviceHandler.SUPPORTED_THING_TYPE,
52 ZWayZWaveDeviceHandler.SUPPORTED_THING_TYPE).collect(Collectors.toSet()));
54 private final Map<ThingUID, ServiceRegistration<?>> discoveryServiceRegs = new HashMap<>();
57 public boolean supportsThingType(ThingTypeUID thingTypeUID) {
58 return SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID);
62 protected @Nullable ThingHandler createHandler(Thing thing) {
63 if (ZWayBridgeHandler.SUPPORTED_THING_TYPE.equals(thing.getThingTypeUID())) {
64 ZWayBridgeHandler handler = new ZWayBridgeHandler((Bridge) thing);
65 registerDeviceDiscoveryService(handler);
68 } else if (ZWayZAutomationDeviceHandler.SUPPORTED_THING_TYPE.equals(thing.getThingTypeUID())) {
69 return new ZWayZAutomationDeviceHandler(thing);
70 } else if (ZWayZWaveDeviceHandler.SUPPORTED_THING_TYPE.equals(thing.getThingTypeUID())) {
71 return new ZWayZWaveDeviceHandler(thing);
78 protected synchronized void removeHandler(ThingHandler thingHandler) {
79 if (thingHandler instanceof ZWayBridgeHandler) {
80 ServiceRegistration<?> serviceReg = this.discoveryServiceRegs.remove(thingHandler.getThing().getUID());
81 if (serviceReg != null) {
82 serviceReg.unregister();
87 private synchronized void registerDeviceDiscoveryService(ZWayBridgeHandler handler) {
88 ZWayDeviceDiscoveryService discoveryService = new ZWayDeviceDiscoveryService(handler);
89 this.discoveryServiceRegs.put(handler.getThing().getUID(),
90 bundleContext.registerService(DiscoveryService.class.getName(), discoveryService, new Hashtable<>()));