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