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 org.eclipse.jdt.annotation.NonNullByDefault;
16 import org.eclipse.jdt.annotation.Nullable;
17 import org.openhab.binding.mqtt.generic.AvailabilityTracker;
18 import org.openhab.binding.mqtt.generic.ChannelStateUpdateListener;
19 import org.openhab.binding.mqtt.generic.TransformationServiceProvider;
20 import org.openhab.core.thing.ThingUID;
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
24 import com.google.gson.Gson;
27 * A factory to create HomeAssistant MQTT components. Those components are specified at:
28 * https://www.home-assistant.io/docs/mqtt/discovery/
30 * @author David Graeff - Initial contribution
33 public class CFactory {
34 private static final Logger logger = LoggerFactory.getLogger(CFactory.class);
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 configJSON 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 @Nullable AbstractComponent<?> createComponent(ThingUID thingUID, HaID haID,
48 String channelConfigurationJSON, ChannelStateUpdateListener updateListener, AvailabilityTracker tracker,
49 Gson gson, TransformationServiceProvider transformationServiceProvider) {
50 ComponentConfiguration componentConfiguration = new ComponentConfiguration(thingUID, haID,
51 channelConfigurationJSON, gson, updateListener, tracker)
52 .transformationProvider(transformationServiceProvider);
54 switch (haID.component) {
55 case "alarm_control_panel":
56 return new ComponentAlarmControlPanel(componentConfiguration);
58 return new ComponentBinarySensor(componentConfiguration);
60 return new ComponentCamera(componentConfiguration);
62 return new ComponentCover(componentConfiguration);
64 return new ComponentFan(componentConfiguration);
66 return new ComponentClimate(componentConfiguration);
68 return new ComponentLight(componentConfiguration);
70 return new ComponentLock(componentConfiguration);
72 return new ComponentSensor(componentConfiguration);
74 return new ComponentSwitch(componentConfiguration);
76 } catch (UnsupportedOperationException e) {
77 logger.warn("Not supported", e);
82 protected static class ComponentConfiguration {
83 private final ThingUID thingUID;
84 private final HaID haID;
85 private final String configJSON;
86 private final ChannelStateUpdateListener updateListener;
87 private final AvailabilityTracker tracker;
88 private final Gson gson;
89 private @Nullable TransformationServiceProvider transformationServiceProvider;
91 protected ComponentConfiguration(ThingUID thingUID, HaID haID, String configJSON, Gson gson,
92 ChannelStateUpdateListener updateListener, AvailabilityTracker tracker) {
93 this.thingUID = thingUID;
95 this.configJSON = configJSON;
97 this.updateListener = updateListener;
98 this.tracker = tracker;
101 public ComponentConfiguration transformationProvider(
102 TransformationServiceProvider transformationServiceProvider) {
103 this.transformationServiceProvider = transformationServiceProvider;
107 public ThingUID getThingUID() {
111 public HaID getHaID() {
115 public String getConfigJSON() {
119 public ChannelStateUpdateListener getUpdateListener() {
120 return updateListener;
124 public TransformationServiceProvider getTransformationServiceProvider() {
125 return transformationServiceProvider;
128 public Gson getGson() {
132 public AvailabilityTracker getTracker() {
136 public <C extends BaseChannelConfiguration> C getConfig(Class<C> clazz) {
137 return BaseChannelConfiguration.fromString(configJSON, gson, clazz);