Skip to content

Commit

Permalink
[lutron] Enable sysvar monitoring in bridge when needed
Browse files Browse the repository at this point in the history
Signed-off-by: Bob Adair <[email protected]>
  • Loading branch information
bobadair committed Jul 12, 2020
1 parent 8f7b188 commit f6b0c49
Showing 1 changed file with 26 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.regex.MatchResult;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
Expand All @@ -31,6 +32,7 @@
import org.eclipse.smarthome.core.thing.ThingStatus;
import org.eclipse.smarthome.core.thing.ThingStatusDetail;
import org.eclipse.smarthome.core.thing.binding.BaseBridgeHandler;
import org.eclipse.smarthome.core.thing.binding.ThingHandler;
import org.eclipse.smarthome.core.types.Command;
import org.openhab.binding.lutron.internal.config.IPBridgeConfig;
import org.openhab.binding.lutron.internal.discovery.LutronDeviceDiscoveryService;
Expand All @@ -56,6 +58,8 @@ public class IPBridgeHandler extends BaseBridgeHandler {
private static final String DB_UPDATE_DATE_FORMAT = "MM/dd/yyyy HH:mm:ss";

private static final Integer MONITOR_PROMPT = 12;
private static final Integer MONITOR_SYSVAR = 10;
private static final Integer MONITOR_ENABLE = 1;
private static final Integer MONITOR_DISABLE = 2;

private static final Integer SYSTEM_DBEXPORTDATETIME = 10;
Expand Down Expand Up @@ -91,6 +95,8 @@ public class IPBridgeHandler extends BaseBridgeHandler {
private Date lastDbUpdateDate;
private LutronDeviceDiscoveryService discoveryService;

private final AtomicBoolean requireSysvarMonitoring = new AtomicBoolean(false);

public void setDiscoveryService(LutronDeviceDiscoveryService discoveryService) {
this.discoveryService = discoveryService;
}
Expand Down Expand Up @@ -203,6 +209,10 @@ private synchronized void connect() {
sendCommand(new LutronCommand(LutronOperation.EXECUTE, LutronCommandType.MONITORING, -1, MONITOR_PROMPT,
MONITOR_DISABLE));

if (requireSysvarMonitoring.get()) {
setSysvarMonitoring(true);
}

// Check the time device database was last updated. On the initial connect, this will trigger
// a scan for paired devices.
sendCommand(new LutronCommand(LutronOperation.QUERY, LutronCommandType.SYSTEM, -1, SYSTEM_DBEXPORTDATETIME));
Expand Down Expand Up @@ -443,6 +453,22 @@ private void scanForDevices() {
}
}

private void setSysvarMonitoring(boolean enable) {
Integer setting = (enable) ? MONITOR_ENABLE : MONITOR_DISABLE;
sendCommand(
new LutronCommand(LutronOperation.EXECUTE, LutronCommandType.MONITORING, -1, MONITOR_SYSVAR, setting));
}

@Override
public void childHandlerInitialized(ThingHandler childHandler, Thing childThing) {
// enable sysvar monitoring the first time a sysvar child thing initializes
if (childHandler instanceof SysvarHandler) {
if (requireSysvarMonitoring.compareAndSet(false, true)) {
setSysvarMonitoring(true);
}
}
}

@Override
public void thingUpdated(Thing thing) {
IPBridgeConfig newConfig = thing.getConfiguration().as(IPBridgeConfig.class);
Expand Down

0 comments on commit f6b0c49

Please sign in to comment.