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 org.eclipse.jdt.annotation.NonNullByDefault;
16 import org.eclipse.jdt.annotation.Nullable;
17 import org.openhab.binding.lutron.internal.protocol.DeviceCommand;
18 import org.openhab.binding.lutron.internal.protocol.GroupCommand;
19 import org.openhab.binding.lutron.internal.protocol.LutronCommandNew;
20 import org.openhab.binding.lutron.internal.protocol.LutronDuration;
21 import org.openhab.binding.lutron.internal.protocol.ModeCommand;
22 import org.openhab.binding.lutron.internal.protocol.OutputCommand;
23 import org.openhab.binding.lutron.internal.protocol.SysvarCommand;
24 import org.openhab.binding.lutron.internal.protocol.TimeclockCommand;
25 import org.openhab.binding.lutron.internal.protocol.lip.LutronCommandType;
26 import org.openhab.binding.lutron.internal.protocol.lip.LutronOperation;
27 import org.openhab.binding.lutron.internal.protocol.lip.TargetType;
28 import org.openhab.core.thing.Bridge;
29 import org.openhab.core.thing.Thing;
30 import org.openhab.core.thing.ThingStatus;
31 import org.openhab.core.thing.ThingStatusDetail;
32 import org.openhab.core.thing.ThingStatusInfo;
33 import org.openhab.core.thing.binding.BaseThingHandler;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
38 * Base type for all Lutron thing handlers.
40 * @author Allan Tong - Initial contribution
41 * @author Bob Adair - Added additional commands and methods for status and state management. Added TargetType to
42 * LutronCommand for LEAP bridge.
45 public abstract class LutronHandler extends BaseThingHandler {
46 private final Logger logger = LoggerFactory.getLogger(LutronHandler.class);
48 public LutronHandler(Thing thing) {
52 public abstract int getIntegrationId();
54 public abstract void handleUpdate(LutronCommandType type, String... parameters);
57 * Queries for any device state needed at initialization time or after losing connectivity to the bridge, and
58 * updates device status. Will be called when bridge status changes to ONLINE and thing has status
59 * OFFLINE:BRIDGE_OFFLINE.
61 protected abstract void initDeviceState();
64 * Called when changing thing status to offline. Subclasses may override to take any needed actions.
66 protected void thingOfflineNotify() {
69 protected @Nullable LutronBridgeHandler getBridgeHandler() {
70 Bridge bridge = getBridge();
72 return bridge == null ? null : (LutronBridgeHandler) bridge.getHandler();
76 public void bridgeStatusChanged(ThingStatusInfo bridgeStatusInfo) {
77 logger.debug("Bridge status changed to {} for lutron device handler {}", bridgeStatusInfo.getStatus(),
80 if (bridgeStatusInfo.getStatus() == ThingStatus.ONLINE
81 && getThing().getStatusInfo().getStatusDetail() == ThingStatusDetail.BRIDGE_OFFLINE) {
84 } else if (bridgeStatusInfo.getStatus() == ThingStatus.OFFLINE) {
85 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.BRIDGE_OFFLINE);
90 protected void sendCommand(LutronCommandNew command) {
91 LutronBridgeHandler bridgeHandler = getBridgeHandler();
93 if (bridgeHandler == null) {
94 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.HANDLER_MISSING_ERROR, "No bridge associated");
97 bridgeHandler.sendCommand(command);
101 protected void output(TargetType type, int action, @Nullable Number parameter, @Nullable LutronDuration fade,
102 @Nullable LutronDuration delay) {
104 new OutputCommand(type, LutronOperation.EXECUTE, getIntegrationId(), action, parameter, fade, delay));
107 protected void queryOutput(TargetType type, int action) {
109 new OutputCommand(type, LutronOperation.QUERY, getIntegrationId(), action, (Integer) null, null, null));
112 protected void device(TargetType type, Integer component, @Nullable Integer leapComponent, Integer action,
113 @Nullable Object parameter) {
114 sendCommand(new DeviceCommand(type, LutronOperation.EXECUTE, getIntegrationId(), component, leapComponent,
118 protected void queryDevice(TargetType type, Integer component, Integer action) {
119 sendCommand(new DeviceCommand(type, LutronOperation.QUERY, getIntegrationId(), component, null, action, null));
122 protected void timeclock(Integer action, @Nullable Object parameter, @Nullable Boolean enable) {
123 sendCommand(new TimeclockCommand(LutronOperation.EXECUTE, getIntegrationId(), action, parameter, enable));
126 protected void queryTimeclock(Integer action) {
127 sendCommand(new TimeclockCommand(LutronOperation.QUERY, getIntegrationId(), action, null, null));
130 protected void sysvar(Integer action, Object parameter) {
131 sendCommand(new SysvarCommand(LutronOperation.EXECUTE, getIntegrationId(), action, parameter));
134 protected void querySysvar(Integer action) {
135 sendCommand(new SysvarCommand(LutronOperation.QUERY, getIntegrationId(), action, null));
138 protected void greenMode(Integer action, @Nullable Integer parameter) {
139 sendCommand(new ModeCommand(LutronOperation.EXECUTE, getIntegrationId(), action, parameter));
142 protected void queryGreenMode(Integer action) {
143 sendCommand(new ModeCommand(LutronOperation.QUERY, getIntegrationId(), action, null));
146 protected void queryGroup(Integer action) {
147 sendCommand(new GroupCommand(LutronOperation.QUERY, getIntegrationId(), action, null));