Skip to content

Commit

Permalink
Review comments
Browse files Browse the repository at this point in the history
Signed-off-by: Hilbrand Bouwkamp <hilbrand@h72.nl>
  • Loading branch information
Hilbrand committed Feb 9, 2022
1 parent e9cfd7a commit fd7c251
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,11 @@
*/
package org.openhab.binding.tplinksmarthome.internal.handler;

import static org.openhab.binding.tplinksmarthome.internal.TPLinkSmartHomeBindingConstants.*;
import static org.openhab.binding.tplinksmarthome.internal.TPLinkSmartHomeBindingConstants.CHANNEL_RSSI;
import static org.openhab.binding.tplinksmarthome.internal.TPLinkSmartHomeBindingConstants.CONFIG_DEVICE_ID;
import static org.openhab.binding.tplinksmarthome.internal.TPLinkSmartHomeBindingConstants.CONFIG_IP;
import static org.openhab.binding.tplinksmarthome.internal.TPLinkSmartHomeBindingConstants.FORCED_REFRESH_BOUNDERY_SECONDS;
import static org.openhab.binding.tplinksmarthome.internal.TPLinkSmartHomeBindingConstants.FORCED_REFRESH_BOUNDERY_SWITCHED_SECONDS;

import java.io.IOException;
import java.time.Duration;
Expand Down Expand Up @@ -184,7 +188,7 @@ Connection createConnection(final TPLinkSmartHomeConfiguration config) {
updateStatus(ThingStatus.ONLINE);
}
return deviceState;
} catch (IOException e) {
} catch (final IOException e) {
// If there is a connection problem retry before throwing an exception
if (retry < CONNECTION_IO_RETRIES) {
logger.trace("Communication error, retry {}", retry, e);
Expand All @@ -193,7 +197,7 @@ Connection createConnection(final TPLinkSmartHomeConfiguration config) {
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, e.getMessage());
return null;
}
} catch (RuntimeException e) {
} catch (final RuntimeException e) {
logger.debug("Obtaining new device data unexpectedly crashed. If this keeps happening please report: ",
e);
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.DISABLED, e.getMessage());
Expand Down Expand Up @@ -256,10 +260,7 @@ private void startAutomaticRefresh(final TPLinkSmartHomeConfiguration config) {

void refreshChannels() {
logger.trace("Update Channels for:{}", thing.getUID());
getThing().getChannels().forEach(channel -> {
final DeviceState deviceState = cache.getValue();
updateChannelState(channel.getUID(), deviceState);
});
getThing().getChannels().forEach(channel -> updateChannelState(channel.getUID(), cache.getValue()));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,14 @@
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.openhab.core.automation.annotation.ActionInput;
import org.openhab.core.automation.annotation.ActionOutput;
import org.openhab.core.automation.annotation.RuleAction;
import org.openhab.core.thing.binding.ThingActions;
import org.openhab.core.thing.binding.ThingActionsScope;
import org.openhab.core.thing.binding.ThingHandler;
import org.openhab.core.thing.binding.ThingHandlerService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* TP-Link Smart Home Rule Actions.
Expand All @@ -32,13 +35,21 @@
@NonNullByDefault
public class TPLinkSmartHomeActions implements ThingActions, ThingHandlerService {

private @Nullable ThingHandler handler;
private final Logger logger = LoggerFactory.getLogger(TPLinkSmartHomeActions.class);

@RuleAction(label = "@text/actions.send.label", description = "@text/actions.send.description")
private @Nullable SmartHomeHandler handler;

@RuleAction(label = "@text/actions.tplinksmarthome.send.label", description = "@text/actions.tplinksmarthome.send.description")
@ActionOutput(name = "response", label = "@text/actions.tplinksmarthome.send.response.label", description = "@text/actions.tplinksmarthome.send.response.description", type = "java.lang.String")
public String send(
@ActionInput(name = "command", label = "@text/actions.send.command.label", description = "@text/actions.send.command.description", type = "java.lang.String", required = true) final String command)
@ActionInput(name = "command", label = "@text/actions.tplinksmarthome.send.command.label", description = "@text/actions.tplinksmarthome.send.command.description", type = "java.lang.String", required = true) final String command)
throws IOException {
return ((SmartHomeHandler) getThingHandler()).getConnection().sendCommand(command);
if (handler instanceof SmartHomeHandler) {
return handler.getConnection().sendCommand(command);
} else {
logger.warn("Could not send command to tplink device because handler not set.");
return "";
}
}

public static String send(final ThingActions actions, final String command) throws IOException {
Expand All @@ -47,7 +58,9 @@ public static String send(final ThingActions actions, final String command) thro

@Override
public void setThingHandler(final ThingHandler handler) {
this.handler = handler;
if (handler instanceof SmartHomeHandler) {
this.handler = (SmartHomeHandler) handler;
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,3 +154,12 @@ channel-type.tplinksmarthome.switch-readonly.label = Switch
channel-type.tplinksmarthome.switch-readonly.description = Shows the switch state of the Smart Home device.
channel-type.tplinksmarthome.voltage.label = Voltage
channel-type.tplinksmarthome.voltage.description = Actual voltage usage.

# actions

actions.tplinksmarthome.send.label = Send Command
actions.tplinksmarthome.send.description = Sends the command, a json string, encrypted to a TP-Link device and decrypts the json response.
actions.tplinksmarthome.send.command.label = Command
actions.tplinksmarthome.send.command.description = The json string command to send to the TP-Link device.
actions.tplinksmarthome.send.response.label = Response
actions.tplinksmarthome.send.response.description = The decrypted json response returned by the TP-Link device.
Original file line number Diff line number Diff line change
Expand Up @@ -66,14 +66,6 @@ public void testHandleCommandBrightness() throws IOException {
"Brightness channel should be handled");
}

@Test
public void kl400_testHandleCommandBrightness() throws IOException {
assertInput("kl400_set_brightness");
setSocketReturnAssert("kl400_set_brightness_response");
assertTrue(device.handleCommand(CHANNEL_UID_BRIGHTNESS, new PercentType(50)),
"Brightness channel should be handled");
}

@Test
public void testHandleCommandBrightnessOnOff() throws IOException {
assertInput("kl430_set_on");
Expand Down Expand Up @@ -128,6 +120,7 @@ public void testHandleCommandColorTemperatureOnOff() throws IOException {
}

// ---- Update ----

@Test
public void testUpdateChannelBrightnessOn() {
assertEquals(new PercentType(92), device.updateChannel(CHANNEL_UID_BRIGHTNESS, deviceState),
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"color_temp": 5100,
"hue": 0,
"saturation": 0,
"on_off": 1,
"on_off": 1,
"transition": 10
}
},
Expand Down

0 comments on commit fd7c251

Please sign in to comment.