[netatmo] Removing feature restriction from binding configuration (#12795)

* Removing feature restriction from binding configuration

Signed-off-by: clinique <gael@lhopital.org>
This commit is contained in:
Gaël L'hopital 2022-05-24 12:59:59 +02:00 committed by GitHub
parent 6283c9a0e2
commit 907e33518e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 11 additions and 47 deletions

View File

@ -30,7 +30,6 @@ The binding has the following configuration options:
| Parameter | Type | Description |
|--------------|---------------|--------------------------------------------------------------------------------------------|
| features | String | The perimeter of functionalities given to the binding WEATHER, AIR_CARE, ENERGY, SECURITY |
| readFriends | Boolean | Enables or disables the discovery of guest weather stations. |

View File

@ -43,6 +43,7 @@ import org.slf4j.LoggerFactory;
*/
@NonNullByDefault
public class AuthenticationApi extends RestManager {
private static final String ALL_SCOPES = FeatureArea.toScopeString(FeatureArea.AS_SET);
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();
@ -58,12 +59,10 @@ public class AuthenticationApi extends RestManager {
this.scheduler = scheduler;
}
public String authorize(ApiHandlerConfiguration credentials, Set<FeatureArea> features, @Nullable String code,
@Nullable String redirectUri) throws NetatmoException {
String clientId = credentials.clientId;
String clientSecret = credentials.clientSecret;
if (!(clientId.isBlank() || clientSecret.isBlank())) {
Map<String, String> params = new HashMap<>(Map.of(SCOPE, toScopeString(features)));
public String authorize(ApiHandlerConfiguration credentials, @Nullable String code, @Nullable String redirectUri)
throws NetatmoException {
if (!(credentials.clientId.isBlank() || credentials.clientSecret.isBlank())) {
Map<String, String> params = new HashMap<>(Map.of(SCOPE, ALL_SCOPES));
String refreshToken = credentials.refreshToken;
if (!refreshToken.isBlank()) {
params.put(REFRESH_TOKEN, refreshToken);
@ -73,7 +72,7 @@ public class AuthenticationApi extends RestManager {
}
}
if (params.size() > 1) {
return requestToken(clientId, clientSecret, params);
return requestToken(credentials.clientId, credentials.clientSecret, params);
}
}
throw new IllegalArgumentException("Inconsistent configuration state, please file a bug report.");
@ -118,12 +117,8 @@ public class AuthenticationApi extends RestManager {
return tokenResponse.isPresent();
}
private static String toScopeString(Set<FeatureArea> features) {
return FeatureArea.toScopeString(features.isEmpty() ? FeatureArea.AS_SET : features);
}
public static UriBuilder getAuthorizationBuilder(String clientId, Set<FeatureArea> features) {
return AUTH_BUILDER.clone().queryParam(CLIENT_ID, clientId).queryParam(SCOPE, toScopeString(features))
.queryParam(STATE, clientId);
public static UriBuilder getAuthorizationBuilder(String clientId) {
return AUTH_BUILDER.clone().queryParam(CLIENT_ID, clientId).queryParam(SCOPE, ALL_SCOPES).queryParam(STATE,
clientId);
}
}

View File

@ -12,10 +12,7 @@
*/
package org.openhab.binding.netatmo.internal.config;
import java.util.Set;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.openhab.binding.netatmo.internal.api.data.NetatmoConstants.FeatureArea;
/**
* The {@link BindingConfiguration} is responsible for holding configuration of the binding itself.
@ -24,11 +21,9 @@ import org.openhab.binding.netatmo.internal.api.data.NetatmoConstants.FeatureAre
*/
@NonNullByDefault
public class BindingConfiguration {
public Set<FeatureArea> features = Set.of();
public boolean readFriends = false;
public void update(BindingConfiguration newConfig) {
this.features = newConfig.features;
this.readFriends = newConfig.readFriends;
}
}

View File

@ -125,7 +125,7 @@ public class ApiBridgeHandler extends BaseBridgeHandler {
try {
logger.debug("Connecting to Netatmo API.");
String refreshToken = connectApi.authorize(configuration, bindingConf.features, code, redirectUri);
String refreshToken = connectApi.authorize(configuration, code, redirectUri);
if (configuration.refreshToken.isBlank()) {
Configuration thingConfig = editConfiguration();
@ -273,7 +273,7 @@ public class ApiBridgeHandler extends BaseBridgeHandler {
}
public UriBuilder formatAuthorizationUrl() {
return AuthenticationApi.getAuthorizationBuilder(getId(), bindingConf.features);
return AuthenticationApi.getAuthorizationBuilder(getId());
}
@Override

View File

@ -9,19 +9,6 @@
<config-description>
<parameter name="features" type="text" required="true" multiple="true">
<label>Feature Area</label>
<description>Defines the set of capabilities you want to operate on.</description>
<options>
<option value="AIR_CARE">Air Care</option>
<option value="WEATHER">Weather</option>
<option value="ENERGY">Energy</option>
<option value="SECURITY">Security</option>
</options>
<limitToOptions>true</limitToOptions>
<default>WEATHER</default>
</parameter>
<parameter name="readFriends" type="boolean">
<label>Access Guests</label>
<description>For Weather Stations: A friend gave you access to their Netatmo Weather Station.</description>

View File

@ -5,12 +5,6 @@ binding.netatmo.description = The Netatmo binding integrates Weather Station wit
# binding config
binding.config.netatmo.features.label = Feature Area
binding.config.netatmo.features.description = Defines the set of capabilities you want to operate on.
binding.config.netatmo.features.option.AIR_CARE = Air Care
binding.config.netatmo.features.option.WEATHER = Weather
binding.config.netatmo.features.option.ENERGY = Energy
binding.config.netatmo.features.option.SECURITY = Security
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.

View File

@ -5,12 +5,6 @@ binding.netatmo.description = Il binding Netatmo integra la Stazione Meteo con a
# binding config
binding.config.netatmo.features.label = Area Feature
binding.config.netatmo.features.description = Definisce il set di funzionalità su cui si desidera operare.
binding.config.netatmo.features.option.AIR_CARE = Cura Aria
binding.config.netatmo.features.option.WEATHER = Meteo
binding.config.netatmo.features.option.ENERGY = Energia
binding.config.netatmo.features.option.SECURITY = Sicurezza
binding.config.netatmo.readFriends.label = Accesso Ospiti
binding.config.netatmo.readFriends.description = Per le Stazioni Meteorologiche\: Un amico ti ha dato accesso alla sua Stazione Meteo Netatmo.