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.tado.internal.handler;
15 import static org.openhab.binding.tado.internal.TadoBindingConstants.*;
17 import java.util.HashMap;
18 import java.util.Hashtable;
22 import org.eclipse.jdt.annotation.NonNullByDefault;
23 import org.eclipse.jdt.annotation.Nullable;
24 import org.openhab.binding.tado.internal.discovery.TadoDiscoveryService;
25 import org.openhab.core.config.discovery.DiscoveryService;
26 import org.openhab.core.thing.Bridge;
27 import org.openhab.core.thing.Thing;
28 import org.openhab.core.thing.ThingTypeUID;
29 import org.openhab.core.thing.ThingUID;
30 import org.openhab.core.thing.binding.BaseThingHandlerFactory;
31 import org.openhab.core.thing.binding.ThingHandler;
32 import org.openhab.core.thing.binding.ThingHandlerFactory;
33 import org.osgi.framework.ServiceRegistration;
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 TadoHandlerFactory} is responsible for creating things and thing
42 * @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 = Set.of(THING_TYPE_HOME, THING_TYPE_ZONE,
49 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 @Nullable 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) {