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