]> git.basschouten.com Git - openhab-addons.git/blob
6a7ebd0868273247f51c7c4bc69f3a96e1326b62
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 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.somfytahoma.internal.handler;
14
15 import static org.openhab.binding.somfytahoma.internal.SomfyTahomaBindingConstants.*;
16
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;
26
27 /**
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.
30  *
31  * @author Ondrej Pecta - Initial contribution
32  */
33 @NonNullByDefault
34 public class SomfyTahomaAdjustableSlatsRollerShutterHandler extends SomfyTahomaBaseThingHandler {
35
36     public SomfyTahomaAdjustableSlatsRollerShutterHandler(Thing thing) {
37         super(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);
43     }
44
45     @Override
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);
53                 }
54             } else {
55                 if (chRocker != null) {
56                     updateState(chRocker.getUID(), OnOffType.OFF);
57                 }
58                 if (ch != null) {
59                     State newState = parseTahomaState(state);
60                     if (newState != null) {
61                         updateState(ch.getUID(), newState);
62                     }
63                 }
64             }
65         } else if (SLATE_ORIENTATION_STATE.equals(state.getName())) {
66             Channel ch = thing.getChannel(ORIENTATION);
67             if (ch != null) {
68                 State newState = parseTahomaState(state);
69                 if (newState != null) {
70                     updateState(ch.getUID(), newState);
71                 }
72             }
73         }
74     }
75
76     @Override
77     public void handleCommand(ChannelUID channelUID, Command command) {
78         super.handleCommand(channelUID, command);
79
80         if (command instanceof RefreshType) {
81             return;
82         }
83
84         switch (channelUID.getId()) {
85             case ROCKER:
86                 if (OnOffType.ON.equals(command)) {
87                     sendCommand(COMMAND_SET_ROCKERPOSITION);
88                 }
89                 break;
90             case CLOSURE_AND_ORIENTATION:
91                 sendCommand(COMMAND_SET_CLOSURE_ORIENTATION, "[" + command.toString() + "]");
92                 break;
93             case CONTROL:
94             case ORIENTATION:
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);
101                     } else {
102                         sendCommand(COMMAND_SET_ROCKERPOSITION);
103                     }
104                 } else {
105                     String param = (COMMAND_SET_CLOSURE.equals(cmd) || COMMAND_SET_ORIENTATION.equals(cmd))
106                             ? "[" + toInteger(command) + "]"
107                             : "[]";
108                     sendCommand(cmd, param);
109                 }
110                 break;
111             default:
112                 return;
113         }
114     }
115
116     protected String getTahomaCommand(String command, String channelId) {
117         switch (command) {
118             case "OFF":
119             case "DOWN":
120             case "CLOSE":
121                 return COMMAND_DOWN;
122             case "ON":
123             case "UP":
124             case "OPEN":
125                 return COMMAND_UP;
126             case "STOP":
127                 return COMMAND_SET_ROCKERPOSITION;
128             default:
129                 if (CONTROL.equals(channelId)) {
130                     return COMMAND_SET_CLOSURE;
131                 } else {
132                     return COMMAND_SET_ORIENTATION;
133                 }
134         }
135     }
136 }