2 * Copyright (c) 2010-2022 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.tado.internal.handler;
15 import static org.openhab.binding.tado.internal.TadoBindingConstants.*;
17 import java.util.Arrays;
18 import java.util.Collections;
19 import java.util.HashMap;
20 import java.util.HashSet;
21 import java.util.Hashtable;
25 import org.openhab.binding.tado.internal.discovery.TadoDiscoveryService;
26 import org.openhab.core.config.discovery.DiscoveryService;
27 import org.openhab.core.thing.Bridge;
28 import org.openhab.core.thing.Thing;
29 import org.openhab.core.thing.ThingTypeUID;
30 import org.openhab.core.thing.ThingUID;
31 import org.openhab.core.thing.binding.BaseThingHandlerFactory;
32 import org.openhab.core.thing.binding.ThingHandler;
33 import org.openhab.core.thing.binding.ThingHandlerFactory;
34 import org.osgi.framework.ServiceRegistration;
35 import org.osgi.service.component.annotations.Activate;
36 import org.osgi.service.component.annotations.Component;
37 import org.osgi.service.component.annotations.Reference;
40 * The {@link TadoHandlerFactory} is responsible for creating things and thing
43 * @author Dennis Frommknecht - Initial contribution
45 @Component(configurationPid = "binding.tado", service = ThingHandlerFactory.class)
46 public class TadoHandlerFactory extends BaseThingHandlerFactory {
48 private static final Set<ThingTypeUID> SUPPORTED_THING_TYPES_UIDS = Collections
49 .unmodifiableSet(new HashSet<>(Arrays.asList(THING_TYPE_HOME, THING_TYPE_ZONE, THING_TYPE_MOBILE_DEVICE)));
51 private final Map<ThingUID, ServiceRegistration<?>> discoveryServiceRegs = new HashMap<>();
53 private final TadoStateDescriptionProvider stateDescriptionProvider;
56 public TadoHandlerFactory(final @Reference TadoStateDescriptionProvider stateDescriptionProvider) {
57 this.stateDescriptionProvider = stateDescriptionProvider;
61 public boolean supportsThingType(ThingTypeUID thingTypeUID) {
62 return SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID);
66 protected ThingHandler createHandler(Thing thing) {
67 ThingTypeUID thingTypeUID = thing.getThingTypeUID();
69 if (thingTypeUID.equals(THING_TYPE_HOME)) {
70 TadoHomeHandler tadoHomeHandler = new TadoHomeHandler((Bridge) thing);
71 registerTadoDiscoveryService(tadoHomeHandler);
72 return tadoHomeHandler;
73 } else if (thingTypeUID.equals(THING_TYPE_ZONE)) {
74 return new TadoZoneHandler(thing, stateDescriptionProvider);
75 } else if (thingTypeUID.equals(THING_TYPE_MOBILE_DEVICE)) {
76 return new TadoMobileDeviceHandler(thing);
82 private synchronized void registerTadoDiscoveryService(TadoHomeHandler tadoHomeHandler) {
83 TadoDiscoveryService discoveryService = new TadoDiscoveryService(tadoHomeHandler);
84 ServiceRegistration<?> serviceRegistration = bundleContext.registerService(DiscoveryService.class.getName(),
85 discoveryService, new Hashtable<>());
86 discoveryService.activate();
87 this.discoveryServiceRegs.put(tadoHomeHandler.getThing().getUID(), serviceRegistration);
91 protected synchronized void removeHandler(ThingHandler thingHandler) {
92 if (thingHandler instanceof TadoHomeHandler) {
93 ServiceRegistration<?> serviceReg = this.discoveryServiceRegs.remove(thingHandler.getThing().getUID());
94 if (serviceReg != null) {
95 TadoDiscoveryService service = (TadoDiscoveryService) bundleContext
96 .getService(serviceReg.getReference());
97 serviceReg.unregister();
98 if (service != null) {