2 * Copyright (c) 2010-2024 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.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;
26 import com.google.gson.Gson;
29 * A factory to create HomeAssistant MQTT components. Those components are specified at:
30 * https://www.home-assistant.io/docs/mqtt/discovery/
32 * @author David Graeff - Initial contribution
35 public class ComponentFactory {
37 * Create a HA MQTT component. The configuration JSon string is required.
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
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
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);
56 return new BinarySensor(componentConfiguration, newStyleChannels);
58 return new Button(componentConfiguration, newStyleChannels);
60 return new Camera(componentConfiguration, newStyleChannels);
62 return new Cover(componentConfiguration, newStyleChannels);
64 return new Fan(componentConfiguration, newStyleChannels);
66 return new Climate(componentConfiguration, newStyleChannels);
67 case "device_automation":
68 return new DeviceTrigger(componentConfiguration, newStyleChannels);
70 return Light.create(componentConfiguration, newStyleChannels);
72 return new Lock(componentConfiguration, newStyleChannels);
74 return new Number(componentConfiguration, newStyleChannels);
76 return new Scene(componentConfiguration, newStyleChannels);
78 return new Select(componentConfiguration, newStyleChannels);
80 return new Sensor(componentConfiguration, newStyleChannels);
82 return new Switch(componentConfiguration, newStyleChannels);
84 return new Update(componentConfiguration, newStyleChannels);
86 return new Vacuum(componentConfiguration, newStyleChannels);
88 throw new UnsupportedComponentException("Component '" + haID + "' is unsupported!");
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;
102 * Provide a thingUID and HomeAssistant topic ID to determine the channel group UID and type.
104 * @param thingUID A ThingUID
105 * @param haID A HomeAssistant topic ID
106 * @param configJSON The configuration string
107 * @param gson A Gson instance
109 protected ComponentConfiguration(ThingUID thingUID, HaID haID, String configJSON, Gson gson,
110 ChannelStateUpdateListener updateListener, AvailabilityTracker tracker,
111 ScheduledExecutorService scheduler) {
112 this.thingUID = thingUID;
114 this.configJSON = configJSON;
116 this.updateListener = updateListener;
117 this.tracker = tracker;
118 this.scheduler = scheduler;
121 public ThingUID getThingUID() {
125 public HaID getHaID() {
129 public String getConfigJSON() {
133 public ChannelStateUpdateListener getUpdateListener() {
134 return updateListener;
137 public Gson getGson() {
141 public AvailabilityTracker getTracker() {
145 public ScheduledExecutorService getScheduler() {
149 public <C extends AbstractChannelConfiguration> C getConfig(Class<C> clazz) {
150 return AbstractChannelConfiguration.fromString(configJSON, gson, clazz);