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.plugwiseha.internal;
15 import org.eclipse.jdt.annotation.NonNullByDefault;
16 import org.eclipse.jdt.annotation.Nullable;
17 import org.eclipse.jetty.client.HttpClient;
18 import org.openhab.binding.plugwiseha.internal.handler.PlugwiseHAApplianceHandler;
19 import org.openhab.binding.plugwiseha.internal.handler.PlugwiseHABridgeHandler;
20 import org.openhab.binding.plugwiseha.internal.handler.PlugwiseHAZoneHandler;
21 import org.openhab.core.config.core.Configuration;
22 import org.openhab.core.io.net.http.HttpClientFactory;
23 import org.openhab.core.thing.Bridge;
24 import org.openhab.core.thing.Thing;
25 import org.openhab.core.thing.ThingTypeUID;
26 import org.openhab.core.thing.ThingUID;
27 import org.openhab.core.thing.binding.BaseThingHandlerFactory;
28 import org.openhab.core.thing.binding.ThingHandler;
29 import org.openhab.core.thing.binding.ThingHandlerFactory;
30 import org.osgi.service.component.annotations.Activate;
31 import org.osgi.service.component.annotations.Component;
32 import org.osgi.service.component.annotations.Reference;
35 * The {@link PlugwiseHAHandlerFactory} is responsible for creating things and
38 * @author Bas van Wetten - Initial contribution
39 * @author Leo Siepel - finish initial contribution
43 @Component(service = ThingHandlerFactory.class, configurationPid = "binding.plugwiseha")
44 public class PlugwiseHAHandlerFactory extends BaseThingHandlerFactory {
46 private final HttpClient httpClient;
51 public PlugwiseHAHandlerFactory(@Reference final HttpClientFactory httpClientFactory) {
52 this.httpClient = httpClientFactory.getCommonHttpClient();
58 * Returns whether the handler is able to create a thing or register a thing
59 * handler for the given type.
61 * @param thingTypeUID the thing type UID
62 * @return true, if the handler supports the thing type, false otherwise
65 public boolean supportsThingType(ThingTypeUID thingTypeUID) {
66 return (PlugwiseHABridgeHandler.supportsThingType(thingTypeUID)
67 || PlugwiseHAZoneHandler.supportsThingType(thingTypeUID))
68 || PlugwiseHAApplianceHandler.supportsThingType(thingTypeUID);
72 * Creates a thing for given arguments.
74 * @param thingTypeUID thing type uid (not null)
75 * @param configuration configuration
76 * @param thingUID thing uid, which can be null
77 * @param bridgeUID bridge uid, which can be null
78 * @return created thing
81 public @Nullable Thing createThing(ThingTypeUID thingTypeUID, Configuration configuration,
82 @Nullable ThingUID thingUID, @Nullable ThingUID bridgeUID) {
83 if (PlugwiseHABridgeHandler.supportsThingType(thingTypeUID)) {
84 return super.createThing(thingTypeUID, configuration, thingUID, null);
85 } else if (PlugwiseHAZoneHandler.supportsThingType(thingTypeUID)) {
86 return super.createThing(thingTypeUID, configuration, thingUID, bridgeUID);
87 } else if (PlugwiseHAApplianceHandler.supportsThingType(thingTypeUID)) {
88 return super.createThing(thingTypeUID, configuration, thingUID, bridgeUID);
91 throw new IllegalArgumentException(
92 "The thing type " + thingTypeUID + " is not supported by the plugwiseha binding.");
95 // Protected and private methods
98 * Creates a {@link ThingHandler} for the given thing.
100 * @param thing the thing
101 * @return thing the created handler
104 protected @Nullable ThingHandler createHandler(Thing thing) {
105 ThingTypeUID thingTypeUID = thing.getThingTypeUID();
107 if (PlugwiseHABridgeHandler.supportsThingType(thingTypeUID)) {
108 return new PlugwiseHABridgeHandler((Bridge) thing, this.httpClient);
109 } else if (PlugwiseHAZoneHandler.supportsThingType(thingTypeUID)) {
110 return new PlugwiseHAZoneHandler(thing);
111 } else if (PlugwiseHAApplianceHandler.supportsThingType(thingTypeUID)) {
112 return new PlugwiseHAApplianceHandler(thing);