Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 (`<bridgeId>` is optional if only one bridge is defined): `openhab:boschindego [<bridgeId>] authorize <paste code>`

### `indego` Thing Configuration

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

import java.util.Arrays;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Stream;

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
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 @@ -54,26 +55,53 @@ public BoschIndegoCommandExtension(final @Reference ThingRegistry thingRegistry)

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

if (args.length == 2 && AUTHORIZE.equals(args[0])) {
try {
bridge = getSingleBridge();
} catch (IllegalStateException e) {
console.println("Error finding indego bridge: " + e.getMessage());
printUsage(console);
return;
}

if (bridge == null) {
console.println("No BoschIndego Bridge defined.");
return;
}

authCode = args[1];
} else if (args.length == 3 && AUTHORIZE.equals(args[1])) {
bridge = getBridgeById(args[0]);
if (bridge == null) {
console.println("Unknown thing id '" + args[0] + "'");
return;
}

authCode = args[2];
} else {
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());
}
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("[<bridgeId>] " + AUTHORIZE + " <AuthToken>", "authorize by authorization code"));
}

@Override
Expand All @@ -84,8 +112,46 @@ public List<String> getUsages() {
@Override
public boolean complete(String[] args, int cursorArgumentIndex, int cursorPosition, List<String> candidates) {
if (cursorArgumentIndex <= 0) {
return new StringsCompleter(
Stream.concat(Stream.of(AUTHORIZE), getBridgeIds().stream().map(ThingUID::getAsString)).toList(),
false).complete(args, cursorArgumentIndex, cursorPosition, candidates);
} else if (cursorArgumentIndex == 1 && !AUTHORIZE.equals(args[0])) {
return SUBCMD_COMPLETER.complete(args, cursorArgumentIndex, cursorPosition, candidates);
}
return false;
}

private List<ThingUID> getBridgeIds() {
List<ThingUID> bridgeIds = new ArrayList<>();
for (Thing thing : thingRegistry.getAll()) {
if (thing.getHandler() instanceof BoschAccountHandler) {
bridgeIds.add(thing.getUID());
}
}
return bridgeIds;
}

private @Nullable Thing getBridgeById(String uid) {
Thing thing;
try {
ThingUID thingUID = new ThingUID(uid);
thing = thingRegistry.get(thingUID);
} catch (IllegalArgumentException e) {
thing = null;
}
return thing;
}

private @Nullable Thing getSingleBridge() {
Thing bridge = null;
for (Thing thing : thingRegistry.getAll()) {
if (thing.getHandler() instanceof BoschAccountHandler) {
if (bridge != null) {
throw new IllegalStateException("More than one BoschIndego Bridge found.");
}
bridge = thing;
}
}
return bridge;
}
}