From c5739eccc978f520473750871074d1e94a7579d5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ga=C3=ABl=20L=27hopital?= Date: Wed, 6 Sep 2023 08:36:58 +0200 Subject: [PATCH] [netatmo] Handle new event types (#15533) * Adding new Netatmo events * Changed NAPushType to record. * Adding impact on WebhookEvent --------- Signed-off-by: clinique --- .../internal/api/data/EventSubType.java | 74 ++++++++++--------- .../netatmo/internal/api/data/EventType.java | 24 +++++- .../netatmo/internal/api/dto/Event.java | 8 +- .../internal/api/dto/WebhookEvent.java | 4 +- .../internal/deserialization/NAPushType.java | 18 +---- .../NAPushTypeDeserializer.java | 4 +- 6 files changed, 72 insertions(+), 60 deletions(-) diff --git a/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/api/data/EventSubType.java b/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/api/data/EventSubType.java index 44748ffc1..1e839f60e 100644 --- a/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/api/data/EventSubType.java +++ b/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/api/data/EventSubType.java @@ -24,53 +24,57 @@ import org.eclipse.jdt.annotation.NonNullByDefault; @NonNullByDefault public enum EventSubType { // SD Card sub events - SD_CARD_MISSING(1, EventType.SD), - SD_CARD_INSERTED(2, EventType.SD), - SD_CARD_FORMATTED(3, EventType.SD), - SD_CARD_WORKING(4, EventType.SD), - SD_CARD_DEFECTIVE(5, EventType.SD), - SD_CARD_INCOMPATIBLE_SPEED(6, EventType.SD), - SD_CARD_INSUFFICIENT_SPACE(7, EventType.SD), + SD_CARD_MISSING("1", EventType.SD), + SD_CARD_INSERTED("2", EventType.SD), + SD_CARD_FORMATTED("3", EventType.SD), + SD_CARD_WORKING("4", EventType.SD), + SD_CARD_DEFECTIVE("5", EventType.SD), + SD_CARD_INCOMPATIBLE_SPEED("6", EventType.SD), + SD_CARD_INSUFFICIENT_SPACE("7", EventType.SD), // Power sub events - ALIM_INCORRECT_POWER(1, EventType.ALIM), - ALIM_CORRECT_POWER(2, EventType.ALIM), + ALIM_INCORRECT_POWER("1", EventType.ALIM), + ALIM_CORRECT_POWER("2", EventType.ALIM), // Smoke detector sub events - DETECTION_CHAMBER_CLEAN(0, EventType.DETECTION_CHAMBER_STATUS), - DETECTION_CHAMBER_DIRTY(1, EventType.DETECTION_CHAMBER_STATUS), - BATTERY_LOW(0, EventType.BATTERY_STATUS), - BATTERY_VERY_LOW(1, EventType.BATTERY_STATUS), - SMOKE_CLEARED(0, EventType.SMOKE), - SMOKE_DETECTED(1, EventType.SMOKE), - HUSH_ACTIVATED(0, EventType.HUSH), - HUSH_DEACTIVATED(1, EventType.HUSH), - SOUND_TEST_OK(0, EventType.SOUND_TEST), - SOUND_TEST_ERROR(1, EventType.SOUND_TEST), - DETECTOR_READY(0, EventType.TAMPERED), - DETECTOR_TAMPERED(1, EventType.TAMPERED), + DETECTION_CHAMBER_CLEAN("0", EventType.DETECTION_CHAMBER_STATUS), + DETECTION_CHAMBER_DIRTY("1", EventType.DETECTION_CHAMBER_STATUS), + BATTERY_LOW("0", EventType.BATTERY_STATUS), + BATTERY_VERY_LOW("1", EventType.BATTERY_STATUS), + SMOKE_CLEARED("0", EventType.SMOKE), + SMOKE_DETECTED("1", EventType.SMOKE), + HUSH_ACTIVATED("0", EventType.HUSH), + HUSH_DEACTIVATED("1", EventType.HUSH), + SOUND_TEST_OK("0", EventType.SOUND_TEST), + SOUND_TEST_ERROR("1", EventType.SOUND_TEST), + DETECTOR_READY("0", EventType.TAMPERED), + DETECTOR_TAMPERED("1", EventType.TAMPERED), // Carbon Monoxide Alarm - CO_OK(0, EventType.CO_DETECTED), - CO_PRE_ALARM(1, EventType.CO_DETECTED), - CO_ALARM(2, EventType.CO_DETECTED), + CO_OK("0", EventType.CO_DETECTED), + CO_PRE_ALARM("1", EventType.CO_DETECTED), + CO_ALARM("2", EventType.CO_DETECTED), - WIFI_STATUS_OK(1, EventType.WIFI_STATUS), - WIFI_STATUS_ERROR(0, EventType.WIFI_STATUS), + WIFI_STATUS_OK("1", EventType.WIFI_STATUS), + WIFI_STATUS_ERROR("0", EventType.WIFI_STATUS), // Artificially implemented by the binding subtypes - PERSON_ARRIVAL(1, EventType.PERSON, EventType.PERSON_HOME), - PERSON_SEEN(2, EventType.PERSON), - PERSON_DEPARTURE(1, EventType.PERSON_AWAY), - MOVEMENT_HUMAN(1, EventType.MOVEMENT, EventType.HUMAN), - MOVEMENT_VEHICLE(2, EventType.MOVEMENT), - MOVEMENT_ANIMAL(3, EventType.MOVEMENT, EventType.ANIMAL); + PERSON_ARRIVAL("1", EventType.PERSON, EventType.PERSON_HOME), + PERSON_SEEN("2", EventType.PERSON), + PERSON_DEPARTURE("1", EventType.PERSON_AWAY), + MOVEMENT_HUMAN("1", EventType.MOVEMENT, EventType.HUMAN), + MOVEMENT_VEHICLE("2", EventType.MOVEMENT), + MOVEMENT_ANIMAL("3", EventType.MOVEMENT, EventType.ANIMAL), + + // Weather station events + RAIN_ALARM("rain", EventType.ALARM_EVENT), + CO2_ALARM("co2", EventType.ALARM_EVENT); public final List types; - public final int subType; + public final String subType; - EventSubType(int i, EventType... types) { + EventSubType(String subType, EventType... types) { this.types = List.of(types); - this.subType = i; + this.subType = subType; } } diff --git a/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/api/data/EventType.java b/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/api/data/EventType.java index 2ff660b6f..fafec0bd4 100644 --- a/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/api/data/EventType.java +++ b/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/api/data/EventType.java @@ -143,7 +143,25 @@ public enum EventType { NEW_DEVICE(ModuleType.HOME), @SerializedName("co_detected") - CO_DETECTED(ModuleType.CO_DETECTOR); + CO_DETECTED(ModuleType.CO_DETECTOR), + + @SerializedName("alarm_event") // an alarm event arrived on a weather station module + ALARM_EVENT(ModuleType.WEATHER_STATION), + + @SerializedName("entered") // the alarm was raised + ALARM_ENTERED(ModuleType.WEATHER_STATION), + + @SerializedName("exited") // the alarm is stopped + ALARM_EXITED(ModuleType.WEATHER_STATION), + + @SerializedName("display_change") // a manual action has been done on the thermostat + DISPLAY_CHANGE(ModuleType.THERMOSTAT), + + @SerializedName("set_point") // a setpoint has been set + SET_POINT(ModuleType.THERMOSTAT), + + @SerializedName("cancel_set_point") // manual setpoint ended + CANCEL_SET_POINT(ModuleType.THERMOSTAT); public static final EnumSet AS_SET = EnumSet.allOf(EventType.class); @@ -161,4 +179,8 @@ public enum EventType { public boolean validFor(ModuleType searched) { return appliesTo.contains(searched); } + + public ModuleType getFirstModule() { + return appliesTo.iterator().next(); + } } diff --git a/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/api/dto/Event.java b/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/api/dto/Event.java index 1f5847f1c..e81232bb5 100644 --- a/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/api/dto/Event.java +++ b/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/api/dto/Event.java @@ -35,7 +35,7 @@ public abstract class Event extends NAObject { protected EventType type = EventType.UNKNOWN; @SerializedName(value = "camera_id", alternate = { "module_id" }) private String cameraId = ""; - protected int subType = -1; + protected String subType = ""; public abstract ZonedDateTime getTime(); @@ -54,11 +54,11 @@ public abstract class Event extends NAObject { @Override public @Nullable String getName() { String localMessage = super.getName(); - return (localMessage != null ? localMessage.replace("", "").replace("", "") : ""); + return localMessage != null ? localMessage.replace("", "").replace("", "") : ""; } public Optional getSubTypeDescription() { - return Stream.of(EventSubType.values()).filter(v -> v.types.contains(getEventType()) && v.subType == subType) - .findFirst(); + return Stream.of(EventSubType.values()) + .filter(v -> v.types.contains(getEventType()) && v.subType.equals(subType)).findFirst(); } } diff --git a/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/api/dto/WebhookEvent.java b/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/api/dto/WebhookEvent.java index 71ef8ff29..c8e843681 100644 --- a/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/api/dto/WebhookEvent.java +++ b/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/api/dto/WebhookEvent.java @@ -33,6 +33,7 @@ import org.openhab.binding.netatmo.internal.deserialization.NAPushType; public class WebhookEvent extends Event { private NAPushType pushType = NAPushType.UNKNOWN; private String homeId = ""; + private String roomId = ""; private String deviceId = ""; private @Nullable String snapshotUrl; private @Nullable String vignetteUrl; @@ -50,7 +51,7 @@ public class WebhookEvent extends Event { @Override public EventType getEventType() { - return pushType.getEvent(); + return pushType.event(); } @Override @@ -77,6 +78,7 @@ public class WebhookEvent extends Event { result.add(getCameraId()); addNotBlank(result, homeId); addNotBlank(result, deviceId); + addNotBlank(result, roomId); addNotBlank(result, getCameraId()); result.addAll(getPersons().keySet()); return result; diff --git a/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/deserialization/NAPushType.java b/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/deserialization/NAPushType.java index aca419119..6a0938ce4 100644 --- a/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/deserialization/NAPushType.java +++ b/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/deserialization/NAPushType.java @@ -22,22 +22,6 @@ import org.openhab.binding.netatmo.internal.api.data.ModuleType; * @author Gaƫl L'hopital - Initial contribution */ @NonNullByDefault -public class NAPushType { +public record NAPushType(ModuleType moduleType, EventType event) { public static final NAPushType UNKNOWN = new NAPushType(ModuleType.UNKNOWN, EventType.UNKNOWN); - - private final ModuleType moduleType; - private final EventType event; - - NAPushType(ModuleType moduleType, EventType event) { - this.moduleType = moduleType; - this.event = event; - } - - public ModuleType getModuleType() { - return moduleType; - } - - public EventType getEvent() { - return event; - } } diff --git a/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/deserialization/NAPushTypeDeserializer.java b/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/deserialization/NAPushTypeDeserializer.java index d552ddea8..57ac0e118 100644 --- a/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/deserialization/NAPushTypeDeserializer.java +++ b/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/deserialization/NAPushTypeDeserializer.java @@ -46,12 +46,12 @@ class NAPushTypeDeserializer implements JsonDeserializer { moduleType = fromNetatmoObject(elements[0]); eventType = fromEvent(elements[1]); } else if (elements.length == 1) { - moduleType = ModuleType.ACCOUNT; eventType = fromEvent(string); + moduleType = eventType.getFirstModule(); } if (moduleType.equals(ModuleType.UNKNOWN) || eventType.equals(EventType.UNKNOWN)) { - logger.warn("Unknown module or event type : {}, deserialized to '{}-{}'", string, moduleType, eventType); + logger.warn("Unknown module or event type: {}, deserialized to '{}-{}'", string, moduleType, eventType); } return new NAPushType(moduleType, eventType);