]> git.basschouten.com Git - openhab-addons.git/blob
49be4ee9238c6603f9a24b9501e2a84a41f7530a
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
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
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.mqtt.homeassistant.internal.component;
14
15 import org.eclipse.jdt.annotation.NonNullByDefault;
16 import org.eclipse.jdt.annotation.Nullable;
17 import org.openhab.binding.mqtt.generic.values.OnOffValue;
18 import org.openhab.binding.mqtt.homeassistant.internal.config.dto.AbstractChannelConfiguration;
19
20 import com.google.gson.annotations.SerializedName;
21
22 /**
23  * A MQTT Fan component, following the https://www.home-assistant.io/components/fan.mqtt/ specification.
24  *
25  * Only ON/OFF is supported so far.
26  *
27  * @author David Graeff - Initial contribution
28  */
29 @NonNullByDefault
30 public class Fan extends AbstractComponent<Fan.ChannelConfiguration> {
31     public static final String SWITCH_CHANNEL_ID = "fan"; // Randomly chosen channel "ID"
32
33     /**
34      * Configuration class for MQTT component
35      */
36     static class ChannelConfiguration extends AbstractChannelConfiguration {
37         ChannelConfiguration() {
38             super("MQTT Fan");
39         }
40
41         @SerializedName("state_topic")
42         protected @Nullable String stateTopic;
43         @SerializedName("command_template")
44         protected @Nullable String commandTemplate;
45         @SerializedName("command_topic")
46         protected String commandTopic = "";
47         @SerializedName("payload_on")
48         protected String payloadOn = "ON";
49         @SerializedName("payload_off")
50         protected String payloadOff = "OFF";
51     }
52
53     public Fan(ComponentFactory.ComponentConfiguration componentConfiguration) {
54         super(componentConfiguration, ChannelConfiguration.class);
55
56         OnOffValue value = new OnOffValue(channelConfiguration.payloadOn, channelConfiguration.payloadOff);
57         buildChannel(SWITCH_CHANNEL_ID, value, channelConfiguration.getName(),
58                 componentConfiguration.getUpdateListener())
59                 .stateTopic(channelConfiguration.stateTopic, channelConfiguration.getValueTemplate())
60                 .commandTopic(channelConfiguration.commandTopic, channelConfiguration.isRetain(),
61                         channelConfiguration.getQos(), channelConfiguration.commandTemplate)
62                 .build();
63     }
64 }