Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Netatmo] Fix Floodlight issue on Presence Camera #14492

Merged
merged 2 commits into from
Feb 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion bundles/org.openhab.binding.netatmo/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -537,7 +537,7 @@ Warnings:
| live | vpn-stream-url (*) | String | Read-only | Url of the live stream for this camera through Netatmo VPN. |
| signal | strength | Number | Read-only | Signal strength (0 for no signal, 1 for weak...) |
| signal | value | Number:Power | Read-only | Signal strength in dBm |
| presence | floodlight | Switch | Read-write | Sets the floodlight to ON/OFF/AUTO |
| presence | floodlight | String | Read-write | Sets the floodlight to ON/OFF/AUTO |
| last-event | type | String | Read-only | Type of event |
| last-event | subtype | String | Read-only | Sub-type of event |
| last-event | time | DateTime | Read-only | Time of occurrence of event |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,8 @@
*/
@NonNullByDefault
public class AuthenticationApi extends RestManager {
private static final UriBuilder OAUTH_BUILDER = getApiBaseBuilder().path(PATH_OAUTH);
private static final UriBuilder AUTH_BUILDER = OAUTH_BUILDER.clone().path(SUB_PATH_AUTHORIZE);
private static final URI TOKEN_URI = OAUTH_BUILDER.clone().path(SUB_PATH_TOKEN).build();
private static final UriBuilder AUTH_BUILDER = getApiBaseBuilder(PATH_OAUTH, SUB_PATH_AUTHORIZE);
private static final URI TOKEN_URI = getApiBaseBuilder(PATH_OAUTH, SUB_PATH_TOKEN).build();

private final Logger logger = LoggerFactory.getLogger(AuthenticationApi.class);
private final ScheduledExecutorService scheduler;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public EnergyApi(ApiBridgeHandler apiClient) {
* response body
*/
public void switchSchedule(String homeId, String scheduleId) throws NetatmoException {
UriBuilder uriBuilder = getAppUriBuilder(SUB_PATH_SWITCH_SCHEDULE, PARAM_HOME_ID, homeId, PARAM_SCHEDULE_ID,
UriBuilder uriBuilder = getApiUriBuilder(SUB_PATH_SWITCH_SCHEDULE, PARAM_HOME_ID, homeId, PARAM_SCHEDULE_ID,
scheduleId);
post(uriBuilder, ApiResponse.Ok.class, null);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import javax.ws.rs.core.UriBuilder;

Expand All @@ -36,9 +37,7 @@
*/
@NonNullByDefault
public abstract class RestManager {
private static final UriBuilder API_BASE_BUILDER = UriBuilder.fromUri(URL_API);
private static final UriBuilder APP_URI_BUILDER = UriBuilder.fromUri(URL_APP).path(PATH_API);
private static final UriBuilder API_URI_BUILDER = getApiBaseBuilder().path(PATH_API);
private static final UriBuilder API_URI_BUILDER = getApiBaseBuilder(PATH_API);

private final Set<Scope> requiredScopes;
private final ApiBridgeHandler apiBridge;
Expand Down Expand Up @@ -76,11 +75,11 @@ private static UriBuilder appendParams(UriBuilder builder, @Nullable Object... p
throw new IllegalArgumentException("appendParams : params count must be even");
}
for (int i = 0; i < params.length; i += 2) {
Object query = params[i];
if (query instanceof String) {
Object param = params[i + 1];
if (param != null) {
builder.queryParam((String) query, param);
Object param1 = params[i];
Object param2 = params[i + 1];
if (param1 instanceof String query) {
if (param2 != null) { // or else just ignore this query element
builder.queryParam(query, param2);
}
} else {
throw new IllegalArgumentException("appendParams : even parameters must be Strings");
Expand All @@ -89,18 +88,16 @@ private static UriBuilder appendParams(UriBuilder builder, @Nullable Object... p
return builder;
}

protected static UriBuilder getApiBaseBuilder() {
return API_BASE_BUILDER.clone();
protected static UriBuilder getApiBaseBuilder(String... paths) {
UriBuilder builder = UriBuilder.fromUri(URL_API);
Stream.of(paths).forEach(path -> builder.path(path));
return builder;
}

public static UriBuilder getApiUriBuilder(String path, @Nullable Object... params) {
return appendParams(API_URI_BUILDER.clone().path(path), params);
}

protected static UriBuilder getAppUriBuilder(String path, @Nullable Object... params) {
return appendParams(APP_URI_BUILDER.clone().path(path), params);
}

private String toRequest(Map<String, String> entries) {
return entries.entrySet().stream().map(e -> e.getKey() + "=" + e.getValue()).collect(Collectors.joining("&"));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,13 +122,13 @@ public void changeStatus(String localCameraURL, boolean setOn) throws NetatmoExc
}

public void changeFloodLightMode(String homeId, String cameraId, FloodLightMode mode) throws NetatmoException {
UriBuilder uriBuilder = getAppUriBuilder(PATH_STATE);
String payload = String.format(PAYLOAD_FLOODLIGHT, homeId, cameraId, mode.name().toLowerCase());
UriBuilder uriBuilder = getApiUriBuilder(PATH_STATE);
String payload = PAYLOAD_FLOODLIGHT.formatted(homeId, cameraId, mode.name().toLowerCase());
post(uriBuilder, ApiResponse.Ok.class, payload);
}

public void setPersonAwayStatus(String homeId, String personId, boolean away) throws NetatmoException {
UriBuilder uriBuilder = getAppUriBuilder(away ? SUB_PATH_PERSON_AWAY : SUB_PATH_PERSON_HOME);
UriBuilder uriBuilder = getApiUriBuilder(away ? SUB_PATH_PERSON_AWAY : SUB_PATH_PERSON_HOME);
String payload = String.format(away ? PAYLOAD_PERSON_AWAY : PAYLOAD_PERSON_HOME, homeId, personId);
post(uriBuilder, ApiResponse.Ok.class, payload);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,6 @@ public enum MeasureClass {

// Netatmo API urls
public static final String URL_API = "https://api.netatmo.com/";
public static final String URL_APP = "https://app.netatmo.net/";
public static final String PATH_OAUTH = "oauth2";
public static final String SUB_PATH_TOKEN = "token";
public static final String SUB_PATH_AUTHORIZE = "authorize";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@
<item-type>String</item-type>
<label>Floodlight</label>
<description>State of the floodlight (On/Off/Auto)</description>
<category>Lightbulb</category>
<state pattern="%s">
<options>
<option value="ON">On</option>
Expand Down