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 org.eclipse.jdt.annotation.NonNullByDefault;
16 import org.eclipse.jdt.annotation.Nullable;
17 import org.openhab.binding.lutron.internal.protocol.LutronCommand;
18 import org.openhab.binding.lutron.internal.protocol.LutronCommandType;
19 import org.openhab.binding.lutron.internal.protocol.LutronOperation;
20 import org.openhab.core.thing.Bridge;
21 import org.openhab.core.thing.Thing;
22 import org.openhab.core.thing.ThingStatus;
23 import org.openhab.core.thing.ThingStatusDetail;
24 import org.openhab.core.thing.ThingStatusInfo;
25 import org.openhab.core.thing.binding.BaseThingHandler;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
30 * Base type for all Lutron thing handlers.
32 * @author Allan Tong - Initial contribution
33 * @author Bob Adair - Added additional commands and methods for status and state management
37 public abstract class LutronHandler extends BaseThingHandler {
38 private final Logger logger = LoggerFactory.getLogger(LutronHandler.class);
40 public LutronHandler(Thing thing) {
44 public abstract int getIntegrationId();
46 public abstract void handleUpdate(LutronCommandType type, String... parameters);
49 * Queries for any device state needed at initialization time or after losing connectivity to the bridge, and
50 * updates device status. Will be called when bridge status changes to ONLINE and thing has status
51 * OFFLINE:BRIDGE_OFFLINE.
53 protected abstract void initDeviceState();
56 * Called when changing thing status to offline. Subclasses may override to take any needed actions.
58 protected void thingOfflineNotify() {
61 protected @Nullable IPBridgeHandler getBridgeHandler() {
62 Bridge bridge = getBridge();
64 return bridge == null ? null : (IPBridgeHandler) bridge.getHandler();
68 public void bridgeStatusChanged(ThingStatusInfo bridgeStatusInfo) {
69 logger.debug("Bridge status changed to {} for lutron device handler {}", bridgeStatusInfo.getStatus(),
72 if (bridgeStatusInfo.getStatus() == ThingStatus.ONLINE
73 && getThing().getStatusInfo().getStatusDetail() == ThingStatusDetail.BRIDGE_OFFLINE) {
76 } else if (bridgeStatusInfo.getStatus() == ThingStatus.OFFLINE) {
77 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.BRIDGE_OFFLINE);
82 private void sendCommand(LutronCommand command) {
83 IPBridgeHandler bridgeHandler = getBridgeHandler();
85 if (bridgeHandler == null) {
86 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.HANDLER_MISSING_ERROR, "No bridge associated");
89 bridgeHandler.sendCommand(command);
93 protected void output(Object... parameters) {
95 new LutronCommand(LutronOperation.EXECUTE, LutronCommandType.OUTPUT, getIntegrationId(), parameters));
98 protected void device(Object... parameters) {
100 new LutronCommand(LutronOperation.EXECUTE, LutronCommandType.DEVICE, getIntegrationId(), parameters));
103 protected void timeclock(Object... parameters) {
104 sendCommand(new LutronCommand(LutronOperation.EXECUTE, LutronCommandType.TIMECLOCK, getIntegrationId(),
108 protected void greenMode(Object... parameters) {
109 sendCommand(new LutronCommand(LutronOperation.EXECUTE, LutronCommandType.MODE, getIntegrationId(), parameters));
112 protected void sysvar(Object... parameters) {
114 new LutronCommand(LutronOperation.EXECUTE, LutronCommandType.SYSVAR, getIntegrationId(), parameters));
117 protected void shadegrp(Object... parameters) {
119 new LutronCommand(LutronOperation.EXECUTE, LutronCommandType.SHADEGRP, getIntegrationId(), parameters));
122 protected void queryOutput(Object... parameters) {
123 sendCommand(new LutronCommand(LutronOperation.QUERY, LutronCommandType.OUTPUT, getIntegrationId(), parameters));
126 protected void queryDevice(Object... parameters) {
127 sendCommand(new LutronCommand(LutronOperation.QUERY, LutronCommandType.DEVICE, getIntegrationId(), parameters));
130 protected void queryTimeclock(Object... parameters) {
132 new LutronCommand(LutronOperation.QUERY, LutronCommandType.TIMECLOCK, getIntegrationId(), parameters));
135 protected void queryGreenMode(Object... parameters) {
136 sendCommand(new LutronCommand(LutronOperation.QUERY, LutronCommandType.MODE, getIntegrationId(), parameters));
139 protected void querySysvar(Object... parameters) {
140 sendCommand(new LutronCommand(LutronOperation.QUERY, LutronCommandType.SYSVAR, getIntegrationId(), parameters));
143 protected void queryShadegrp(Object... parameters) {
145 new LutronCommand(LutronOperation.QUERY, LutronCommandType.SHADEGRP, getIntegrationId(), parameters));