2 * Copyright (c) 2010-2020 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;
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.core.thing.ThingUID;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
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 CFactory {
36 private static final Logger logger = LoggerFactory.getLogger(CFactory.class);
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 configJSON 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 @Nullable AbstractComponent<?> createComponent(ThingUID thingUID, HaID haID,
50 String channelConfigurationJSON, ChannelStateUpdateListener updateListener, AvailabilityTracker tracker,
51 ScheduledExecutorService scheduler, Gson gson,
52 TransformationServiceProvider transformationServiceProvider) {
53 ComponentConfiguration componentConfiguration = new ComponentConfiguration(thingUID, haID,
54 channelConfigurationJSON, gson, updateListener, tracker, scheduler)
55 .transformationProvider(transformationServiceProvider);
57 switch (haID.component) {
58 case "alarm_control_panel":
59 return new ComponentAlarmControlPanel(componentConfiguration);
61 return new ComponentBinarySensor(componentConfiguration);
63 return new ComponentCamera(componentConfiguration);
65 return new ComponentCover(componentConfiguration);
67 return new ComponentFan(componentConfiguration);
69 return new ComponentClimate(componentConfiguration);
71 return new ComponentLight(componentConfiguration);
73 return new ComponentLock(componentConfiguration);
75 return new ComponentSensor(componentConfiguration);
77 return new ComponentSwitch(componentConfiguration);
79 } catch (UnsupportedOperationException e) {
80 logger.warn("Not supported", e);
85 protected static class ComponentConfiguration {
86 private final ThingUID thingUID;
87 private final HaID haID;
88 private final String configJSON;
89 private final ChannelStateUpdateListener updateListener;
90 private final AvailabilityTracker tracker;
91 private final Gson gson;
92 private final ScheduledExecutorService scheduler;
93 private @Nullable TransformationServiceProvider transformationServiceProvider;
95 protected ComponentConfiguration(ThingUID thingUID, HaID haID, String configJSON, Gson gson,
96 ChannelStateUpdateListener updateListener, AvailabilityTracker tracker,
97 ScheduledExecutorService scheduler) {
98 this.thingUID = thingUID;
100 this.configJSON = configJSON;
102 this.updateListener = updateListener;
103 this.tracker = tracker;
104 this.scheduler = scheduler;
107 public ComponentConfiguration transformationProvider(
108 TransformationServiceProvider transformationServiceProvider) {
109 this.transformationServiceProvider = transformationServiceProvider;
113 public ThingUID getThingUID() {
117 public HaID getHaID() {
121 public String getConfigJSON() {
125 public ChannelStateUpdateListener getUpdateListener() {
126 return updateListener;
130 public TransformationServiceProvider getTransformationServiceProvider() {
131 return transformationServiceProvider;
134 public Gson getGson() {
138 public AvailabilityTracker getTracker() {
142 public ScheduledExecutorService getScheduler() {
146 public <C extends BaseChannelConfiguration> C getConfig(Class<C> clazz) {
147 return BaseChannelConfiguration.fromString(configJSON, gson, clazz);