From fd18cfb751397dc12c9b9cd6917c1b64895ab3fc Mon Sep 17 00:00:00 2001 From: Christoph Weitkamp Date: Sat, 13 Feb 2021 11:48:53 +0100 Subject: [PATCH 1/2] Removed internal vendor Map and minor additional improvements Signed-off-by: Christoph Weitkamp --- .../hue/internal/handler/HueGroupHandler.java | 15 +++++++++++ .../hue/internal/handler/HueLightHandler.java | 25 +++---------------- .../resources/OH-INF/i18n/hue_de.properties | 2 ++ .../internal/handler/HueLightHandlerTest.java | 22 ++++++++-------- .../hue/internal/handler/HueLightState.java | 5 +++- 5 files changed, 36 insertions(+), 33 deletions(-) diff --git a/bundles/org.openhab.binding.hue/src/main/java/org/openhab/binding/hue/internal/handler/HueGroupHandler.java b/bundles/org.openhab.binding.hue/src/main/java/org/openhab/binding/hue/internal/handler/HueGroupHandler.java index a8e87730afe9b..f3e1c87bf9b24 100644 --- a/bundles/org.openhab.binding.hue/src/main/java/org/openhab/binding/hue/internal/handler/HueGroupHandler.java +++ b/bundles/org.openhab.binding.hue/src/main/java/org/openhab/binding/hue/internal/handler/HueGroupHandler.java @@ -16,6 +16,7 @@ import java.math.BigDecimal; import java.util.List; +import java.util.Map; import java.util.Objects; import java.util.Set; import java.util.concurrent.ScheduledFuture; @@ -60,6 +61,7 @@ @NonNullByDefault public class HueGroupHandler extends BaseThingHandler implements GroupStatusListener { public static final Set SUPPORTED_THING_TYPES = Set.of(THING_TYPE_GROUP); + public static final String PROPERTY_MEMBERS = "members"; private final Logger logger = LoggerFactory.getLogger(HueGroupHandler.class); private final HueStateDescriptionOptionProvider stateDescriptionOptionProvider; @@ -72,6 +74,8 @@ public class HueGroupHandler extends BaseThingHandler implements GroupStatusList private ColorTemperature colorTemperatureCapabilties = new ColorTemperature(); private long defaultFadeTime = 400; + private boolean propertiesInitializedSuccessfully; + private @Nullable HueClient hueClient; private @Nullable ScheduledFuture scheduledFuture; @@ -123,6 +127,15 @@ private void initializeThing(@Nullable ThingStatus bridgeStatus) { } } + private synchronized void initializeProperties(@Nullable FullGroup fullGroup) { + if (!propertiesInitializedSuccessfully && fullGroup != null) { + Map properties = editProperties(); + properties.put(PROPERTY_MEMBERS, fullGroup.getLightIds().stream().collect(Collectors.joining(","))); + updateProperties(properties); + propertiesInitializedSuccessfully = true; + } + } + @Override public void dispose() { logger.debug("Hue group handler disposes. Unregistering listener."); @@ -379,6 +392,8 @@ public boolean onGroupStateChanged(FullGroup group) { logger.trace("New state for group {}", groupId); + initializeProperties(group); + lastSentColorTemp = null; lastSentBrightness = null; 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 336791740865f..fcd6d37f43035 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 @@ -81,15 +81,10 @@ public class HueLightHandler extends BaseThingHandler implements LightStatusList THING_TYPE_COLOR_TEMPERATURE_LIGHT, THING_TYPE_DIMMABLE_LIGHT, THING_TYPE_EXTENDED_COLOR_LIGHT, THING_TYPE_ON_OFF_LIGHT, THING_TYPE_ON_OFF_PLUG, THING_TYPE_DIMMABLE_PLUG); - private static final Map> VENDOR_MODEL_MAP = Map.of( // - "Philips", List.of("LCT001", "LCT002", "LCT003", "LCT007", "LLC001", "LLC006", "LLC007", "LLC010", // - "LLC011", "LLC012", "LLC013", "LLC020", "LST001", "LST002", "LWB004", "LWB006", "LWB007", // - "LWL001"), - "OSRAM", List.of("Classic_A60_RGBW", "PAR16_50_TW", "Surface_Light_TW", "Plug_01")); - - private static final String OSRAM_PAR16_50_TW_MODEL_ID = "PAR16_50_TW"; + public static final String OSRAM_PAR16_50_TW_MODEL_ID = "PAR16_50_TW"; private final Logger logger = LoggerFactory.getLogger(HueLightHandler.class); + private final HueStateDescriptionOptionProvider stateDescriptionOptionProvider; private @NonNullByDefault({}) String lightId; @@ -170,13 +165,8 @@ private synchronized void initializeProperties(@Nullable FullLight fullLight) { 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(PROPERTY_VENDOR, fullLight.getManufacturerName()); properties.put(PRODUCT_NAME, fullLight.getProductName()); String uniqueID = fullLight.getUniqueID(); if (uniqueID != null) { @@ -215,15 +205,6 @@ private void initializeCapabilities(@Nullable FullLight fullLight) { } } - private @Nullable String getVendor(String modelId) { - for (String vendor : VENDOR_MODEL_MAP.keySet()) { - if (VENDOR_MODEL_MAP.get(vendor).contains(modelId)) { - return vendor; - } - } - return null; - } - @Override public void dispose() { logger.debug("Hue light handler disposes. Unregistering listener."); diff --git a/bundles/org.openhab.binding.hue/src/main/resources/OH-INF/i18n/hue_de.properties b/bundles/org.openhab.binding.hue/src/main/resources/OH-INF/i18n/hue_de.properties index b40602f287905..378433e0837df 100644 --- a/bundles/org.openhab.binding.hue/src/main/resources/OH-INF/i18n/hue_de.properties +++ b/bundles/org.openhab.binding.hue/src/main/resources/OH-INF/i18n/hue_de.properties @@ -103,6 +103,8 @@ channel-type.hue.alert.state.option.SELECT = Einmaliges Blinken channel-type.hue.alert.state.option.LSELECT = Mehrfaches Blinken channel-type.hue.effect.label = Farbeffekt channel-type.hue.effect.description = Erm�glicht einen automatischen Farbwechsels. +channel-type.hue.scene.label = Szene +channel-type.hue.scene.description = Erm�glicht das Anwenden einer Szene f�r alle Lichter, die zur Gruppe geh�ren. channel-type.hue.last_updated.label = Letzte Aktualisierung channel-type.hue.last_updated.description = Zeit, zu der sich dieser Wert ge�ndert hat. diff --git a/bundles/org.openhab.binding.hue/src/test/java/org/openhab/binding/hue/internal/handler/HueLightHandlerTest.java b/bundles/org.openhab.binding.hue/src/test/java/org/openhab/binding/hue/internal/handler/HueLightHandlerTest.java index 2d62bbe8a5506..ea16da229cff9 100644 --- a/bundles/org.openhab.binding.hue/src/test/java/org/openhab/binding/hue/internal/handler/HueLightHandlerTest.java +++ b/bundles/org.openhab.binding.hue/src/test/java/org/openhab/binding/hue/internal/handler/HueLightHandlerTest.java @@ -17,7 +17,7 @@ import static org.mockito.Mockito.*; import static org.openhab.binding.hue.internal.HueBindingConstants.*; -import java.util.Collections; +import java.util.Map; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; @@ -62,8 +62,9 @@ public class HueLightHandlerTest { private static final int MAX_COLOR_TEMPERATURE = 500; private static final int COLOR_TEMPERATURE_RANGE = MAX_COLOR_TEMPERATURE - MIN_COLOR_TEMPERATURE; - private static final String OSRAM_MODEL_TYPE = "PAR16 50 TW"; - private static final String OSRAM_MODEL_TYPE_ID = "PAR16_50_TW"; + private static final String OSRAM = "OSRAM"; + private static final String OSRAM_MODEL_TYPE = HueLightHandler.OSRAM_PAR16_50_TW_MODEL_ID; + private static final String OSRAM_MODEL_TYPE_ID = HueLightHandler.OSRAM_PAR16_50_TW_MODEL_ID; private Gson gson; @@ -75,25 +76,26 @@ public void setUp() { @Test public void assertCommandForOsramPar1650ForColorTemperatureChannelOn() { String expectedReply = "{\"on\" : true, \"bri\" : 254}"; - assertSendCommandForColorTempForPar16(OnOffType.ON, new HueLightState(OSRAM_MODEL_TYPE), expectedReply); + assertSendCommandForColorTempForPar16(OnOffType.ON, new HueLightState(OSRAM_MODEL_TYPE, OSRAM), expectedReply); } @Test public void assertCommandForOsramPar1650ForColorTemperatureChannelOff() { String expectedReply = "{\"on\" : false, \"transitiontime\" : 0}"; - assertSendCommandForColorTempForPar16(OnOffType.OFF, new HueLightState(OSRAM_MODEL_TYPE), expectedReply); + assertSendCommandForColorTempForPar16(OnOffType.OFF, new HueLightState(OSRAM_MODEL_TYPE, OSRAM), expectedReply); } @Test public void assertCommandForOsramPar1650ForBrightnessChannelOn() { String expectedReply = "{\"on\" : true, \"bri\" : 254}"; - assertSendCommandForBrightnessForPar16(OnOffType.ON, new HueLightState(OSRAM_MODEL_TYPE), expectedReply); + assertSendCommandForBrightnessForPar16(OnOffType.ON, new HueLightState(OSRAM_MODEL_TYPE, OSRAM), expectedReply); } @Test public void assertCommandForOsramPar1650ForBrightnessChannelOff() { String expectedReply = "{\"on\" : false, \"transitiontime\" : 0}"; - assertSendCommandForBrightnessForPar16(OnOffType.OFF, new HueLightState(OSRAM_MODEL_TYPE), expectedReply); + assertSendCommandForBrightnessForPar16(OnOffType.OFF, new HueLightState(OSRAM_MODEL_TYPE, OSRAM), + expectedReply); } @Test @@ -341,12 +343,12 @@ public void assertCommandForEffectChannel() { private void assertSendCommandForColorTempForPar16(Command command, HueLightState currentState, String expectedReply) { - assertSendCommand(CHANNEL_COLORTEMPERATURE, command, currentState, expectedReply, OSRAM_MODEL_TYPE_ID, "OSRAM"); + assertSendCommand(CHANNEL_COLORTEMPERATURE, command, currentState, expectedReply, OSRAM_MODEL_TYPE_ID, OSRAM); } private void assertSendCommandForBrightnessForPar16(Command command, HueLightState currentState, String expectedReply) { - assertSendCommand(CHANNEL_BRIGHTNESS, command, currentState, expectedReply, OSRAM_MODEL_TYPE_ID, "OSRAM"); + assertSendCommand(CHANNEL_BRIGHTNESS, command, currentState, expectedReply, OSRAM_MODEL_TYPE_ID, OSRAM); } private void assertSendCommandForColor(Command command, HueLightState currentState, String expectedReply) { @@ -390,7 +392,7 @@ private void assertSendCommand(String channel, Command command, HueLightState cu when(mockBridge.getStatus()).thenReturn(ThingStatus.ONLINE); Thing mockThing = mock(Thing.class); - when(mockThing.getConfiguration()).thenReturn(new Configuration(Collections.singletonMap(LIGHT_ID, "1"))); + when(mockThing.getConfiguration()).thenReturn(new Configuration(Map.of(LIGHT_ID, "1"))); HueClient mockClient = mock(HueClient.class); when(mockClient.getLightById(any())).thenReturn(light); diff --git a/bundles/org.openhab.binding.hue/src/test/java/org/openhab/binding/hue/internal/handler/HueLightState.java b/bundles/org.openhab.binding.hue/src/test/java/org/openhab/binding/hue/internal/handler/HueLightState.java index f7dc42f03e5ad..1d99ee7fd1ca5 100644 --- a/bundles/org.openhab.binding.hue/src/test/java/org/openhab/binding/hue/internal/handler/HueLightState.java +++ b/bundles/org.openhab.binding.hue/src/test/java/org/openhab/binding/hue/internal/handler/HueLightState.java @@ -33,12 +33,14 @@ public class HueLightState { String effect = "none"; String colorMode = "hs"; String model = "LCT001"; + String vendor = "Philips"; public HueLightState() { } - public HueLightState(String model) { + public HueLightState(String model, String vendor) { this.model = model; + this.vendor = vendor; } public HueLightState bri(int brightness) { @@ -105,6 +107,7 @@ public String toString() { " \"type\": \"Extended color light\"," + // " \"name\": \"Hue Light 1\"," + // " \"modelid\": \"" + model + "\"," + // + " \"manufacturername\": \"" + vendor + "\"," + // " \"swversion\": \"65003148\"," + // " \"uniqueid\": \"00:17:88:01:00:e1:88:29-0b\"," + // " \"pointsymbol\": {" + // From 9851368304866ed41c226cae6c8b201a2937111f Mon Sep 17 00:00:00 2001 From: Christoph Weitkamp Date: Sat, 13 Feb 2021 17:42:10 +0100 Subject: [PATCH 2/2] Incorporated comments from review Signed-off-by: Christoph Weitkamp --- .../binding/hue/internal/console/HueCommandExtension.java | 6 +++--- .../binding/hue/internal/handler/HueGroupHandler.java | 5 +---- 2 files changed, 4 insertions(+), 7 deletions(-) diff --git a/bundles/org.openhab.binding.hue/src/main/java/org/openhab/binding/hue/internal/console/HueCommandExtension.java b/bundles/org.openhab.binding.hue/src/main/java/org/openhab/binding/hue/internal/console/HueCommandExtension.java index 3c695720ea36e..ce5aef384cb49 100644 --- a/bundles/org.openhab.binding.hue/src/main/java/org/openhab/binding/hue/internal/console/HueCommandExtension.java +++ b/bundles/org.openhab.binding.hue/src/main/java/org/openhab/binding/hue/internal/console/HueCommandExtension.java @@ -75,10 +75,10 @@ public void execute(String[] args, Console console) { console.println("Bad thing id '" + args[0] + "'"); printUsage(console); } else if (thingHandler == null) { - console.println("No handler initialized for the thing id '" + args[0] + "'"); + console.println("No handler initialized for the thingUID '" + args[0] + "'"); printUsage(console); } else if (bridgeHandler == null && groupHandler == null) { - console.println("'" + args[0] + "' is neither a hue bridge id nor a hue group thing id"); + console.println("'" + args[0] + "' is neither a Hue bridgeUID nor a Hue groupThingUID"); printUsage(console); } else { switch (args[1]) { @@ -87,7 +87,7 @@ public void execute(String[] args, Console console) { String userName = bridgeHandler.getUserName(); console.println("Your user name is " + (userName != null ? userName : "undefined")); } else { - console.println("'" + args[0] + "' is not a hue bridge id"); + console.println("'" + args[0] + "' is not a Hue bridgeUID"); printUsage(console); } break; diff --git a/bundles/org.openhab.binding.hue/src/main/java/org/openhab/binding/hue/internal/handler/HueGroupHandler.java b/bundles/org.openhab.binding.hue/src/main/java/org/openhab/binding/hue/internal/handler/HueGroupHandler.java index f3e1c87bf9b24..27cdb5f0af72e 100644 --- a/bundles/org.openhab.binding.hue/src/main/java/org/openhab/binding/hue/internal/handler/HueGroupHandler.java +++ b/bundles/org.openhab.binding.hue/src/main/java/org/openhab/binding/hue/internal/handler/HueGroupHandler.java @@ -74,8 +74,6 @@ public class HueGroupHandler extends BaseThingHandler implements GroupStatusList private ColorTemperature colorTemperatureCapabilties = new ColorTemperature(); private long defaultFadeTime = 400; - private boolean propertiesInitializedSuccessfully; - private @Nullable HueClient hueClient; private @Nullable ScheduledFuture scheduledFuture; @@ -128,11 +126,10 @@ private void initializeThing(@Nullable ThingStatus bridgeStatus) { } private synchronized void initializeProperties(@Nullable FullGroup fullGroup) { - if (!propertiesInitializedSuccessfully && fullGroup != null) { + if (fullGroup != null) { Map properties = editProperties(); properties.put(PROPERTY_MEMBERS, fullGroup.getLightIds().stream().collect(Collectors.joining(","))); updateProperties(properties); - propertiesInitializedSuccessfully = true; } }