]> git.basschouten.com Git - openhab-addons.git/blob
ab9db84f52c8e2bbfdeb29c3ad61d248cf09d1c2
[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.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.auth.client.oauth2.OAuthFactory;
46 import org.openhab.core.config.core.ConfigParser;
47 import org.openhab.core.io.net.http.HttpClientFactory;
48 import org.openhab.core.thing.Bridge;
49 import org.openhab.core.thing.Thing;
50 import org.openhab.core.thing.ThingTypeUID;
51 import org.openhab.core.thing.binding.BaseThingHandler;
52 import org.openhab.core.thing.binding.BaseThingHandlerFactory;
53 import org.openhab.core.thing.binding.ThingHandler;
54 import org.openhab.core.thing.binding.ThingHandlerFactory;
55 import org.osgi.service.component.annotations.Activate;
56 import org.osgi.service.component.annotations.Component;
57 import org.osgi.service.component.annotations.Modified;
58 import org.osgi.service.component.annotations.Reference;
59 import org.osgi.service.http.HttpService;
60 import org.slf4j.Logger;
61 import org.slf4j.LoggerFactory;
62
63 /**
64  * The {@link NetatmoHandlerFactory} is responsible for creating things and
65  * thing handlers.
66  *
67  * @author GaĆ«l L'hopital - Initial contribution
68  */
69 @NonNullByDefault
70 @Component(service = ThingHandlerFactory.class, configurationPid = "binding.netatmo")
71 public class NetatmoHandlerFactory extends BaseThingHandlerFactory {
72     private final Logger logger = LoggerFactory.getLogger(NetatmoHandlerFactory.class);
73
74     private final BindingConfiguration configuration = new BindingConfiguration();
75     private final NetatmoDescriptionProvider stateDescriptionProvider;
76     private final NADeserializer deserializer;
77     private final HttpClient httpClient;
78     private final HttpService httpService;
79     private final OAuthFactory oAuthFactory;
80
81     @Activate
82     public NetatmoHandlerFactory(final @Reference NetatmoDescriptionProvider stateDescriptionProvider,
83             final @Reference HttpClientFactory factory, final @Reference NADeserializer deserializer,
84             final @Reference HttpService httpService, final @Reference OAuthFactory oAuthFactory,
85             Map<String, @Nullable Object> config) {
86         this.stateDescriptionProvider = stateDescriptionProvider;
87         this.httpClient = factory.getCommonHttpClient();
88         this.deserializer = deserializer;
89         this.httpService = httpService;
90         this.oAuthFactory = oAuthFactory;
91         configChanged(config);
92     }
93
94     @Modified
95     public void configChanged(Map<String, @Nullable Object> config) {
96         BindingConfiguration newConf = ConfigParser.configurationAs(config, BindingConfiguration.class);
97         if (newConf != null) {
98             configuration.update(newConf);
99         }
100     }
101
102     @Override
103     public boolean supportsThingType(ThingTypeUID thingTypeUID) {
104         return ModuleType.AS_SET.stream().anyMatch(mt -> mt.thingTypeUID.equals(thingTypeUID));
105     }
106
107     @Override
108     protected @Nullable ThingHandler createHandler(Thing thing) {
109         ThingTypeUID thingTypeUID = thing.getThingTypeUID();
110         return ModuleType.AS_SET.stream().filter(mt -> mt.thingTypeUID.equals(thingTypeUID)).findFirst()
111                 .map(mt -> buildHandler(thing, mt)).orElse(null);
112     }
113
114     private BaseThingHandler buildHandler(Thing thing, ModuleType moduleType) {
115         if (ModuleType.ACCOUNT.equals(moduleType)) {
116             return new ApiBridgeHandler((Bridge) thing, httpClient, deserializer, configuration, httpService,
117                     oAuthFactory);
118         }
119         CommonInterface handler = moduleType.isABridge() ? new DeviceHandler((Bridge) thing) : new ModuleHandler(thing);
120
121         List<ChannelHelper> helpers = new ArrayList<>();
122
123         helpers.addAll(moduleType.channelGroups.stream().map(ChannelGroup::getHelperInstance).toList());
124
125         moduleType.capabilities.forEach(capability -> {
126             Capability newCap = null;
127             if (capability == DeviceCapability.class) {
128                 newCap = new DeviceCapability(handler);
129             } else if (capability == AirCareCapability.class) {
130                 newCap = new AirCareCapability(handler);
131             } else if (capability == HomeCapability.class) {
132                 newCap = new HomeCapability(handler, stateDescriptionProvider);
133             } else if (capability == WeatherCapability.class) {
134                 newCap = new WeatherCapability(handler);
135             } else if (capability == RoomCapability.class) {
136                 newCap = new RoomCapability(handler);
137             } else if (capability == DoorbellCapability.class) {
138                 newCap = new DoorbellCapability(handler, stateDescriptionProvider, helpers);
139             } else if (capability == PersonCapability.class) {
140                 newCap = new PersonCapability(handler, stateDescriptionProvider, helpers);
141             } else if (capability == CameraCapability.class) {
142                 newCap = new CameraCapability(handler, stateDescriptionProvider, helpers);
143             } else if (capability == AlarmEventCapability.class) {
144                 newCap = new AlarmEventCapability(handler, stateDescriptionProvider, helpers);
145             } else if (capability == PresenceCapability.class) {
146                 newCap = new PresenceCapability(handler, stateDescriptionProvider, helpers);
147             } else if (capability == MeasureCapability.class) {
148                 newCap = new MeasureCapability(handler, helpers);
149             } else if (capability == ChannelHelperCapability.class) {
150                 newCap = new ChannelHelperCapability(handler, helpers);
151             }
152             if (newCap != null) {
153                 handler.getCapabilities().put(newCap);
154             } else {
155                 logger.warn("No factory entry defined to create Capability : {}", capability);
156             }
157         });
158
159         return (BaseThingHandler) handler;
160     }
161 }