2 * Copyright (c) 2010-2021 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 @SuppressWarnings("PMD.CompareObjectsWithEquals")
111 static @Nullable Command handleCommand(ChannelUID channelUID, String channelId, Command command,
112 VeluxBridgeHandler thisBridgeHandler) {
113 LOGGER.debug("handleCommand({},{},{},{}) called.", channelUID, channelId, command, thisBridgeHandler);
114 Command newValue = null;
115 // ThingProperty sceneLevels
116 if (!ThingConfiguration.exists(thisBridgeHandler, channelUID,
117 VeluxBindingProperties.PROPERTY_VSHUTTER_SCENELEVELS)) {
118 LOGGER.trace("handleCommand(): aborting processing as scene levels are not set.");
121 String sceneLevels = (String) ThingConfiguration.getValue(thisBridgeHandler, channelUID,
122 VeluxBindingProperties.PROPERTY_VSHUTTER_SCENELEVELS);
123 // ThingProperty currentLevel
124 if (!ThingConfiguration.exists(thisBridgeHandler, channelUID,
125 VeluxBindingProperties.PROPERTY_VSHUTTER_CURRENTLEVEL)) {
126 LOGGER.trace("handleCommand(): aborting processing as current scene level is not set.");
129 // Don't know why OH2 returns BigDecimal.
130 BigDecimal rollershutterLevelBC = (BigDecimal) ThingConfiguration.getValue(thisBridgeHandler, channelUID,
131 VeluxBindingProperties.PROPERTY_VSHUTTER_CURRENTLEVEL);
132 int currentLevel = rollershutterLevelBC.intValue();
133 LOGGER.trace("handleCommand(): current level is {}.", currentLevel);
135 VeluxRSBindingConfig thisRSBindingConfig = new VeluxRSBindingConfig(VeluxItemType.VSHUTTER_POSITION,
136 sceneLevels, currentLevel);
138 if ((UpDownType) command == UpDownType.UP) {
139 currentLevel = thisRSBindingConfig.getNextDescendingLevel();
140 } else if ((UpDownType) command == UpDownType.DOWN) {
141 currentLevel = thisRSBindingConfig.getNextAscendingLevel();
143 LOGGER.info("handleCommand({},{}): ignoring command.", channelUID.getAsString(), command);
146 LOGGER.trace("handleCommand(): next level is {}.", currentLevel);
147 String sceneName = thisRSBindingConfig.getSceneName();
148 LOGGER.trace("handleCommand(): scene name is {}.", sceneName);
149 VeluxScene thisScene2 = thisBridgeHandler.bridgeParameters.scenes.getChannel().existingScenes
150 .get(new SceneName(sceneName));
151 if (thisScene2 == VeluxScene.UNKNOWN) {
153 "handleCommand(): aborting command as scene with name {} is not registered; please check your KLF scene definitions.",
157 newValue = new PercentType(currentLevel);
158 LOGGER.trace("handleCommand(): executing scene {} with index {}.", thisScene2,
159 thisScene2.getBridgeSceneIndex().toInt());
160 new VeluxBridgeRunScene().execute(thisBridgeHandler.thisBridge, thisScene2.getBridgeSceneIndex().toInt());
162 LOGGER.trace("handleCommand(): updating level to {}.", currentLevel);
163 ThingProperty.setValue(thisBridgeHandler, channelUID, VeluxBindingProperties.PROPERTY_VSHUTTER_CURRENTLEVEL,
164 thisRSBindingConfig.getLevel().toString());
165 LOGGER.trace("handleCommand() returns {}.", newValue);