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;
16 import java.util.Map.Entry;
18 import org.eclipse.jdt.annotation.NonNullByDefault;
19 import org.openhab.binding.velux.internal.config.VeluxThingConfiguration;
20 import org.openhab.binding.velux.internal.handler.utils.ExtendedBaseThingHandler;
21 import org.openhab.binding.velux.internal.utils.Localization;
22 import org.openhab.core.config.core.Configuration;
23 import org.openhab.core.thing.Bridge;
24 import org.openhab.core.thing.ChannelUID;
25 import org.openhab.core.thing.Thing;
26 import org.openhab.core.thing.ThingStatus;
27 import org.openhab.core.thing.ThingStatusDetail;
28 import org.openhab.core.thing.binding.BridgeHandler;
29 import org.openhab.core.types.Command;
30 import org.openhab.core.types.RefreshType;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
35 * The{@link VeluxHandler} is responsible for handling commands, which are
36 * sent via {@link VeluxBridgeHandler} to one of the channels.
38 * @author Guenther Schreiner - Initial contribution
41 public class VeluxHandler extends ExtendedBaseThingHandler {
42 private final Logger logger = LoggerFactory.getLogger(VeluxHandler.class);
44 VeluxThingConfiguration configuration = new VeluxThingConfiguration();
46 public VeluxHandler(Thing thing, Localization localization) {
48 logger.trace("VeluxHandler(thing={},localization={}) constructor called.", thing, localization);
52 public void initialize() {
53 logger.trace("initialize() called.");
54 Bridge thisBridge = getBridge();
55 logger.debug("initialize(): Initializing thing {} in combination with bridge {}.", getThing().getUID(),
57 if (thisBridge == null) {
58 logger.trace("initialize() updating ThingStatus to OFFLINE/CONFIGURATION_PENDING.");
59 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_PENDING);
61 } else if (thisBridge.getStatus() == ThingStatus.ONLINE) {
62 logger.trace("initialize() updating ThingStatus to ONLINE.");
63 updateStatus(ThingStatus.ONLINE);
64 initializeProperties();
66 logger.trace("initialize() updating ThingStatus to OFFLINE/BRIDGE_OFFLINE.");
67 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.BRIDGE_OFFLINE);
69 logger.trace("initialize() done.");
72 private synchronized void initializeProperties() {
73 configuration = getConfigAs(VeluxThingConfiguration.class);
74 logger.trace("initializeProperties() done.");
78 public void dispose() {
79 logger.trace("dispose() called.");
84 public void channelLinked(ChannelUID channelUID) {
85 logger.trace("channelLinked({}) called.", channelUID.getAsString());
87 if (thing.getStatus() == ThingStatus.ONLINE) {
88 handleCommand(channelUID, RefreshType.REFRESH);
93 public void handleCommand(ChannelUID channelUID, Command command) {
94 logger.trace("handleCommand({},{}) initiated by {}.", channelUID.getAsString(), command,
95 Thread.currentThread());
96 Bridge bridge = getBridge();
98 logger.trace("handleCommand() nothing yet to do as there is no bridge available.");
100 BridgeHandler handler = bridge.getHandler();
101 if (handler == null) {
102 logger.trace("handleCommand() nothing yet to do as thing is not initialized.");
104 handler.handleCommand(channelUID, command);
107 logger.trace("handleCommand() done.");
111 public void handleConfigurationUpdate(Map<String, Object> configurationParameters) {
112 if (isInitialized()) { // prevents change of address
113 validateConfigurationParameters(configurationParameters);
114 Configuration configuration = editConfiguration();
115 for (Entry<String, Object> configurationParameter : configurationParameters.entrySet()) {
116 logger.trace("handleConfigurationUpdate(): found modified config entry {}.",
117 configurationParameter.getKey());
118 configuration.put(configurationParameter.getKey(), configurationParameter.getValue());
120 // persist new configuration and reinitialize handler
122 updateConfiguration(configuration);
125 super.handleConfigurationUpdate(configurationParameters);