]> git.basschouten.com Git - openhab-addons.git/blob
1771a2d473f4a4c15708aa333ae9a7d528d3bdd1
[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             case "vacuum":
81                 return new Vacuum(componentConfiguration);
82             default:
83                 throw new UnsupportedComponentException("Component '" + haID + "' is unsupported!");
84         }
85     }
86
87     protected static class ComponentConfiguration {
88         private final ThingUID thingUID;
89         private final HaID haID;
90         private final String configJSON;
91         private final ChannelStateUpdateListener updateListener;
92         private final AvailabilityTracker tracker;
93         private final Gson gson;
94         private final ScheduledExecutorService scheduler;
95         private @Nullable TransformationServiceProvider transformationServiceProvider;
96
97         /**
98          * Provide a thingUID and HomeAssistant topic ID to determine the channel group UID and type.
99          *
100          * @param thingUID A ThingUID
101          * @param haID A HomeAssistant topic ID
102          * @param configJSON The configuration string
103          * @param gson A Gson instance
104          */
105         protected ComponentConfiguration(ThingUID thingUID, HaID haID, String configJSON, Gson gson,
106                 ChannelStateUpdateListener updateListener, AvailabilityTracker tracker,
107                 ScheduledExecutorService scheduler) {
108             this.thingUID = thingUID;
109             this.haID = haID;
110             this.configJSON = configJSON;
111             this.gson = gson;
112             this.updateListener = updateListener;
113             this.tracker = tracker;
114             this.scheduler = scheduler;
115         }
116
117         public ComponentConfiguration transformationProvider(
118                 TransformationServiceProvider transformationServiceProvider) {
119             this.transformationServiceProvider = transformationServiceProvider;
120             return this;
121         }
122
123         public ThingUID getThingUID() {
124             return thingUID;
125         }
126
127         public HaID getHaID() {
128             return haID;
129         }
130
131         public String getConfigJSON() {
132             return configJSON;
133         }
134
135         public ChannelStateUpdateListener getUpdateListener() {
136             return updateListener;
137         }
138
139         @Nullable
140         public TransformationServiceProvider getTransformationServiceProvider() {
141             return transformationServiceProvider;
142         }
143
144         public Gson getGson() {
145             return gson;
146         }
147
148         public AvailabilityTracker getTracker() {
149             return tracker;
150         }
151
152         public ScheduledExecutorService getScheduler() {
153             return scheduler;
154         }
155
156         public <C extends AbstractChannelConfiguration> C getConfig(Class<C> clazz) {
157             return AbstractChannelConfiguration.fromString(configJSON, gson, clazz);
158         }
159     }
160 }