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