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.somfytahoma.internal.handler;
15 import static org.openhab.binding.somfytahoma.internal.SomfyTahomaBindingConstants.*;
17 import org.eclipse.jdt.annotation.NonNullByDefault;
18 import org.openhab.binding.somfytahoma.internal.model.SomfyTahomaState;
19 import org.openhab.core.library.types.OnOffType;
20 import org.openhab.core.thing.Channel;
21 import org.openhab.core.thing.ChannelUID;
22 import org.openhab.core.thing.Thing;
23 import org.openhab.core.types.Command;
24 import org.openhab.core.types.RefreshType;
25 import org.openhab.core.types.State;
28 * The {@link SomfyTahomaAdjustableSlatsRollerShutterHandler} is responsible for handling commands,
29 * which are sent to one of the channels of the adjustable slats roller shutter thing.
31 * @author Ondrej Pecta - Initial contribution
34 public class SomfyTahomaAdjustableSlatsRollerShutterHandler extends SomfyTahomaBaseThingHandler {
36 public SomfyTahomaAdjustableSlatsRollerShutterHandler(Thing thing) {
38 stateNames.put(CONTROL, CLOSURE_OR_ROCKER_STATE);
39 stateNames.put(ROCKER, CLOSURE_OR_ROCKER_STATE);
40 stateNames.put(ORIENTATION, SLATE_ORIENTATION_STATE);
41 // override state type because the control may return string 'rocker'
42 cacheStateType(CONTROL, TYPE_PERCENT);
46 public void updateThingChannels(SomfyTahomaState state) {
47 if (CLOSURE_OR_ROCKER_STATE.equals(state.getName())) {
48 Channel ch = thing.getChannel(CONTROL);
49 Channel chRocker = thing.getChannel(ROCKER);
50 if ("rocker".equals(state.getValue())) {
51 if (chRocker != null) {
52 updateState(chRocker.getUID(), OnOffType.ON);
55 if (chRocker != null) {
56 updateState(chRocker.getUID(), OnOffType.OFF);
59 State newState = parseTahomaState(state);
60 if (newState != null) {
61 updateState(ch.getUID(), newState);
65 } else if (SLATE_ORIENTATION_STATE.equals(state.getName())) {
66 Channel ch = thing.getChannel(ORIENTATION);
68 State newState = parseTahomaState(state);
69 if (newState != null) {
70 updateState(ch.getUID(), newState);
77 public void handleCommand(ChannelUID channelUID, Command command) {
78 super.handleCommand(channelUID, command);
80 if (command instanceof RefreshType) {
84 switch (channelUID.getId()) {
86 if (OnOffType.ON.equals(command)) {
87 sendCommand(COMMAND_SET_ROCKERPOSITION);
90 case CLOSURE_AND_ORIENTATION:
91 sendCommand(COMMAND_SET_CLOSURE_ORIENTATION, "[" + command.toString() + "]");
95 String cmd = getTahomaCommand(command.toString(), channelUID.getId());
96 if (COMMAND_SET_ROCKERPOSITION.equals(cmd)) {
97 String executionId = getCurrentExecutions();
98 if (executionId != null) {
99 // Check if the roller shutter is moving and rocker is sent => STOP it
100 cancelExecution(executionId);
102 sendCommand(COMMAND_SET_ROCKERPOSITION);
105 String param = (COMMAND_SET_CLOSURE.equals(cmd) || COMMAND_SET_ORIENTATION.equals(cmd))
106 ? "[" + toInteger(command) + "]"
108 sendCommand(cmd, param);
116 protected String getTahomaCommand(String command, String channelId) {
127 return COMMAND_SET_ROCKERPOSITION;
129 if (CONTROL.equals(channelId)) {
130 return COMMAND_SET_CLOSURE;
132 return COMMAND_SET_ORIENTATION;