2 * Copyright (c) 2010-2023 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.component;
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;
24 * A base class for common elements between JSON schema and template schema lights.
26 * @author Cody Cutrer - Initial contribution
29 abstract class AbstractRawSchemaLight extends Light {
30 protected static final String RAW_CHANNEL_ID = "raw";
32 protected ComponentChannel rawChannel;
34 public AbstractRawSchemaLight(ComponentFactory.ComponentConfiguration 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())
42 protected boolean handleCommand(Command command) {
44 if (colorValue.getChannelState() instanceof HSBType) {
45 newState = (HSBType) colorValue.getChannelState();
47 newState = HSBType.WHITE;
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);
56 } else if (command instanceof HSBType) {
57 newState = (HSBType) command;
58 } else if (command instanceof PercentType) {
59 newState = new HSBType(newState.getHue(), newState.getSaturation(), (PercentType) command);
64 publishState(newState);
68 protected abstract void publishState(HSBType state);