Skip to content
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.boschindego/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ To authorize, please follow these steps:
- With developer tools showing on the right, go to [Bosch Indego login page](https://prodindego.b2clogin.com/prodindego.onmicrosoft.com/b2c_1a_signup_signin/oauth2/v2.0/authorize?redirect_uri=com.bosch.indegoconnect://login&client_id=65bb8c9d-1070-4fb4-aa95-853618acc876&response_type=code&scope=openid%20offline_access%20https://prodindego.onmicrosoft.com/indego-mobile-api/Indego.Mower.User) again.
- "Please wait..." should now be displayed.
- Find the `authresp` and copy the code: `com.bosch.indegoconnect://login/?code=<copy this>`
- Use the openHAB console to authorize with this code: `openhab:boschindego authorize <paste code>`
- Use the openHAB console to authorize with this code: `openhab:boschindego authorize <bridgeId> <paste code>`

### `indego` Thing Configuration

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
*/
package org.openhab.binding.boschindego.internal.console;

import java.util.Arrays;
import static org.openhab.binding.boschindego.internal.BoschIndegoBindingConstants.THING_TYPE_ACCOUNT;

import java.util.List;

import org.eclipse.jdt.annotation.NonNullByDefault;
Expand All @@ -27,7 +28,7 @@
import org.openhab.core.io.console.extensions.ConsoleCommandExtension;
import org.openhab.core.thing.Thing;
import org.openhab.core.thing.ThingRegistry;
import org.openhab.core.thing.binding.ThingHandler;
import org.openhab.core.thing.ThingUID;
import org.osgi.service.component.annotations.Activate;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;
Expand All @@ -42,7 +43,7 @@
public class BoschIndegoCommandExtension extends AbstractConsoleCommandExtension implements ConsoleCommandCompleter {

private static final String AUTHORIZE = "authorize";
private static final StringsCompleter SUBCMD_COMPLETER = new StringsCompleter(List.of(AUTHORIZE), false);
private static final StringsCompleter CMD_COMPLETER = new StringsCompleter(List.of(AUTHORIZE), false);

private final ThingRegistry thingRegistry;

Expand All @@ -54,26 +55,37 @@ public BoschIndegoCommandExtension(final @Reference ThingRegistry thingRegistry)

@Override
public void execute(String[] args, Console console) {
if (args.length != 2 || !AUTHORIZE.equals(args[0])) {

if (args.length != 3 || !AUTHORIZE.equals(args[0])) {
printUsage(console);
return;
}

for (Thing thing : thingRegistry.getAll()) {
ThingHandler thingHandler = thing.getHandler();
if (thingHandler instanceof BoschAccountHandler accountHandler) {
try {
accountHandler.authorize(args[1]);
} catch (IndegoAuthenticationException e) {
console.println("Authorization error: " + e.getMessage());
}
String bridgeId = args[1];
String authCode = args[2];

Thing bridge = getBridgeById(bridgeId);
if (bridge == null) {
console.println("Unknown bridge id '" + bridgeId + "'");
return;
}

if (bridge.getHandler() instanceof BoschAccountHandler accountHandler) {
try {
accountHandler.authorize(authCode);
} catch (IndegoAuthenticationException e) {
console.println("Authorization error: " + e.getMessage());
}
} else {
console.println("Bridge is not a valid BoschIndego bridge");
printUsage(console);
}
}

@Override
public List<String> getUsages() {
return Arrays.asList(buildCommandUsage(AUTHORIZE, "authorize by authorization code"));
return List.of(
buildCommandUsage(AUTHORIZE + " <bridgeId> <authorizationCode>", "authorize by authorization code"));
}

@Override
Expand All @@ -84,8 +96,24 @@ public List<String> getUsages() {
@Override
public boolean complete(String[] args, int cursorArgumentIndex, int cursorPosition, List<String> candidates) {
if (cursorArgumentIndex <= 0) {
return SUBCMD_COMPLETER.complete(args, cursorArgumentIndex, cursorPosition, candidates);
return CMD_COMPLETER.complete(args, cursorArgumentIndex, cursorPosition, candidates);
} else if (cursorArgumentIndex == 1 && AUTHORIZE.equals(args[0])) {
return new StringsCompleter(getBridgeIds(), true).complete(args, cursorArgumentIndex, cursorPosition,
candidates);
}
return false;
}

private List<String> getBridgeIds() {
return thingRegistry.getAll().stream().filter(thing -> thing.getHandler() instanceof BoschAccountHandler)
.map(thing -> thing.getUID().getId()).toList();
}

private @Nullable Thing getBridgeById(String bridgeId) {
try {
return thingRegistry.get(new ThingUID(THING_TYPE_ACCOUNT, bridgeId));
} catch (IllegalArgumentException e) {
return null;
}
}
}