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 org.eclipse.jdt.annotation.NonNullByDefault;
16 import org.eclipse.jdt.annotation.Nullable;
17 import org.openhab.binding.velux.internal.VeluxBindingProperties;
18 import org.openhab.binding.velux.internal.bridge.VeluxBridgeRunScene;
19 import org.openhab.binding.velux.internal.handler.utils.ThingConfiguration;
20 import org.openhab.binding.velux.internal.things.VeluxProductVelocity;
21 import org.openhab.binding.velux.internal.things.VeluxScene;
22 import org.openhab.binding.velux.internal.things.VeluxScene.SceneName;
23 import org.openhab.core.library.types.OnOffType;
24 import org.openhab.core.thing.ChannelUID;
25 import org.openhab.core.types.Command;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
30 * <B>Channel-specific retrieval and modification.</B>
32 * This class implements the Channel <B>action</B> of the Thing <B>scene</B> :
34 * <LI><I>Velux</I> <B>bridge</B> → <B>OpenHAB</B>:
36 * Information retrieval by method {@link #handleRefresh}.</LI>
39 * <LI><B>OpenHAB</B> Event Bus → <I>Velux</I> <B>bridge</B>
41 * Sending commands and value updates by method {@link #handleCommand}.</LI>
44 * @author Guenther Schreiner - Initial contribution.
47 final class ChannelSceneAction extends ChannelHandlerTemplate {
48 private static final Logger LOGGER = LoggerFactory.getLogger(ChannelSceneAction.class);
51 * ************************
52 * ***** Constructors *****
55 // Suppress default constructor for non-instantiability
57 private ChannelSceneAction() {
58 throw new AssertionError();
62 * Communication method to retrieve information to update the channel value.
64 * @param channelUID The item passed as type {@link ChannelUID} for which a refresh is intended.
65 * @param channelId The same item passed as type {@link String} for which a refresh is intended.
66 * @param thisBridgeHandler The Velux bridge handler with a specific communication protocol which provides
67 * information for this channel.
68 * @return newState The value retrieved for the passed channel, or <I>null</I> in case if there is no (new) value.
72 * Communication method to update the real world according to the passed channel value (or command).
74 * @param channelUID The item passed as type {@link ChannelUID} for which to following command is addressed to.
75 * @param channelId The same item passed as type {@link String} for which a refresh is intended.
76 * @param command The command passed as type {@link Command} for the mentioned item.
77 * @param thisBridgeHandler The Velux bridge handler with a specific communication protocol which provides
78 * information for this channel.
79 * @return newValue ...
81 static @Nullable Command handleCommand(ChannelUID channelUID, String channelId, Command command,
82 VeluxBridgeHandler thisBridgeHandler) {
83 LOGGER.debug("handleCommand({},{},{},{}) called.", channelUID, channelId, command, thisBridgeHandler);
84 Command newValue = null;
85 do { // just for common exit
86 if (command != OnOffType.ON) {
87 LOGGER.trace("handleCommand(): ignoring OFF command.");
90 if (!ThingConfiguration.exists(thisBridgeHandler, channelUID, VeluxBindingProperties.PROPERTY_SCENE_NAME)) {
91 LOGGER.trace("handleCommand(): aborting processing as scene name is not set.");
94 String sceneName = (String) ThingConfiguration.getValue(thisBridgeHandler, channelUID,
95 VeluxBindingProperties.PROPERTY_SCENE_NAME);
96 if (!thisBridgeHandler.bridgeParameters.scenes.getChannel().existingScenes
97 .isRegistered(new SceneName(sceneName))) {
98 LOGGER.info("handleCommand({},{}): cannot activate unknown scene: {}.", channelUID.getAsString(),
102 String velocityName = (String) ThingConfiguration.getValue(thisBridgeHandler, channelUID,
103 VeluxBindingProperties.PROPERTY_SCENE_VELOCITY);
104 LOGGER.debug("handleCommand(): activating known scene {}.", sceneName);
105 VeluxScene thisScene = thisBridgeHandler.bridgeParameters.scenes.getChannel().existingScenes
106 .get(new SceneName(sceneName));
107 LOGGER.trace("handleCommand(): execution of scene {}.", thisScene);
108 if (VeluxProductVelocity.getByName(velocityName) == VeluxProductVelocity.UNDEFTYPE) {
109 new VeluxBridgeRunScene().execute(thisBridgeHandler.thisBridge,
110 thisScene.getBridgeSceneIndex().toInt());
112 LOGGER.trace("handleCommand(): using velocity {}.",
113 VeluxProductVelocity.getByName(velocityName).toString());
114 new VeluxBridgeRunScene().execute(thisBridgeHandler.thisBridge, thisScene.getBridgeSceneIndex().toInt(),
115 VeluxProductVelocity.getByName(velocityName).getVelocity());
117 LOGGER.trace("handleCommand(): execution of scene finished.");
118 } while (false); // common exit