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.tplinksmarthome.internal.device;
15 import java.io.IOException;
17 import org.eclipse.jdt.annotation.NonNullByDefault;
18 import org.eclipse.jdt.annotation.Nullable;
19 import org.openhab.binding.tplinksmarthome.internal.Commands;
20 import org.openhab.binding.tplinksmarthome.internal.Connection;
21 import org.openhab.binding.tplinksmarthome.internal.TPLinkSmartHomeConfiguration;
22 import org.openhab.binding.tplinksmarthome.internal.model.ErrorResponse;
23 import org.openhab.binding.tplinksmarthome.internal.model.HasErrorResponse;
24 import org.openhab.core.thing.ChannelUID;
25 import org.openhab.core.types.Command;
26 import org.openhab.core.types.State;
29 * Abstract class as base for Smart Home device implementations.
31 * @author Hilbrand Bouwkamp - Initial contribution
34 public abstract class SmartHomeDevice {
36 protected final Commands commands = new Commands();
37 protected @NonNullByDefault({}) Connection connection;
38 protected @NonNullByDefault({}) TPLinkSmartHomeConfiguration configuration;
41 * Checks if the response object contains errors and if so throws an {@link IOException} when an error code was set.
43 * @param response The response to check for errors.
44 * @throws IOException if an error code was set in the response object
46 protected void checkErrors(@Nullable HasErrorResponse response) throws IOException {
47 final ErrorResponse errorResponse = response == null ? null : response.getErrorResponse();
49 if (errorResponse != null && errorResponse.getErrorCode() != 0) {
50 throw new IOException("Error (" + errorResponse.getErrorCode() + "): " + errorResponse.getErrorMessage());
55 * Sets connection and configuration values.
57 * @param connection The connection to the device
58 * @param configuration The global configuration
60 public void initialize(Connection connection, TPLinkSmartHomeConfiguration configuration) {
61 this.connection = connection;
62 this.configuration = configuration;
66 * @return the json string to send to the device to get the state of the device.
68 public abstract String getUpdateCommand();
71 * Handle the command for the given channel
73 * @param channelUID The channel the command is for
74 * @param command The command to be send to the device
75 * @return Returns true if the commands successfully was send to the device
76 * @throws IOException In case of communications error or the device returned an error
78 public abstract boolean handleCommand(ChannelUID channelUID, Command command) throws IOException;
81 * Returns the {@link State} value for the given value extracted from the deviceState data.
83 * @param channelUid channel to get state for
84 * @param deviceState state object containing the state
85 * @return {@link State} value for the given channel
87 public abstract State updateChannel(ChannelUID channelUid, DeviceState deviceState);
90 * Called with the new device state after the new device state is retrieved from the device.
92 * @param deviceState new device state
94 public void refreshedDeviceState(@Nullable DeviceState deviceState) {