2 * Copyright (c) 2010-2020 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 static final String LEGACY_SUBCMD_LIST_ACCESSORIES = "listAccessories";
44 private static final String LEGACY_SUBCMD_PRINT_ACCESSORY = "printAccessory";
46 private final Logger logger = LoggerFactory.getLogger(HomekitCommandExtension.class);
48 private @NonNullByDefault({}) Homekit homekit;
50 public HomekitCommandExtension() {
51 super("homekit", "Interact with the HomeKit integration.");
55 public void execute(String[] args, Console console) {
56 if (args.length > 0) {
57 String subCommand = args[0];
59 case SUBCMD_CLEAR_PAIRINGS:
60 clearHomekitPairings(console);
63 case SUBCMD_ALLOW_UNAUTHENTICATED:
64 if (args.length > 1) {
65 boolean allow = Boolean.parseBoolean(args[1]);
66 allowUnauthenticatedHomekitRequests(allow, console);
68 console.println("true/false is required as an argument");
71 case SUBCMD_LIST_ACCESSORIES:
72 case LEGACY_SUBCMD_LIST_ACCESSORIES:
73 listAccessories(console);
74 if (subCommand.equalsIgnoreCase(LEGACY_SUBCMD_LIST_ACCESSORIES)) {
76 console.println("Hey, you can use the shorter command 'homekit list'");
79 case SUBCMD_PRINT_ACCESSORY:
80 case LEGACY_SUBCMD_PRINT_ACCESSORY:
81 if (args.length > 1) {
82 printAccessory(args[1], console);
84 console.println("accessory id or name is required as an argument");
86 if (subCommand.equalsIgnoreCase(LEGACY_SUBCMD_PRINT_ACCESSORY)) {
88 console.println("Hey, you can use the shorter command 'homekit show <accessory_id|name>'");
92 console.println("Unknown command '" + subCommand + "'");
102 public List<String> getUsages() {
103 return Arrays.asList(buildCommandUsage(SUBCMD_LIST_ACCESSORIES, "list all HomeKit accessories"),
104 buildCommandUsage(SUBCMD_PRINT_ACCESSORY + " <accessory id | accessory name>",
105 "print additional details of the accessories which partially match provided ID or name."),
106 buildCommandUsage(SUBCMD_CLEAR_PAIRINGS, "removes all pairings with HomeKit clients."),
107 buildCommandUsage(SUBCMD_ALLOW_UNAUTHENTICATED + " <boolean>",
108 "enables or disables unauthenticated access to facilitate debugging"));
112 public void setHomekit(Homekit homekit) {
113 this.homekit = homekit;
116 private void clearHomekitPairings(Console console) {
117 homekit.clearHomekitPairings();
118 console.println("Cleared HomeKit pairings");
121 private void allowUnauthenticatedHomekitRequests(boolean allow, Console console) {
122 homekit.allowUnauthenticatedRequests(allow);
123 console.println((allow ? "Enabled " : "Disabled ") + "unauthenticated HomeKit access");
126 private void listAccessories(Console console) {
127 homekit.getAccessories().forEach(v -> {
129 console.println(v.getId() + " " + v.getName().get());
130 } catch (InterruptedException | ExecutionException e) {
131 logger.warn("Cannot list accessories", e);
136 private void printAccessory(String id, Console console) {
137 homekit.getAccessories().forEach(v -> {
139 if (("" + v.getId()).contains(id) || ((v.getName().get() != null)
140 && (v.getName().get().toUpperCase().contains(id.toUpperCase())))) {
141 console.println(v.getId() + " " + v.getName().get());
142 console.println("Services:");
143 v.getServices().forEach(s -> {
144 console.println(" Service Type: " + s.getType());
145 console.println(" Characteristics: ");
146 s.getCharacteristics().forEach(c -> console.println(" : " + c.getClass()));
150 } catch (InterruptedException | ExecutionException e) {
151 logger.warn("Cannot print accessory", e);