]> git.basschouten.com Git - openhab-addons.git/blob
0c48a72805a9187eadef9775a0db30136d69ea8f
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2024 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, boolean newStyleChannels)
52             throws ConfigurationException {
53         ComponentConfiguration componentConfiguration = new ComponentConfiguration(thingUID, haID,
54                 channelConfigurationJSON, gson, updateListener, tracker, scheduler)
55                 .transformationProvider(transformationServiceProvider);
56         switch (haID.component) {
57             case "alarm_control_panel":
58                 return new AlarmControlPanel(componentConfiguration, newStyleChannels);
59             case "binary_sensor":
60                 return new BinarySensor(componentConfiguration, newStyleChannels);
61             case "button":
62                 return new Button(componentConfiguration, newStyleChannels);
63             case "camera":
64                 return new Camera(componentConfiguration, newStyleChannels);
65             case "cover":
66                 return new Cover(componentConfiguration, newStyleChannels);
67             case "fan":
68                 return new Fan(componentConfiguration, newStyleChannels);
69             case "climate":
70                 return new Climate(componentConfiguration, newStyleChannels);
71             case "device_automation":
72                 return new DeviceTrigger(componentConfiguration, newStyleChannels);
73             case "light":
74                 return Light.create(componentConfiguration, newStyleChannels);
75             case "lock":
76                 return new Lock(componentConfiguration, newStyleChannels);
77             case "number":
78                 return new Number(componentConfiguration, newStyleChannels);
79             case "scene":
80                 return new Scene(componentConfiguration, newStyleChannels);
81             case "select":
82                 return new Select(componentConfiguration, newStyleChannels);
83             case "sensor":
84                 return new Sensor(componentConfiguration, newStyleChannels);
85             case "switch":
86                 return new Switch(componentConfiguration, newStyleChannels);
87             case "update":
88                 return new Update(componentConfiguration, newStyleChannels);
89             case "vacuum":
90                 return new Vacuum(componentConfiguration, newStyleChannels);
91             default:
92                 throw new UnsupportedComponentException("Component '" + haID + "' is unsupported!");
93         }
94     }
95
96     protected static class ComponentConfiguration {
97         private final ThingUID thingUID;
98         private final HaID haID;
99         private final String configJSON;
100         private final ChannelStateUpdateListener updateListener;
101         private final AvailabilityTracker tracker;
102         private final Gson gson;
103         private final ScheduledExecutorService scheduler;
104         private @Nullable TransformationServiceProvider transformationServiceProvider;
105
106         /**
107          * Provide a thingUID and HomeAssistant topic ID to determine the channel group UID and type.
108          *
109          * @param thingUID A ThingUID
110          * @param haID A HomeAssistant topic ID
111          * @param configJSON The configuration string
112          * @param gson A Gson instance
113          */
114         protected ComponentConfiguration(ThingUID thingUID, HaID haID, String configJSON, Gson gson,
115                 ChannelStateUpdateListener updateListener, AvailabilityTracker tracker,
116                 ScheduledExecutorService scheduler) {
117             this.thingUID = thingUID;
118             this.haID = haID;
119             this.configJSON = configJSON;
120             this.gson = gson;
121             this.updateListener = updateListener;
122             this.tracker = tracker;
123             this.scheduler = scheduler;
124         }
125
126         public ComponentConfiguration transformationProvider(
127                 TransformationServiceProvider transformationServiceProvider) {
128             this.transformationServiceProvider = transformationServiceProvider;
129             return this;
130         }
131
132         public ThingUID getThingUID() {
133             return thingUID;
134         }
135
136         public HaID getHaID() {
137             return haID;
138         }
139
140         public String getConfigJSON() {
141             return configJSON;
142         }
143
144         public ChannelStateUpdateListener getUpdateListener() {
145             return updateListener;
146         }
147
148         @Nullable
149         public TransformationServiceProvider getTransformationServiceProvider() {
150             return transformationServiceProvider;
151         }
152
153         public Gson getGson() {
154             return gson;
155         }
156
157         public AvailabilityTracker getTracker() {
158             return tracker;
159         }
160
161         public ScheduledExecutorService getScheduler() {
162             return scheduler;
163         }
164
165         public <C extends AbstractChannelConfiguration> C getConfig(Class<C> clazz) {
166             return AbstractChannelConfiguration.fromString(configJSON, gson, clazz);
167         }
168     }
169 }