2 * Copyright (c) 2010-2020 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.lutron.internal.handler;
15 import static org.openhab.binding.lutron.internal.LutronBindingConstants.CHANNEL_VARSTATE;
17 import java.math.BigDecimal;
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.eclipse.jdt.annotation.Nullable;
21 import org.openhab.binding.lutron.internal.config.SysvarConfig;
22 import org.openhab.binding.lutron.internal.protocol.LutronCommandType;
23 import org.openhab.core.library.types.DecimalType;
24 import org.openhab.core.thing.Bridge;
25 import org.openhab.core.thing.ChannelUID;
26 import org.openhab.core.thing.Thing;
27 import org.openhab.core.thing.ThingStatus;
28 import org.openhab.core.thing.ThingStatusDetail;
29 import org.openhab.core.types.Command;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
34 * Handler responsible for getting/setting sysvar state for HomeWorks QS
36 * @author Bob Adair - Initial contribution
40 public class SysvarHandler extends LutronHandler {
41 private static final Integer ACTION_GETSETSYSVAR = 1;
43 private final Logger logger = LoggerFactory.getLogger(SysvarHandler.class);
45 private @Nullable SysvarConfig config;
46 private int integrationId;
48 public SysvarHandler(Thing thing) {
53 public int getIntegrationId() {
54 SysvarConfig config = this.config;
56 return config.integrationId;
58 throw new IllegalStateException("handler not initialized");
63 public void initialize() {
64 SysvarConfig config = getThing().getConfiguration().as(SysvarConfig.class);
66 if (config.integrationId <= 0) {
67 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, "No integrationId configured");
69 integrationId = config.integrationId;
70 logger.debug("Initializing Sysvar handler for integration ID {}", integrationId);
76 protected void initDeviceState() {
77 logger.debug("Initializing handler state for sysvar id {}", integrationId);
78 Bridge bridge = getBridge();
80 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, "No bridge configured");
81 } else if (bridge.getStatus() == ThingStatus.ONLINE) {
82 updateStatus(ThingStatus.UNKNOWN, ThingStatusDetail.NONE, "Awaiting initial response");
83 querySysvar(ACTION_GETSETSYSVAR); // handleUpdate() will set thing status to online when response arrives
85 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.BRIDGE_OFFLINE);
90 public void channelLinked(ChannelUID channelUID) {
91 if (channelUID.getId().equals(CHANNEL_VARSTATE)) {
92 // Refresh state when new item is linked.
93 querySysvar(ACTION_GETSETSYSVAR);
98 public void handleCommand(ChannelUID channelUID, Command command) {
99 if (channelUID.getId().equals(CHANNEL_VARSTATE)) {
100 if (command instanceof Number) {
101 int state = ((Number) command).intValue();
102 sysvar(ACTION_GETSETSYSVAR, state);
108 public void handleUpdate(LutronCommandType type, String... parameters) {
109 if (type == LutronCommandType.SYSVAR && parameters.length > 1
110 && ACTION_GETSETSYSVAR.toString().equals(parameters[0])) {
111 BigDecimal state = new BigDecimal(parameters[1]);
112 if (getThing().getStatus() == ThingStatus.UNKNOWN) {
113 updateStatus(ThingStatus.ONLINE);
115 updateState(CHANNEL_VARSTATE, new DecimalType(state));