Skip to content

Commit

Permalink
Add channels for last motion/temperature sensor update (API v2)
Browse files Browse the repository at this point in the history
Resolves openhab#15546

Signed-off-by: Jacob Laursen <jacob-github@vindvejr.dk>
  • Loading branch information
jlaur committed Oct 10, 2023
1 parent 06d8e75 commit 4e5453a
Show file tree
Hide file tree
Showing 19 changed files with 437 additions and 67 deletions.
50 changes: 26 additions & 24 deletions bundles/org.openhab.binding.hue/doc/readme_v2.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,30 +59,32 @@ The configuration of all things (as described above) is the same regardless of w

Device things support some of the following channels:

| Channel ID | Item Type | Description |
|-----------------------|--------------------|---------------------------------------------------------------------------------------------------------------------|
| color | Color | Supports full color control with hue, saturation and brightness values, or brightness only, or switching on or off. |
| brightness | Dimmer | Supports control of the brightness value, or switching on or off. |
| color-temperature | Dimmer | Supports control of the color temperature in percent from cold (0%) to warm (100%). |
| color-temperature-abs | Number:Temperature | Supports control of the color temperature via a QuantityType having a temperature unit e.g. Kelvin. (Advanced) |
| switch | Switch | Supports switching the device on and off. |
| dynamics | Number:Time | Sets the duration of dynamic transitions between light states. (Advanced) |
| alert | String | Allows setting an alert on a light e.g. flashing them. (Advanced) |
| effect | String | Allows setting an effect on a light e.g. 'candle' effect. (Advanced) |
| button-last-event | (String) | Informs which button was last pressed in the device. (Trigger Channel) |
| rotary-steps | (String) | Informs about the number of rotary steps of the last rotary dial movement. (Trigger Channel) |
| motion | Switch | Shows if motion has been detected by the sensor. (Read Only) |
| motion-enabled | Switch | Supports enabling / disabling the motion sensor. (Advanced) |
| light-level | Number:Illuminance | Shows the current light level measured by the sensor. (Read Only) |
| light-level-enabled | Switch | Supports enabling / disabling the light level sensor. (Advanced) |
| temperature | Number:Temperature | Shows the current temperature measured by the sensor. (Read Only) |
| temperature-enabled | Switch | Supports enabling / disabling the temperature sensor. (Advanced) |
| battery-level | Number | Shows the battery level. (Read Only) |
| battery-low | Switch | Indicates whether the battery is low or not. (Read Only) |
| last-updated | DateTime | The date and time when the thing state was last updated. (Read Only) (Advanced) |
| color-xy-only | Color | Allows access to the `color-xy` parameter of the light(s) only. Has no impact on `dimming` or `on-off` parameters. |
| dimming-only | Dimmer | Allows access to the `dimming` parameter of the light(s) only. Has no impact on `color-xy` or `on-off` parameters. |
| on-off-only | Switch | Allows access to the `on-off` parameter of the light(s) only. Has no impact on `color-xy` or `dimming` parameters. |
| Channel ID | Item Type | Description |
|--------------------------|--------------------|---------------------------------------------------------------------------------------------------------------------|
| color | Color | Supports full color control with hue, saturation and brightness values, or brightness only, or switching on or off. |
| brightness | Dimmer | Supports control of the brightness value, or switching on or off. |
| color-temperature | Dimmer | Supports control of the color temperature in percent from cold (0%) to warm (100%). |
| color-temperature-abs | Number:Temperature | Supports control of the color temperature via a QuantityType having a temperature unit e.g. Kelvin. (Advanced) |
| switch | Switch | Supports switching the device on and off. |
| dynamics | Number:Time | Sets the duration of dynamic transitions between light states. (Advanced) |
| alert | String | Allows setting an alert on a light e.g. flashing them. (Advanced) |
| effect | String | Allows setting an effect on a light e.g. 'candle' effect. (Advanced) |
| button-last-event | (String) | Informs which button was last pressed in the device. (Trigger Channel) |
| rotary-steps | (String) | Informs about the number of rotary steps of the last rotary dial movement. (Trigger Channel) |
| motion | Switch | Shows if motion has been detected by the sensor. (Read Only) |
| motion-enabled | Switch | Supports enabling / disabling the motion sensor. (Advanced) |
| motion-last-updated | DateTime | The date and time when the motion value was last updated. (Read Only) (Advanced) |
| light-level | Number:Illuminance | Shows the current light level measured by the sensor. (Read Only) |
| light-level-enabled | Switch | Supports enabling / disabling the light level sensor. (Advanced) |
| temperature | Number:Temperature | Shows the current temperature measured by the sensor. (Read Only) |
| temperature-last-updated | DateTime | The date and time when the temperature was last updated. (Read Only) (Advanced) |
| temperature-enabled | Switch | Supports enabling / disabling the temperature sensor. (Advanced) |
| battery-level | Number | Shows the battery level. (Read Only) |
| battery-low | Switch | Indicates whether the battery is low or not. (Read Only) |
| last-updated | DateTime | The date and time when the thing state was last updated. (Read Only) (Advanced) |
| color-xy-only | Color | Allows access to the `color-xy` parameter of the light(s) only. Has no impact on `dimming` or `on-off` parameters. |
| dimming-only | Dimmer | Allows access to the `dimming` parameter of the light(s) only. Has no impact on `color-xy` or `on-off` parameters. |
| on-off-only | Switch | Allows access to the `on-off` parameter of the light(s) only. Has no impact on `color-xy` or `dimming` parameters. |

