]> git.basschouten.com Git - openhab-addons.git/blob
1acc4ac18bd4ce9820c91732a89fbd4e6cbda96e
[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.openhab.binding.mqtt.generic.AvailabilityTracker;
19 import org.openhab.binding.mqtt.generic.ChannelStateUpdateListener;
20 import org.openhab.binding.mqtt.homeassistant.internal.HaID;
21 import org.openhab.binding.mqtt.homeassistant.internal.config.dto.AbstractChannelConfiguration;
22 import org.openhab.binding.mqtt.homeassistant.internal.exception.ConfigurationException;
23 import org.openhab.binding.mqtt.homeassistant.internal.exception.UnsupportedComponentException;
24 import org.openhab.core.thing.ThingUID;
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 ComponentFactory {
36     /**
37      * Create a HA MQTT component. The configuration JSon string is required.
38      *
39      * @param thingUID The Thing UID that this component will belong to.
40      * @param haID The location of this component. The HomeAssistant ID contains the object-id, node-id and
41      *            component-id.
42      * @param channelConfigurationJSON Most components expect a "name", a "state_topic" and "command_topic" like with
43      *            "{name:'Name',state_topic:'homeassistant/switch/0/object/state',command_topic:'homeassistant/switch/0/object/set'".
44      * @param updateListener A channel state update listener
45      * @return A HA MQTT Component
46      */
47     public static AbstractComponent<?> createComponent(ThingUID thingUID, HaID haID, String channelConfigurationJSON,
48             ChannelStateUpdateListener updateListener, AvailabilityTracker tracker, ScheduledExecutorService scheduler,
49             Gson gson, boolean newStyleChannels) throws ConfigurationException {
50         ComponentConfiguration componentConfiguration = new ComponentConfiguration(thingUID, haID,
51                 channelConfigurationJSON, gson, updateListener, tracker, scheduler);
52         switch (haID.component) {
53             case "alarm_control_panel":
54                 return new AlarmControlPanel(componentConfiguration, newStyleChannels);
55             case "binary_sensor":
56                 return new BinarySensor(componentConfiguration, newStyleChannels);
57             case "button":
58                 return new Button(componentConfiguration, newStyleChannels);
59             case "camera":
60                 return new Camera(componentConfiguration, newStyleChannels);
61             case "cover":
62                 return new Cover(componentConfiguration, newStyleChannels);
63             case "fan":
64                 return new Fan(componentConfiguration, newStyleChannels);
65             case "climate":
66                 return new Climate(componentConfiguration, newStyleChannels);
67             case "device_automation":
68                 return new DeviceTrigger(componentConfiguration, newStyleChannels);
69             case "light":
70                 return Light.create(componentConfiguration, newStyleChannels);
71             case "lock":
72                 return new Lock(componentConfiguration, newStyleChannels);
73             case "number":
74                 return new Number(componentConfiguration, newStyleChannels);
75             case "scene":
76                 return new Scene(componentConfiguration, newStyleChannels);
77             case "select":
78                 return new Select(componentConfiguration, newStyleChannels);
79             case "sensor":
80                 return new Sensor(componentConfiguration, newStyleChannels);
81             case "switch":
82                 return new Switch(componentConfiguration, newStyleChannels);
83             case "update":
84                 return new Update(componentConfiguration, newStyleChannels);
85             case "vacuum":
86                 return new Vacuum(componentConfiguration, newStyleChannels);
87             default:
88                 throw new UnsupportedComponentException("Component '" + haID + "' is unsupported!");
89         }
90     }
91
92     protected static class ComponentConfiguration {
93         private final ThingUID thingUID;
94         private final HaID haID;
95         private final String configJSON;
96         private final ChannelStateUpdateListener updateListener;
97         private final AvailabilityTracker tracker;
98         private final Gson gson;
99         private final ScheduledExecutorService scheduler;
100
101         /**
102          * Provide a thingUID and HomeAssistant topic ID to determine the channel group UID and type.
103          *
104          * @param thingUID A ThingUID
105          * @param haID A HomeAssistant topic ID
106          * @param configJSON The configuration string
107          * @param gson A Gson instance
108          */
109         protected ComponentConfiguration(ThingUID thingUID, HaID haID, String configJSON, Gson gson,
110                 ChannelStateUpdateListener updateListener, AvailabilityTracker tracker,
111                 ScheduledExecutorService scheduler) {
112             this.thingUID = thingUID;
113             this.haID = haID;
114             this.configJSON = configJSON;
115             this.gson = gson;
116             this.updateListener = updateListener;
117             this.tracker = tracker;
118             this.scheduler = scheduler;
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         public Gson getGson() {
138             return gson;
139         }
140
141         public AvailabilityTracker getTracker() {
142             return tracker;
143         }
144
145         public ScheduledExecutorService getScheduler() {
146             return scheduler;
147         }
148
149         public <C extends AbstractChannelConfiguration> C getConfig(Class<C> clazz) {
150             return AbstractChannelConfiguration.fromString(configJSON, gson, clazz);
151         }
152     }
153 }