2 * Copyright (c) 2010-2023 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.binding.vizio.internal.console;
15 import static org.openhab.binding.vizio.internal.VizioBindingConstants.*;
17 import java.math.BigDecimal;
18 import java.util.List;
20 import org.eclipse.jdt.annotation.NonNullByDefault;
21 import org.openhab.binding.vizio.internal.VizioException;
22 import org.openhab.binding.vizio.internal.handler.VizioHandler;
23 import org.openhab.core.io.console.Console;
24 import org.openhab.core.io.console.extensions.AbstractConsoleCommandExtension;
25 import org.openhab.core.io.console.extensions.ConsoleCommandExtension;
26 import org.openhab.core.thing.Thing;
27 import org.openhab.core.thing.ThingRegistry;
28 import org.openhab.core.thing.ThingUID;
29 import org.openhab.core.thing.binding.ThingHandler;
30 import org.osgi.service.component.annotations.Activate;
31 import org.osgi.service.component.annotations.Component;
32 import org.osgi.service.component.annotations.Reference;
35 * The {@link VizioCommandExtension} is responsible for handling console commands
37 * @author Michael Lobstein - Initial contribution
41 @Component(service = ConsoleCommandExtension.class)
42 public class VizioCommandExtension extends AbstractConsoleCommandExtension {
43 private static final String START_PAIRING = "start_pairing";
44 private static final String SUBMIT_CODE = "submit_code";
46 private final ThingRegistry thingRegistry;
49 public VizioCommandExtension(final @Reference ThingRegistry thingRegistry) {
50 super("vizio", "Interact with the Vizio binding to get an authentication token from the TV.");
51 this.thingRegistry = thingRegistry;
55 public void execute(String[] args, Console console) {
56 if (args.length == 3) {
59 ThingUID thingUID = new ThingUID(args[0]);
60 thing = thingRegistry.get(thingUID);
61 } catch (IllegalArgumentException e) {
64 ThingHandler thingHandler = null;
65 VizioHandler handler = null;
67 thingHandler = thing.getHandler();
68 if (thingHandler instanceof VizioHandler vizioHandler) {
69 handler = vizioHandler;
73 console.println("Bad thing id '" + args[0] + "'");
75 } else if (thingHandler == null) {
76 console.println("No handler initialized for the thing id '" + args[0] + "'");
78 } else if (handler == null) {
79 console.println("'" + args[0] + "' is not a Vizio thing id");
82 String host = (String) thing.getConfiguration().get(PROPERTY_HOST_NAME);
83 BigDecimal port = (BigDecimal) thing.getConfiguration().get(PROPERTY_PORT);
85 if (host == null || host.isEmpty() || port.signum() < 1) {
87 "Error! Host Name and Port must be specified in thing configuration before paring.");
94 int pairingToken = handler.startPairing(args[2]);
96 if (pairingToken != -1) {
97 console.println("Pairing has been started!");
99 "Please note the 4 digit code displayed on the TV and substitute it into the following console command:");
101 "openhab:vizio " + handler.getThing().getUID() + " " + SUBMIT_CODE + " <NNNN>");
103 console.println("Unable to obtain pairing token!");
105 } catch (VizioException e) {
106 console.println("Error! Unable to start pairing process.");
107 console.println("Exception was: " + e.getMessage());
112 Integer.valueOf(args[2]);
113 String authToken = handler.submitPairingCode(args[2]);
115 if (authToken != EMPTY) {
116 console.println("Pairing complete!");
117 console.println("The auth token: " + authToken
118 + " was received and will be added to the thing configuration.");
120 "If the thing is provisioned via a file, the token must be manually added to the thing configuration.");
122 handler.saveAuthToken(authToken);
124 console.println("Unable to obtain auth token!");
126 } catch (NumberFormatException nfe) {
128 "Error! Pairing code must be numeric. Check console command and try again.");
129 } catch (IllegalStateException ise) {
130 console.println("Error! '" + START_PAIRING + "' command must be completed first.");
132 "Please issue the following command and substitute the desired device name.");
133 console.println("openhab:vizio " + handler.getThing().getUID() + " " + START_PAIRING
135 } catch (VizioException e) {
136 console.println("Error! Unable to complete pairing process.");
137 console.println("Exception was: " + e.getMessage());
151 public List<String> getUsages() {
152 return List.of(new String[] {
153 buildCommandUsage("<thingUID> " + START_PAIRING + " <deviceName>", "start pairing process"),
154 buildCommandUsage("<thingUID> " + SUBMIT_CODE + " <pairingCode>", "submit pairing code") });