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.sensibo.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.eclipse.jetty.client.HttpClient;
26 import org.openhab.binding.sensibo.internal.discovery.SensiboDiscoveryService;
27 import org.openhab.binding.sensibo.internal.handler.SensiboAccountHandler;
28 import org.openhab.binding.sensibo.internal.handler.SensiboSkyHandler;
29 import org.openhab.core.config.discovery.DiscoveryService;
30 import org.openhab.core.io.net.http.HttpClientFactory;
31 import org.openhab.core.thing.Bridge;
32 import org.openhab.core.thing.Thing;
33 import org.openhab.core.thing.ThingTypeUID;
34 import org.openhab.core.thing.ThingUID;
35 import org.openhab.core.thing.binding.BaseThingHandlerFactory;
36 import org.openhab.core.thing.binding.ThingHandler;
37 import org.openhab.core.thing.binding.ThingHandlerFactory;
38 import org.osgi.framework.ServiceRegistration;
39 import org.osgi.service.component.annotations.Activate;
40 import org.osgi.service.component.annotations.Component;
41 import org.osgi.service.component.annotations.Reference;
44 * The {@link SensiboHandlerFactory} is responsible for creating things and thing
47 * @author Arne Seime - Initial contribution
50 @Component(configurationPid = "binding.sensibo", service = ThingHandlerFactory.class)
51 public class SensiboHandlerFactory extends BaseThingHandlerFactory {
52 public static final Set<ThingTypeUID> SUPPORTED_THING_TYPES_UIDS = Collections.unmodifiableSet(
53 Stream.of(SensiboBindingConstants.THING_TYPE_ACCOUNT, SensiboBindingConstants.THING_TYPE_SENSIBOSKY)
54 .collect(Collectors.toSet()));
55 private final HttpClient httpClient;
56 private Map<ThingUID, ServiceRegistration<?>> discoveryServiceRegs = new HashMap<>();
59 public SensiboHandlerFactory(@Reference HttpClientFactory httpClientFactory) {
60 this.httpClient = httpClientFactory.getCommonHttpClient();
64 protected @Nullable ThingHandler createHandler(final Thing thing) {
65 final ThingTypeUID thingTypeUID = thing.getThingTypeUID();
66 if (SensiboBindingConstants.THING_TYPE_SENSIBOSKY.equals(thingTypeUID)) {
67 return new SensiboSkyHandler(thing);
68 } else if (SensiboBindingConstants.THING_TYPE_ACCOUNT.equals(thingTypeUID)) {
69 final SensiboAccountHandler handler = new SensiboAccountHandler((Bridge) thing, httpClient);
70 registerDeviceDiscoveryService(handler);
76 private void registerDeviceDiscoveryService(SensiboAccountHandler bridgeHandler) {
77 SensiboDiscoveryService discoveryService = new SensiboDiscoveryService(bridgeHandler);
78 discoveryServiceRegs.put(bridgeHandler.getThing().getUID(),
79 bundleContext.registerService(DiscoveryService.class.getName(), discoveryService, new Hashtable<>()));
82 private void unregisterDeviceDiscoveryService(ThingUID thingUID) {
83 if (discoveryServiceRegs.containsKey(thingUID)) {
84 ServiceRegistration<?> serviceReg = discoveryServiceRegs.get(thingUID);
85 serviceReg.unregister();
86 discoveryServiceRegs.remove(thingUID);
91 protected void removeHandler(ThingHandler thingHandler) {
92 if (thingHandler instanceof SensiboAccountHandler) {
93 ThingUID thingUID = thingHandler.getThing().getUID();
94 unregisterDeviceDiscoveryService(thingUID);
96 super.removeHandler(thingHandler);
100 public boolean supportsThingType(final ThingTypeUID thingTypeUID) {
101 return SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID);