2 * Copyright (c) 2010-2022 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
7 * This program and the accompanying materials are made available under the
8 * terms of the Eclipse Public License 2.0 which is available at
9 * http://www.eclipse.org/legal/epl-2.0
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.io.homekit.internal;
15 import java.util.Arrays;
16 import java.util.List;
17 import java.util.concurrent.ExecutionException;
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.openhab.core.io.console.Console;
21 import org.openhab.core.io.console.extensions.AbstractConsoleCommandExtension;
22 import org.openhab.core.io.console.extensions.ConsoleCommandExtension;
23 import org.openhab.io.homekit.Homekit;
24 import org.osgi.service.component.annotations.Component;
25 import org.osgi.service.component.annotations.Reference;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
29 import io.github.hapjava.services.Service;
32 * Console commands for interacting with the HomeKit integration
34 * @author Andy Lintner - Initial contribution
36 @Component(service = ConsoleCommandExtension.class)
38 public class HomekitCommandExtension extends AbstractConsoleCommandExtension {
39 private static final String SUBCMD_CLEAR_PAIRINGS = "clearPairings";
40 private static final String SUBCMD_LIST_ACCESSORIES = "list";
41 private static final String SUBCMD_PRINT_ACCESSORY = "show";
42 private static final String SUBCMD_ALLOW_UNAUTHENTICATED = "allowUnauthenticated";
44 private final Logger logger = LoggerFactory.getLogger(HomekitCommandExtension.class);
46 private @NonNullByDefault({}) Homekit homekit;
48 public HomekitCommandExtension() {
49 super("homekit", "Interact with the HomeKit integration.");
53 public void execute(String[] args, Console console) {
54 if (args.length > 0) {
55 String subCommand = args[0];
57 case SUBCMD_CLEAR_PAIRINGS:
58 clearHomekitPairings(console);
61 case SUBCMD_ALLOW_UNAUTHENTICATED:
62 if (args.length > 1) {
63 boolean allow = Boolean.parseBoolean(args[1]);
64 allowUnauthenticatedHomekitRequests(allow, console);
66 console.println("true/false is required as an argument");
69 case SUBCMD_LIST_ACCESSORIES:
70 listAccessories(console);
72 case SUBCMD_PRINT_ACCESSORY:
73 if (args.length > 1) {
74 printAccessory(args[1], console);
76 console.println("accessory id or name is required as an argument");
80 console.println("Unknown command '" + subCommand + "'");
90 public List<String> getUsages() {
91 return Arrays.asList(buildCommandUsage(SUBCMD_LIST_ACCESSORIES, "list all HomeKit accessories"),
92 buildCommandUsage(SUBCMD_PRINT_ACCESSORY + " <accessory id | accessory name>",
93 "print additional details of the accessories which partially match provided ID or name."),
94 buildCommandUsage(SUBCMD_CLEAR_PAIRINGS, "removes all pairings with HomeKit clients."),
95 buildCommandUsage(SUBCMD_ALLOW_UNAUTHENTICATED + " <boolean>",
96 "enables or disables unauthenticated access to facilitate debugging"));
100 public void setHomekit(Homekit homekit) {
101 this.homekit = homekit;
104 private void clearHomekitPairings(Console console) {
105 homekit.clearHomekitPairings();
106 console.println("Cleared HomeKit pairings");
109 private void allowUnauthenticatedHomekitRequests(boolean allow, Console console) {
110 homekit.allowUnauthenticatedRequests(allow);
111 console.println((allow ? "Enabled " : "Disabled ") + "unauthenticated HomeKit access");
114 private void listAccessories(Console console) {
115 homekit.getAccessories().forEach(v -> {
117 console.println(v.getId() + " " + v.getName().get());
118 } catch (InterruptedException | ExecutionException e) {
119 logger.warn("Cannot list accessories", e);
124 private void printService(Console console, Service service, int indent) {
125 console.println(" ".repeat(indent) + "Service Type: " + service.getClass().getSimpleName() + " ("
126 + service.getType() + ")");
127 console.println(" ".repeat(indent + 2) + "Characteristics:");
128 service.getCharacteristics().forEach((c) -> {
131 " ".repeat(indent + 4) + c.getClass().getSimpleName() + ": " + c.toJson(0).get().toString());
132 } catch (InterruptedException | ExecutionException e) {
135 if (service.getLinkedServices().isEmpty()) {
138 console.println(" ".repeat(indent + 2) + "Linked Services:");
139 service.getLinkedServices().forEach((s) -> printService(console, s, indent + 2));
142 private void printAccessory(String id, Console console) {
143 homekit.getAccessories().forEach(v -> {
145 if (("" + v.getId()).contains(id) || ((v.getName().get() != null)
146 && (v.getName().get().toUpperCase().contains(id.toUpperCase())))) {
147 console.println(v.getId() + " " + v.getName().get());
148 console.println("Services:");
149 v.getServices().forEach(s -> printService(console, s, 2));
152 } catch (InterruptedException | ExecutionException e) {
153 logger.warn("Cannot print accessory", e);