2 * Copyright (c) 2010-2021 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;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
30 import com.google.gson.Gson;
33 * A factory to create HomeAssistant MQTT components. Those components are specified at:
34 * https://www.home-assistant.io/docs/mqtt/discovery/
36 * @author David Graeff - Initial contribution
39 public class ComponentFactory {
40 private static final Logger LOGGER = LoggerFactory.getLogger(ComponentFactory.class);
43 * Create a HA MQTT component. The configuration JSon string is required.
45 * @param thingUID The Thing UID that this component will belong to.
46 * @param haID The location of this component. The HomeAssistant ID contains the object-id, node-id and
48 * @param channelConfigurationJSON Most components expect a "name", a "state_topic" and "command_topic" like with
49 * "{name:'Name',state_topic:'homeassistant/switch/0/object/state',command_topic:'homeassistant/switch/0/object/set'".
50 * @param updateListener A channel state update listener
51 * @return A HA MQTT Component
53 public static AbstractComponent<?> createComponent(ThingUID thingUID, HaID haID, String channelConfigurationJSON,
54 ChannelStateUpdateListener updateListener, AvailabilityTracker tracker, ScheduledExecutorService scheduler,
55 Gson gson, TransformationServiceProvider transformationServiceProvider) throws ConfigurationException {
56 ComponentConfiguration componentConfiguration = new ComponentConfiguration(thingUID, haID,
57 channelConfigurationJSON, gson, updateListener, tracker, scheduler)
58 .transformationProvider(transformationServiceProvider);
59 switch (haID.component) {
60 case "alarm_control_panel":
61 return new AlarmControlPanel(componentConfiguration);
63 return new BinarySensor(componentConfiguration);
65 return new Camera(componentConfiguration);
67 return new Cover(componentConfiguration);
69 return new Fan(componentConfiguration);
71 return new Climate(componentConfiguration);
73 return new Light(componentConfiguration);
75 return new Lock(componentConfiguration);
77 return new Sensor(componentConfiguration);
79 return new Switch(componentConfiguration);
81 return new Vacuum(componentConfiguration);
83 throw new UnsupportedComponentException("Component '" + haID + "' is unsupported!");
87 protected static class ComponentConfiguration {
88 private final ThingUID thingUID;
89 private final HaID haID;
90 private final String configJSON;
91 private final ChannelStateUpdateListener updateListener;
92 private final AvailabilityTracker tracker;
93 private final Gson gson;
94 private final ScheduledExecutorService scheduler;
95 private @Nullable TransformationServiceProvider transformationServiceProvider;
98 * Provide a thingUID and HomeAssistant topic ID to determine the channel group UID and type.
100 * @param thingUID A ThingUID
101 * @param haID A HomeAssistant topic ID
102 * @param configJSON The configuration string
103 * @param gson A Gson instance
105 protected ComponentConfiguration(ThingUID thingUID, HaID haID, String configJSON, Gson gson,
106 ChannelStateUpdateListener updateListener, AvailabilityTracker tracker,
107 ScheduledExecutorService scheduler) {
108 this.thingUID = thingUID;
110 this.configJSON = configJSON;
112 this.updateListener = updateListener;
113 this.tracker = tracker;
114 this.scheduler = scheduler;
117 public ComponentConfiguration transformationProvider(
118 TransformationServiceProvider transformationServiceProvider) {
119 this.transformationServiceProvider = transformationServiceProvider;
123 public ThingUID getThingUID() {
127 public HaID getHaID() {
131 public String getConfigJSON() {
135 public ChannelStateUpdateListener getUpdateListener() {
136 return updateListener;
140 public TransformationServiceProvider getTransformationServiceProvider() {
141 return transformationServiceProvider;
144 public Gson getGson() {
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);