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.Arrays;
18 import java.util.Collections;
19 import java.util.HashMap;
20 import java.util.HashSet;
21 import java.util.Hashtable;
25 import org.eclipse.jdt.annotation.NonNullByDefault;
26 import org.eclipse.jdt.annotation.Nullable;
27 import org.openhab.binding.tado.internal.discovery.TadoDiscoveryService;
28 import org.openhab.core.config.discovery.DiscoveryService;
29 import org.openhab.core.thing.Bridge;
30 import org.openhab.core.thing.Thing;
31 import org.openhab.core.thing.ThingTypeUID;
32 import org.openhab.core.thing.ThingUID;
33 import org.openhab.core.thing.binding.BaseThingHandlerFactory;
34 import org.openhab.core.thing.binding.ThingHandler;
35 import org.openhab.core.thing.binding.ThingHandlerFactory;
36 import org.osgi.framework.ServiceRegistration;
37 import org.osgi.service.component.annotations.Activate;
38 import org.osgi.service.component.annotations.Component;
39 import org.osgi.service.component.annotations.Reference;
42 * The {@link TadoHandlerFactory} is responsible for creating things and thing
45 * @author Dennis Frommknecht - Initial contribution
48 @Component(configurationPid = "binding.tado", service = ThingHandlerFactory.class)
49 public class TadoHandlerFactory extends BaseThingHandlerFactory {
51 private static final Set<ThingTypeUID> SUPPORTED_THING_TYPES_UIDS = Collections
52 .unmodifiableSet(new HashSet<>(Arrays.asList(THING_TYPE_HOME, THING_TYPE_ZONE, THING_TYPE_MOBILE_DEVICE)));
54 private final Map<ThingUID, ServiceRegistration<?>> discoveryServiceRegs = new HashMap<>();
56 private final TadoStateDescriptionProvider stateDescriptionProvider;
59 public TadoHandlerFactory(final @Reference TadoStateDescriptionProvider stateDescriptionProvider) {
60 this.stateDescriptionProvider = stateDescriptionProvider;
64 public boolean supportsThingType(ThingTypeUID thingTypeUID) {
65 return SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID);
69 protected @Nullable ThingHandler createHandler(Thing thing) {
70 ThingTypeUID thingTypeUID = thing.getThingTypeUID();
72 if (thingTypeUID.equals(THING_TYPE_HOME)) {
73 TadoHomeHandler tadoHomeHandler = new TadoHomeHandler((Bridge) thing);
74 registerTadoDiscoveryService(tadoHomeHandler);
75 return tadoHomeHandler;
76 } else if (thingTypeUID.equals(THING_TYPE_ZONE)) {
77 return new TadoZoneHandler(thing, stateDescriptionProvider);
78 } else if (thingTypeUID.equals(THING_TYPE_MOBILE_DEVICE)) {
79 return new TadoMobileDeviceHandler(thing);
85 private synchronized void registerTadoDiscoveryService(TadoHomeHandler tadoHomeHandler) {
86 TadoDiscoveryService discoveryService = new TadoDiscoveryService(tadoHomeHandler);
87 ServiceRegistration<?> serviceRegistration = bundleContext.registerService(DiscoveryService.class.getName(),
88 discoveryService, new Hashtable<>());
89 discoveryService.activate();
90 this.discoveryServiceRegs.put(tadoHomeHandler.getThing().getUID(), serviceRegistration);
94 protected synchronized void removeHandler(ThingHandler thingHandler) {
95 if (thingHandler instanceof TadoHomeHandler) {
96 ServiceRegistration<?> serviceReg = this.discoveryServiceRegs.remove(thingHandler.getThing().getUID());
97 if (serviceReg != null) {
98 TadoDiscoveryService service = (TadoDiscoveryService) bundleContext
99 .getService(serviceReg.getReference());
100 serviceReg.unregister();
101 if (service != null) {
102 service.deactivate();