]> git.basschouten.com Git - openhab-addons.git/blob
6b216a2bcb3c1589e78ff666d6eddd6ea5d6756c
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2024 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.ComponentChannelType;
19 import org.openhab.binding.mqtt.homeassistant.internal.config.dto.AbstractChannelConfiguration;
20
21 import com.google.gson.annotations.SerializedName;
22
23 /**
24  * A MQTT Fan component, following the https://www.home-assistant.io/components/fan.mqtt/ specification.
25  *
26  * Only ON/OFF is supported so far.
27  *
28  * @author David Graeff - Initial contribution
29  */
30 @NonNullByDefault
31 public class Fan extends AbstractComponent<Fan.ChannelConfiguration> {
32     public static final String SWITCH_CHANNEL_ID = "fan"; // Randomly chosen channel "ID"
33
34     /**
35      * Configuration class for MQTT component
36      */
37     static class ChannelConfiguration extends AbstractChannelConfiguration {
38         ChannelConfiguration() {
39             super("MQTT Fan");
40         }
41
42         @SerializedName("state_topic")
43         protected @Nullable String stateTopic;
44         @SerializedName("command_template")
45         protected @Nullable String commandTemplate;
46         @SerializedName("command_topic")
47         protected String commandTopic = "";
48         @SerializedName("payload_on")
49         protected String payloadOn = "ON";
50         @SerializedName("payload_off")
51         protected String payloadOff = "OFF";
52     }
53
54     public Fan(ComponentFactory.ComponentConfiguration componentConfiguration, boolean newStyleChannels) {
55         super(componentConfiguration, ChannelConfiguration.class, newStyleChannels);
56
57         OnOffValue value = new OnOffValue(channelConfiguration.payloadOn, channelConfiguration.payloadOff);
58         buildChannel(SWITCH_CHANNEL_ID, ComponentChannelType.SWITCH, value, getName(),
59                 componentConfiguration.getUpdateListener())
60                 .stateTopic(channelConfiguration.stateTopic, channelConfiguration.getValueTemplate())
61                 .commandTopic(channelConfiguration.commandTopic, channelConfiguration.isRetain(),
62                         channelConfiguration.getQos(), channelConfiguration.commandTemplate)
63                 .build();
64     }
65 }