Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[hue] Refactored state handling and fix polling after command #7518

Merged
merged 21 commits into from May 29, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 11 additions & 0 deletions bundles/org.openhab.binding.hue/.classpath
Expand Up @@ -28,5 +28,16 @@
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" path="target/generated-sources/annotations">
<attributes>
<attribute name="optional" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" output="target/test-classes" path="target/generated-test-sources/test-annotations">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="test" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="output" path="target/classes"/>
</classpath>
Expand Up @@ -36,13 +36,9 @@
import org.openhab.binding.hue.internal.FullHueObject;
import org.openhab.binding.hue.internal.FullLight;
import org.openhab.binding.hue.internal.FullSensor;
import org.openhab.binding.hue.internal.HueBridge;
import org.openhab.binding.hue.internal.handler.GroupStatusListener;
import org.openhab.binding.hue.internal.handler.HueBridgeHandler;
import org.openhab.binding.hue.internal.handler.HueGroupHandler;
import org.openhab.binding.hue.internal.handler.HueLightHandler;
import org.openhab.binding.hue.internal.handler.LightStatusListener;
import org.openhab.binding.hue.internal.handler.SensorStatusListener;
import org.openhab.binding.hue.internal.handler.sensors.ClipHandler;
import org.openhab.binding.hue.internal.handler.sensors.DimmerSwitchHandler;
import org.openhab.binding.hue.internal.handler.sensors.LightLevelHandler;
Expand All @@ -67,8 +63,7 @@
* @author Laurent Garnier - Added support for groups
*/
@NonNullByDefault
public class HueLightDiscoveryService extends AbstractDiscoveryService
implements LightStatusListener, SensorStatusListener, GroupStatusListener {
public class HueLightDiscoveryService extends AbstractDiscoveryService {
public static final Set<ThingTypeUID> SUPPORTED_THING_TYPES = Collections.unmodifiableSet(Stream
.of(HueLightHandler.SUPPORTED_THING_TYPES.stream(), DimmerSwitchHandler.SUPPORTED_THING_TYPES.stream(),
TapSwitchHandler.SUPPORTED_THING_TYPES.stream(), PresenceHandler.SUPPORTED_THING_TYPES.stream(),
Expand Down Expand Up @@ -107,17 +102,13 @@ public HueLightDiscoveryService(HueBridgeHandler hueBridgeHandler) {
}

public void activate() {
hueBridgeHandler.registerLightStatusListener(this);
hueBridgeHandler.registerSensorStatusListener(this);
hueBridgeHandler.registerGroupStatusListener(this);
hueBridgeHandler.registerDiscoveryListener(this);
}

@Override
public void deactivate() {
removeOlderResults(new Date().getTime(), hueBridgeHandler.getThing().getUID());
hueBridgeHandler.unregisterLightStatusListener(this);
hueBridgeHandler.unregisterSensorStatusListener(this);
hueBridgeHandler.unregisterGroupStatusListener(this);
hueBridgeHandler.unregisterDiscoveryListener();
}

@Override
Expand All @@ -129,15 +120,15 @@ public Set<ThingTypeUID> getSupportedThingTypes() {
public void startScan() {
List<FullLight> lights = hueBridgeHandler.getFullLights();
for (FullLight l : lights) {
onLightAddedInternal(l);
addLightDiscovery(l);
}
List<FullSensor> sensors = hueBridgeHandler.getFullSensors();
for (FullSensor s : sensors) {
onSensorAddedInternal(s);
addSensorDiscovery(s);
}
List<FullGroup> groups = hueBridgeHandler.getFullGroups();
for (FullGroup g : groups) {
onGroupAddedInternal(g);
addGroupDiscovery(g);
}
// search for unpaired lights
hueBridgeHandler.startSearch();
Expand All @@ -149,12 +140,7 @@ protected synchronized void stopScan() {
removeOlderResults(getTimestampOfLastScan(), hueBridgeHandler.getThing().getUID());
}

@Override
public void onLightAdded(@Nullable HueBridge bridge, FullLight light) {
onLightAddedInternal(light);
}

private void onLightAddedInternal(FullLight light) {
public void addLightDiscovery(FullLight light) {
ThingUID thingUID = getThingUID(light);
ThingTypeUID thingTypeUID = getThingTypeUID(light);

Expand Down Expand Up @@ -183,29 +169,14 @@ private void onLightAddedInternal(FullLight light) {
}
}

@Override
public void onLightGone(@Nullable HueBridge bridge, FullLight light) {
onLightRemovedInternal(light);
}

@Override
public void onLightRemoved(@Nullable HueBridge bridge, FullLight light) {
onLightRemovedInternal(light);
}

private void onLightRemovedInternal(FullLight light) {
public void removeLightDiscovery(FullLight light) {
ThingUID thingUID = getThingUID(light);

if (thingUID != null) {
thingRemoved(thingUID);
}
}

@Override
public void onLightStateChanged(@Nullable HueBridge bridge, FullLight light) {
// nothing to do
}

private @Nullable ThingUID getThingUID(FullHueObject hueObject) {
ThingUID bridgeUID = hueBridgeHandler.getThing().getUID();
ThingTypeUID thingTypeUID = getThingTypeUID(hueObject);
Expand All @@ -223,12 +194,7 @@ public void onLightStateChanged(@Nullable HueBridge bridge, FullLight light) {
return thingTypeId != null ? new ThingTypeUID(BINDING_ID, thingTypeId) : null;
}

@Override
public void onSensorAdded(@Nullable HueBridge bridge, FullSensor sensor) {
onSensorAddedInternal(sensor);
}

private void onSensorAddedInternal(FullSensor sensor) {
public void addSensorDiscovery(FullSensor sensor) {
ThingUID thingUID = getThingUID(sensor);
ThingTypeUID thingTypeUID = getThingTypeUID(sensor);

Expand Down Expand Up @@ -256,35 +222,15 @@ private void onSensorAddedInternal(FullSensor sensor) {
}
}

@Override
public void onSensorGone(@Nullable HueBridge bridge, FullSensor sensor) {
onSensorRemovedInternal(sensor);
}

@Override
public void onSensorRemoved(@Nullable HueBridge bridge, FullSensor sensor) {
onSensorRemovedInternal(sensor);
}

private void onSensorRemovedInternal(FullSensor sensor) {
public void removeSensorDiscovery(FullSensor sensor) {
ThingUID thingUID = getThingUID(sensor);

if (thingUID != null) {
thingRemoved(thingUID);
}
}

@Override
public void onSensorStateChanged(@Nullable HueBridge bridge, FullSensor sensor) {
// nothing to do
}

@Override
public void onGroupAdded(@Nullable HueBridge bridge, FullGroup group) {
onGroupAddedInternal(group);
}

private void onGroupAddedInternal(FullGroup group) {
public void addGroupDiscovery(FullGroup group) {
// Ignore the Hue Entertainment Areas
if ("Entertainment".equalsIgnoreCase(group.getType())) {
return;
Expand All @@ -305,24 +251,9 @@ private void onGroupAddedInternal(FullGroup group) {
thingDiscovered(discoveryResult);
}

@Override
public void onGroupGone(@Nullable HueBridge bridge, FullGroup group) {
onGroupRemovedInternal(group);
}

@Override
public void onGroupRemoved(@Nullable HueBridge bridge, FullGroup group) {
onGroupRemovedInternal(group);
}

private void onGroupRemovedInternal(FullGroup group) {
public void removeGroupDiscovery(FullGroup group) {
ThingUID bridgeUID = hueBridgeHandler.getThing().getUID();
ThingUID thingUID = new ThingUID(THING_TYPE_GROUP, bridgeUID, group.getId());
thingRemoved(thingUID);
}

@Override
public void onGroupStateChanged(@Nullable HueBridge bridge, FullGroup group) {
// nothing to do
}
}
Expand Up @@ -25,14 +25,22 @@
@NonNullByDefault
public interface GroupStatusListener {

/**
* This method returns the group id of listener
*
* @return groupId String
*/
String getGroupId();

/**
* This method is called whenever the state of the given group has changed. The new state can be obtained by
* {@link FullGroup#getState()}.
*
* @param bridge The bridge the changed group is connected to.
* @param group The group which received the state update.
* @return
*/
void onGroupStateChanged(@Nullable HueBridge bridge, FullGroup group);
boolean onGroupStateChanged(@Nullable HueBridge bridge, FullGroup group);

/**
* This method is called whenever a group is removed.
Expand Down