]> git.basschouten.com Git - openhab-addons.git/blob
5be1b36e3bae1dc012166bcd9e131a2bca14f740
[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.component;
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.binding.mqtt.homeassistant.internal.HaID;
23 import org.openhab.binding.mqtt.homeassistant.internal.config.dto.AbstractChannelConfiguration;
24 import org.openhab.binding.mqtt.homeassistant.internal.exception.ConfigurationException;
25 import org.openhab.binding.mqtt.homeassistant.internal.exception.UnsupportedComponentException;
26 import org.openhab.core.thing.ThingUID;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29
30 import com.google.gson.Gson;
31
32 /**
33  * A factory to create HomeAssistant MQTT components. Those components are specified at:
34  * https://www.home-assistant.io/docs/mqtt/discovery/
35  *
36  * @author David Graeff - Initial contribution
37  */
38 @NonNullByDefault
39 public class ComponentFactory {
40     private static final Logger LOGGER = LoggerFactory.getLogger(ComponentFactory.class);
41
42     /**
43      * Create a HA MQTT component. The configuration JSon string is required.
44      *
45      * @param thingUID The Thing UID that this component will belong to.
46      * @param haID The location of this component. The HomeAssistant ID contains the object-id, node-id and
47      *            component-id.
48      * @param channelConfigurationJSON Most components expect a "name", a "state_topic" and "command_topic" like with
49      *            "{name:'Name',state_topic:'homeassistant/switch/0/object/state',command_topic:'homeassistant/switch/0/object/set'".
50      * @param updateListener A channel state update listener
51      * @return A HA MQTT Component
52      */
53     public static AbstractComponent<?> createComponent(ThingUID thingUID, HaID haID, String channelConfigurationJSON,
54             ChannelStateUpdateListener updateListener, AvailabilityTracker tracker, ScheduledExecutorService scheduler,
55             Gson gson, TransformationServiceProvider transformationServiceProvider) throws ConfigurationException {
56         ComponentConfiguration componentConfiguration = new ComponentConfiguration(thingUID, haID,
57                 channelConfigurationJSON, gson, updateListener, tracker, scheduler)
58                         .transformationProvider(transformationServiceProvider);
59         switch (haID.component) {
60             case "alarm_control_panel":
61                 return new AlarmControlPanel(componentConfiguration);
62             case "binary_sensor":
63                 return new BinarySensor(componentConfiguration);
64             case "camera":
65                 return new Camera(componentConfiguration);
66             case "cover":
67                 return new Cover(componentConfiguration);
68             case "fan":
69                 return new Fan(componentConfiguration);
70             case "climate":
71                 return new Climate(componentConfiguration);
72             case "light":
73                 return new Light(componentConfiguration);
74             case "lock":
75                 return new Lock(componentConfiguration);
76             case "sensor":
77                 return new Sensor(componentConfiguration);
78             case "switch":
79                 return new Switch(componentConfiguration);
80             default:
81                 throw new UnsupportedComponentException("Component '" + haID + "' is unsupported!");
82         }
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         /**
96          * Provide a thingUID and HomeAssistant topic ID to determine the channel group UID and type.
97          *
98          * @param thingUID A ThingUID
99          * @param haID A HomeAssistant topic ID
100          * @param configJSON The configuration string
101          * @param gson A Gson instance
102          */
103         protected ComponentConfiguration(ThingUID thingUID, HaID haID, String configJSON, Gson gson,
104                 ChannelStateUpdateListener updateListener, AvailabilityTracker tracker,
105                 ScheduledExecutorService scheduler) {
106             this.thingUID = thingUID;
107             this.haID = haID;
108             this.configJSON = configJSON;
109             this.gson = gson;
110             this.updateListener = updateListener;
111             this.tracker = tracker;
112             this.scheduler = scheduler;
113         }
114
115         public ComponentConfiguration transformationProvider(
116                 TransformationServiceProvider transformationServiceProvider) {
117             this.transformationServiceProvider = transformationServiceProvider;
118             return this;
119         }
120
121         public ThingUID getThingUID() {
122             return thingUID;
123         }
124
125         public HaID getHaID() {
126             return haID;
127         }
128
129         public String getConfigJSON() {
130             return configJSON;
131         }
132
133         public ChannelStateUpdateListener getUpdateListener() {
134             return updateListener;
135         }
136
137         @Nullable
138         public TransformationServiceProvider getTransformationServiceProvider() {
139             return transformationServiceProvider;
140         }
141
142         public Gson getGson() {
143             return gson;
144         }
145
146         public AvailabilityTracker getTracker() {
147             return tracker;
148         }
149
150         public ScheduledExecutorService getScheduler() {
151             return scheduler;
152         }
153
154         public <C extends AbstractChannelConfiguration> C getConfig(Class<C> clazz) {
155             return AbstractChannelConfiguration.fromString(configJSON, gson, clazz);
156         }
157     }
158 }