]> git.basschouten.com Git - openhab-addons.git/blob
cb755ac0fef7c676d457794f3d0719cbaf4c7ee4
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2022 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
28 import com.google.gson.Gson;
29
30 /**
31  * A factory to create HomeAssistant MQTT components. Those components are specified at:
32  * https://www.home-assistant.io/docs/mqtt/discovery/
33  *
34  * @author David Graeff - Initial contribution
35  */
36 @NonNullByDefault
37 public class ComponentFactory {
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 channelConfigurationJSON 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 AbstractComponent<?> createComponent(ThingUID thingUID, HaID haID, String channelConfigurationJSON,
50             ChannelStateUpdateListener updateListener, AvailabilityTracker tracker, ScheduledExecutorService scheduler,
51             Gson gson, TransformationServiceProvider transformationServiceProvider) throws ConfigurationException {
52         ComponentConfiguration componentConfiguration = new ComponentConfiguration(thingUID, haID,
53                 channelConfigurationJSON, gson, updateListener, tracker, scheduler)
54                         .transformationProvider(transformationServiceProvider);
55         switch (haID.component) {
56             case "alarm_control_panel":
57                 return new AlarmControlPanel(componentConfiguration);
58             case "binary_sensor":
59                 return new BinarySensor(componentConfiguration);
60             case "camera":
61                 return new Camera(componentConfiguration);
62             case "cover":
63                 return new Cover(componentConfiguration);
64             case "fan":
65                 return new Fan(componentConfiguration);
66             case "climate":
67                 return new Climate(componentConfiguration);
68             case "light":
69                 return new Light(componentConfiguration);
70             case "lock":
71                 return new Lock(componentConfiguration);
72             case "sensor":
73                 return new Sensor(componentConfiguration);
74             case "switch":
75                 return new Switch(componentConfiguration);
76             case "vacuum":
77                 return new Vacuum(componentConfiguration);
78             default:
79                 throw new UnsupportedComponentException("Component '" + haID + "' is unsupported!");
80         }
81     }
82
83     protected static class ComponentConfiguration {
84         private final ThingUID thingUID;
85         private final HaID haID;
86         private final String configJSON;
87         private final ChannelStateUpdateListener updateListener;
88         private final AvailabilityTracker tracker;
89         private final Gson gson;
90         private final ScheduledExecutorService scheduler;
91         private @Nullable TransformationServiceProvider transformationServiceProvider;
92
93         /**
94          * Provide a thingUID and HomeAssistant topic ID to determine the channel group UID and type.
95          *
96          * @param thingUID A ThingUID
97          * @param haID A HomeAssistant topic ID
98          * @param configJSON The configuration string
99          * @param gson A Gson instance
100          */
101         protected ComponentConfiguration(ThingUID thingUID, HaID haID, String configJSON, Gson gson,
102                 ChannelStateUpdateListener updateListener, AvailabilityTracker tracker,
103                 ScheduledExecutorService scheduler) {
104             this.thingUID = thingUID;
105             this.haID = haID;
106             this.configJSON = configJSON;
107             this.gson = gson;
108             this.updateListener = updateListener;
109             this.tracker = tracker;
110             this.scheduler = scheduler;
111         }
112
113         public ComponentConfiguration transformationProvider(
114                 TransformationServiceProvider transformationServiceProvider) {
115             this.transformationServiceProvider = transformationServiceProvider;
116             return this;
117         }
118
119         public ThingUID getThingUID() {
120             return thingUID;
121         }
122
123         public HaID getHaID() {
124             return haID;
125         }
126
127         public String getConfigJSON() {
128             return configJSON;
129         }
130
131         public ChannelStateUpdateListener getUpdateListener() {
132             return updateListener;
133         }
134
135         @Nullable
136         public TransformationServiceProvider getTransformationServiceProvider() {
137             return transformationServiceProvider;
138         }
139
140         public Gson getGson() {
141             return gson;
142         }
143
144         public AvailabilityTracker getTracker() {
145             return tracker;
146         }
147
148         public ScheduledExecutorService getScheduler() {
149             return scheduler;
150         }
151
152         public <C extends AbstractChannelConfiguration> C getConfig(Class<C> clazz) {
153             return AbstractChannelConfiguration.fromString(configJSON, gson, clazz);
154         }
155     }
156 }