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.velux.internal.handler;
15 import static org.openhab.binding.velux.internal.VeluxBindingConstants.CHANNEL_VSHUTTER_POSITION;
17 import java.math.BigDecimal;
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.eclipse.jdt.annotation.Nullable;
21 import org.openhab.binding.velux.internal.VeluxBindingProperties;
22 import org.openhab.binding.velux.internal.VeluxItemType;
23 import org.openhab.binding.velux.internal.VeluxRSBindingConfig;
24 import org.openhab.binding.velux.internal.bridge.VeluxBridgeRunScene;
25 import org.openhab.binding.velux.internal.handler.utils.ThingConfiguration;
26 import org.openhab.binding.velux.internal.handler.utils.ThingProperty;
27 import org.openhab.binding.velux.internal.things.VeluxScene;
28 import org.openhab.binding.velux.internal.things.VeluxScene.SceneName;
29 import org.openhab.core.library.types.PercentType;
30 import org.openhab.core.library.types.UpDownType;
31 import org.openhab.core.thing.ChannelUID;
32 import org.openhab.core.types.Command;
33 import org.openhab.core.types.State;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
38 * <B>Channel-specific retrieval and modification.</B>
40 * This class implements the Channel <B>position</B> of the Thing <B>vshutter</B> :
42 * <LI><I>Velux</I> <B>bridge</B> → <B>OpenHAB</B>:
44 * Information retrieval by method {@link #handleRefresh}.</LI>
47 * <LI><B>OpenHAB</B> Event Bus → <I>Velux</I> <B>bridge</B>
49 * Sending commands and value updates by method {@link #handleCommand}.</LI>
52 * @author Guenther Schreiner - Initial contribution.
55 final class ChannelVShutterPosition extends ChannelHandlerTemplate {
56 private static final Logger LOGGER = LoggerFactory.getLogger(ChannelVShutterPosition.class);
59 * ************************
60 * ***** Constructors *****
63 // Suppress default constructor for non-instantiability
65 private ChannelVShutterPosition() {
66 throw new AssertionError();
70 * Communication method to retrieve information to update the channel value.
72 * @param channelUID The item passed as type {@link ChannelUID} for which a refresh is intended.
73 * @param channelId The same item passed as type {@link String} for which a refresh is intended.
74 * @param thisBridgeHandler The Velux bridge handler with a specific communication protocol which provides
75 * information for this channel.
76 * @return newState The value retrieved for the passed channel, or <I>null</I> in case if there is no (new) value.
78 static @Nullable State handleRefresh(ChannelUID channelUID, String channelId,
79 VeluxBridgeHandler thisBridgeHandler) {
80 LOGGER.debug("handleRefresh({},{},{}) called.", channelUID, channelId, thisBridgeHandler);
81 assert (channelId == CHANNEL_VSHUTTER_POSITION);
82 State newState = null;
83 do { // just for common exit
84 if (!ThingConfiguration.exists(thisBridgeHandler, channelUID,
85 VeluxBindingProperties.PROPERTY_VSHUTTER_CURRENTLEVEL)) {
86 LOGGER.trace("handleRefresh(): aborting processing as current scene level is not set.");
89 // Don't know why OH2 returns BigDecimal.
90 BigDecimal rollershutterLevelBC = (BigDecimal) ThingConfiguration.getValue(thisBridgeHandler, channelUID,
91 VeluxBindingProperties.PROPERTY_VSHUTTER_CURRENTLEVEL);
92 int rollershutterLevel = rollershutterLevelBC.intValue();
93 LOGGER.trace("handleRefresh(): current level is {}.", rollershutterLevel);
94 newState = new PercentType(rollershutterLevel);
95 } while (false); // common exit
96 LOGGER.trace("handleRefresh() returns {}.", newState);
101 * Communication method to update the real world according to the passed channel value (or command).
103 * @param channelUID The item passed as type {@link ChannelUID} for which to following command is addressed to.
104 * @param channelId The same item passed as type {@link String} for which a refresh is intended.
105 * @param command The command passed as type {@link Command} for the mentioned item.
106 * @param thisBridgeHandler The Velux bridge handler with a specific communication protocol which provides
107 * information for this channel.
108 * @return newValue ...
110 static @Nullable Command handleCommand(ChannelUID channelUID, String channelId, Command command,
111 VeluxBridgeHandler thisBridgeHandler) {
112 LOGGER.debug("handleCommand({},{},{},{}) called.", channelUID, channelId, command, thisBridgeHandler);
113 Command newValue = null;
114 // ThingProperty sceneLevels
115 if (!ThingConfiguration.exists(thisBridgeHandler, channelUID,
116 VeluxBindingProperties.PROPERTY_VSHUTTER_SCENELEVELS)) {
117 LOGGER.trace("handleCommand(): aborting processing as scene levels are not set.");
120 String sceneLevels = (String) ThingConfiguration.getValue(thisBridgeHandler, channelUID,
121 VeluxBindingProperties.PROPERTY_VSHUTTER_SCENELEVELS);
122 // ThingProperty currentLevel
123 if (!ThingConfiguration.exists(thisBridgeHandler, channelUID,
124 VeluxBindingProperties.PROPERTY_VSHUTTER_CURRENTLEVEL)) {
125 LOGGER.trace("handleCommand(): aborting processing as current scene level is not set.");
128 // Don't know why OH2 returns BigDecimal.
129 BigDecimal rollershutterLevelBC = (BigDecimal) ThingConfiguration.getValue(thisBridgeHandler, channelUID,
130 VeluxBindingProperties.PROPERTY_VSHUTTER_CURRENTLEVEL);
131 int currentLevel = rollershutterLevelBC.intValue();
132 LOGGER.trace("handleCommand(): current level is {}.", currentLevel);
134 VeluxRSBindingConfig thisRSBindingConfig = new VeluxRSBindingConfig(VeluxItemType.VSHUTTER_POSITION,
135 sceneLevels, currentLevel);
137 if ((UpDownType) command == UpDownType.UP) {
138 currentLevel = thisRSBindingConfig.getNextDescendingLevel();
139 } else if ((UpDownType) command == UpDownType.DOWN) {
140 currentLevel = thisRSBindingConfig.getNextAscendingLevel();
142 LOGGER.info("handleCommand({},{}): ignoring command.", channelUID.getAsString(), command);
145 LOGGER.trace("handleCommand(): next level is {}.", currentLevel);
146 String sceneName = thisRSBindingConfig.getSceneName();
147 LOGGER.trace("handleCommand(): scene name is {}.", sceneName);
148 VeluxScene thisScene2 = thisBridgeHandler.bridgeParameters.scenes.getChannel().existingScenes
149 .get(new SceneName(sceneName));
150 if (VeluxScene.UNKNOWN.equals(thisScene2)) {
152 "handleCommand(): aborting command as scene with name {} is not registered; please check your KLF scene definitions.",
156 newValue = new PercentType(currentLevel);
157 LOGGER.trace("handleCommand(): executing scene {} with index {}.", thisScene2,
158 thisScene2.getBridgeSceneIndex().toInt());
159 new VeluxBridgeRunScene().execute(thisBridgeHandler.thisBridge, thisScene2.getBridgeSceneIndex().toInt());
161 LOGGER.trace("handleCommand(): updating level to {}.", currentLevel);
162 ThingProperty.setValue(thisBridgeHandler, channelUID, VeluxBindingProperties.PROPERTY_VSHUTTER_CURRENTLEVEL,
163 thisRSBindingConfig.getLevel().toString());
164 LOGGER.trace("handleCommand() returns {}.", newValue);