]> git.basschouten.com Git - openhab-addons.git/blob
4053372bf3cff5efbabbc0c2570a05c9bc2ef347
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
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
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.netatmo.internal;
14
15 import java.util.ArrayList;
16 import java.util.List;
17 import java.util.Map;
18
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;
60
61 /**
62  * The {@link NetatmoHandlerFactory} is responsible for creating things and
63  * thing handlers.
64  *
65  * @author GaĆ«l L'hopital - Initial contribution
66  */
67 @NonNullByDefault
68 @Component(service = ThingHandlerFactory.class, configurationPid = "binding.netatmo")
69 public class NetatmoHandlerFactory extends BaseThingHandlerFactory {
70     private final Logger logger = LoggerFactory.getLogger(NetatmoHandlerFactory.class);
71
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;
77
78     @Activate
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);
87     }
88
89     @Modified
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);
94         }
95     }
96
97     @Override
98     public boolean supportsThingType(ThingTypeUID thingTypeUID) {
99         return ModuleType.AS_SET.stream().anyMatch(mt -> mt.thingTypeUID.equals(thingTypeUID));
100     }
101
102     @Override
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);
107     }
108
109     private BaseThingHandler buildHandler(Thing thing, ModuleType moduleType) {
110         if (ModuleType.ACCOUNT.equals(moduleType)) {
111             return new ApiBridgeHandler((Bridge) thing, httpClient, deserializer, configuration, httpService);
112         }
113         CommonInterface handler = moduleType.isABridge() ? new DeviceHandler((Bridge) thing) : new ModuleHandler(thing);
114
115         List<ChannelHelper> helpers = new ArrayList<>();
116         moduleType.channelGroups
117                 .forEach(channelGroup -> channelGroup.getHelperInstance().ifPresent(helper -> helpers.add(helper)));
118
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);
145             }
146             if (newCap != null) {
147                 handler.getCapabilities().put(newCap);
148             } else {
149                 logger.warn("No factory entry defined to create Capability : {}", capability);
150             }
151         });
152
153         return (BaseThingHandler) handler;
154     }
155 }