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.souliss.internal.handler;
15 import java.math.BigDecimal;
17 import org.eclipse.jdt.annotation.NonNullByDefault;
18 import org.eclipse.jdt.annotation.Nullable;
19 import org.openhab.binding.souliss.internal.SoulissBindingConstants;
20 import org.openhab.binding.souliss.internal.SoulissProtocolConstants;
21 import org.openhab.core.library.types.OnOffType;
22 import org.openhab.core.library.types.PercentType;
23 import org.openhab.core.library.types.UpDownType;
24 import org.openhab.core.thing.ChannelUID;
25 import org.openhab.core.thing.Thing;
26 import org.openhab.core.thing.ThingStatus;
27 import org.openhab.core.types.Command;
28 import org.openhab.core.types.PrimitiveType;
29 import org.openhab.core.types.RefreshType;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
34 * The {@link SoulissT19Handler} is responsible for handling commands, which are
35 * sent to one of the channels.
37 * @author Tonino Fazio - Initial contribution
38 * @author Luca Calcaterra - Refactor for OH3
42 public class SoulissT19Handler extends SoulissGenericHandler {
43 private final Logger logger = LoggerFactory.getLogger(SoulissT19Handler.class);
44 byte t1nRawStateByte0 = 0xF;
45 byte t1nRawStateBrigthnessByte1 = 0x00;
49 public SoulissT19Handler(Thing thing) {
54 public void handleCommand(ChannelUID channelUID, Command command) {
55 if (command instanceof RefreshType) {
56 switch (channelUID.getId()) {
57 case SoulissBindingConstants.ONOFF_CHANNEL:
58 OnOffType valOnOff = getOhStateOnOffFromSoulissVal(t1nRawStateByte0);
59 if (valOnOff != null) {
60 updateState(channelUID, valOnOff);
63 case SoulissBindingConstants.DIMMER_BRIGHTNESS_CHANNEL:
64 updateState(SoulissBindingConstants.DIMMER_BRIGHTNESS_CHANNEL,
65 PercentType.valueOf(String.valueOf((t1nRawStateBrigthnessByte1 / 255) * 100)));
71 switch (channelUID.getId()) {
72 case SoulissBindingConstants.ONOFF_CHANNEL:
73 if (command.equals(OnOffType.ON)) {
74 commandSEND(SoulissProtocolConstants.SOULISS_T1N_ON_CMD);
76 } else if (command.equals(OnOffType.OFF)) {
77 commandSEND(SoulissProtocolConstants.SOULISS_T1N_OFF_CMD);
81 case SoulissBindingConstants.DIMMER_BRIGHTNESS_CHANNEL:
82 if (command instanceof PercentType) {
83 updateState(SoulissBindingConstants.DIMMER_BRIGHTNESS_CHANNEL, (PercentType) command);
84 commandSEND(SoulissProtocolConstants.SOULISS_T1N_SET,
85 (byte) (((PercentType) command).shortValue() * 255.00 / 100.00));
86 } else if (command.equals(OnOffType.ON)) {
87 commandSEND(SoulissProtocolConstants.SOULISS_T1N_ON_CMD);
89 } else if (command.equals(OnOffType.OFF)) {
90 commandSEND(SoulissProtocolConstants.SOULISS_T1N_OFF_CMD);
94 case SoulissBindingConstants.ROLLER_BRIGHTNESS_CHANNEL:
95 if (command.equals(UpDownType.UP)) {
96 commandSEND(SoulissProtocolConstants.SOULISS_T1N_BRIGHT_UP);
97 } else if (command.equals(UpDownType.DOWN)) {
98 commandSEND(SoulissProtocolConstants.SOULISS_T1N_BRIGHT_DOWN);
101 case SoulissBindingConstants.SLEEP_CHANNEL:
102 if (command instanceof OnOffType) {
103 commandSEND((byte) (SoulissProtocolConstants.SOULISS_T1N_TIMED + xSleepTime));
113 public void initialize() {
116 updateStatus(ThingStatus.UNKNOWN);
118 var configurationMap = getThing().getConfiguration();
119 if (configurationMap.get(SoulissBindingConstants.SLEEP_CHANNEL) != null) {
120 xSleepTime = ((BigDecimal) configurationMap.get(SoulissBindingConstants.SLEEP_CHANNEL)).byteValue();
122 if (configurationMap.get(SoulissBindingConstants.CONFIG_SECURE_SEND) != null) {
123 bSecureSend = ((Boolean) configurationMap.get(SoulissBindingConstants.CONFIG_SECURE_SEND)).booleanValue();
127 public void setState(@Nullable PrimitiveType state) {
128 super.setLastStatusStored();
130 updateState(SoulissBindingConstants.SLEEP_CHANNEL, OnOffType.OFF);
131 logger.debug("T19, setting state to {}", state.toFullString());
132 this.updateState(SoulissBindingConstants.ONOFF_CHANNEL, (OnOffType) state);
136 public void setRawStateDimmerValue(byte dimmerValue) {
138 if (dimmerValue != t1nRawStateByte0 && dimmerValue >= 0) {
139 logger.debug("T19, setting dimmer to {}", dimmerValue);
140 updateState(SoulissBindingConstants.DIMMER_BRIGHTNESS_CHANNEL,
141 PercentType.valueOf(String.valueOf(Math.round(((double) dimmerValue / 255) * 100))));
143 } catch (Exception ex) {
144 logger.warn("UUID: {}, had an update dimmer state error:{}", this.getThing().getUID().getAsString(),
150 public void setRawState(byte rawState) {
151 // update Last Status stored time
152 super.setLastStatusStored();
153 // update item state only if it is different from previous
154 if (t1nRawStateByte0 != rawState) {
155 this.setState(getOhStateOnOffFromSoulissVal(rawState));
157 t1nRawStateByte0 = rawState;
161 public byte getRawState() {
162 return t1nRawStateByte0;
165 public byte getRawStateDimmerValue() {
166 return t1nRawStateBrigthnessByte1;
170 public byte getExpectedRawState(byte bCmd) {
172 if (bCmd == SoulissProtocolConstants.SOULISS_T1N_ON_CMD) {
173 return SoulissProtocolConstants.SOULISS_T1N_ON_COIL;
174 } else if (bCmd == SoulissProtocolConstants.SOULISS_T1N_OFF_CMD) {
175 return SoulissProtocolConstants.SOULISS_T1N_OFF_COIL;
176 } else if (bCmd >= SoulissProtocolConstants.SOULISS_T1N_TIMED) {
178 return SoulissProtocolConstants.SOULISS_T1N_ON_COIL;