]> git.basschouten.com Git - openhab-addons.git/blob
c7a43e57bbd8ce3a9bb9225989751b3324016a17
[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.openhab.binding.mqtt.generic.values.TextValue;
17 import org.openhab.binding.mqtt.homeassistant.internal.ComponentChannel;
18 import org.openhab.binding.mqtt.homeassistant.internal.ComponentChannelType;
19 import org.openhab.core.library.types.HSBType;
20 import org.openhab.core.library.types.OnOffType;
21 import org.openhab.core.library.types.PercentType;
22 import org.openhab.core.types.Command;
23
24 /**
25  * A base class for common elements between JSON schema and template schema lights.
26  *
27  * @author Cody Cutrer - Initial contribution
28  */
29 @NonNullByDefault
30 abstract class AbstractRawSchemaLight extends Light {
31     protected static final String RAW_CHANNEL_ID = "raw";
32
33     protected ComponentChannel rawChannel;
34
35     public AbstractRawSchemaLight(ComponentFactory.ComponentConfiguration builder, boolean newStyleChannels) {
36         super(builder, newStyleChannels);
37         hiddenChannels.add(rawChannel = buildChannel(RAW_CHANNEL_ID, ComponentChannelType.STRING, new TextValue(),
38                 "Raw state", this).stateTopic(channelConfiguration.stateTopic)
39                 .commandTopic(channelConfiguration.commandTopic, channelConfiguration.isRetain(),
40                         channelConfiguration.getQos())
41                 .build(false));
42     }
43
44     protected boolean handleCommand(Command command) {
45         HSBType newState;
46         if (colorValue.getChannelState() instanceof HSBType) {
47             newState = (HSBType) colorValue.getChannelState();
48         } else {
49             newState = HSBType.WHITE;
50         }
51
52         if (command.equals(PercentType.ZERO) || command.equals(OnOffType.OFF)) {
53             newState = HSBType.BLACK;
54         } else if (command.equals(OnOffType.ON)) {
55             if (newState.getBrightness().equals(PercentType.ZERO)) {
56                 newState = new HSBType(newState.getHue(), newState.getSaturation(), PercentType.HUNDRED);
57             }
58         } else if (command instanceof HSBType hsb) {
59             newState = hsb;
60         } else if (command instanceof PercentType brightness) {
61             newState = new HSBType(newState.getHue(), newState.getSaturation(), brightness);
62         } else {
63             return false;
64         }
65
66         publishState(newState);
67         return false;
68     }
69
70     protected abstract void publishState(HSBType state);
71 }