diff --git a/bundles/org.openhab.binding.hue/src/main/java/org/openhab/binding/hue/internal/handler/HueLightHandler.java b/bundles/org.openhab.binding.hue/src/main/java/org/openhab/binding/hue/internal/handler/HueLightHandler.java index 0db4ca9f844c5..8270f21792750 100644 --- a/bundles/org.openhab.binding.hue/src/main/java/org/openhab/binding/hue/internal/handler/HueLightHandler.java +++ b/bundles/org.openhab.binding.hue/src/main/java/org/openhab/binding/hue/internal/handler/HueLightHandler.java @@ -46,7 +46,6 @@ import org.eclipse.smarthome.core.thing.binding.ThingHandlerService; import org.eclipse.smarthome.core.types.Command; import org.eclipse.smarthome.core.types.UnDefType; -import org.openhab.binding.hue.internal.FullHueObject; import org.openhab.binding.hue.internal.FullLight; import org.openhab.binding.hue.internal.HueBridge; import org.openhab.binding.hue.internal.State; @@ -94,14 +93,13 @@ public class HueLightHandler extends BaseThingHandler implements LightStatusList private static final String OSRAM_PAR16_50_TW_MODEL_ID = "PAR16_50_TW"; - @NonNullByDefault({}) - private String lightId; + private final Logger logger = LoggerFactory.getLogger(HueLightHandler.class); + + private @NonNullByDefault({}) String lightId; private @Nullable Integer lastSentColorTemp; private @Nullable Integer lastSentBrightness; - private final Logger logger = LoggerFactory.getLogger(HueLightHandler.class); - // Flag to indicate whether the bulb is of type Osram par16 50 TW or not private boolean isOsramPar16 = false; @@ -134,11 +132,11 @@ private void initializeThing(@Nullable ThingStatus bridgeStatus) { } lightId = configLightId; - // note: this call implicitly registers our handler as a listener on - // the bridge - if (getHueClient() != null) { + // note: this call implicitly registers our handler as a listener on the bridge + HueClient bridgeHandler = getHueClient(); + if (bridgeHandler != null) { if (bridgeStatus == ThingStatus.ONLINE) { - initializeProperties(); + initializeProperties(bridgeHandler.getLightById(lightId)); updateStatus(ThingStatus.ONLINE); } else { updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.BRIDGE_OFFLINE); @@ -152,9 +150,8 @@ private void initializeThing(@Nullable ThingStatus bridgeStatus) { } } - private synchronized void initializeProperties() { + private synchronized void initializeProperties(@Nullable FullLight fullLight) { if (!propertiesInitializedSuccessfully) { - FullHueObject fullLight = getLight(); if (fullLight != null) { Map properties = editProperties(); String softwareVersion = fullLight.getSoftwareVersion(); @@ -205,27 +202,19 @@ public void dispose() { } } - private @Nullable FullLight getLight() { - HueClient bridgeHandler = getHueClient(); - if (bridgeHandler != null) { - return bridgeHandler.getLightById(lightId); - } - return null; - } - @Override public void handleCommand(ChannelUID channelUID, Command command) { handleCommand(channelUID.getId(), command, defaultFadeTime); } public void handleCommand(String channel, Command command, long fadeTime) { - HueClient hueBridge = getHueClient(); - if (hueBridge == null) { + HueClient bridgeHandler = getHueClient(); + if (bridgeHandler == null) { logger.warn("hue bridge handler not found. Cannot handle command without bridge."); return; } - FullLight light = getLight(); + FullLight light = bridgeHandler.getLightById(lightId); if (light == null) { logger.debug("hue light not known on bridge. Cannot handle command."); updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, @@ -349,7 +338,7 @@ public void handleCommand(String channel, Command command, long fadeTime) { if (tmpColorTemp != null) { lastSentColorTemp = tmpColorTemp; } - hueBridge.updateLightState(light, lightState); + bridgeHandler.updateLightState(light, lightState); } else { logger.warn("Command sent to an unknown channel id: {}:{}", getThing().getUID(), channel); } @@ -448,7 +437,7 @@ public void onLightStateChanged(@Nullable HueBridge bridge, FullLight fullLight) return; } - initializeProperties(); + initializeProperties(fullLight); lastSentColorTemp = null; lastSentBrightness = null; diff --git a/bundles/org.openhab.binding.hue/src/main/java/org/openhab/binding/hue/internal/handler/HueSensorHandler.java b/bundles/org.openhab.binding.hue/src/main/java/org/openhab/binding/hue/internal/handler/HueSensorHandler.java index 520db00152ecc..b7c2d20a266d1 100644 --- a/bundles/org.openhab.binding.hue/src/main/java/org/openhab/binding/hue/internal/handler/HueSensorHandler.java +++ b/bundles/org.openhab.binding.hue/src/main/java/org/openhab/binding/hue/internal/handler/HueSensorHandler.java @@ -38,7 +38,6 @@ import org.eclipse.smarthome.core.thing.binding.BaseThingHandler; import org.eclipse.smarthome.core.thing.binding.ThingHandler; import org.eclipse.smarthome.core.types.Command; -import org.openhab.binding.hue.internal.FullHueObject; import org.openhab.binding.hue.internal.FullSensor; import org.openhab.binding.hue.internal.HueBridge; import org.openhab.binding.hue.internal.SensorConfigUpdate; @@ -55,10 +54,10 @@ @NonNullByDefault public abstract class HueSensorHandler extends BaseThingHandler implements SensorStatusListener { - private @NonNullByDefault({}) String sensorId; - private final Logger logger = LoggerFactory.getLogger(HueSensorHandler.class); + private @NonNullByDefault({}) String sensorId; + private boolean configInitializedSuccessfully; private boolean propertiesInitializedSuccessfully; @@ -81,9 +80,10 @@ private void initializeThing(@Nullable ThingStatus bridgeStatus) { if (configSensorId != null) { sensorId = configSensorId; // note: this call implicitly registers our handler as a listener on the bridge - if (getHueClient() != null) { + HueClient bridgeHandler = getHueClient(); + if (bridgeHandler != null) { if (bridgeStatus == ThingStatus.ONLINE) { - initializeProperties(); + initializeProperties(bridgeHandler.getSensorById(sensorId)); updateStatus(ThingStatus.ONLINE); } else { updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.BRIDGE_OFFLINE); @@ -97,9 +97,8 @@ private void initializeThing(@Nullable ThingStatus bridgeStatus) { } } - private synchronized void initializeProperties() { + private synchronized void initializeProperties(@Nullable FullSensor fullSensor) { if (!propertiesInitializedSuccessfully) { - FullHueObject fullSensor = getSensor(); if (fullSensor != null) { Map properties = editProperties(); String softwareVersion = fullSensor.getSoftwareVersion(); @@ -166,8 +165,13 @@ public void handleCommand(ChannelUID channelUID, Command command) { } public void handleCommand(String channel, Command command) { - // updateSensorState - FullSensor sensor = getSensor(); + HueClient bridgeHandler = getHueClient(); + if (bridgeHandler == null) { + logger.warn("hue bridge handler not found. Cannot handle command without bridge."); + return; + } + + FullSensor sensor = bridgeHandler.getSensorById(sensorId); if (sensor == null) { logger.debug("hue sensor not known on bridge. Cannot handle command."); updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, @@ -175,12 +179,6 @@ public void handleCommand(String channel, Command command) { return; } - HueClient hueBridge = getHueClient(); - if (hueBridge == null) { - logger.warn("hue bridge handler not found. Cannot handle command without bridge."); - return; - } - StateUpdate sensorState = new StateUpdate(); switch (channel) { case STATE_STATUS: @@ -192,7 +190,7 @@ public void handleCommand(String channel, Command command) { } if (sensorState != null) { - hueBridge.updateSensorState(sensor, sensorState); + bridgeHandler.updateSensorState(sensor, sensorState); } else { logger.warn("Command sent to an unknown channel id: {}:{}", getThing().getUID(), channel); } @@ -235,7 +233,7 @@ public void onSensorStateChanged(@Nullable HueBridge bridge, FullSensor sensor) return; } - initializeProperties(); + initializeProperties(sensor); if (Boolean.TRUE.equals(sensor.getConfig().get(CONFIG_REACHABLE))) { updateStatus(ThingStatus.ONLINE);