2 * Copyright (c) 2010-2020 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.nest.internal;
15 import static java.util.stream.Collectors.toSet;
16 import static org.openhab.binding.nest.internal.NestBindingConstants.*;
18 import java.util.HashMap;
19 import java.util.Hashtable;
22 import java.util.stream.Stream;
24 import javax.ws.rs.client.ClientBuilder;
26 import org.eclipse.jdt.annotation.NonNullByDefault;
27 import org.eclipse.jdt.annotation.Nullable;
28 import org.openhab.binding.nest.internal.discovery.NestDiscoveryService;
29 import org.openhab.binding.nest.internal.handler.NestBridgeHandler;
30 import org.openhab.binding.nest.internal.handler.NestCameraHandler;
31 import org.openhab.binding.nest.internal.handler.NestSmokeDetectorHandler;
32 import org.openhab.binding.nest.internal.handler.NestStructureHandler;
33 import org.openhab.binding.nest.internal.handler.NestThermostatHandler;
34 import org.openhab.core.config.discovery.DiscoveryService;
35 import org.openhab.core.thing.Bridge;
36 import org.openhab.core.thing.Thing;
37 import org.openhab.core.thing.ThingTypeUID;
38 import org.openhab.core.thing.ThingUID;
39 import org.openhab.core.thing.binding.BaseThingHandlerFactory;
40 import org.openhab.core.thing.binding.ThingHandler;
41 import org.openhab.core.thing.binding.ThingHandlerFactory;
42 import org.osgi.framework.ServiceRegistration;
43 import org.osgi.service.component.annotations.Activate;
44 import org.osgi.service.component.annotations.Component;
45 import org.osgi.service.component.annotations.Reference;
46 import org.osgi.service.jaxrs.client.SseEventSourceFactory;
49 * The {@link NestHandlerFactory} is responsible for creating things and thing
50 * handlers. It also sets up the discovery service to track things from the bridge
51 * when the bridge is created.
53 * @author David Bennett - Initial contribution
56 @Component(service = ThingHandlerFactory.class, configurationPid = "binding.nest")
57 public class NestHandlerFactory extends BaseThingHandlerFactory {
58 private static final Set<ThingTypeUID> SUPPORTED_THING_TYPES_UIDS = Stream.of(THING_TYPE_THERMOSTAT,
59 THING_TYPE_CAMERA, THING_TYPE_BRIDGE, THING_TYPE_STRUCTURE, THING_TYPE_SMOKE_DETECTOR).collect(toSet());
61 private final ClientBuilder clientBuilder;
62 private final SseEventSourceFactory eventSourceFactory;
63 private final Map<ThingUID, ServiceRegistration<?>> discoveryService = new HashMap<>();
66 public NestHandlerFactory(@Reference ClientBuilder clientBuilder,
67 @Reference SseEventSourceFactory eventSourceFactory) {
68 this.clientBuilder = clientBuilder;
69 this.eventSourceFactory = eventSourceFactory;
73 * The things this factory supports creating.
76 public boolean supportsThingType(ThingTypeUID thingTypeUID) {
77 return SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID);
81 * Creates a handler for the specific thing. THis also creates the discovery service
82 * when the bridge is created.
85 protected @Nullable ThingHandler createHandler(Thing thing) {
86 ThingTypeUID thingTypeUID = thing.getThingTypeUID();
88 if (THING_TYPE_THERMOSTAT.equals(thingTypeUID)) {
89 return new NestThermostatHandler(thing);
92 if (THING_TYPE_CAMERA.equals(thingTypeUID)) {
93 return new NestCameraHandler(thing);
96 if (THING_TYPE_STRUCTURE.equals(thingTypeUID)) {
97 return new NestStructureHandler(thing);
100 if (THING_TYPE_SMOKE_DETECTOR.equals(thingTypeUID)) {
101 return new NestSmokeDetectorHandler(thing);
104 if (THING_TYPE_BRIDGE.equals(thingTypeUID)) {
105 NestBridgeHandler handler = new NestBridgeHandler((Bridge) thing, clientBuilder, eventSourceFactory);
106 NestDiscoveryService service = new NestDiscoveryService(handler);
108 // Register the discovery service.
109 discoveryService.put(handler.getThing().getUID(),
110 bundleContext.registerService(DiscoveryService.class.getName(), service, new Hashtable<>()));
118 * Removes the handler for the specific thing. This also handles disabling the discovery
119 * service when the bridge is removed.
122 protected void removeHandler(ThingHandler thingHandler) {
123 if (thingHandler instanceof NestBridgeHandler) {
124 ServiceRegistration<?> reg = discoveryService.get(thingHandler.getThing().getUID());
126 // Unregister the discovery service.
127 NestDiscoveryService service = (NestDiscoveryService) bundleContext.getService(reg.getReference());
128 service.deactivate();
130 discoveryService.remove(thingHandler.getThing().getUID());
133 super.removeHandler(thingHandler);