]> git.basschouten.com Git - openhab-addons.git/blob
36e9bbd66bdf165217f0896db26120b97a45894d
[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 import com.hubspot.jinjava.Jinjava;
28
29 /**
30  * A factory to create HomeAssistant MQTT components. Those components are specified at:
31  * https://www.home-assistant.io/docs/mqtt/discovery/
32  *
33  * @author David Graeff - Initial contribution
34  */
35 @NonNullByDefault
36 public class ComponentFactory {
37     /**
38      * Create a HA MQTT component. The configuration JSon string is required.
39      *
40      * @param thingUID The Thing UID that this component will belong to.
41      * @param haID The location of this component. The HomeAssistant ID contains the object-id, node-id and
42      *            component-id.
43      * @param channelConfigurationJSON Most components expect a "name", a "state_topic" and "command_topic" like with
44      *            "{name:'Name',state_topic:'homeassistant/switch/0/object/state',command_topic:'homeassistant/switch/0/object/set'".
45      * @param updateListener A channel state update listener
46      * @return A HA MQTT Component
47      */
48     public static AbstractComponent<?> createComponent(ThingUID thingUID, HaID haID, String channelConfigurationJSON,
49             ChannelStateUpdateListener updateListener, AvailabilityTracker tracker, ScheduledExecutorService scheduler,
50             Gson gson, Jinjava jinjava, boolean newStyleChannels) throws ConfigurationException {
51         ComponentConfiguration componentConfiguration = new ComponentConfiguration(thingUID, haID,
52                 channelConfigurationJSON, gson, jinjava, updateListener, tracker, scheduler);
53         switch (haID.component) {
54             case "alarm_control_panel":
55                 return new AlarmControlPanel(componentConfiguration, newStyleChannels);
56             case "binary_sensor":
57                 return new BinarySensor(componentConfiguration, newStyleChannels);
58             case "button":
59                 return new Button(componentConfiguration, newStyleChannels);
60             case "camera":
61                 return new Camera(componentConfiguration, newStyleChannels);
62             case "cover":
63                 return new Cover(componentConfiguration, newStyleChannels);
64             case "fan":
65                 return new Fan(componentConfiguration, newStyleChannels);
66             case "climate":
67                 return new Climate(componentConfiguration, newStyleChannels);
68             case "device_automation":
69                 return new DeviceTrigger(componentConfiguration, newStyleChannels);
70             case "light":
71                 return Light.create(componentConfiguration, newStyleChannels);
72             case "lock":
73                 return new Lock(componentConfiguration, newStyleChannels);
74             case "number":
75                 return new Number(componentConfiguration, newStyleChannels);
76             case "scene":
77                 return new Scene(componentConfiguration, newStyleChannels);
78             case "select":
79                 return new Select(componentConfiguration, newStyleChannels);
80             case "sensor":
81                 return new Sensor(componentConfiguration, newStyleChannels);
82             case "switch":
83                 return new Switch(componentConfiguration, newStyleChannels);
84             case "update":
85                 return new Update(componentConfiguration, newStyleChannels);
86             case "vacuum":
87                 return new Vacuum(componentConfiguration, newStyleChannels);
88             default:
89                 throw new UnsupportedComponentException("Component '" + haID + "' is unsupported!");
90         }
91     }
92
93     protected static class ComponentConfiguration {
94         private final ThingUID thingUID;
95         private final HaID haID;
96         private final String configJSON;
97         private final ChannelStateUpdateListener updateListener;
98         private final AvailabilityTracker tracker;
99         private final Gson gson;
100         private final Jinjava jinjava;
101         private final ScheduledExecutorService scheduler;
102
103         /**
104          * Provide a thingUID and HomeAssistant topic ID to determine the channel group UID and type.
105          *
106          * @param thingUID A ThingUID
107          * @param haID A HomeAssistant topic ID
108          * @param configJSON The configuration string
109          * @param gson A Gson instance
110          */
111         protected ComponentConfiguration(ThingUID thingUID, HaID haID, String configJSON, Gson gson, Jinjava jinjava,
112                 ChannelStateUpdateListener updateListener, AvailabilityTracker tracker,
113                 ScheduledExecutorService scheduler) {
114             this.thingUID = thingUID;
115             this.haID = haID;
116             this.configJSON = configJSON;
117             this.gson = gson;
118             this.jinjava = jinjava;
119             this.updateListener = updateListener;
120             this.tracker = tracker;
121             this.scheduler = scheduler;
122         }
123
124         public ThingUID getThingUID() {
125             return thingUID;
126         }
127
128         public HaID getHaID() {
129             return haID;
130         }
131
132         public String getConfigJSON() {
133             return configJSON;
134         }
135
136         public ChannelStateUpdateListener getUpdateListener() {
137             return updateListener;
138         }
139
140         public Gson getGson() {
141             return gson;
142         }
143
144         public Jinjava getJinjava() {
145             return jinjava;
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 }