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_SWITCH;
17 import java.math.BigDecimal;
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.openhab.binding.lutron.internal.protocol.OutputCommand;
21 import org.openhab.binding.lutron.internal.protocol.lip.LutronCommandType;
22 import org.openhab.binding.lutron.internal.protocol.lip.TargetType;
23 import org.openhab.core.library.types.OnOffType;
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 communicating with a switch.
36 * @author Allan Tong - Initial contribution
37 * @author Bob Adair - Added initDeviceState method
40 public class SwitchHandler extends LutronHandler {
41 private final Logger logger = LoggerFactory.getLogger(SwitchHandler.class);
43 private int integrationId;
45 public SwitchHandler(Thing thing) {
50 public void initialize() {
51 Number id = (Number) getThing().getConfiguration().get("integrationId");
53 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, "No integrationId");
56 integrationId = id.intValue();
57 logger.debug("Initializing Switch handler for integration ID {}", id);
63 protected void initDeviceState() {
64 logger.debug("Initializing device state for Switch {}", getIntegrationId());
65 Bridge bridge = getBridge();
67 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, "No bridge configured");
68 } else if (bridge.getStatus() == ThingStatus.ONLINE) {
69 updateStatus(ThingStatus.UNKNOWN, ThingStatusDetail.NONE, "Awaiting initial response");
70 queryOutput(TargetType.SWITCH, OutputCommand.ACTION_ZONELEVEL);
71 // handleUpdate() will set thing status to online when response arrives
73 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.BRIDGE_OFFLINE);
78 public void handleCommand(ChannelUID channelUID, Command command) {
79 if (channelUID.getId().equals(CHANNEL_SWITCH)) {
80 if (command.equals(OnOffType.ON)) {
81 output(TargetType.SWITCH, OutputCommand.ACTION_ZONELEVEL, 100, null, null);
82 } else if (command.equals(OnOffType.OFF)) {
83 output(TargetType.SWITCH, OutputCommand.ACTION_ZONELEVEL, 0, null, null);
89 public int getIntegrationId() {
90 return this.integrationId;
94 public void handleUpdate(LutronCommandType type, String... parameters) {
95 if (type == LutronCommandType.OUTPUT && parameters.length > 1
96 && OutputCommand.ACTION_ZONELEVEL.toString().equals(parameters[0])) {
97 BigDecimal level = new BigDecimal(parameters[1]);
98 if (getThing().getStatus() == ThingStatus.UNKNOWN) {
99 updateStatus(ThingStatus.ONLINE);
101 postCommand(CHANNEL_SWITCH, OnOffType.from(level.compareTo(BigDecimal.ZERO) != 0));
106 public void channelLinked(ChannelUID channelUID) {
107 if (channelUID.getId().equals(CHANNEL_SWITCH)) {
108 // Refresh state when new item is linked.
109 queryOutput(TargetType.SWITCH, OutputCommand.ACTION_ZONELEVEL);