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.harmonyhub.internal;
15 import java.util.HashMap;
16 import java.util.Hashtable;
19 import java.util.stream.Collectors;
20 import java.util.stream.Stream;
22 import org.eclipse.jdt.annotation.NonNullByDefault;
23 import org.eclipse.jdt.annotation.Nullable;
24 import org.eclipse.jetty.client.HttpClient;
25 import org.openhab.binding.harmonyhub.internal.discovery.HarmonyDeviceDiscoveryService;
26 import org.openhab.binding.harmonyhub.internal.handler.HarmonyDeviceHandler;
27 import org.openhab.binding.harmonyhub.internal.handler.HarmonyHubHandler;
28 import org.openhab.core.config.discovery.DiscoveryService;
29 import org.openhab.core.io.net.http.HttpClientFactory;
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.Activate;
39 import org.osgi.service.component.annotations.Component;
40 import org.osgi.service.component.annotations.Reference;
43 * The {@link HarmonyHubHandlerFactory} is responsible for creating things and thing
46 * @author Dan Cunningham - Initial contribution
47 * @author Wouter Born - Add null annotations
50 @Component(service = { ThingHandlerFactory.class }, configurationPid = "binding.harmonyhub")
51 public class HarmonyHubHandlerFactory extends BaseThingHandlerFactory {
53 private static final Set<ThingTypeUID> SUPPORTED_THING_TYPES_UIDS = Stream
54 .concat(HarmonyHubHandler.SUPPORTED_THING_TYPES_UIDS.stream(),
55 HarmonyDeviceHandler.SUPPORTED_THING_TYPES_UIDS.stream())
56 .collect(Collectors.toSet());
58 private final Map<ThingUID, ServiceRegistration<?>> discoveryServiceRegs = new HashMap<>();
59 private final HttpClient httpClient;
61 private final HarmonyHubDynamicTypeProvider dynamicTypeProvider;
64 public HarmonyHubHandlerFactory(@Reference final HttpClientFactory httpClientFactory,
65 @Reference HarmonyHubDynamicTypeProvider dynamicTypeProvider) {
66 this.httpClient = httpClientFactory.getCommonHttpClient();
67 this.dynamicTypeProvider = dynamicTypeProvider;
71 public boolean supportsThingType(ThingTypeUID thingTypeUID) {
72 return SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID);
76 protected @Nullable ThingHandler createHandler(Thing thing) {
77 ThingTypeUID thingTypeUID = thing.getThingTypeUID();
79 if (thingTypeUID.equals(HarmonyHubBindingConstants.HARMONY_HUB_THING_TYPE)) {
80 HarmonyHubHandler harmonyHubHandler = new HarmonyHubHandler((Bridge) thing, dynamicTypeProvider,
82 registerHarmonyDeviceDiscoveryService(harmonyHubHandler);
83 return harmonyHubHandler;
86 if (thingTypeUID.equals(HarmonyHubBindingConstants.HARMONY_DEVICE_THING_TYPE)) {
87 return new HarmonyDeviceHandler(thing, dynamicTypeProvider);
94 protected synchronized void removeHandler(ThingHandler thingHandler) {
95 if (thingHandler instanceof HarmonyHubHandler) {
96 ServiceRegistration<?> serviceReg = this.discoveryServiceRegs.remove(thingHandler.getThing().getUID());
97 if (serviceReg != null) {
98 serviceReg.unregister();
104 * Adds HarmonyHubHandler to the discovery service to find Harmony Devices
106 * @param harmonyHubHandler
108 private synchronized void registerHarmonyDeviceDiscoveryService(HarmonyHubHandler harmonyHubHandler) {
109 HarmonyDeviceDiscoveryService discoveryService = new HarmonyDeviceDiscoveryService(harmonyHubHandler);
110 this.discoveryServiceRegs.put(harmonyHubHandler.getThing().getUID(),
111 bundleContext.registerService(DiscoveryService.class.getName(), discoveryService, new Hashtable<>()));