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..7ea07b913fa51 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,34 +150,31 @@ private void initializeThing(@Nullable ThingStatus bridgeStatus) { } } - private synchronized void initializeProperties() { - if (!propertiesInitializedSuccessfully) { - FullHueObject fullLight = getLight(); - if (fullLight != null) { - Map properties = editProperties(); - String softwareVersion = fullLight.getSoftwareVersion(); - if (softwareVersion != null) { - properties.put(PROPERTY_FIRMWARE_VERSION, softwareVersion); - } - String modelId = fullLight.getNormalizedModelID(); - if (modelId != null) { - properties.put(PROPERTY_MODEL_ID, modelId); - String vendor = getVendor(modelId); - if (vendor != null) { - properties.put(PROPERTY_VENDOR, vendor); - } - } else { - properties.put(PROPERTY_VENDOR, fullLight.getManufacturerName()); - } - properties.put(PRODUCT_NAME, fullLight.getProductName()); - String uniqueID = fullLight.getUniqueID(); - if (uniqueID != null) { - properties.put(UNIQUE_ID, uniqueID); + private synchronized void initializeProperties(@Nullable FullLight fullLight) { + if (!propertiesInitializedSuccessfully && fullLight != null) { + Map properties = editProperties(); + String softwareVersion = fullLight.getSoftwareVersion(); + if (softwareVersion != null) { + properties.put(PROPERTY_FIRMWARE_VERSION, softwareVersion); + } + String modelId = fullLight.getNormalizedModelID(); + if (modelId != null) { + properties.put(PROPERTY_MODEL_ID, modelId); + String vendor = getVendor(modelId); + if (vendor != null) { + properties.put(PROPERTY_VENDOR, vendor); } - updateProperties(properties); - isOsramPar16 = OSRAM_PAR16_50_TW_MODEL_ID.equals(modelId); - propertiesInitializedSuccessfully = true; + } else { + properties.put(PROPERTY_VENDOR, fullLight.getManufacturerName()); + } + properties.put(PRODUCT_NAME, fullLight.getProductName()); + String uniqueID = fullLight.getUniqueID(); + if (uniqueID != null) { + properties.put(UNIQUE_ID, uniqueID); } + updateProperties(properties); + isOsramPar16 = OSRAM_PAR16_50_TW_MODEL_ID.equals(modelId); + propertiesInitializedSuccessfully = true; } } @@ -205,27 +200,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 +336,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 +435,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..daaf1910bc482 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,28 +97,25 @@ private void initializeThing(@Nullable ThingStatus bridgeStatus) { } } - private synchronized void initializeProperties() { - if (!propertiesInitializedSuccessfully) { - FullHueObject fullSensor = getSensor(); - if (fullSensor != null) { - Map properties = editProperties(); - String softwareVersion = fullSensor.getSoftwareVersion(); - if (softwareVersion != null) { - properties.put(PROPERTY_FIRMWARE_VERSION, softwareVersion); - } - String modelId = fullSensor.getNormalizedModelID(); - if (modelId != null) { - properties.put(PROPERTY_MODEL_ID, modelId); - } - properties.put(PROPERTY_VENDOR, fullSensor.getManufacturerName()); - properties.put(PRODUCT_NAME, fullSensor.getProductName()); - String uniqueID = fullSensor.getUniqueID(); - if (uniqueID != null) { - properties.put(UNIQUE_ID, uniqueID); - } - updateProperties(properties); - propertiesInitializedSuccessfully = true; + private synchronized void initializeProperties(@Nullable FullSensor fullSensor) { + if (!propertiesInitializedSuccessfully && fullSensor != null) { + Map properties = editProperties(); + String softwareVersion = fullSensor.getSoftwareVersion(); + if (softwareVersion != null) { + properties.put(PROPERTY_FIRMWARE_VERSION, softwareVersion); + } + String modelId = fullSensor.getNormalizedModelID(); + if (modelId != null) { + properties.put(PROPERTY_MODEL_ID, modelId); } + properties.put(PROPERTY_VENDOR, fullSensor.getManufacturerName()); + properties.put(PRODUCT_NAME, fullSensor.getProductName()); + String uniqueID = fullSensor.getUniqueID(); + if (uniqueID != null) { + properties.put(UNIQUE_ID, uniqueID); + } + updateProperties(properties); + propertiesInitializedSuccessfully = true; } } @@ -166,8 +163,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 +177,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 +188,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 +231,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);