diff --git a/bundles/org.openhab.binding.boschshc/src/main/java/org/openhab/binding/boschshc/internal/BoschSHCBindingConstants.java b/bundles/org.openhab.binding.boschshc/src/main/java/org/openhab/binding/boschshc/internal/BoschSHCBindingConstants.java index f35d8de9a82f..6d091b4f1a93 100644 --- a/bundles/org.openhab.binding.boschshc/src/main/java/org/openhab/binding/boschshc/internal/BoschSHCBindingConstants.java +++ b/bundles/org.openhab.binding.boschshc/src/main/java/org/openhab/binding/boschshc/internal/BoschSHCBindingConstants.java @@ -32,6 +32,7 @@ public class BoschSHCBindingConstants { public static final ThingTypeUID THING_TYPE_INWALL_SWITCH = new ThingTypeUID(BINDING_ID, "in-wall-switch"); public static final ThingTypeUID THING_TYPE_TWINGUARD = new ThingTypeUID(BINDING_ID, "twinguard"); public static final ThingTypeUID THING_TYPE_WINDOW_CONTACT = new ThingTypeUID(BINDING_ID, "window-contact"); + public static final ThingTypeUID THING_TYPE_MOTION_DETECTOR = new ThingTypeUID(BINDING_ID, "motion-detector"); // List of all Channel IDs // Auto-generated from thing-types.xml via script, don't modify @@ -47,5 +48,6 @@ public class BoschSHCBindingConstants { public static final String CHANNEL_PURITY_RATING = "purity-rating"; public static final String CHANNEL_COMBINED_RATING = "combined-rating"; public static final String CHANNEL_CONTACT = "contact"; + public static final String CHANNEL_LATEST_MOTION = "latest-motion"; } diff --git a/bundles/org.openhab.binding.boschshc/src/main/java/org/openhab/binding/boschshc/internal/BoschSHCHandlerFactory.java b/bundles/org.openhab.binding.boschshc/src/main/java/org/openhab/binding/boschshc/internal/BoschSHCHandlerFactory.java index 67a0b1f1af3b..236e89af9489 100644 --- a/bundles/org.openhab.binding.boschshc/src/main/java/org/openhab/binding/boschshc/internal/BoschSHCHandlerFactory.java +++ b/bundles/org.openhab.binding.boschshc/src/main/java/org/openhab/binding/boschshc/internal/BoschSHCHandlerFactory.java @@ -46,7 +46,7 @@ public class BoschSHCHandlerFactory extends BaseThingHandlerFactory { // List of all supported Bosch devices. public static final Collection SUPPORTED_THING_TYPES_UIDS = Arrays.asList(THING_TYPE_SHC, - THING_TYPE_INWALL_SWITCH, THING_TYPE_TWINGUARD, THING_TYPE_WINDOW_CONTACT); + THING_TYPE_INWALL_SWITCH, THING_TYPE_TWINGUARD, THING_TYPE_WINDOW_CONTACT, THING_TYPE_MOTION_DETECTOR); @Override public boolean supportsThingType(ThingTypeUID thingTypeUID) { @@ -77,6 +77,10 @@ else if (THING_TYPE_WINDOW_CONTACT.equals(thingTypeUID)) { return new WindowContactHandler(thing); } + else if (THING_TYPE_MOTION_DETECTOR.equals(thingTypeUID)) { + return new MotionDetectorHandler(thing); + } + else { logger.warn("Failed to find handler for device: {}", thingTypeUID); } diff --git a/bundles/org.openhab.binding.boschshc/src/main/java/org/openhab/binding/boschshc/internal/LatestMotionState.java b/bundles/org.openhab.binding.boschshc/src/main/java/org/openhab/binding/boschshc/internal/LatestMotionState.java new file mode 100644 index 000000000000..a27d7cb0e0e8 --- /dev/null +++ b/bundles/org.openhab.binding.boschshc/src/main/java/org/openhab/binding/boschshc/internal/LatestMotionState.java @@ -0,0 +1,29 @@ +package org.openhab.binding.boschshc.internal; + +import com.google.gson.annotations.SerializedName; + +/** + * { + * "result": [ + * { + * "path": "/devices/hdm:ZigBee:000d6f0004b95a62/services/LatestMotion", + * "@type": "DeviceServiceData", + * "id": "LatestMotion", + * "state": { + * "latestMotionDetected": "2020-04-03T19:02:19.054Z", + * "@type": "latestMotionState" + * }, + * "deviceId": "hdm:ZigBee:000d6f0004b95a62" + * } + * ], + * "jsonrpc": "2.0" + * } + * + */ +public class LatestMotionState { + + @SerializedName("@type") + String type; + + String latestMotionDetected; +} diff --git a/bundles/org.openhab.binding.boschshc/src/main/java/org/openhab/binding/boschshc/internal/MotionDetectorHandler.java b/bundles/org.openhab.binding.boschshc/src/main/java/org/openhab/binding/boschshc/internal/MotionDetectorHandler.java new file mode 100644 index 000000000000..6b621ef16631 --- /dev/null +++ b/bundles/org.openhab.binding.boschshc/src/main/java/org/openhab/binding/boschshc/internal/MotionDetectorHandler.java @@ -0,0 +1,73 @@ +package org.openhab.binding.boschshc.internal; + +import static org.openhab.binding.boschshc.internal.BoschSHCBindingConstants.CHANNEL_LATEST_MOTION; + +import org.eclipse.jdt.annotation.NonNull; +import org.eclipse.smarthome.core.library.types.DateTimeType; +import org.eclipse.smarthome.core.thing.Bridge; +import org.eclipse.smarthome.core.thing.ChannelUID; +import org.eclipse.smarthome.core.thing.Thing; +import org.eclipse.smarthome.core.thing.ThingStatus; +import org.eclipse.smarthome.core.thing.ThingStatusDetail; +import org.eclipse.smarthome.core.types.Command; +import org.eclipse.smarthome.core.types.RefreshType; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import com.google.gson.Gson; +import com.google.gson.JsonElement; +import com.google.gson.JsonSyntaxException; + +public class MotionDetectorHandler extends BoschSHCHandler { + private final Logger logger = LoggerFactory.getLogger(BoschSHCHandler.class); + + public MotionDetectorHandler(Thing thing) { + super(thing); + logger.warn("Creating motion detector thing: {}", thing.getLabel()); + } + + @Override + public void handleCommand(ChannelUID channelUID, Command command) { + + BoschSHCConfiguration config = super.getBoschConfig(); + Bridge bridge = this.getBridge(); + + if (bridge != null && config != null) { + + logger.info("Handle command for: {} - {}", config.id, command); + BoschSHCBridgeHandler bridgeHandler = (BoschSHCBridgeHandler) bridge.getHandler(); + + if (bridgeHandler != null) { + + if (CHANNEL_LATEST_MOTION.equals(channelUID.getId())) { + if (command instanceof RefreshType) { + + // Refresh the temperature from the Bosch Twinguard device. + // Might not be necessary, can just wait until we get one + logger.warn("Refreshing the temperature is not yet supported."); + } + // Otherwise: not action supported here. + } + } + } else { + updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, "Bridge or config is NUL"); + } + } + + @Override + public void processUpdate(String id, @NonNull JsonElement state) { + logger.warn("Motion detector: received update: {} {}", id, state); + Gson gson = new Gson(); + + try { + LatestMotionState parsed = gson.fromJson(state, LatestMotionState.class); + + DateTimeType date = new DateTimeType(parsed.latestMotionDetected); + logger.warn("Parsed date of latest motion to {}: {} as date {}", this.getBoschID(), parsed, date); + updateState(CHANNEL_LATEST_MOTION, date); + + } catch (JsonSyntaxException e) { + logger.warn("Received unknown update in in-wall switch: {}", state); + } + } +} diff --git a/bundles/org.openhab.binding.boschshc/src/main/resources/ESH-INF/thing/thing-types.xml b/bundles/org.openhab.binding.boschshc/src/main/resources/ESH-INF/thing/thing-types.xml index 05cd63b4c8ad..02cf311c7afb 100644 --- a/bundles/org.openhab.binding.boschshc/src/main/resources/ESH-INF/thing/thing-types.xml +++ b/bundles/org.openhab.binding.boschshc/src/main/resources/ESH-INF/thing/thing-types.xml @@ -98,6 +98,24 @@ + + + + + Bosch Motion Detector + + + + + + + + + Device ID of the contact + true + + + @@ -173,4 +191,10 @@ A Bosch window and door contact. + + DateTime + + Timestamp of the latest motion. + +