The exact list of channels in a given device is determined at run time when the system is started.
Each device reports its own live list of capabilities, and the respective list of channels is created accordingly.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,10 +153,12 @@ public class HueBindingConstants {
public static final String CHANNEL_2_BUTTON_LAST_EVENT = "button-last-event";
public static final String CHANNEL_2_ROTARY_STEPS = "rotary-steps";
public static final String CHANNEL_2_MOTION = "motion";
public static final String CHANNEL_2_MOTION_LAST_UPDATED = "motion-last-updated";
public static final String CHANNEL_2_MOTION_ENABLED = "motion-enabled";
public static final String CHANNEL_2_LIGHT_LEVEL = "light-level";
public static final String CHANNEL_2_LIGHT_LEVEL_ENABLED = "light-level-enabled";
public static final String CHANNEL_2_TEMPERATURE = CHANNEL_TEMPERATURE;
public static final String CHANNEL_2_TEMPERATURE_LAST_UPDATED = "temperature-last-updated";
public static final String CHANNEL_2_TEMPERATURE_ENABLED = "temperature-enabled";
public static final String CHANNEL_2_BATTERY_LEVEL = "battery-level";
public static final String CHANNEL_2_BATTERY_LOW = "battery-low";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,12 +81,14 @@
import org.openhab.binding.hue.internal.exceptions.ApiException;
import org.openhab.binding.hue.internal.exceptions.HttpUnauthorizedException;
import org.openhab.binding.hue.internal.handler.Clip2BridgeHandler;
import org.openhab.binding.hue.internal.serialization.InstantDeserializer;
import org.openhab.core.io.net.http.HttpClientFactory;
import org.openhab.core.io.net.http.HttpUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonParseException;
Expand Down Expand Up @@ -556,7 +558,8 @@ public static boolean isClip2Supported(String hostName) throws IOException {
private final String registrationUrl;
private final String applicationKey;
private final Clip2BridgeHandler bridgeHandler;
private final Gson jsonParser = new Gson();
private final Gson jsonParser = new GsonBuilder().registerTypeAdapter(Instant.class, new InstantDeserializer())
.create();
private final Semaphore streamMutex = new Semaphore(MAX_CONCURRENT_STREAMS, true); // i.e. fair
private final ReadWriteLock sessionUseCreateLock = new ReentrantReadWriteLock(true); // i.e. fair
private final Map<Integer, Future<?>> fatalErrorTasks = new ConcurrentHashMap<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
package org.openhab.binding.hue.internal.dto.clip2;

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.openhab.core.library.types.OnOffType;
import org.openhab.core.types.State;
import org.openhab.core.types.UnDefType;
Expand All @@ -28,11 +29,24 @@
public class Motion {
private boolean motion;
private @SerializedName("motion_valid") boolean motionValid;
private @Nullable @SerializedName("motion_report") MotionReport motionReport;

/**
* The underlying field is deprecated in the CLIP 2 API.
* Moved to motion_report/motion.
* Should be used only as fallback for older firmwares.
*
* @return true if motion is detected
*/
public boolean isMotion() {
return motion;
}

/**
* The underlying field is deprecated in the CLIP 2 API.
* Motion is valid when motion_report property is present, invalid when absent.
* Should be used only as fallback for older firmwares.
*/
public boolean isMotionValid() {
return motionValid;
}
Expand All @@ -44,4 +58,8 @@ public State getMotionState() {
public State getMotionValidState() {
return OnOffType.from(motionValid);
}

public @Nullable MotionReport getMotionReport() {
return motionReport;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/**
* Copyright (c) 2010-2023 Contributors to the openHAB project
*
* See the NOTICE file(s) distributed with this work for additional
* information.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.openhab.binding.hue.internal.dto.clip2;

import java.time.Instant;

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;

/**
* DTO for CLIP 2 motion sensor report.
*
* @author Jacob Laursen - Initial contribution
*/
@NonNullByDefault
public class MotionReport {
private @Nullable Instant changed;
private boolean motion;

/**
* @return last time the value of this property is changed.
*/
public @Nullable Instant getLastChanged() {
return changed;
}

/**
* @return true if motion is detected
*/
public boolean isMotion() {
return motion;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
import java.math.MathContext;
import java.math.RoundingMode;
import java.time.Duration;
import java.time.Instant;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.List;
import java.util.Map;
import java.util.Objects;
Expand All @@ -28,12 +31,14 @@
import org.openhab.binding.hue.internal.dto.clip2.enums.ResourceType;
import org.openhab.binding.hue.internal.dto.clip2.enums.ZigbeeStatus;
import org.openhab.binding.hue.internal.exceptions.DTOPresentButEmptyException;
import org.openhab.core.library.types.DateTimeType;
import org.openhab.core.library.types.DecimalType;
import org.openhab.core.library.types.HSBType;
import org.openhab.core.library.types.OnOffType;
import org.openhab.core.library.types.PercentType;
import org.openhab.core.library.types.QuantityType;
import org.openhab.core.library.types.StringType;
import org.openhab.core.library.unit.SIUnits;
import org.openhab.core.types.Command;
import org.openhab.core.types.State;
import org.openhab.core.types.UnDefType;
Expand Down Expand Up @@ -386,7 +391,30 @@ public State getLightLevelState() {

public State getMotionState() {
Motion motion = this.motion;
return Objects.nonNull(motion) ? motion.getMotionState() : UnDefType.NULL;
if (motion == null) {
return UnDefType.NULL;
}
MotionReport motionReport = motion.getMotionReport();
if (motionReport == null) {
return motion.getMotionState();
}
return OnOffType.from(motionReport.isMotion());
}

public State getMotionLastUpdatedState(ZoneId zoneId) {
Motion motion = this.motion;
if (motion == null) {
return UnDefType.NULL;
}
MotionReport motionReport = motion.getMotionReport();
if (motionReport == null) {
return UnDefType.UNDEF;
}
Instant lastChanged = motionReport.getLastChanged();
if (lastChanged == null) {
return UnDefType.UNDEF;
}
return new DateTimeType(ZonedDateTime.ofInstant(lastChanged, zoneId));
}

public State getMotionValidState() {
Expand Down Expand Up @@ -509,7 +537,30 @@ public JsonObject getStatus() {

public State getTemperatureState() {
Temperature temperature = this.temperature;
return Objects.nonNull(temperature) ? temperature.getTemperatureState() : UnDefType.NULL;
if (temperature == null) {
return UnDefType.NULL;
}
TemperatureReport temperatureReport = temperature.getTemperatureReport();
if (temperatureReport == null) {
return temperature.getTemperatureState();
}
return new QuantityType<>(temperatureReport.getTemperature(), SIUnits.CELSIUS);
}

public State getTemperatureLastUpdatedState(ZoneId zoneId) {
Temperature temperature = this.temperature;
if (temperature == null) {
return UnDefType.NULL;
}
TemperatureReport temperatureReport = temperature.getTemperatureReport();
if (temperatureReport == null) {
return UnDefType.UNDEF;
}
Instant lastChanged = temperatureReport.getLastChanged();
if (lastChanged == null) {
return UnDefType.UNDEF;
}
return new DateTimeType(ZonedDateTime.ofInstant(lastChanged, zoneId));
}

public State getTemperatureValidState() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
package org.openhab.binding.hue.internal.dto.clip2;

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.openhab.core.library.types.OnOffType;
import org.openhab.core.library.types.QuantityType;
import org.openhab.core.library.unit.SIUnits;
Expand All @@ -30,11 +31,24 @@
public class Temperature {
private float temperature;
private @SerializedName("temperature_valid") boolean temperatureValid;
private @Nullable @SerializedName("temperature_report") TemperatureReport temperatureReport;

/**
* The underlying field is deprecated in the CLIP 2 API.
* Moved to temperature_report/temperature.
* Should be used only as fallback for older firmwares.
*
* @return temperature in 1.00 degrees Celsius
*/
public float getTemperature() {
return temperature;
}

/**
* The underlying field is deprecated in the CLIP 2 API.
* Indication whether the value presented in temperature is valid
* Should be used only as fallback for older firmwares.
*/
public boolean isTemperatureValid() {
return temperatureValid;
}
Expand All @@ -46,4 +60,8 @@ public State getTemperatureState() {
public State getTemperatureValidState() {
return OnOffType.from(temperatureValid);
}

public @Nullable TemperatureReport getTemperatureReport() {
return temperatureReport;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/**
* Copyright (c) 2010-2023 Contributors to the openHAB project
*
* See the NOTICE file(s) distributed with this work for additional
* information.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.openhab.binding.hue.internal.dto.clip2;

import java.time.Instant;

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;

/**
* DTO for CLIP 2 temperature sensor report.
*
* @author Jacob Laursen - Initial contribution
*/
@NonNullByDefault
public class TemperatureReport {
private @Nullable Instant changed;
private float temperature;

/**
* @return last time the value of this property is changed.
*/
public @Nullable Instant getLastChanged() {
return changed;
}

/**
* @return temperature in 1.00 degrees Celsius
*/
public float getTemperature() {
return temperature;
}
}
Loading

0 comments on commit 4e5453a

Please sign in to comment.