diff --git a/bundles/org.openhab.binding.netatmo/README.md b/bundles/org.openhab.binding.netatmo/README.md index f7356d2582fa0..adcb2bc596a5c 100644 --- a/bundles/org.openhab.binding.netatmo/README.md +++ b/bundles/org.openhab.binding.netatmo/README.md @@ -52,6 +52,13 @@ The Account bridge has the following configuration elements: (*) Strictly said this parameter is not mandatory at first run, until you grant your binding on Netatmo Connect. Once present, you'll not have to grant again. +**Supported channels for the Account bridge thing:** + +| Channel Group | Channel Id | Item Type | Description | +|---------------|---------------|-----------|-------------------------------------------------------------------| +| monitoring | request-count | Number | Number of request transmitted to Netatmo API during the last hour | + + ### Configure the Bridge 1. Complete the Netatmo Application Registration if you have not already done so, see above. 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 a1d7258fa5453..4165bb6bae235 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 @@ -58,6 +58,7 @@ public class NetatmoBindingConstants { public static final String GROUP_PROPERTIES = "properties"; public static final String GROUP_SETPOINT = "setpoint"; public static final String GROUP_LOCATION = "location"; + public static final String GROUP_MONITORING = "monitoring"; // Alternative extended groups public static final String OPTION_EXTENDED = "-extended"; @@ -153,4 +154,5 @@ public class NetatmoBindingConstants { public static final String CHANNEL_HOME_EVENT = "home-event"; public static final String CHANNEL_SETPOINT_DURATION = "setpoint-duration"; public static final String CHANNEL_FLOODLIGHT = "floodlight"; + public static final String CHANNEL_REQUEST_COUNT = "request-count"; } diff --git a/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/api/data/ModuleType.java b/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/api/data/ModuleType.java index f999e6a1e1684..fc313af17f519 100644 --- a/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/api/data/ModuleType.java +++ b/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/api/data/ModuleType.java @@ -39,6 +39,7 @@ import org.openhab.binding.netatmo.internal.handler.capability.SmokeCapability; import org.openhab.binding.netatmo.internal.handler.capability.WeatherCapability; import org.openhab.binding.netatmo.internal.handler.channelhelper.AirQualityChannelHelper; +import org.openhab.binding.netatmo.internal.handler.channelhelper.ApiBridgeChannelHelper; import org.openhab.binding.netatmo.internal.handler.channelhelper.CameraChannelHelper; import org.openhab.binding.netatmo.internal.handler.channelhelper.EnergyChannelHelper; import org.openhab.binding.netatmo.internal.handler.channelhelper.EventChannelHelper; @@ -64,7 +65,7 @@ @NonNullByDefault public enum ModuleType { UNKNOWN(FeatureArea.NONE, "", null, Set.of()), - ACCOUNT(FeatureArea.NONE, "", null, Set.of()), + ACCOUNT(FeatureArea.NONE, "", null, Set.of(), new ChannelGroup(ApiBridgeChannelHelper.class, GROUP_MONITORING)), HOME(FeatureArea.NONE, "NAHome", ACCOUNT, Set.of(DeviceCapability.class, HomeCapability.class, ChannelHelperCapability.class), diff --git a/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/handler/ApiBridgeHandler.java b/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/handler/ApiBridgeHandler.java index 702c7820cfcf9..bdbccab760c3e 100644 --- a/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/handler/ApiBridgeHandler.java +++ b/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/handler/ApiBridgeHandler.java @@ -12,12 +12,17 @@ */ package org.openhab.binding.netatmo.internal.handler; +import static org.openhab.binding.netatmo.internal.NetatmoBindingConstants.*; + import java.io.ByteArrayInputStream; import java.io.InputStream; import java.lang.reflect.Constructor; import java.net.URI; import java.nio.charset.StandardCharsets; +import java.time.LocalDateTime; +import java.util.ArrayDeque; import java.util.Collection; +import java.util.Deque; import java.util.HashMap; import java.util.Map; import java.util.Objects; @@ -55,6 +60,7 @@ import org.openhab.binding.netatmo.internal.servlet.GrantServlet; import org.openhab.binding.netatmo.internal.servlet.WebhookServlet; import org.openhab.core.config.core.Configuration; +import org.openhab.core.library.types.DecimalType; import org.openhab.core.thing.Bridge; import org.openhab.core.thing.ChannelUID; import org.openhab.core.thing.Thing; @@ -88,6 +94,8 @@ public class ApiBridgeHandler extends BaseBridgeHandler { private Map, RestManager> managers = new HashMap<>(); private @Nullable WebhookServlet webHookServlet; private @Nullable GrantServlet grantServlet; + private Deque requestsTimestamps; + private final ChannelUID requestCountChannelUID; public ApiBridgeHandler(Bridge bridge, HttpClient httpClient, NADeserializer deserializer, BindingConfiguration configuration, HttpService httpService) { @@ -97,6 +105,8 @@ public ApiBridgeHandler(Bridge bridge, HttpClient httpClient, NADeserializer des this.httpClient = httpClient; this.deserializer = deserializer; this.httpService = httpService; + this.requestsTimestamps = new ArrayDeque<>(200); + this.requestCountChannelUID = new ChannelUID(getThing().getUID(), GROUP_MONITORING, CHANNEL_REQUEST_COUNT); } @Override @@ -105,7 +115,7 @@ public void initialize() { updateStatus(ThingStatus.UNKNOWN); GrantServlet servlet = new GrantServlet(this, httpService); servlet.startListening(); - this.grantServlet = servlet; + grantServlet = servlet; scheduler.execute(() -> openConnection(null, null)); } @@ -235,6 +245,15 @@ public synchronized T executeUri(URI uri, HttpMethod method, Class clazz, } } + if (isLinked(requestCountChannelUID)) { + LocalDateTime now = LocalDateTime.now(); + LocalDateTime oneHourAgo = now.minusHours(1); + requestsTimestamps.addLast(now); + while (requestsTimestamps.getFirst().isBefore(oneHourAgo)) { + requestsTimestamps.removeFirst(); + } + updateState(requestCountChannelUID, new DecimalType(requestsTimestamps.size())); + } ContentResponse response = request.send(); Code statusCode = HttpStatus.getCode(response.getStatus()); diff --git a/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/handler/channelhelper/ApiBridgeChannelHelper.java b/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/handler/channelhelper/ApiBridgeChannelHelper.java new file mode 100644 index 0000000000000..a24d3afddd73f --- /dev/null +++ b/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/handler/channelhelper/ApiBridgeChannelHelper.java @@ -0,0 +1,31 @@ +/** + * Copyright (c) 2010-2022 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.netatmo.internal.handler.channelhelper; + +import java.util.Set; + +import org.eclipse.jdt.annotation.NonNullByDefault; + +/** + * The {@link ApiBridgeChannelHelper} handle specifics channels the Netatmo Bridge + * + * @author Gaƫl L'hopital - Initial contribution + * + */ +@NonNullByDefault +public class ApiBridgeChannelHelper extends ChannelHelper { + + public ApiBridgeChannelHelper(Set providedGroups) { + super(providedGroups); + } +} diff --git a/bundles/org.openhab.binding.netatmo/src/main/resources/OH-INF/i18n/netatmo.properties b/bundles/org.openhab.binding.netatmo/src/main/resources/OH-INF/i18n/netatmo.properties index 51f17f9b90e42..a716315f36b21 100644 --- a/bundles/org.openhab.binding.netatmo/src/main/resources/OH-INF/i18n/netatmo.properties +++ b/bundles/org.openhab.binding.netatmo/src/main/resources/OH-INF/i18n/netatmo.properties @@ -9,7 +9,7 @@ binding.config.netatmo.readFriends.label = Access Guests binding.config.netatmo.readFriends.description = For Weather Stations: A friend gave you access to their Netatmo Weather Station. # channel group types - +channel-group-type.netatmo.monitoring.label = API Monitoring channel-group-type.netatmo.airquality-extended.label = Air Quality channel-group-type.netatmo.airquality.label = Air Quality channel-group-type.netatmo.battery-extended.label = Battery @@ -117,7 +117,8 @@ channel-group-type.netatmo.wind.channel.max-strength-date.label = Date Max Wind channel-group-type.netatmo.wind.channel.max-strength-date.description = Moment when max wind strength was recorded. # channel types - +channel-type.netatmo.request-count.label = Request Count +channel-type.netatmo.request-count.description = Number of request transmitted to Netatmo API during the last hour. channel-type.netatmo.absolute-pressure.label = Absolute Pressure channel-type.netatmo.absolute-pressure.description = Pressure measured relative to a full vacuum. channel-type.netatmo.alim-status.label = Alim State diff --git a/bundles/org.openhab.binding.netatmo/src/main/resources/OH-INF/thing/channels.xml b/bundles/org.openhab.binding.netatmo/src/main/resources/OH-INF/thing/channels.xml index 5b60f29de173f..d91a77d0e3656 100644 --- a/bundles/org.openhab.binding.netatmo/src/main/resources/OH-INF/thing/channels.xml +++ b/bundles/org.openhab.binding.netatmo/src/main/resources/OH-INF/thing/channels.xml @@ -85,6 +85,13 @@ + + Number + + Number of request transmitted to Netatmo API during the last hour. + + + Number diff --git a/bundles/org.openhab.binding.netatmo/src/main/resources/OH-INF/thing/common.xml b/bundles/org.openhab.binding.netatmo/src/main/resources/OH-INF/thing/common.xml index 1fd7424fa7046..5f45b72661c0b 100644 --- a/bundles/org.openhab.binding.netatmo/src/main/resources/OH-INF/thing/common.xml +++ b/bundles/org.openhab.binding.netatmo/src/main/resources/OH-INF/thing/common.xml @@ -4,6 +4,13 @@ xmlns:thing="https://openhab.org/schemas/thing-description/v1.0.0" xsi:schemaLocation="https://openhab.org/schemas/thing-description/v1.0.0 https://openhab.org/schemas/thing-description-1.0.0.xsd"> + + + + + + +