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.elroconnects.internal;
15 import static org.openhab.binding.elroconnects.internal.ElroConnectsBindingConstants.*;
17 import org.eclipse.jdt.annotation.NonNullByDefault;
18 import org.eclipse.jdt.annotation.Nullable;
19 import org.eclipse.jetty.client.HttpClient;
20 import org.eclipse.jetty.client.WWWAuthenticationProtocolHandler;
21 import org.openhab.binding.elroconnects.internal.handler.ElroConnectsAccountHandler;
22 import org.openhab.binding.elroconnects.internal.handler.ElroConnectsBridgeHandler;
23 import org.openhab.binding.elroconnects.internal.handler.ElroConnectsCOAlarmHandler;
24 import org.openhab.binding.elroconnects.internal.handler.ElroConnectsDeviceHandler;
25 import org.openhab.binding.elroconnects.internal.handler.ElroConnectsEntrySensorHandler;
26 import org.openhab.binding.elroconnects.internal.handler.ElroConnectsHeatAlarmHandler;
27 import org.openhab.binding.elroconnects.internal.handler.ElroConnectsMotionSensorHandler;
28 import org.openhab.binding.elroconnects.internal.handler.ElroConnectsPowerSocketHandler;
29 import org.openhab.binding.elroconnects.internal.handler.ElroConnectsSmokeAlarmHandler;
30 import org.openhab.binding.elroconnects.internal.handler.ElroConnectsWaterAlarmHandler;
31 import org.openhab.core.io.net.http.HttpClientFactory;
32 import org.openhab.core.io.net.http.HttpClientInitializationException;
33 import org.openhab.core.net.NetworkAddressService;
34 import org.openhab.core.thing.Bridge;
35 import org.openhab.core.thing.Thing;
36 import org.openhab.core.thing.ThingTypeUID;
37 import org.openhab.core.thing.binding.BaseThingHandlerFactory;
38 import org.openhab.core.thing.binding.ThingHandler;
39 import org.openhab.core.thing.binding.ThingHandlerFactory;
40 import org.osgi.service.component.annotations.Activate;
41 import org.osgi.service.component.annotations.Component;
42 import org.osgi.service.component.annotations.Deactivate;
43 import org.osgi.service.component.annotations.Reference;
44 import org.slf4j.Logger;
45 import org.slf4j.LoggerFactory;
48 * The {@link ElroConnectsHandlerFactory} is responsible for creating things and thing
51 * @author Mark Herwege - Initial contribution
54 @Component(configurationPid = "binding.elroconnects", service = ThingHandlerFactory.class)
55 public class ElroConnectsHandlerFactory extends BaseThingHandlerFactory {
56 private final Logger logger = LoggerFactory.getLogger(ElroConnectsHandlerFactory.class);
58 private final HttpClientFactory httpClientFactory;
59 private final NetworkAddressService networkAddressService;
60 private final ElroConnectsDynamicStateDescriptionProvider dynamicStateDescriptionProvider;
62 private @Nullable HttpClient httpClient;
65 public ElroConnectsHandlerFactory(final @Reference HttpClientFactory httpClientFactory,
66 final @Reference NetworkAddressService networkAddressService,
67 final @Reference ElroConnectsDynamicStateDescriptionProvider dynamicStateDescriptionProvider) {
68 this.httpClientFactory = httpClientFactory;
69 this.networkAddressService = networkAddressService;
70 this.dynamicStateDescriptionProvider = dynamicStateDescriptionProvider;
74 public void deactivate() {
75 HttpClient client = httpClient;
80 } catch (Exception e) {
81 // catching exception is necessary due to the signature of HttpClient.stop()
82 logger.debug("Failed to stop http client: {}", e.getMessage());
89 public boolean supportsThingType(ThingTypeUID thingTypeUID) {
90 return SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID);
94 protected @Nullable ThingHandler createHandler(Thing thing) {
95 switch (thing.getThingTypeUID().getId()) {
97 return createElroConnectsAccountHandler(thing);
99 return new ElroConnectsBridgeHandler((Bridge) thing, networkAddressService,
100 dynamicStateDescriptionProvider);
101 case TYPE_SMOKEALARM:
102 return new ElroConnectsSmokeAlarmHandler(thing);
103 case TYPE_WATERALARM:
104 return new ElroConnectsWaterAlarmHandler(thing);
106 return new ElroConnectsCOAlarmHandler(thing);
108 return new ElroConnectsHeatAlarmHandler(thing);
109 case TYPE_ENTRYSENSOR:
110 return new ElroConnectsEntrySensorHandler(thing);
111 case TYPE_MOTIONSENSOR:
112 return new ElroConnectsMotionSensorHandler(thing);
113 case TYPE_POWERSOCKET:
114 return new ElroConnectsPowerSocketHandler(thing);
116 return new ElroConnectsDeviceHandler(thing);
122 private ThingHandler createElroConnectsAccountHandler(Thing thing) {
123 // Create and start the httpClient for the first ElroConnectsAccountHandler that gets created. We cannot use the
124 // common http client because we need to disable the authentication protocol handler.
125 HttpClient client = httpClient;
126 if (client == null) {
127 client = httpClientFactory.createHttpClient(BINDING_ID);
133 // The getAccessToken call in the ElroConnectsAccountHandler returns an invalid 401 response on
134 // authentication error, missing the www-authentication header. This header should be there according to
135 // RFC7235. This workaround removes the protocol handler and the check.
136 client.getProtocolHandlers().remove(WWWAuthenticationProtocolHandler.NAME);
137 } catch (Exception e) {
138 // catching exception is necessary due to the signature of HttpClient.start()
139 logger.debug("Failed to start http client: {}", e.getMessage());
140 throw new HttpClientInitializationException("Could not initialize HttpClient", e);
143 return new ElroConnectsAccountHandler((Bridge) thing, client);