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_SWITCH;
17 import java.math.BigDecimal;
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.openhab.binding.lutron.internal.protocol.LutronCommandType;
21 import org.openhab.core.library.types.OnOffType;
22 import org.openhab.core.thing.Bridge;
23 import org.openhab.core.thing.ChannelUID;
24 import org.openhab.core.thing.Thing;
25 import org.openhab.core.thing.ThingStatus;
26 import org.openhab.core.thing.ThingStatusDetail;
27 import org.openhab.core.types.Command;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
32 * Handler responsible for communicating with a switch.
34 * @author Allan Tong - Initial contribution
35 * @author Bob Adair - Added initDeviceState method
38 public class SwitchHandler extends LutronHandler {
39 private static final Integer ACTION_ZONELEVEL = 1;
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(ACTION_ZONELEVEL); // handleUpdate() will set thing status to online when response arrives
72 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.BRIDGE_OFFLINE);
77 public void handleCommand(ChannelUID channelUID, Command command) {
78 if (channelUID.getId().equals(CHANNEL_SWITCH)) {
79 if (command.equals(OnOffType.ON)) {
80 output(ACTION_ZONELEVEL, 100);
81 } else if (command.equals(OnOffType.OFF)) {
82 output(ACTION_ZONELEVEL, 0);
88 public int getIntegrationId() {
89 return this.integrationId;
93 public void handleUpdate(LutronCommandType type, String... parameters) {
94 if (type == LutronCommandType.OUTPUT && parameters.length > 1
95 && ACTION_ZONELEVEL.toString().equals(parameters[0])) {
96 BigDecimal level = new BigDecimal(parameters[1]);
97 if (getThing().getStatus() == ThingStatus.UNKNOWN) {
98 updateStatus(ThingStatus.ONLINE);
100 postCommand(CHANNEL_SWITCH, level.compareTo(BigDecimal.ZERO) == 0 ? OnOffType.OFF : OnOffType.ON);
105 public void channelLinked(ChannelUID channelUID) {
106 if (channelUID.getId().equals(CHANNEL_SWITCH)) {
107 // Refresh state when new item is linked.
108 queryOutput(ACTION_ZONELEVEL);