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.openhab.binding.souliss.internal.SoulissBindingConstants;
19 import org.openhab.binding.souliss.internal.SoulissProtocolConstants;
20 import org.openhab.core.library.types.OnOffType;
21 import org.openhab.core.thing.ChannelUID;
22 import org.openhab.core.thing.Thing;
23 import org.openhab.core.thing.ThingStatus;
24 import org.openhab.core.types.Command;
25 import org.openhab.core.types.PrimitiveType;
26 import org.openhab.core.types.RefreshType;
29 * The {@link SoulissT12Handler} is responsible for handling commands, which are
30 * sent to one of the channels.
32 * @author Tonino Fazio - Initial contribution
33 * @author Luca Calcaterra - Refactor for OH3
36 public class SoulissT12Handler extends SoulissGenericHandler {
38 private byte t1nRawState = 0xF;
39 private byte xSleepTime = 0;
41 public SoulissT12Handler(Thing thing) {
46 public void initialize() {
49 updateStatus(ThingStatus.UNKNOWN);
51 var configurationMap = getThing().getConfiguration();
52 if (configurationMap.get(SoulissBindingConstants.SLEEP_CHANNEL) != null) {
53 xSleepTime = ((BigDecimal) configurationMap.get(SoulissBindingConstants.SLEEP_CHANNEL)).byteValue();
55 if (configurationMap.get(SoulissBindingConstants.CONFIG_SECURE_SEND) != null) {
56 bSecureSend = ((Boolean) configurationMap.get(SoulissBindingConstants.CONFIG_SECURE_SEND)).booleanValue();
61 public void handleCommand(ChannelUID channelUID, Command command) {
62 if (command instanceof RefreshType) {
63 switch (channelUID.getId()) {
64 case SoulissBindingConstants.ONOFF_CHANNEL:
65 switch (t1nRawState) {
66 case SoulissProtocolConstants.SOULISS_T1N_ON_COIL_AUTO:
67 case SoulissProtocolConstants.SOULISS_T1N_ON_COIL:
68 this.setState(OnOffType.ON);
70 case SoulissProtocolConstants.SOULISS_T1N_OFF_COIL_AUTO:
71 case SoulissProtocolConstants.SOULISS_T1N_OFF_COIL:
72 this.setState(OnOffType.OFF);
78 case SoulissBindingConstants.AUTOMODE_CHANNEL:
79 switch (t1nRawState) {
80 case SoulissProtocolConstants.SOULISS_T1N_ON_COIL_AUTO:
81 case SoulissProtocolConstants.SOULISS_T1N_OFF_COIL_AUTO:
82 this.setStateAutomode(OnOffType.ON);
84 case SoulissProtocolConstants.SOULISS_T1N_ON_COIL:
85 case SoulissProtocolConstants.SOULISS_T1N_OFF_COIL:
86 this.setStateAutomode(OnOffType.OFF);
98 switch (channelUID.getId()) {
99 case SoulissBindingConstants.ONOFF_CHANNEL:
100 if (command.equals(OnOffType.ON)) {
101 commandSEND(SoulissProtocolConstants.SOULISS_T1N_ON_CMD);
102 } else if (command.equals(OnOffType.OFF)) {
103 commandSEND(SoulissProtocolConstants.SOULISS_T1N_OFF_CMD);
106 case SoulissBindingConstants.AUTOMODE_CHANNEL:
107 if (command.equals(OnOffType.ON)) {
108 commandSEND(SoulissProtocolConstants.SOULISS_T1N_AUTO_CMD);
111 case SoulissBindingConstants.SLEEP_CHANNEL:
112 if (command.equals(OnOffType.ON)) {
113 commandSEND((byte) (SoulissProtocolConstants.SOULISS_T1N_TIMED + xSleepTime));
115 updateState(channelUID, OnOffType.OFF);
125 public void setState(PrimitiveType state) {
126 this.updateState(SoulissBindingConstants.ONOFF_CHANNEL, (OnOffType) state);
129 public void setStateAutomode(PrimitiveType state) {
130 this.updateState(SoulissBindingConstants.AUTOMODE_CHANNEL, (OnOffType) state);
134 public void setRawState(byte rawState) {
135 // update Last Status stored time
136 super.setLastStatusStored();
138 // update item state only if it is different from previous
139 if (t1nRawState != rawState) {
140 if (rawState == SoulissProtocolConstants.SOULISS_T1N_ON_COIL_AUTO) {
141 this.setState(OnOffType.ON);
142 this.setStateAutomode(OnOffType.ON);
143 } else if (rawState == SoulissProtocolConstants.SOULISS_T1N_OFF_COIL_AUTO) {
144 this.setState(OnOffType.OFF);
145 this.setStateAutomode(OnOffType.ON);
146 } else if (rawState == SoulissProtocolConstants.SOULISS_T1N_ON_COIL) {
147 this.setState(OnOffType.ON);
148 this.setStateAutomode(OnOffType.OFF);
149 } else if (rawState == SoulissProtocolConstants.SOULISS_T1N_OFF_COIL) {
150 this.setState(OnOffType.OFF);
151 this.setStateAutomode(OnOffType.OFF);
154 t1nRawState = rawState;
158 public byte getRawState() {
163 public byte getExpectedRawState(byte bCommand) {
165 if (bCommand == SoulissProtocolConstants.SOULISS_T1N_ON_CMD) {
166 return SoulissProtocolConstants.SOULISS_T1N_ON_COIL;
167 } else if (bCommand == SoulissProtocolConstants.SOULISS_T1N_OFF_CMD) {
168 return SoulissProtocolConstants.SOULISS_T1N_OFF_COIL;
169 } else if (bCommand >= SoulissProtocolConstants.SOULISS_T1N_TIMED) {
171 return SoulissProtocolConstants.SOULISS_T1N_ON_COIL;