Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public HueSensorEntry(GenericItem item) throws IllegalArgumentException {
this.type = "CLIPGenericFlag"; // "flag" (bool)
break;
default:
throw new IllegalArgumentException("Item type not supported as sensor");
throw new IllegalArgumentException("Item type '" + item.getType() + "' not supported as sensor");
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,10 +143,9 @@ protected void deactivate() {

@Override
public synchronized void added(Item newElement) {
if (!(newElement instanceof GenericItem)) {
if (!(newElement instanceof GenericItem element)) {
return;
}
GenericItem element = (GenericItem) newElement;

if (!(element instanceof GroupItem) && !ALLOWED_ITEM_TYPES.contains(element.getType())) {
return;
Expand Down Expand Up @@ -210,18 +209,17 @@ public synchronized void removed(Item element) {
@SuppressWarnings({ "null", "unused" })
@Override
public synchronized void updated(Item oldElement, Item newElement) {
if (!(newElement instanceof GenericItem)) {
if (!(newElement instanceof GenericItem element)) {
return;
}
GenericItem element = (GenericItem) newElement;

String hueID = cs.mapItemUIDtoHueID(element);

HueGroupEntry hueGroup = cs.ds.groups.get(hueID);
if (hueGroup != null) {
DeviceType t = StateUtils.determineTargetType(cs, element);
if (t != null && element instanceof GroupItem) {
hueGroup.updateItem((GroupItem) element);
if (t != null && element instanceof GroupItem groupElement) {
hueGroup.updateItem(groupElement);
} else {
cs.ds.groups.remove(hueID);
}
Expand All @@ -235,8 +233,9 @@ public synchronized void updated(Item oldElement, Item newElement) {
}

// Check if type can still be determined (tags and category is still sufficient)
// and that it's still an allowed item type
DeviceType t = StateUtils.determineTargetType(cs, element);
if (t == null) {
if (t == null || !ALLOWED_ITEM_TYPES.contains(element.getType())) {
removed(element);
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,10 +112,9 @@ protected void deactivate() {

@Override
public synchronized void added(Item newElement) {
if (!(newElement instanceof GenericItem)) {
if (!(newElement instanceof GenericItem element)) {
return;
}
GenericItem element = (GenericItem) newElement;

if (!ALLOWED_ITEM_TYPES.contains(element.getType())) {
return;
Expand All @@ -136,10 +135,14 @@ public synchronized void removed(Item element) {

@Override
public synchronized void updated(Item oldElement, Item newElement) {
if (!(newElement instanceof GenericItem)) {
if (!(newElement instanceof GenericItem element)) {
return;
}

if (!ALLOWED_ITEM_TYPES.contains(element.getType())) {
removed(element);
return;
}
GenericItem element = (GenericItem) newElement;

String hueID = cs.mapItemUIDtoHueID(element);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import org.openhab.core.items.ItemRegistry;
import org.openhab.core.items.events.ItemCommandEvent;
import org.openhab.core.library.items.ColorItem;
import org.openhab.core.library.items.StringItem;
import org.openhab.core.library.items.SwitchItem;
import org.openhab.core.library.types.HSBType;
import org.openhab.core.library.types.OnOffType;
Expand Down Expand Up @@ -163,6 +164,21 @@ public void removeGroupWithoutTypeAndTag() {
assertThat(cs.ds.groups.get(hueID), nullValue());
}

@Test
public void removeGroupNoLongerGroup() {
String groupName = "group1";
GroupItem item = new GroupItem(groupName, null);
item.addTag("Switchable");
itemRegistry.add(item);

String hueID = cs.mapItemUIDtoHueID(item);
assertThat(cs.ds.groups.get(hueID), notNullValue());

subject.updated(item, new StringItem(groupName));

assertThat(cs.ds.groups.get(hueID), nullValue());
}

@Test
public void updateSwitchable() {
SwitchItem item = new SwitchItem("switch1");
Expand All @@ -183,16 +199,50 @@ public void updateSwitchable() {
assertThat(device.item, is(newitem));
assertThat(device.state, is(instanceOf(HueStatePlug.class)));
assertThat(device.name, is("labelNew"));
}

@Test
public void updateSwitchableNoTags() {
SwitchItem item = new SwitchItem("switch1");
item.setLabel("labelOld");
item.addTag("Switchable");
itemRegistry.add(item);
String hueID = cs.mapItemUIDtoHueID(item);
HueLightEntry device = cs.ds.lights.get(hueID);
assertThat(device.item, is(item));
assertThat(device.state, is(instanceOf(HueStatePlug.class)));
assertThat(device.name, is("labelOld"));

// Update with an item that has no tags anymore -> should be removed
SwitchItem newitemWithoutTag = new SwitchItem("switch1");
newitemWithoutTag.setLabel("labelNew2");
subject.updated(newitem, newitemWithoutTag);
SwitchItem newitem = new SwitchItem("switch1");
newitem.setLabel("labelNew");
subject.updated(item, newitem);

device = cs.ds.lights.get(hueID);
assertThat(device, nullValue());
}

@Test
public void updateSwitchableUnsupportedItemType() {
SwitchItem item = new SwitchItem("switch1");
item.setLabel("label");
item.addTag("Switchable");
itemRegistry.add(item);
String hueID = cs.mapItemUIDtoHueID(item);
HueLightEntry device = cs.ds.lights.get(hueID);
assertThat(device.item, is(item));
assertThat(device.state, is(instanceOf(HueStatePlug.class)));
assertThat(device.name, is("label"));

// Update with an item where item type is not supported anymore -> should be removed
StringItem newitem = new StringItem("switch1");
newitem.setLabel("label");
newitem.addTag("Switchable");
subject.updated(item, newitem);
device = cs.ds.lights.get(hueID);
assertThat(device, nullValue());
}

@Test
public void changeSwitchState() throws Exception {
assertThat(((HueStatePlug) cs.ds.lights.get("1").state).on, is(false));
Expand Down