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;
27 import com.hubspot.jinjava.Jinjava;
30 * A factory to create HomeAssistant MQTT components. Those components are specified at:
31 * https://www.home-assistant.io/docs/mqtt/discovery/
33 * @author David Graeff - Initial contribution
36 public class ComponentFactory {
38 * Create a HA MQTT component. The configuration JSon string is required.
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
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
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);
57 return new BinarySensor(componentConfiguration, newStyleChannels);
59 return new Button(componentConfiguration, newStyleChannels);
61 return new Camera(componentConfiguration, newStyleChannels);
63 return new Cover(componentConfiguration, newStyleChannels);
65 return new Fan(componentConfiguration, newStyleChannels);
67 return new Climate(componentConfiguration, newStyleChannels);
68 case "device_automation":
69 return new DeviceTrigger(componentConfiguration, newStyleChannels);
71 return Light.create(componentConfiguration, newStyleChannels);
73 return new Lock(componentConfiguration, newStyleChannels);
75 return new Number(componentConfiguration, newStyleChannels);
77 return new Scene(componentConfiguration, newStyleChannels);
79 return new Select(componentConfiguration, newStyleChannels);
81 return new Sensor(componentConfiguration, newStyleChannels);
83 return new Switch(componentConfiguration, newStyleChannels);
85 return new Update(componentConfiguration, newStyleChannels);
87 return new Vacuum(componentConfiguration, newStyleChannels);
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 Jinjava jinjava;
101 private final ScheduledExecutorService scheduler;
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, Jinjava jinjava,
112 ChannelStateUpdateListener updateListener, AvailabilityTracker tracker,
113 ScheduledExecutorService scheduler) {
114 this.thingUID = thingUID;
116 this.configJSON = configJSON;
118 this.jinjava = jinjava;
119 this.updateListener = updateListener;
120 this.tracker = tracker;
121 this.scheduler = scheduler;
124 public ThingUID getThingUID() {
128 public HaID getHaID() {
132 public String getConfigJSON() {
136 public ChannelStateUpdateListener getUpdateListener() {
137 return updateListener;
140 public Gson getGson() {
144 public Jinjava getJinjava() {
148 public AvailabilityTracker getTracker() {
152 public ScheduledExecutorService getScheduler() {
156 public <C extends AbstractChannelConfiguration> C getConfig(Class<C> clazz) {
157 return AbstractChannelConfiguration.fromString(configJSON, gson, clazz);