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