diff --git a/bundles/org.openhab.binding.netatmo/NOTICE b/bundles/org.openhab.binding.netatmo/NOTICE index d843d599a8177..41d3f500d0473 100644 --- a/bundles/org.openhab.binding.netatmo/NOTICE +++ b/bundles/org.openhab.binding.netatmo/NOTICE @@ -39,10 +39,10 @@ oltu.oauth2 * Project: https://oltu.apache.org * Source: https://svn.apache.org/viewvc/oltu/trunk -netatmo-java-retrofit -* License: Apache 2.0 License +netatmo-swagger-decl +* License: MIT License * Project: https://dev.netatmo.com -* Source: https://github.com/cbornet/netatmo-swagger-api +* Source: https://github.com/cbornet/netatmo-swagger-decl retrofit * License: Apache 2.0 License diff --git a/bundles/org.openhab.binding.netatmo/README.md b/bundles/org.openhab.binding.netatmo/README.md index f4e26dc70f019..989f1916791b9 100644 --- a/bundles/org.openhab.binding.netatmo/README.md +++ b/bundles/org.openhab.binding.netatmo/README.md @@ -42,6 +42,7 @@ Bridge netatmo:netatmoapi:home [ clientId="", clientSecret=" - - com.github.cbornet.netatmo-swagger-api - netatmo-java-retrofit - 1.1.3 - compile - org.openhab.osgiify org.json.json @@ -63,4 +57,29 @@ + + + + io.swagger + swagger-codegen-maven-plugin + 2.1.6 + + + + generate + + + https://raw.githubusercontent.com/cbornet/netatmo-swagger-decl/master/spec/swagger.yaml + java + retrofit + + src/main/java + + + + + + + + diff --git a/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/NetatmoBindingConstants.java b/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/NetatmoBindingConstants.java index dd2b5cd76ca94..3e4c472bb5b9f 100644 --- a/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/NetatmoBindingConstants.java +++ b/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/NetatmoBindingConstants.java @@ -227,6 +227,8 @@ public class NetatmoBindingConstants { public static final String CHANNEL_WELCOME_HOME_EVENT = "welcomeHomeEvent"; + public static final String CHANNEL_CAMERA_EVENT = "cameraEvent"; + public static final String CHANNEL_WELCOME_PERSON_LASTSEEN = "welcomePersonLastSeen"; public static final String CHANNEL_WELCOME_PERSON_ATHOME = "welcomePersonAtHome"; public static final String CHANNEL_WELCOME_PERSON_AVATAR_URL = "welcomePersonAvatarUrl"; diff --git a/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/camera/CameraHandler.java b/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/camera/CameraHandler.java index da94ff23d4d0c..b86f06d875379 100644 --- a/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/camera/CameraHandler.java +++ b/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/camera/CameraHandler.java @@ -48,7 +48,7 @@ protected void updateProperties(NAWelcomeCamera moduleData) { @SuppressWarnings("null") @Override - protected State getNAThingProperty(String channelId) { + protected State getNAThingProperty(@NonNull String channelId) { switch (channelId) { case CHANNEL_CAMERA_STATUS: return getStatusState(); diff --git a/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/handler/AbstractNetatmoThingHandler.java b/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/handler/AbstractNetatmoThingHandler.java index 10e3e375a6a34..74266f326f9b7 100644 --- a/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/handler/AbstractNetatmoThingHandler.java +++ b/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/handler/AbstractNetatmoThingHandler.java @@ -32,12 +32,7 @@ import org.eclipse.smarthome.config.core.Configuration; import org.eclipse.smarthome.core.library.unit.SIUnits; import org.eclipse.smarthome.core.library.unit.SmartHomeUnits; -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.thing.ThingStatusInfo; +import org.eclipse.smarthome.core.thing.*; import org.eclipse.smarthome.core.thing.binding.BaseThingHandler; import org.eclipse.smarthome.core.thing.binding.BridgeHandler; import org.eclipse.smarthome.core.thing.type.ChannelKind; @@ -120,7 +115,7 @@ private void initializeThing(ThingStatus bridgeStatus) { protected abstract void initializeThing(); - protected State getNAThingProperty(String channelId) { + protected State getNAThingProperty(@NonNull String channelId) { Optional result; result = batteryHelper.flatMap(helper -> helper.getNAThingProperty(channelId)); @@ -137,16 +132,39 @@ protected State getNAThingProperty(String channelId) { } protected void updateChannels() { - getThing().getChannels().stream().filter(channel -> channel.getKind() != ChannelKind.TRIGGER) + updateDataChannels(); + + triggerEventChannels(); + } + + private void updateDataChannels() { + getThing().getChannels().stream().filter(channel -> !channel.getKind().equals(ChannelKind.TRIGGER)) .forEach(channel -> { - String channelId = channel.getUID().getId(); - if (isLinked(channelId)) { - State state = getNAThingProperty(channelId); - if (state != null) { - updateState(channel.getUID(), state); - } - } - }); + + String channelId = channel.getUID().getId(); + if (isLinked(channelId)) { + State state = getNAThingProperty(channelId); + if (state != null) { + updateState(channel.getUID(), state); + } + } + }); + } + + /** + * Triggers all event/trigger channels + * (when a channel is triggered, a rule can get all other information from the updated non-trigger channels) + */ + private void triggerEventChannels() { + getThing().getChannels().stream().filter(channel -> channel.getKind().equals(ChannelKind.TRIGGER)) + .forEach(channel -> triggerChannelIfRequired(channel.getUID().getId())); + } + + /** + * Triggers the trigger channel with the given channel id when required (when an update is available) + * @param channelId channel id + */ + protected void triggerChannelIfRequired(@NonNull String channelId) { } @Override diff --git a/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/handler/NetatmoDeviceHandler.java b/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/handler/NetatmoDeviceHandler.java index ade2f286c83a6..bbc53664708c6 100644 --- a/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/handler/NetatmoDeviceHandler.java +++ b/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/handler/NetatmoDeviceHandler.java @@ -23,6 +23,7 @@ import java.util.concurrent.ScheduledFuture; import java.util.concurrent.TimeUnit; +import org.eclipse.jdt.annotation.NonNull; import org.eclipse.jdt.annotation.Nullable; import org.eclipse.smarthome.core.library.types.DecimalType; import org.eclipse.smarthome.core.library.types.PointType; @@ -89,7 +90,7 @@ public void dispose() { } } - protected abstract DEVICE updateReadings(); + protected abstract @Nullable DEVICE updateReadings(); protected void updateProperties(DEVICE deviceData) { } @@ -148,7 +149,7 @@ protected void updateChannels() { } @Override - protected State getNAThingProperty(String channelId) { + protected State getNAThingProperty(@NonNull String channelId) { try { switch (channelId) { case CHANNEL_LAST_STATUS_STORE: diff --git a/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/handler/NetatmoModuleHandler.java b/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/handler/NetatmoModuleHandler.java index 9656992571eeb..fd0a628c892c4 100644 --- a/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/handler/NetatmoModuleHandler.java +++ b/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/handler/NetatmoModuleHandler.java @@ -20,6 +20,7 @@ import java.util.concurrent.ScheduledFuture; import java.util.concurrent.TimeUnit; +import org.eclipse.jdt.annotation.NonNull; import org.eclipse.jdt.annotation.Nullable; import org.eclipse.smarthome.core.thing.Thing; import org.eclipse.smarthome.core.thing.ThingStatus; @@ -71,7 +72,7 @@ public boolean childOf(AbstractNetatmoThingHandler naThingHandler) { } @Override - protected State getNAThingProperty(String channelId) { + protected State getNAThingProperty(@NonNull String channelId) { try { if (channelId.equalsIgnoreCase(CHANNEL_LAST_MESSAGE) && module != null) { Method getLastMessage = module.getClass().getMethod("getLastMessage"); diff --git a/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/homecoach/NAHealthyHomeCoachHandler.java b/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/homecoach/NAHealthyHomeCoachHandler.java index da0f74303e59a..f76e3d77917e1 100644 --- a/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/homecoach/NAHealthyHomeCoachHandler.java +++ b/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/homecoach/NAHealthyHomeCoachHandler.java @@ -53,7 +53,7 @@ protected void updateProperties(NAHealthyHomeCoach deviceData) { } @Override - protected State getNAThingProperty(String channelId) { + protected State getNAThingProperty(@NonNull String channelId) { if (device != null) { NADashboardData dashboardData = device.getDashboardData(); switch (channelId) { diff --git a/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/station/NAMainHandler.java b/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/station/NAMainHandler.java index e944c3f21320a..15814e3470b1e 100644 --- a/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/station/NAMainHandler.java +++ b/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/station/NAMainHandler.java @@ -21,6 +21,7 @@ import java.util.Optional; import java.util.concurrent.ConcurrentHashMap; +import org.eclipse.jdt.annotation.NonNull; import org.eclipse.jdt.annotation.Nullable; import org.eclipse.smarthome.core.thing.Thing; import org.eclipse.smarthome.core.types.State; @@ -165,7 +166,7 @@ private void updateMonthMeasurements() { } @Override - protected State getNAThingProperty(String channelId) { + protected State getNAThingProperty(@NonNull String channelId) { if (device != null) { NADashboardData dashboardData = device.getDashboardData(); if (dashboardData != null) { diff --git a/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/station/NAModule1Handler.java b/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/station/NAModule1Handler.java index e570ac9f74258..e7243d808f9de 100644 --- a/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/station/NAModule1Handler.java +++ b/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/station/NAModule1Handler.java @@ -20,6 +20,7 @@ import java.util.Map; import java.util.concurrent.ConcurrentHashMap; +import org.eclipse.jdt.annotation.NonNull; import org.eclipse.smarthome.core.thing.Thing; import org.eclipse.smarthome.core.types.State; import org.openhab.binding.netatmo.internal.WeatherUtils; @@ -101,7 +102,7 @@ private void updateMonthMeasurements() { } @Override - protected State getNAThingProperty(String channelId) { + protected State getNAThingProperty(@NonNull String channelId) { if (module != null) { NADashboardData dashboardData = module.getDashboardData(); if (dashboardData != null) { diff --git a/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/station/NAModule2Handler.java b/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/station/NAModule2Handler.java index 76376aec66157..20cbd98f716b6 100644 --- a/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/station/NAModule2Handler.java +++ b/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/station/NAModule2Handler.java @@ -15,6 +15,7 @@ import static org.openhab.binding.netatmo.internal.ChannelTypeUtils.*; import static org.openhab.binding.netatmo.internal.NetatmoBindingConstants.*; +import org.eclipse.jdt.annotation.NonNull; import org.eclipse.smarthome.core.thing.Thing; import org.eclipse.smarthome.core.types.State; import org.openhab.binding.netatmo.internal.handler.NetatmoModuleHandler; @@ -40,7 +41,7 @@ protected void updateProperties(NAStationModule moduleData) { } @Override - protected State getNAThingProperty(String channelId) { + protected State getNAThingProperty(@NonNull String channelId) { if (module != null) { NADashboardData dashboardData = module.getDashboardData(); if (dashboardData != null) { diff --git a/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/station/NAModule3Handler.java b/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/station/NAModule3Handler.java index 27ae505b6baf7..bf89553bfa597 100644 --- a/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/station/NAModule3Handler.java +++ b/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/station/NAModule3Handler.java @@ -64,7 +64,7 @@ public void updateMeasurements() { } @Override - protected State getNAThingProperty(String channelId) { + protected State getNAThingProperty(@NonNull String channelId) { if (module != null) { NADashboardData dashboardData = module.getDashboardData(); if (dashboardData != null) { diff --git a/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/station/NAModule4Handler.java b/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/station/NAModule4Handler.java index d6af322edb830..cea1c49479ae4 100644 --- a/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/station/NAModule4Handler.java +++ b/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/station/NAModule4Handler.java @@ -20,6 +20,7 @@ import java.util.Map; import java.util.concurrent.ConcurrentHashMap; +import org.eclipse.jdt.annotation.NonNull; import org.eclipse.smarthome.core.thing.Thing; import org.eclipse.smarthome.core.types.State; import org.openhab.binding.netatmo.internal.WeatherUtils; @@ -113,7 +114,7 @@ private void updateMonthMeasurements() { } @Override - protected State getNAThingProperty(String channelId) { + protected State getNAThingProperty(@NonNull String channelId) { if (module != null) { NADashboardData dashboardData = module.getDashboardData(); if (dashboardData != null) { diff --git a/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/thermostat/NAPlugHandler.java b/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/thermostat/NAPlugHandler.java index 5f3db29ec4803..c551ce2b93b00 100644 --- a/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/thermostat/NAPlugHandler.java +++ b/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/thermostat/NAPlugHandler.java @@ -62,7 +62,7 @@ protected void updateProperties(NAPlug deviceData) { } @Override - protected State getNAThingProperty(String channelId) { + protected State getNAThingProperty(@NonNull String channelId) { switch (channelId) { case CHANNEL_CONNECTED_BOILER: return device != null ? toOnOffType(device.getPlugConnectedBoiler()) : UnDefType.UNDEF; diff --git a/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/thermostat/NATherm1Handler.java b/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/thermostat/NATherm1Handler.java index 3a95e34af5058..fc59b026d6d18 100644 --- a/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/thermostat/NATherm1Handler.java +++ b/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/thermostat/NATherm1Handler.java @@ -100,7 +100,7 @@ private void updateStateDescription(NAThermostat thermostat) { @SuppressWarnings("null") @Override - protected State getNAThingProperty(String channelId) { + protected State getNAThingProperty(@NonNull String channelId) { switch (channelId) { case CHANNEL_THERM_ORIENTATION: return module != null ? toDecimalType(module.getThermOrientation()) : UnDefType.UNDEF; diff --git a/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/welcome/NAWelcomeCameraHandler.java b/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/welcome/NAWelcomeCameraHandler.java index c0a0c845efbec..385d00094b08b 100644 --- a/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/welcome/NAWelcomeCameraHandler.java +++ b/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/welcome/NAWelcomeCameraHandler.java @@ -33,7 +33,7 @@ public NAWelcomeCameraHandler(@NonNull Thing thing) { @SuppressWarnings("null") @Override - protected State getNAThingProperty(String channelId) { + protected State getNAThingProperty(@NonNull String channelId) { switch (channelId) { case CHANNEL_WELCOME_CAMERA_STATUS: return getStatusState(); diff --git a/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/welcome/NAWelcomeHomeHandler.java b/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/welcome/NAWelcomeHomeHandler.java index 55d76b51b4b71..fb593238aa22e 100644 --- a/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/welcome/NAWelcomeHomeHandler.java +++ b/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/welcome/NAWelcomeHomeHandler.java @@ -15,28 +15,25 @@ import static org.openhab.binding.netatmo.internal.ChannelTypeUtils.*; import static org.openhab.binding.netatmo.internal.NetatmoBindingConstants.*; -import java.util.Calendar; -import java.util.Optional; +import java.util.*; +import java.util.function.Function; -import org.eclipse.jdt.annotation.NonNull; +import io.swagger.client.model.*; +import org.eclipse.jdt.annotation.NonNullByDefault; import org.eclipse.jdt.annotation.Nullable; import org.eclipse.smarthome.core.library.types.DecimalType; -import org.eclipse.smarthome.core.library.types.OnOffType; import org.eclipse.smarthome.core.library.types.StringType; import org.eclipse.smarthome.core.thing.Thing; import org.eclipse.smarthome.core.types.State; import org.eclipse.smarthome.core.types.UnDefType; import org.eclipse.smarthome.io.net.http.HttpUtil; +import org.openhab.binding.netatmo.internal.ChannelTypeUtils; import org.openhab.binding.netatmo.internal.handler.AbstractNetatmoThingHandler; import org.openhab.binding.netatmo.internal.handler.NetatmoDeviceHandler; +import org.openhab.binding.netatmo.internal.webhook.NAWebhookCameraEvent; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import io.swagger.client.model.NAWelcomeEvent; -import io.swagger.client.model.NAWelcomeHome; -import io.swagger.client.model.NAWelcomeHomeData; -import io.swagger.client.model.NAWelcomeSnapshot; - /** * {@link NAWelcomeHomeHandler} is the class used to handle the Welcome Home Data * @@ -44,21 +41,23 @@ * @author Ing. Peter Weiss - Welcome camera implementation * */ +@NonNullByDefault public class NAWelcomeHomeHandler extends NetatmoDeviceHandler { - private Logger logger = LoggerFactory.getLogger(NAWelcomeHomeHandler.class); + private final Logger logger = LoggerFactory.getLogger(NAWelcomeHomeHandler.class); private int iPersons = -1; private int iUnknowns = -1; - private NAWelcomeEvent lastEvent; - private Integer dataTimeStamp; + private Optional lastEvent = Optional.empty(); + private boolean isNewLastEvent; + private @Nullable Integer dataTimeStamp; - public NAWelcomeHomeHandler(@NonNull Thing thing) { + public NAWelcomeHomeHandler(Thing thing) { super(thing); } @Override - protected NAWelcomeHome updateReadings() { - NAWelcomeHome result = null; + protected @Nullable NAWelcomeHome updateReadings() { + @Nullable NAWelcomeHome result = null; NAWelcomeHomeData homeDataBody = getBridgeHandler().getWelcomeDataBody(getId()); if (homeDataBody != null) { // data time stamp is updated to now as WelcomeDataBody does not provide any information according to this @@ -83,11 +82,9 @@ protected NAWelcomeHome updateReadings() { } }); - result.getEvents().forEach(event -> { - if (lastEvent == null || lastEvent.getTime() < event.getTime()) { - lastEvent = event; - } - }); + Optional previousLastEvent = lastEvent; + lastEvent = result.getEvents().stream().min(Comparator.comparingInt(NAWelcomeEvent::getTime)); + isNewLastEvent = previousLastEvent.isPresent() && !previousLastEvent.equals(lastEvent); } } return result; @@ -97,56 +94,46 @@ protected NAWelcomeHome updateReadings() { protected State getNAThingProperty(String channelId) { switch (channelId) { case CHANNEL_WELCOME_HOME_CITY: - return device != null ? toStringType(device.getPlace().getCity()) : UnDefType.UNDEF; + return getPlaceInfo(NAWelcomePlace::getCity); case CHANNEL_WELCOME_HOME_COUNTRY: - return device != null ? toStringType(device.getPlace().getCountry()) : UnDefType.UNDEF; + return getPlaceInfo(NAWelcomePlace::getCountry); case CHANNEL_WELCOME_HOME_TIMEZONE: - return device != null ? toStringType(device.getPlace().getTimezone()) : UnDefType.UNDEF; + return getPlaceInfo(NAWelcomePlace::getTimezone); case CHANNEL_WELCOME_HOME_PERSONCOUNT: return iPersons != -1 ? new DecimalType(iPersons) : UnDefType.UNDEF; case CHANNEL_WELCOME_HOME_UNKNOWNCOUNT: return iUnknowns != -1 ? new DecimalType(iUnknowns) : UnDefType.UNDEF; case CHANNEL_WELCOME_EVENT_TYPE: - return lastEvent != null ? toStringType(lastEvent.getType()) : UnDefType.UNDEF; + return lastEvent.map(e -> toStringType(e.getType())).orElse(UnDefType.UNDEF); case CHANNEL_WELCOME_EVENT_TIME: - return lastEvent != null ? toDateTimeType(lastEvent.getTime()) : UnDefType.UNDEF; + return lastEvent.map(e -> toDateTimeType(e.getTime())).orElse(UnDefType.UNDEF); case CHANNEL_WELCOME_EVENT_CAMERAID: - if (lastEvent != null) { + if (lastEvent.isPresent()) { Optional camera = getBridgeHandler() - .findNAThing(lastEvent.getCameraId()); + .findNAThing(lastEvent.get().getCameraId()); return camera.map(c -> toStringType(c.getThing().getLabel())).orElse(UnDefType.UNDEF); } else { return UnDefType.UNDEF; } case CHANNEL_WELCOME_EVENT_PERSONID: - if (lastEvent != null) { + if (lastEvent.isPresent()) { Optional person = getBridgeHandler() - .findNAThing(lastEvent.getPersonId()); + .findNAThing(lastEvent.get().getPersonId()); return person.map(p -> toStringType(p.getThing().getLabel())).orElse(UnDefType.UNDEF); } else { return UnDefType.UNDEF; } case CHANNEL_WELCOME_EVENT_SNAPSHOT: - if (lastEvent != null) { - String url = getSnapshotURL(lastEvent.getSnapshot()); - return url != null ? HttpUtil.downloadImage(url) : UnDefType.UNDEF; - } else { - return UnDefType.UNDEF; - } + return findSnapshotURL().map(url -> (State)HttpUtil.downloadImage(url)).orElse(UnDefType.UNDEF); case CHANNEL_WELCOME_EVENT_SNAPSHOT_URL: - if (lastEvent != null) { - String snapshotURL = getSnapshotURL(lastEvent.getSnapshot()); - return toStringType(snapshotURL); - } else { - return UnDefType.UNDEF; - } + return findSnapshotURL().map(ChannelTypeUtils::toStringType).orElse(UnDefType.UNDEF); case CHANNEL_WELCOME_EVENT_VIDEO_URL: - if (lastEvent != null && lastEvent.getVideoId() != null) { - String cameraId = lastEvent.getCameraId(); + if (lastEvent.isPresent() && lastEvent.get().getVideoId() != null) { + String cameraId = lastEvent.get().getCameraId(); Optional thing = getBridgeHandler().findNAThing(cameraId); if (thing.isPresent()) { NAWelcomeCameraHandler eventCamera = (NAWelcomeCameraHandler) thing.get(); - String streamUrl = eventCamera.getStreamURL(lastEvent.getVideoId()); + String streamUrl = eventCamera.getStreamURL(lastEvent.get().getVideoId()); if (streamUrl != null) { return new StringType(streamUrl); } @@ -154,39 +141,98 @@ protected State getNAThingProperty(String channelId) { } return UnDefType.UNDEF; case CHANNEL_WELCOME_EVENT_VIDEOSTATUS: - return lastEvent != null ? toStringType(lastEvent.getVideoStatus()) : UnDefType.UNDEF; + return lastEvent.map(e -> toStringType(e.getVideoStatus())).orElse(UnDefType.UNDEF); case CHANNEL_WELCOME_EVENT_ISARRIVAL: - return lastEvent != null ? lastEvent.getIsArrival() != null ? OnOffType.ON : OnOffType.OFF - : UnDefType.UNDEF; + return lastEvent.map(e -> toOnOffType(e.getIsArrival())).orElse(UnDefType.UNDEF); case CHANNEL_WELCOME_EVENT_MESSAGE: - return lastEvent != null && lastEvent.getMessage() != null - ? new StringType(lastEvent.getMessage().replace("", "").replace("", "")) - : UnDefType.UNDEF; + return findEventMessage().map( + m -> (State)new StringType(m.replace("", "").replace("", "")) + ).orElse(UnDefType.UNDEF); case CHANNEL_WELCOME_EVENT_SUBTYPE: - return lastEvent != null ? toDecimalType(lastEvent.getSubType()) : UnDefType.UNDEF; + return lastEvent.map(e -> toDecimalType(e.getSubType())).orElse(UnDefType.UNDEF); } return super.getNAThingProperty(channelId); } + @Override + protected void triggerChannelIfRequired(String channelId) { + if (isNewLastEvent) { + if (CHANNEL_CAMERA_EVENT.equals(channelId)) { + findDetectedObjectTypes(lastEvent).forEach(detectedType -> triggerChannel(channelId, detectedType)); + } + } + super.triggerChannelIfRequired(channelId); + } + + private static Set findDetectedObjectTypes(Optional eventOptional) { + Set detectedObjectTypes = new TreeSet<>(); + if (!eventOptional.isPresent()) { + return detectedObjectTypes; + } + + NAWelcomeEvent event = eventOptional.get(); + + if (event.getPersonId() != null) { + detectedObjectTypes.add(NAWelcomeSubEvent.TypeEnum.HUMAN.name()); + } + + if (NAWebhookCameraEvent.EventTypeEnum.MOVEMENT.toString().equals(event.getType())) { + detectedObjectTypes.add(NAWebhookCameraEvent.EventTypeEnum.MOVEMENT.name()); + } + + event.getEventList().forEach(subEvent -> { + String detectedObjectType = subEvent.getType().name(); + detectedObjectTypes.add(detectedObjectType); + }); + return detectedObjectTypes; + } + + private Optional findEventMessage() { + if (lastEvent.isPresent()) { + @Nullable String message = lastEvent.get().getMessage(); + if (message != null) { + return Optional.of(message); + } + + return findFirstSubEvent(lastEvent).map(NAWelcomeSubEvent::getMessage); + } + return Optional.empty(); + } + /** * Returns the Url of the picture * * @return Url of the picture or null */ - protected String getSnapshotURL(NAWelcomeSnapshot snapshot) { - String result = null; - - if (snapshot != null && snapshot.getId() != null && snapshot.getKey() != null) { - result = WELCOME_PICTURE_URL + "?" + WELCOME_PICTURE_IMAGEID + "=" + snapshot.getId() + "&" - + WELCOME_PICTURE_KEY + "=" + snapshot.getKey(); - } else { - logger.debug("Unable to build snapshot url for Home : {}", getId()); + protected Optional findSnapshotURL() { + if (lastEvent.isPresent()) { + @Nullable NAWelcomeSnapshot snapshot = lastEvent.get().getSnapshot(); + if (snapshot == null) { + snapshot = findFirstSubEvent(lastEvent).map(NAWelcomeSubEvent::getSnapshot).orElse(null); + } + + if (snapshot != null && snapshot.getId() != null && snapshot.getKey() != null) { + return Optional.of(WELCOME_PICTURE_URL + "?" + WELCOME_PICTURE_IMAGEID + "=" + snapshot.getId() + "&" + + WELCOME_PICTURE_KEY + "=" + snapshot.getKey()); + } else { + logger.debug("Unable to build snapshot url for Home : {}", getId()); + } } - return result; + return Optional.empty(); } @Override protected @Nullable Integer getDataTimestamp() { return dataTimeStamp; } + + private State getPlaceInfo(Function infoGetFunction) { + return Optional.ofNullable(device).map( + d -> toStringType(infoGetFunction.apply(d.getPlace())) + ).orElse(UnDefType.UNDEF); + } + + private static Optional findFirstSubEvent(Optional event) { + return event.map(NAWelcomeEvent::getEventList).flatMap(subEvents -> subEvents.stream().findFirst()); + } } diff --git a/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/welcome/NAWelcomePersonHandler.java b/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/welcome/NAWelcomePersonHandler.java index 45c490c4103c6..580024c51aed8 100644 --- a/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/welcome/NAWelcomePersonHandler.java +++ b/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/welcome/NAWelcomePersonHandler.java @@ -68,7 +68,7 @@ public void updateChannels(Object module) { } @Override - protected State getNAThingProperty(String channelId) { + protected State getNAThingProperty(@NonNull String channelId) { switch (channelId) { case CHANNEL_WELCOME_PERSON_LASTSEEN: return module != null ? toDateTimeType(module.getLastSeen()) : UnDefType.UNDEF; diff --git a/bundles/org.openhab.binding.netatmo/src/main/resources/ESH-INF/thing/welcomehome.xml b/bundles/org.openhab.binding.netatmo/src/main/resources/ESH-INF/thing/welcomehome.xml index f49c25620cc25..0cda714c6d15e 100644 --- a/bundles/org.openhab.binding.netatmo/src/main/resources/ESH-INF/thing/welcomehome.xml +++ b/bundles/org.openhab.binding.netatmo/src/main/resources/ESH-INF/thing/welcomehome.xml @@ -32,6 +32,8 @@ + + id @@ -138,4 +140,17 @@ + + trigger + + + + + + + + + + +