]> git.basschouten.com Git - openhab-addons.git/blob
7b0678108471e15393dc949e36327fbc520ea6f1
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
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
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.tado.internal.handler;
14
15 import static org.openhab.binding.tado.internal.TadoBindingConstants.*;
16
17 import java.util.HashMap;
18 import java.util.Hashtable;
19 import java.util.Map;
20 import java.util.Set;
21
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;
37
38 /**
39  * The {@link TadoHandlerFactory} is responsible for creating things and thing
40  * handlers.
41  *
42  * @author Dennis Frommknecht - Initial contribution
43  */
44 @NonNullByDefault
45 @Component(configurationPid = "binding.tado", service = ThingHandlerFactory.class)
46 public class TadoHandlerFactory extends BaseThingHandlerFactory {
47
48     private static final Set<ThingTypeUID> SUPPORTED_THING_TYPES_UIDS = Set.of(THING_TYPE_HOME, THING_TYPE_ZONE,
49             THING_TYPE_MOBILE_DEVICE);
50
51     private final Map<ThingUID, ServiceRegistration<?>> discoveryServiceRegs = new HashMap<>();
52
53     private final TadoStateDescriptionProvider stateDescriptionProvider;
54
55     @Activate
56     public TadoHandlerFactory(final @Reference TadoStateDescriptionProvider stateDescriptionProvider) {
57         this.stateDescriptionProvider = stateDescriptionProvider;
58     }
59
60     @Override
61     public boolean supportsThingType(ThingTypeUID thingTypeUID) {
62         return SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID);
63     }
64
65     @Override
66     protected @Nullable ThingHandler createHandler(Thing thing) {
67         ThingTypeUID thingTypeUID = thing.getThingTypeUID();
68
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);
77         }
78
79         return null;
80     }
81
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);
88     }
89
90     @Override
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) {
99                     service.deactivate();
100                 }
101             }
102         }
103     }
104 }