2 * Copyright (c) 2010-2023 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.mqtt.homeassistant.internal.component;
15 import java.util.concurrent.ScheduledExecutorService;
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;
28 import com.google.gson.Gson;
31 * A factory to create HomeAssistant MQTT components. Those components are specified at:
32 * https://www.home-assistant.io/docs/mqtt/discovery/
34 * @author David Graeff - Initial contribution
37 public class ComponentFactory {
39 * Create a HA MQTT component. The configuration JSon string is required.
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
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
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);
59 return new BinarySensor(componentConfiguration);
61 return new Button(componentConfiguration);
63 return new Camera(componentConfiguration);
65 return new Cover(componentConfiguration);
67 return new Fan(componentConfiguration);
69 return new Climate(componentConfiguration);
70 case "device_automation":
71 return new DeviceTrigger(componentConfiguration);
73 return Light.create(componentConfiguration);
75 return new Lock(componentConfiguration);
77 return new Number(componentConfiguration);
79 return new Scene(componentConfiguration);
81 return new Select(componentConfiguration);
83 return new Sensor(componentConfiguration);
85 return new Switch(componentConfiguration);
87 return new Vacuum(componentConfiguration);
89 throw new UnsupportedComponentException("Component '" + haID + "' is unsupported!");
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 ScheduledExecutorService scheduler;
101 private @Nullable TransformationServiceProvider transformationServiceProvider;
104 * Provide a thingUID and HomeAssistant topic ID to determine the channel group UID and type.
106 * @param thingUID A ThingUID
107 * @param haID A HomeAssistant topic ID
108 * @param configJSON The configuration string
109 * @param gson A Gson instance
111 protected ComponentConfiguration(ThingUID thingUID, HaID haID, String configJSON, Gson gson,
112 ChannelStateUpdateListener updateListener, AvailabilityTracker tracker,
113 ScheduledExecutorService scheduler) {
114 this.thingUID = thingUID;
116 this.configJSON = configJSON;
118 this.updateListener = updateListener;
119 this.tracker = tracker;
120 this.scheduler = scheduler;
123 public ComponentConfiguration transformationProvider(
124 TransformationServiceProvider transformationServiceProvider) {
125 this.transformationServiceProvider = transformationServiceProvider;
129 public ThingUID getThingUID() {
133 public HaID getHaID() {
137 public String getConfigJSON() {
141 public ChannelStateUpdateListener getUpdateListener() {
142 return updateListener;
146 public TransformationServiceProvider getTransformationServiceProvider() {
147 return transformationServiceProvider;
150 public Gson getGson() {
154 public AvailabilityTracker getTracker() {
158 public ScheduledExecutorService getScheduler() {
162 public <C extends AbstractChannelConfiguration> C getConfig(Class<C> clazz) {
163 return AbstractChannelConfiguration.fromString(configJSON, gson, clazz);