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.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.SysvarCommand;
23 import org.openhab.binding.lutron.internal.protocol.lip.LutronCommandType;
24 import org.openhab.core.library.types.DecimalType;
25 import org.openhab.core.thing.Bridge;
26 import org.openhab.core.thing.ChannelUID;
27 import org.openhab.core.thing.Thing;
28 import org.openhab.core.thing.ThingStatus;
29 import org.openhab.core.thing.ThingStatusDetail;
30 import org.openhab.core.types.Command;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
35 * Handler responsible for getting/setting sysvar state for HomeWorks QS
37 * @author Bob Adair - Initial contribution
41 public class SysvarHandler extends LutronHandler {
42 private final Logger logger = LoggerFactory.getLogger(SysvarHandler.class);
44 private @Nullable SysvarConfig config;
45 private int integrationId;
47 public SysvarHandler(Thing thing) {
52 public int getIntegrationId() {
53 SysvarConfig config = this.config;
55 return config.integrationId;
57 throw new IllegalStateException("handler not initialized");
62 public void initialize() {
63 SysvarConfig config = getThing().getConfiguration().as(SysvarConfig.class);
65 if (config.integrationId <= 0) {
66 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, "No integrationId configured");
68 integrationId = config.integrationId;
69 logger.debug("Initializing Sysvar handler for integration ID {}", integrationId);
75 protected void initDeviceState() {
76 logger.debug("Initializing handler state for sysvar id {}", integrationId);
77 Bridge bridge = getBridge();
79 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, "No bridge configured");
80 } else if (bridge.getStatus() == ThingStatus.ONLINE) {
81 updateStatus(ThingStatus.UNKNOWN, ThingStatusDetail.NONE, "Awaiting initial response");
82 querySysvar(SysvarCommand.ACTION_GETSETSYSVAR);
83 // 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(SysvarCommand.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(SysvarCommand.ACTION_GETSETSYSVAR, state);
108 public void handleUpdate(LutronCommandType type, String... parameters) {
109 if (type == LutronCommandType.SYSVAR && parameters.length > 1
110 && SysvarCommand.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));