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;
30 * Console commands for interacting with the HomeKit integration
32 * @author Andy Lintner - Initial contribution
34 @Component(service = ConsoleCommandExtension.class)
36 public class HomekitCommandExtension extends AbstractConsoleCommandExtension {
37 private static final String SUBCMD_CLEAR_PAIRINGS = "clearPairings";
38 private static final String SUBCMD_LIST_ACCESSORIES = "list";
39 private static final String SUBCMD_PRINT_ACCESSORY = "show";
40 private static final String SUBCMD_ALLOW_UNAUTHENTICATED = "allowUnauthenticated";
42 private final Logger logger = LoggerFactory.getLogger(HomekitCommandExtension.class);
44 private @NonNullByDefault({}) Homekit homekit;
46 public HomekitCommandExtension() {
47 super("homekit", "Interact with the HomeKit integration.");
51 public void execute(String[] args, Console console) {
52 if (args.length > 0) {
53 String subCommand = args[0];
55 case SUBCMD_CLEAR_PAIRINGS:
56 clearHomekitPairings(console);
59 case SUBCMD_ALLOW_UNAUTHENTICATED:
60 if (args.length > 1) {
61 boolean allow = Boolean.parseBoolean(args[1]);
62 allowUnauthenticatedHomekitRequests(allow, console);
64 console.println("true/false is required as an argument");
67 case SUBCMD_LIST_ACCESSORIES:
68 listAccessories(console);
70 case SUBCMD_PRINT_ACCESSORY:
71 if (args.length > 1) {
72 printAccessory(args[1], console);
74 console.println("accessory id or name is required as an argument");
78 console.println("Unknown command '" + subCommand + "'");
88 public List<String> getUsages() {
89 return Arrays.asList(buildCommandUsage(SUBCMD_LIST_ACCESSORIES, "list all HomeKit accessories"),
90 buildCommandUsage(SUBCMD_PRINT_ACCESSORY + " <accessory id | accessory name>",
91 "print additional details of the accessories which partially match provided ID or name."),
92 buildCommandUsage(SUBCMD_CLEAR_PAIRINGS, "removes all pairings with HomeKit clients."),
93 buildCommandUsage(SUBCMD_ALLOW_UNAUTHENTICATED + " <boolean>",
94 "enables or disables unauthenticated access to facilitate debugging"));
98 public void setHomekit(Homekit homekit) {
99 this.homekit = homekit;
102 private void clearHomekitPairings(Console console) {
103 homekit.clearHomekitPairings();
104 console.println("Cleared HomeKit pairings");
107 private void allowUnauthenticatedHomekitRequests(boolean allow, Console console) {
108 homekit.allowUnauthenticatedRequests(allow);
109 console.println((allow ? "Enabled " : "Disabled ") + "unauthenticated HomeKit access");
112 private void listAccessories(Console console) {
113 homekit.getAccessories().forEach(v -> {
115 console.println(v.getId() + " " + v.getName().get());
116 } catch (InterruptedException | ExecutionException e) {
117 logger.warn("Cannot list accessories", e);
122 private void printAccessory(String id, Console console) {
123 homekit.getAccessories().forEach(v -> {
125 if (("" + v.getId()).contains(id) || ((v.getName().get() != null)
126 && (v.getName().get().toUpperCase().contains(id.toUpperCase())))) {
127 console.println(v.getId() + " " + v.getName().get());
128 console.println("Services:");
129 v.getServices().forEach(s -> {
130 console.println(" Service Type: " + s.getType());
131 console.println(" Characteristics: ");
132 s.getCharacteristics().forEach(c -> console.println(" : " + c.getClass()));
136 } catch (InterruptedException | ExecutionException e) {
137 logger.warn("Cannot print accessory", e);