]> git.basschouten.com Git - openhab-addons.git/blob
b82610e57c840e283a55e50a3414271c7ecc33c8
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2020 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.mqtt.homeassistant.internal;
14
15 import org.eclipse.jdt.annotation.NonNullByDefault;
16 import org.eclipse.jdt.annotation.Nullable;
17 import org.openhab.binding.mqtt.generic.AvailabilityTracker;
18 import org.openhab.binding.mqtt.generic.ChannelStateUpdateListener;
19 import org.openhab.binding.mqtt.generic.TransformationServiceProvider;
20 import org.openhab.core.thing.ThingUID;
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
23
24 import com.google.gson.Gson;
25
26 /**
27  * A factory to create HomeAssistant MQTT components. Those components are specified at:
28  * https://www.home-assistant.io/docs/mqtt/discovery/
29  *
30  * @author David Graeff - Initial contribution
31  */
32 @NonNullByDefault
33 public class CFactory {
34     private static final Logger logger = LoggerFactory.getLogger(CFactory.class);
35
36     /**
37      * Create a HA MQTT component. The configuration JSon string is required.
38      *
39      * @param thingUID The Thing UID that this component will belong to.
40      * @param haID The location of this component. The HomeAssistant ID contains the object-id, node-id and
41      *            component-id.
42      * @param configJSON Most components expect a "name", a "state_topic" and "command_topic" like with
43      *            "{name:'Name',state_topic:'homeassistant/switch/0/object/state',command_topic:'homeassistant/switch/0/object/set'".
44      * @param updateListener A channel state update listener
45      * @return A HA MQTT Component
46      */
47     public static @Nullable AbstractComponent<?> createComponent(ThingUID thingUID, HaID haID,
48             String channelConfigurationJSON, ChannelStateUpdateListener updateListener, AvailabilityTracker tracker,
49             Gson gson, TransformationServiceProvider transformationServiceProvider) {
50         ComponentConfiguration componentConfiguration = new ComponentConfiguration(thingUID, haID,
51                 channelConfigurationJSON, gson, updateListener, tracker)
52                         .transformationProvider(transformationServiceProvider);
53         try {
54             switch (haID.component) {
55                 case "alarm_control_panel":
56                     return new ComponentAlarmControlPanel(componentConfiguration);
57                 case "binary_sensor":
58                     return new ComponentBinarySensor(componentConfiguration);
59                 case "camera":
60                     return new ComponentCamera(componentConfiguration);
61                 case "cover":
62                     return new ComponentCover(componentConfiguration);
63                 case "fan":
64                     return new ComponentFan(componentConfiguration);
65                 case "climate":
66                     return new ComponentClimate(componentConfiguration);
67                 case "light":
68                     return new ComponentLight(componentConfiguration);
69                 case "lock":
70                     return new ComponentLock(componentConfiguration);
71                 case "sensor":
72                     return new ComponentSensor(componentConfiguration);
73                 case "switch":
74                     return new ComponentSwitch(componentConfiguration);
75             }
76         } catch (UnsupportedOperationException e) {
77             logger.warn("Not supported", e);
78         }
79         return null;
80     }
81
82     protected static class ComponentConfiguration {
83         private final ThingUID thingUID;
84         private final HaID haID;
85         private final String configJSON;
86         private final ChannelStateUpdateListener updateListener;
87         private final AvailabilityTracker tracker;
88         private final Gson gson;
89         private @Nullable TransformationServiceProvider transformationServiceProvider;
90
91         protected ComponentConfiguration(ThingUID thingUID, HaID haID, String configJSON, Gson gson,
92                 ChannelStateUpdateListener updateListener, AvailabilityTracker tracker) {
93             this.thingUID = thingUID;
94             this.haID = haID;
95             this.configJSON = configJSON;
96             this.gson = gson;
97             this.updateListener = updateListener;
98             this.tracker = tracker;
99         }
100
101         public ComponentConfiguration transformationProvider(
102                 TransformationServiceProvider transformationServiceProvider) {
103             this.transformationServiceProvider = transformationServiceProvider;
104             return this;
105         }
106
107         public ThingUID getThingUID() {
108             return thingUID;
109         }
110
111         public HaID getHaID() {
112             return haID;
113         }
114
115         public String getConfigJSON() {
116             return configJSON;
117         }
118
119         public ChannelStateUpdateListener getUpdateListener() {
120             return updateListener;
121         }
122
123         @Nullable
124         public TransformationServiceProvider getTransformationServiceProvider() {
125             return transformationServiceProvider;
126         }
127
128         public Gson getGson() {
129             return gson;
130         }
131
132         public AvailabilityTracker getTracker() {
133             return tracker;
134         }
135
136         public <C extends BaseChannelConfiguration> C getConfig(Class<C> clazz) {
137             return BaseChannelConfiguration.fromString(configJSON, gson, clazz);
138         }
139     }
140 }