forked from openhab/openhab-addons
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[netatmo] Console command to show all devices/modules ids (openhab#13555
) * [netatmo] Console command to show all devices/modules ids Fix openhab#13091 Signed-off-by: Laurent Garnier <lg.hc@free.fr>
- Loading branch information
Showing
5 changed files
with
187 additions
and
77 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
97 changes: 97 additions & 0 deletions
97
...o/src/main/java/org/openhab/binding/netatmo/internal/console/NetatmoCommandExtension.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
/** | ||
* Copyright (c) 2010-2022 Contributors to the openHAB project | ||
* | ||
* See the NOTICE file(s) distributed with this work for additional | ||
* information. | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Eclipse Public License 2.0 which is available at | ||
* http://www.eclipse.org/legal/epl-2.0 | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
*/ | ||
package org.openhab.binding.netatmo.internal.console; | ||
|
||
import java.util.Arrays; | ||
import java.util.List; | ||
import java.util.Optional; | ||
|
||
import org.eclipse.jdt.annotation.NonNullByDefault; | ||
import org.eclipse.jdt.annotation.Nullable; | ||
import org.openhab.binding.netatmo.internal.NetatmoBindingConstants; | ||
import org.openhab.binding.netatmo.internal.api.data.ModuleType; | ||
import org.openhab.binding.netatmo.internal.api.dto.NAModule; | ||
import org.openhab.binding.netatmo.internal.handler.ApiBridgeHandler; | ||
import org.openhab.core.io.console.Console; | ||
import org.openhab.core.io.console.extensions.AbstractConsoleCommandExtension; | ||
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.ThingUID; | ||
import org.openhab.core.thing.binding.ThingHandler; | ||
import org.osgi.service.component.annotations.Activate; | ||
import org.osgi.service.component.annotations.Component; | ||
import org.osgi.service.component.annotations.Reference; | ||
|
||
/** | ||
* The {@link NetatmoCommandExtension} is responsible for handling console commands | ||
* | ||
* @author Laurent Garnier - Initial contribution | ||
*/ | ||
|
||
@NonNullByDefault | ||
@Component(service = ConsoleCommandExtension.class) | ||
public class NetatmoCommandExtension extends AbstractConsoleCommandExtension { | ||
|
||
private static final String SHOW_IDS = "showIds"; | ||
|
||
private final ThingRegistry thingRegistry; | ||
private @Nullable Console console; | ||
|
||
@Activate | ||
public NetatmoCommandExtension(final @Reference ThingRegistry thingRegistry) { | ||
super(NetatmoBindingConstants.BINDING_ID, "Interact with the Netatmo binding."); | ||
this.thingRegistry = thingRegistry; | ||
} | ||
|
||
@Override | ||
public void execute(String[] args, Console console) { | ||
if (args.length == 1 && SHOW_IDS.equals(args[0])) { | ||
this.console = console; | ||
for (Thing thing : thingRegistry.getAll()) { | ||
ThingHandler thingHandler = thing.getHandler(); | ||
if (thingHandler instanceof ApiBridgeHandler) { | ||
console.println("Account bridge: " + thing.getLabel()); | ||
((ApiBridgeHandler) thingHandler).identifyAllModulesAndApplyAction(this::printThing); | ||
} | ||
} | ||
} else { | ||
printUsage(console); | ||
} | ||
} | ||
|
||
private Optional<ThingUID> printThing(NAModule module, ThingUID bridgeUID) { | ||
Console localConsole = this.console; | ||
Optional<ThingUID> moduleUID = findThingUID(module.getType(), module.getId(), bridgeUID); | ||
if (localConsole != null && moduleUID.isPresent()) { | ||
String indent = ""; | ||
for (int i = 2; i <= module.getType().getDepth(); i++) { | ||
indent += " "; | ||
} | ||
localConsole.println(String.format("%s- ID \"%s\" for \"%s\" (thing type %s)", indent, module.getId(), | ||
module.getName() != null ? module.getName() : "...", module.getType().thingTypeUID)); | ||
} | ||
return moduleUID; | ||
} | ||
|
||
private Optional<ThingUID> findThingUID(ModuleType moduleType, String thingId, ThingUID bridgeUID) { | ||
return moduleType.apiName.isBlank() ? Optional.empty() | ||
: Optional.ofNullable( | ||
new ThingUID(moduleType.thingTypeUID, bridgeUID, thingId.replaceAll("[^a-zA-Z0-9_]", ""))); | ||
} | ||
|
||
@Override | ||
public List<String> getUsages() { | ||
return Arrays.asList(buildCommandUsage(SHOW_IDS, "list all devices and modules ids")); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters