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.netatmo.internal;
15 import java.util.ArrayList;
16 import java.util.List;
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.netatmo.internal.api.data.ModuleType;
23 import org.openhab.binding.netatmo.internal.config.BindingConfiguration;
24 import org.openhab.binding.netatmo.internal.deserialization.NADeserializer;
25 import org.openhab.binding.netatmo.internal.handler.ApiBridgeHandler;
26 import org.openhab.binding.netatmo.internal.handler.CommonInterface;
27 import org.openhab.binding.netatmo.internal.handler.DeviceHandler;
28 import org.openhab.binding.netatmo.internal.handler.ModuleHandler;
29 import org.openhab.binding.netatmo.internal.handler.capability.AirCareCapability;
30 import org.openhab.binding.netatmo.internal.handler.capability.CameraCapability;
31 import org.openhab.binding.netatmo.internal.handler.capability.Capability;
32 import org.openhab.binding.netatmo.internal.handler.capability.ChannelHelperCapability;
33 import org.openhab.binding.netatmo.internal.handler.capability.DeviceCapability;
34 import org.openhab.binding.netatmo.internal.handler.capability.DoorbellCapability;
35 import org.openhab.binding.netatmo.internal.handler.capability.HomeCapability;
36 import org.openhab.binding.netatmo.internal.handler.capability.MeasureCapability;
37 import org.openhab.binding.netatmo.internal.handler.capability.PersonCapability;
38 import org.openhab.binding.netatmo.internal.handler.capability.PresenceCapability;
39 import org.openhab.binding.netatmo.internal.handler.capability.RoomCapability;
40 import org.openhab.binding.netatmo.internal.handler.capability.SmokeCapability;
41 import org.openhab.binding.netatmo.internal.handler.capability.WeatherCapability;
42 import org.openhab.binding.netatmo.internal.handler.channelhelper.ChannelHelper;
43 import org.openhab.binding.netatmo.internal.providers.NetatmoDescriptionProvider;
44 import org.openhab.core.config.core.ConfigParser;
45 import org.openhab.core.io.net.http.HttpClientFactory;
46 import org.openhab.core.thing.Bridge;
47 import org.openhab.core.thing.Thing;
48 import org.openhab.core.thing.ThingTypeUID;
49 import org.openhab.core.thing.binding.BaseThingHandler;
50 import org.openhab.core.thing.binding.BaseThingHandlerFactory;
51 import org.openhab.core.thing.binding.ThingHandler;
52 import org.openhab.core.thing.binding.ThingHandlerFactory;
53 import org.osgi.service.component.annotations.Activate;
54 import org.osgi.service.component.annotations.Component;
55 import org.osgi.service.component.annotations.Modified;
56 import org.osgi.service.component.annotations.Reference;
57 import org.osgi.service.http.HttpService;
58 import org.slf4j.Logger;
59 import org.slf4j.LoggerFactory;
62 * The {@link NetatmoHandlerFactory} is responsible for creating things and
65 * @author Gaƫl L'hopital - Initial contribution
68 @Component(service = ThingHandlerFactory.class, configurationPid = "binding.netatmo")
69 public class NetatmoHandlerFactory extends BaseThingHandlerFactory {
70 private final Logger logger = LoggerFactory.getLogger(NetatmoHandlerFactory.class);
72 private final BindingConfiguration configuration = new BindingConfiguration();
73 private final NetatmoDescriptionProvider stateDescriptionProvider;
74 private final NADeserializer deserializer;
75 private final HttpClient httpClient;
76 private final HttpService httpService;
79 public NetatmoHandlerFactory(@Reference NetatmoDescriptionProvider stateDescriptionProvider,
80 @Reference HttpClientFactory factory, @Reference NADeserializer deserializer,
81 @Reference HttpService httpService, Map<String, @Nullable Object> config) {
82 this.stateDescriptionProvider = stateDescriptionProvider;
83 this.httpClient = factory.getCommonHttpClient();
84 this.deserializer = deserializer;
85 this.httpService = httpService;
86 configChanged(config);
90 public void configChanged(Map<String, @Nullable Object> config) {
91 BindingConfiguration newConf = ConfigParser.configurationAs(config, BindingConfiguration.class);
92 if (newConf != null) {
93 configuration.update(newConf);
98 public boolean supportsThingType(ThingTypeUID thingTypeUID) {
99 return ModuleType.AS_SET.stream().anyMatch(mt -> mt.thingTypeUID.equals(thingTypeUID));
103 protected @Nullable ThingHandler createHandler(Thing thing) {
104 ThingTypeUID thingTypeUID = thing.getThingTypeUID();
105 return ModuleType.AS_SET.stream().filter(mt -> mt.thingTypeUID.equals(thingTypeUID)).findFirst()
106 .map(mt -> buildHandler(thing, mt)).orElse(null);
109 private BaseThingHandler buildHandler(Thing thing, ModuleType moduleType) {
110 if (ModuleType.ACCOUNT.equals(moduleType)) {
111 return new ApiBridgeHandler((Bridge) thing, httpClient, deserializer, configuration, httpService);
113 CommonInterface handler = moduleType.isABridge() ? new DeviceHandler((Bridge) thing) : new ModuleHandler(thing);
115 List<ChannelHelper> helpers = new ArrayList<>();
116 moduleType.channelGroups
117 .forEach(channelGroup -> channelGroup.getHelperInstance().ifPresent(helper -> helpers.add(helper)));
119 moduleType.capabilities.forEach(capability -> {
120 Capability newCap = null;
121 if (capability == DeviceCapability.class) {
122 newCap = new DeviceCapability(handler);
123 } else if (capability == AirCareCapability.class) {
124 newCap = new AirCareCapability(handler);
125 } else if (capability == HomeCapability.class) {
126 newCap = new HomeCapability(handler, stateDescriptionProvider);
127 } else if (capability == WeatherCapability.class) {
128 newCap = new WeatherCapability(handler);
129 } else if (capability == RoomCapability.class) {
130 newCap = new RoomCapability(handler);
131 } else if (capability == DoorbellCapability.class) {
132 newCap = new DoorbellCapability(handler, stateDescriptionProvider, helpers);
133 } else if (capability == PersonCapability.class) {
134 newCap = new PersonCapability(handler, stateDescriptionProvider, helpers);
135 } else if (capability == CameraCapability.class) {
136 newCap = new CameraCapability(handler, stateDescriptionProvider, helpers);
137 } else if (capability == SmokeCapability.class) {
138 newCap = new SmokeCapability(handler, stateDescriptionProvider, helpers);
139 } else if (capability == PresenceCapability.class) {
140 newCap = new PresenceCapability(handler, stateDescriptionProvider, helpers);
141 } else if (capability == MeasureCapability.class) {
142 newCap = new MeasureCapability(handler, helpers);
143 } else if (capability == ChannelHelperCapability.class) {
144 newCap = new ChannelHelperCapability(handler, helpers);
146 if (newCap != null) {
147 handler.getCapabilities().put(newCap);
149 logger.warn("No factory entry defined to create Capability : {}", capability);
153 return (BaseThingHandler) handler;