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.evohome.internal;
15 import java.util.HashMap;
16 import java.util.Hashtable;
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.eclipse.jdt.annotation.Nullable;
21 import org.eclipse.jetty.client.HttpClient;
22 import org.openhab.binding.evohome.internal.discovery.EvohomeDiscoveryService;
23 import org.openhab.binding.evohome.internal.handler.EvohomeAccountBridgeHandler;
24 import org.openhab.binding.evohome.internal.handler.EvohomeHeatingZoneHandler;
25 import org.openhab.binding.evohome.internal.handler.EvohomeTemperatureControlSystemHandler;
26 import org.openhab.core.config.discovery.DiscoveryService;
27 import org.openhab.core.io.net.http.HttpClientFactory;
28 import org.openhab.core.thing.Bridge;
29 import org.openhab.core.thing.Thing;
30 import org.openhab.core.thing.ThingTypeUID;
31 import org.openhab.core.thing.ThingUID;
32 import org.openhab.core.thing.binding.BaseThingHandlerFactory;
33 import org.openhab.core.thing.binding.ThingHandler;
34 import org.openhab.core.thing.binding.ThingHandlerFactory;
35 import org.osgi.framework.ServiceRegistration;
36 import org.osgi.service.component.annotations.Activate;
37 import org.osgi.service.component.annotations.Component;
38 import org.osgi.service.component.annotations.Reference;
41 * Provides the thing factory for this binding
43 * @author Jasper van Zuijlen - Initial contribution
46 @Component(service = ThingHandlerFactory.class, configurationPid = "binding.evohome")
48 public class EvohomeHandlerFactory extends BaseThingHandlerFactory {
50 private final Map<ThingUID, ServiceRegistration<?>> discoveryServiceRegs = new HashMap<>();
52 private final HttpClient httpClient;
55 public EvohomeHandlerFactory(@Reference final HttpClientFactory httpClientFactory) {
56 this.httpClient = httpClientFactory.getCommonHttpClient();
60 public boolean supportsThingType(ThingTypeUID thingTypeUID) {
61 return EvohomeBindingConstants.SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID);
65 protected @Nullable ThingHandler createHandler(Thing thing) {
66 ThingTypeUID thingTypeUID = thing.getThingTypeUID();
67 if (thingTypeUID.equals(EvohomeBindingConstants.THING_TYPE_EVOHOME_ACCOUNT)) {
68 EvohomeAccountBridgeHandler bridge = new EvohomeAccountBridgeHandler((Bridge) thing, httpClient);
69 registerEvohomeDiscoveryService(bridge);
71 } else if (thingTypeUID.equals(EvohomeBindingConstants.THING_TYPE_EVOHOME_DISPLAY)) {
72 return new EvohomeTemperatureControlSystemHandler(thing);
73 } else if (thingTypeUID.equals(EvohomeBindingConstants.THING_TYPE_EVOHOME_HEATING_ZONE)) {
74 return new EvohomeHeatingZoneHandler(thing);
80 private void registerEvohomeDiscoveryService(EvohomeAccountBridgeHandler evohomeBridgeHandler) {
81 EvohomeDiscoveryService discoveryService = new EvohomeDiscoveryService(evohomeBridgeHandler);
83 this.discoveryServiceRegs.put(evohomeBridgeHandler.getThing().getUID(),
84 bundleContext.registerService(DiscoveryService.class.getName(), discoveryService, new Hashtable<>()));
88 public ThingHandler registerHandler(Thing thing) {
89 return super.registerHandler(thing);
93 protected void removeHandler(ThingHandler thingHandler) {
94 if (thingHandler instanceof EvohomeAccountBridgeHandler) {
95 ServiceRegistration<?> serviceReg = this.discoveryServiceRegs.get(thingHandler.getThing().getUID());
96 if (serviceReg != null) {
97 EvohomeDiscoveryService service = (EvohomeDiscoveryService) bundleContext
98 .getService(serviceReg.getReference());
99 if (service != null) {
100 service.deactivate();
102 serviceReg.unregister();
103 discoveryServiceRegs.remove(thingHandler.getThing().getUID());