Skip to content

Commit 0f9c0f9

Browse files
authored
[boschindego] Add support for multiple accounts / bridges (openhab#19545)
Signed-off-by: Andreas Lanz <andreas.lanz@gmx.de>
1 parent d2913b7 commit 0f9c0f9

2 files changed

Lines changed: 43 additions & 15 deletions

File tree

bundles/org.openhab.binding.boschindego/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ To authorize, please follow these steps:
2525
- 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.
2626
- "Please wait..." should now be displayed.
2727
- Find the `authresp` and copy the code: `com.bosch.indegoconnect://login/?code=<copy this>`
28-
- Use the openHAB console to authorize with this code: `openhab:boschindego authorize <paste code>`
28+
- Use the openHAB console to authorize with this code: `openhab:boschindego authorize <bridgeId> <paste code>`
2929

3030
### `indego` Thing Configuration
3131

bundles/org.openhab.binding.boschindego/src/main/java/org/openhab/binding/boschindego/internal/console/BoschIndegoCommandExtension.java

Lines changed: 42 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212
*/
1313
package org.openhab.binding.boschindego.internal.console;
1414

15-
import java.util.Arrays;
15+
import static org.openhab.binding.boschindego.internal.BoschIndegoBindingConstants.THING_TYPE_ACCOUNT;
16+
1617
import java.util.List;
1718

1819
import org.eclipse.jdt.annotation.NonNullByDefault;
@@ -27,7 +28,7 @@
2728
import org.openhab.core.io.console.extensions.ConsoleCommandExtension;
2829
import org.openhab.core.thing.Thing;
2930
import org.openhab.core.thing.ThingRegistry;
30-
import org.openhab.core.thing.binding.ThingHandler;
31+
import org.openhab.core.thing.ThingUID;
3132
import org.osgi.service.component.annotations.Activate;
3233
import org.osgi.service.component.annotations.Component;
3334
import org.osgi.service.component.annotations.Reference;
@@ -42,7 +43,7 @@
4243
public class BoschIndegoCommandExtension extends AbstractConsoleCommandExtension implements ConsoleCommandCompleter {
4344

4445
private static final String AUTHORIZE = "authorize";
45-
private static final StringsCompleter SUBCMD_COMPLETER = new StringsCompleter(List.of(AUTHORIZE), false);
46+
private static final StringsCompleter CMD_COMPLETER = new StringsCompleter(List.of(AUTHORIZE), false);
4647

4748
private final ThingRegistry thingRegistry;
4849

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

5556
@Override
5657
public void execute(String[] args, Console console) {
57-
if (args.length != 2 || !AUTHORIZE.equals(args[0])) {
58+
59+
if (args.length != 3 || !AUTHORIZE.equals(args[0])) {
5860
printUsage(console);
5961
return;
6062
}
6163

62-
for (Thing thing : thingRegistry.getAll()) {
63-
ThingHandler thingHandler = thing.getHandler();
64-
if (thingHandler instanceof BoschAccountHandler accountHandler) {
65-
try {
66-
accountHandler.authorize(args[1]);
67-
} catch (IndegoAuthenticationException e) {
68-
console.println("Authorization error: " + e.getMessage());
69-
}
64+
String bridgeId = args[1];
65+
String authCode = args[2];
66+
67+
Thing bridge = getBridgeById(bridgeId);
68+
if (bridge == null) {
69+
console.println("Unknown bridge id '" + bridgeId + "'");
70+
return;
71+
}
72+
73+
if (bridge.getHandler() instanceof BoschAccountHandler accountHandler) {
74+
try {
75+
accountHandler.authorize(authCode);
76+
} catch (IndegoAuthenticationException e) {
77+
console.println("Authorization error: " + e.getMessage());
7078
}
79+
} else {
80+
console.println("Bridge is not a valid BoschIndego bridge");
81+
printUsage(console);
7182
}
7283
}
7384

7485
@Override
7586
public List<String> getUsages() {
76-
return Arrays.asList(buildCommandUsage(AUTHORIZE, "authorize by authorization code"));
87+
return List.of(
88+
buildCommandUsage(AUTHORIZE + " <bridgeId> <authorizationCode>", "authorize by authorization code"));
7789
}
7890

7991
@Override
@@ -84,8 +96,24 @@ public List<String> getUsages() {
8496
@Override
8597
public boolean complete(String[] args, int cursorArgumentIndex, int cursorPosition, List<String> candidates) {
8698
if (cursorArgumentIndex <= 0) {
87-
return SUBCMD_COMPLETER.complete(args, cursorArgumentIndex, cursorPosition, candidates);
99+
return CMD_COMPLETER.complete(args, cursorArgumentIndex, cursorPosition, candidates);
100+
} else if (cursorArgumentIndex == 1 && AUTHORIZE.equals(args[0])) {
101+
return new StringsCompleter(getBridgeIds(), true).complete(args, cursorArgumentIndex, cursorPosition,
102+
candidates);
88103
}
89104
return false;
90105
}
106+
107+
private List<String> getBridgeIds() {
108+
return thingRegistry.getAll().stream().filter(thing -> thing.getHandler() instanceof BoschAccountHandler)
109+
.map(thing -> thing.getUID().getId()).toList();
110+
}
111+
112+
private @Nullable Thing getBridgeById(String bridgeId) {
113+
try {
114+
return thingRegistry.get(new ThingUID(THING_TYPE_ACCOUNT, bridgeId));
115+
} catch (IllegalArgumentException e) {
116+
return null;
117+
}
118+
}
91119
}

0 commit comments

Comments
 (0)